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

📄 stylesheetcollection.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        final StyleCollectionEntry se = allElements[ai];
        final ElementStyleSheet es = se.getStyleSheet();

        final List parents = es.getParents();
        // reversed add order .. last parent must be added first ..
        final ElementStyleSheet[] parentArray =
            (ElementStyleSheet[]) parents.toArray(new ElementStyleSheet[parents.size()]);
        for (int i = parentArray.length - 1; i >= 0; i--)
        {
          final String name = parentArray[i].getName();
          final InstanceID id = parentArray[i].getId();
          es.removeParent(parentArray[i]);
          final StyleCollectionEntry seParent = col.findStyleSheet(name, id);
          if (seParent == null)
          {
            throw new IllegalStateException
                ("A parent of an stylesheet was not found in this collection.");
          }
          es.addParent(seParent.getStyleSheet());
        }
      }
    }
    // until now, all cloned stylesheets are unregistered ...
    // restore the registration now ...
    it = col.styleSheets.keySet().iterator();
    while (it.hasNext())
    {
      final Object key = it.next();
      final int len = col.styleSheets.getValueCount(key);
      allElements = (StyleCollectionEntry[]) col.styleSheets.toArray(key, allElements);
      for (int i = 0; i < len; i++)
      {
        final StyleCollectionEntry se = allElements[i];
        final ElementStyleSheet es = se.getStyleSheet();
        es.registerStyleSheetCollection(col);
      }
    }

    return col;
  }

  /**
   * Searches for a stylesheet with the same name and id. This method returns
   * null, if no such stylesheet exists.
   *
   * @param name the name of the stylesheet.
   * @param id the instance id of the stylesheet
   * @return the found stylesheet entry or null if not found.
   */
  private StyleCollectionEntry findStyleSheet(final String name, final InstanceID id)
  {
    final int len = styleSheets.getValueCount(name);
    if (len == 0)
    {
      return null;
    }

    final StyleCollectionEntry[] data = (StyleCollectionEntry[])
        styleSheets.toArray(name, new StyleCollectionEntry[len]);

    for (int i = 0; i < data.length; i++)
    {
      final StyleCollectionEntry es = data[i];
      if (es.getStyleSheet().getId() == id)
      {
        return es;
      }
    }
    return null;
  }

  /**
   * Updates a stylesheet reference from this collection. This is usually done after
   * a clone() operation to update the parents of the given element stylesheet.
   * <p>
   * This operation will remove all parents of the stylesheet and repace them with
   * stylesheets from this collection with the same name and Id.
   *
   * @param es the elements stylesheet that should be replaced.
   */
  public void updateStyleSheet(final ElementStyleSheet es)
  {
    if (es.getStyleSheetCollection() != null)
    {
      throw new IllegalArgumentException
          ("This stylesheet instance is already registered with an collection.");
    }
    if (contains(es) == false)
    {
      throw new IllegalArgumentException
        ("This stylesheet is not in the collection." + es.getName());
    }
    else
    {
      final StyleCollectionEntry entry = findStyleSheet(es.getName(), es.getId());
      styleSheets.remove(es.getName(), entry);
      styleSheets.add(es.getName(), new StyleCollectionEntry(entry.getReferenceCount(), es));

      final List parents = es.getParents();
      // reversed add order .. last parent must be added first ..
      final ElementStyleSheet[] parentArray =
          (ElementStyleSheet[]) parents.toArray(new ElementStyleSheet[parents.size()]);
      for (int i = parentArray.length - 1; i >= 0; i--)
      {
        final String name = parentArray[i].getName();
        final InstanceID id = parentArray[i].getId();
        es.removeParent(parentArray[i]);
        final StyleCollectionEntry seParent = findStyleSheet(name, id);
        if (seParent == null)
        {
          throw new IllegalStateException
              ("A parent of an stylesheet was not found in this collection.");
        }
        es.addParent(seParent.getStyleSheet());
      }
      es.registerStyleSheetCollection(this);
    }
  }

  /**
   * Adds all parents of the given stylesheet recursivly to this collection.
   *
   * @param es the element style sheet whose parents should be added.
   */
  protected void addParents(final ElementStyleSheet es)
  {
    final List parents = es.getParents();
    for (int i = 0; i < parents.size(); i++)
    {
      final ElementStyleSheet esp = (ElementStyleSheet) parents.get(i);
      addStyleSheet(esp, false);
    }
    final List defaultParents = es.getDefaultParents();
    for (int i = 0; i < defaultParents.size(); i++)
    {
      final ElementStyleSheet esp = (ElementStyleSheet) defaultParents.get(i);
      addStyleSheet(esp, false);
    }
  }

  /**
   * Updates the reference count of all stylesheets.
   * This method is expensive and is (like the whole class) a candidate for
   * an performance redesign ...
   */
  protected void updateReferences()
  {
    Iterator keyIterator = styleSheets.keys();
    StyleCollectionEntry[] allElements = new StyleCollectionEntry[0];
    while (keyIterator.hasNext())
    {
      // reset the reference count ...
      final Object key = keyIterator.next();
      final int len = styleSheets.getValueCount(key);
      allElements = (StyleCollectionEntry[]) styleSheets.toArray(key, allElements);
      for (int i = 0; i < len; i++)
      {
        final StyleCollectionEntry se = allElements[i];
        se.setReferenceCount(0);
      }
    }

    keyIterator = styleSheets.keys();
    while (keyIterator.hasNext())
    {
      // compute the reference count ...
      final Object key = keyIterator.next();
      final int len = styleSheets.getValueCount(key);
      allElements = (StyleCollectionEntry[]) styleSheets.toArray(key, allElements);
      for (int ai = 0; ai < len; ai++)
      {
        final StyleCollectionEntry se = allElements[ai];
        final ElementStyleSheet es = se.getStyleSheet();

        final List parents = es.getParents();
        for (int i = 0; i < parents.size(); i++)
        {
          final ElementStyleSheet esp = (ElementStyleSheet) parents.get(i);
          final StyleCollectionEntry sep = findStyleSheet(esp.getName(), esp.getId());
          if (sep == null)
          {
            throw new NullPointerException("StyleSheet '" + esp.getName() + "' is not known.");
          }
          sep.setReferenceCount(sep.getReferenceCount() + 1);
        }
        final List defaultParents = es.getDefaultParents();
        for (int i = 0; i < defaultParents.size(); i++)
        {
          final ElementStyleSheet esp = (ElementStyleSheet) defaultParents.get(i);
          final StyleCollectionEntry sep = findStyleSheet(esp.getName(), esp.getId());
          if (sep == null)
          {
            throw new NullPointerException("StyleSheet '" + esp.getName() + "' is not known.");
          }
          /*
          if (esp.getName().equals("band-default"))
          {
            Log.debug ("Update: " + esp.getName() + " -> " + sep.getReferenceCount());
          }
          */
          sep.setReferenceCount(sep.getReferenceCount() + 1);
        }
      }
    }
    // Styles with an RefCount of 0 can be removed if requested...
  }

  /**
   * Returns true, if removing the stylesheet was successfull, false
   * if the stylesheet is still referenced and will not be removed.
   *
   * @param es the element stylesheet that should be removed.
   * @return true, if the stylesheet was removed, false otherwise.
   */
  public boolean remove(final ElementStyleSheet es)
  {
    return remove(es, true);
  }

  /**
   * Returns true, if the stylesheet if removing was successfull, false
   * if the stylesheet is still referenced and won't be removed.
   *
   * @param update true, if the reference counts should be updated, false otherwise.
   * @param es the element stylesheet that should be removed.
   * @return true, if the stylesheet was removed, false otherwise.
   */
  protected boolean remove(final ElementStyleSheet es, final boolean update)
  {
    if (contains(es) == false)
    {
      return true;
    }
    else
    {
      final StyleCollectionEntry se = findStyleSheet(es.getName(), es.getId());
      if (se.getReferenceCount() != 0)
      {
        return false;
      }

      // finally remove the stylesheet itself ...
      if (styleSheets.remove(es.getName(), se) == false)
      {
        return false;
      }
      else
      {
        // Log.debug ("Successfully removed: " + es.getName());
        es.unregisterStyleSheetCollection(this);
      }

      // check whether we can remove the parents ...
      final List parents = es.getParents();
      for (int i = 0; i < parents.size(); i++)
      {
        final ElementStyleSheet esp = (ElementStyleSheet) parents.get(i);
        final StyleCollectionEntry sep = findStyleSheet(esp.getName(), esp.getId());
        sep.setReferenceCount(sep.getReferenceCount() - 1);
        remove(esp, false);
      }
      final List defaultParents = es.getDefaultParents();
      for (int i = 0; i < defaultParents.size(); i++)
      {
        final ElementStyleSheet esp = (ElementStyleSheet) defaultParents.get(i);
        final StyleCollectionEntry sep = findStyleSheet(esp.getName(), esp.getId());
        sep.setReferenceCount(sep.getReferenceCount() - 1);
        remove(esp, false);
      }
      if (update)
      {
        updateReferences();
      }
      return true;
    }

  }

  /**
   * Returns the names of all registered stylesheets as iterator. There can
   * be more than one stylesheet be registered with a certain name.
   *
   * @return the names of all stylesheets.
   */
  public Iterator keys()
  {
    final Set keySet = styleSheets.keySet();
    return Collections.unmodifiableSet(keySet).iterator();
  }

  /**
   * Prints debug messages.
   */
  /*
  public void debug()
  {
    Log.debug ("DEBUG----------------------------------------------");
    Iterator it = keys();
    while (it.hasNext())
    {
      String name = (String) it.next();
      Iterator ses = styleSheets.getAll (name);
      while (ses.hasNext())
      {
        StyleCollectionEntry se = (StyleCollectionEntry) ses.next();
        Log.debug ("ES: " + name + " RefC: " + se.getReferenceCount());
      }
    }

  }
  */
}

⌨️ 快捷键说明

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