⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elementstylesheet.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    {
      parents.add(position, parent);
      parentsCached = null;
      parentsListCached = null;
      if (parent.isGlobalDefault() == false)
      {
        parent.addListener(this);
      }
      if (getStyleSheetCollection() != null)
      {
        parent.registerStyleSheetCollection(getStyleSheetCollection());
      }
    }
    else
    {
      throw new IllegalArgumentException("Cannot add parent as child.");
    }
  }

  /**
   * Adds a parent style-sheet. Parents on a lower position are queried before any
   * parent with an higher position in the list.
   * <p>
   * The default parents operations are reserved for the system internal stylesheet
   * operations. If you want to add own stylesheets, use the addParent methods.
   * <p>
   * Only default style sheets should be added with this method.
   *
   * @param position the position where to insert the parent style sheet
   * @param parent  the parent (<code>null</code> not permitted).
   *
   * @throws IndexOutOfBoundsException if the position is invalid (pos &lt; 0 or pos &gt;=
   *         numberOfParents)
   */
  public synchronized void addDefaultParent(final int position, final ElementStyleSheet parent)
  {
    if (parent == null)
    {
      throw new NullPointerException("ElementStyleSheet.addParent(...): parent is null.");
    }
    if (parent.isSubStyleSheet(this) == false)
    {
      defaultSheets.add(position, parent);
      defaultCached = null;
      defaultParentsListCached = null;
      if (getStyleSheetCollection() != null)
      {
        parent.registerStyleSheetCollection(getStyleSheetCollection());
      }
      if (parent.isGlobalDefault() == false)
      {
        parent.addListener(this);
      }
    }
    else
    {
      throw new IllegalArgumentException("Cannot add parent as child.");
    }
  }

  /**
   * Checks, whether the given element stylesheet is already added as child into
   * the stylesheet tree.
   *
   * @param parent the element that should be tested.
   * @return true, if the element is a child of this element style sheet,
   * false otherwise.
   */
  public boolean isSubStyleSheet(final ElementStyleSheet parent)
  {
    for (int i = 0; i < parents.size(); i++)
    {
      final ElementStyleSheet es = (ElementStyleSheet) parents.get(i);
      if (es == parent)
      {
        return true;
      }

      if (es.isSubStyleSheet(parent) == true)
      {
        return true;
      }
    }
    for (int i = 0; i < defaultSheets.size(); i++)
    {
      final ElementStyleSheet es = (ElementStyleSheet) defaultSheets.get(i);
      if (es == parent)
      {
        return true;
      }

      if (es.isSubStyleSheet(parent) == true)
      {
        return true;
      }
    }
    return false;
  }

  /**
   * Removes a parent style-sheet.
   *
   * @param parent  the style-sheet to remove (<code>null</code> not permitted).
   */
  public synchronized void removeParent(final ElementStyleSheet parent)
  {
    if (parent == null)
    {
      throw new NullPointerException("ElementStyleSheet.removeParent(...): parent is null.");
    }
    if (parents.contains(parent) == false)
    {
      // do nothing if this is none of the parents ...
      return;
    }
    parents.remove(parent);
    if (parent.isGlobalDefault() == false)
    {
      parent.removeListener(this);
    }
    parentsCached = null;
    parentsListCached = null;
    if (getStyleSheetCollection() != null)
    {
      parent.unregisterStyleSheetCollection(getStyleSheetCollection());
    }
  }

  /**
   * Removes a parent style-sheet.
   *
   * @param parent  the style-sheet to remove (<code>null</code> not permitted).
   */
  public synchronized void removeDefaultParent(final ElementStyleSheet parent)
  {
    if (parent == null)
    {
      throw new NullPointerException("ElementStyleSheet.removeParent(...): parent is null.");
    }
    defaultSheets.remove(parent);
    if (parent.isGlobalDefault() == false)
    {
      parent.removeListener(this);
    }
    defaultCached = null;
    defaultParentsListCached = null;
  }

  /**
   * Returns a list of the parent style-sheets.
   * <p>
   * The list is unmodifiable.
   *
   * @return the list.
   */
  public List getParents()
  {
    if (parentsListCached == null)
    {
      parentsListCached = Collections.unmodifiableList(parents);
    }
    return parentsListCached;
  }

  /**
   * Returns a list of the default style-sheets.
   * <p>
   * The list is unmodifiable.
   *
   * @return the list.
   */
  public List getDefaultParents()
  {
    if (defaultParentsListCached == null)
    {
      defaultParentsListCached = Collections.unmodifiableList(defaultSheets);
    }
    return defaultParentsListCached;
  }

  /**
   * Returns the value of a style.  If the style is not found in this style-sheet, the code looks
   * in the parent style-sheets.  If the style is not found in any of the parent style-sheets, then
   * <code>null</code> is returned.
   *
   * @param key  the style key.
   *
   * @return the value.
   */
  public Object getStyleProperty(final StyleKey key)
  {
    return getStyleProperty(key, null);
  }

  /**
   * Returns the value of a style.  If the style is not found in this style-sheet, the code looks
   * in the parent style-sheets.  If the style is not found in any of the parent style-sheets, then
   * the default value (possibly <code>null</code>) is returned.
   *
   * @param key  the style key.
   * @param defaultValue  the default value (<code>null</code> permitted).
   *
   * @return the value.
   */
  public Object getStyleProperty(final StyleKey key, final Object defaultValue)
  {
    Object value = properties.get(key);
    if (value != null)
    {
      return value;
    }

    if (styleCache != null)
    {
      value = styleCache.get(key);
      if (value != null)
      {
        if (value == UNDEFINED_VALUE)
        {
          return defaultValue;
        }
        return value;
      }
    }

    parentsToCache();

    for (int i = 0; i < parentsCached.length; i++)
    {
      final ElementStyleSheet st = parentsCached[i];
      value = st.getStyleProperty(key, null);
      if (value == null)
      {
        continue;
      }
      putInCache(key, value);
      return value;
    }

    defaultToCache();

    for (int i = 0; i < defaultCached.length; i++)
    {
      final ElementStyleSheet st = defaultCached[i];
      value = st.getStyleProperty(key, null);
      if (value == null)
      {
        continue;
      }
      putInCache(key, value);
      return value;
    }
    putInCache(key, UNDEFINED_VALUE);
    return defaultValue;
  }

  /**
   * Puts an object into the cache (if caching is enabled).
   *
   * @param key the stylekey for that object
   * @param value the object.
   */
  private void putInCache(final StyleKey key, final Object value)
  {
    if (isAllowCaching())
    {
      if (styleCache == null)
      {
        styleCache = new HashMap();
      }
      styleCache.put(key, value);
    }
  }

  /**
   * Sets a boolean style property.
   *
   * @param key  the style key (<code>null</code> not permitted).
   * @param value  the value.
   * @throws NullPointerException if the given key is null.
   * @throws ClassCastException if the value cannot be assigned with the given key.
   */
  public void setBooleanStyleProperty(final StyleKey key, final boolean value)
  {
    if (value)
    {
      setStyleProperty(key, Boolean.TRUE);
    }
    else
    {
      setStyleProperty(key, Boolean.FALSE);
    }
  }

  /**
   * Sets a style property (or removes the style if the value is <code>null</code>).
   *
   * @param key  the style key (<code>null</code> not permitted).
   * @param value  the value.
   * @throws NullPointerException if the given key is null.
   * @throws ClassCastException if the value cannot be assigned with the given key.
   */
  public void setStyleProperty(final StyleKey key, final Object value)
  {
    if (key == null)
    {
      throw new NullPointerException("ElementStyleSheet.setStyleProperty: key is null.");
    }
    if (isFontDefinitionProperty(key))
    {
      fontDefinition = null;
    }
    if (value == null)
    {
      properties.remove(key);
      styleChangeSupport.fireStyleRemoved(key);
    }
    else
    {
      if (key.getValueType().isAssignableFrom(value.getClass()) == false)
      {
        throw new ClassCastException("Value for key " + key.getName()
            + " is not assignable: " + value.getClass()
            + " is not assignable from " + key.getValueType());
      }
      properties.put(key, value);
      styleChangeSupport.fireStyleChanged(key, value);
    }
  }

  /**
   * Clones the style-sheet. The assigned parent style sheets are not cloned.
   * The stylesheets are not assigned to the contained stylesheet collection,
   * you have to reassign them manually ...
   *
   * @return the clone.
   */
  public ElementStyleSheet getCopy()
  {
    try
    {
      final ElementStyleSheet sc = (ElementStyleSheet) super.clone();
      sc.properties = (HashMap) properties.clone();
      if (styleCache != null)
      {
        sc.styleCache = new HashMap(styleCache);
      }
      sc.styleChangeSupport = new StyleChangeSupport(sc);
      sc.parents = new ArrayList();// parents.clone();
      final ElementStyleSheet[] cloneParentsCached = new ElementStyleSheet[parents.size()];
      final ElementStyleSheet[] cloneDefaultCached = new ElementStyleSheet[defaultSheets.size()];
      sc.parentsListCached = null;
      sc.defaultParentsListCached = null;
      sc.collectionHelper = new ElementStyleSheetCollectionHelper(sc);

      // Clone all parents ...
      parentsToCache();
      for (int i = parentsCached.length - 1; i >= 0; i--)
      {
        sc.addParent(parentsCached[i]);
        cloneParentsCached[i] = parentsCached[i];
      }

      // Clone all default parents ...
      defaultToCache();
      sc.defaultSheets = new ArrayList();// defaultSheets.clone();

      for (int i = defaultCached.length - 1; i >= 0; i--)
      {
        sc.addDefaultParent(defaultCached[i]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -