configdescriptionmodel.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 484 行 · 第 1/2 页

JAVA
484
字号
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner (taquera@sherito.org);
 *
 * (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * ------------------------------
 * ConfigDescriptionModel.java
 * ------------------------------
 * (C)opyright 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: ConfigDescriptionModel.java,v 1.8 2003/11/07 18:33:52 taqua Exp $
 *
 * Changes
 * -------------------------
 * 26-Jul-2003 : Initial version
 *
 */

package org.jfree.report.modules.gui.config.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import javax.swing.AbstractListModel;
import javax.xml.parsers.ParserConfigurationException;

import org.jfree.report.modules.gui.config.xml.DOMUtilities;
import org.jfree.report.modules.gui.config.xml.DOMWriter;
import org.jfree.report.util.CharacterEntityParser;
import org.jfree.report.util.ReportConfiguration;
import org.jfree.report.util.StringUtil;
import org.jfree.xml.writer.AttributeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This list model implementation collects all config description entries
 * defined in JFreeReport. This model is used to create a configuration
 * key definition; it directly manipulates the metadata for the keys as stored
 * in the config-description.xml file.
 * 
 * @author Thomas Morgner
 */
public class ConfigDescriptionModel extends AbstractListModel
{
  /**
   * Compares an config description entry against an other entry.
   * This simple implementation just compares the names of the two entries.
   */
  private static class ConfigEntryComparator implements Comparator
  {
    /**
     * DefaultConstructor.
     *
     */
    public ConfigEntryComparator ()
    {
    }
    
    /**
     * Compares its two arguments for order.  Returns a negative integer,
     * zero, or a positive integer as the first argument is less than, equal
     * to, or greater than the second.<p>
     *
     * @param o1 the first object to compare
     * @param o2 the second object to compare
     * @return an integer indicating the comparison result.
     */
    public int compare(final Object o1, final Object o2)
    {
      if (o1 == null && o2 == null)
      {
        return 0;
      }
      if (o1 == null)
      {
        return -1;
      }
      if (o2 == null)
      {
        return 1;
      }
      final ConfigDescriptionEntry e1 = (ConfigDescriptionEntry) o1;
      final ConfigDescriptionEntry e2 = (ConfigDescriptionEntry) o2;
      return e1.getKeyName().compareTo(e2.getKeyName());
    }
  }

  /** The content of this list; all config description entries. */
  private final ArrayList content;

  /**
   * Creates a new, initially empty ConfigDescriptionModel.
   */
  public ConfigDescriptionModel()
  {
    content = new ArrayList();
  }

  /**
   * Adds the given entry to the end of the list.
   * 
   * @param entry the new entry.
   */
  public void add (final ConfigDescriptionEntry entry)
  {
    if (entry == null)
    {
      throw new NullPointerException ("Entry is null.");
    }
    // Only add unique elements ...
    if (content.contains(entry) == false)
    {
      content.add (entry);
    }
    updated();
  }

  /**
   * Removes the given entry from the list.
   * 
   * @param entry the entry that should be removed.
   */
  public void remove (final ConfigDescriptionEntry entry)
  {
    if (entry == null)
    {
      throw new NullPointerException("The given entry is null.");
    }
    content.remove(entry);
    updated();
  }

  /**
   * Returns the entry stored on the given list position.
   * 
   * @param pos the position
   * @return the entry
   * @throws IndexOutOfBoundsException if the position is invalid.
   */
  public ConfigDescriptionEntry get (final int pos)
  {
    return (ConfigDescriptionEntry) content.get(pos);
  }

  /**
   * Fires an contents changed event for all elements in the list.
   */
  public void updated ()
  {
    fireContentsChanged(this, 0, getSize());
  }

  /**
   * Returns the index of the given entry or -1, if the entry is not 
   * in the list.
   * 
   * @param entry the entry whose position should be searched. 
   * @return the position of the entry
   */
  public int indexOf (final ConfigDescriptionEntry entry)
  {
    if (entry == null)
    {
      throw new NullPointerException("The given entry is null.");
    }
    return content.indexOf(entry);
  }

  /**
   * Checks, whether the given entry is already contained in this list.
   * 
   * @param entry the entry that should be checked.
   * @return true, if the entry is already added, false otherwise.
   */
  public boolean contains (final ConfigDescriptionEntry entry)
  {
    if (entry == null)
    {
      throw new NullPointerException("The given entry is null.");
    }
    return content.contains(entry);
  }

  /**
   * Sorts the entries of the list. Be aware that calling this method
   * does not fire an updat event; you have to do this manually.
   */
  public void sort ()
  {
    Collections.sort(content, new ConfigEntryComparator());
  }

  /**
   * Returns the contents of this model as object array.
   * 
   * @return the contents of the model as array.
   */
  public ConfigDescriptionEntry[] toArray()
  {
    return (ConfigDescriptionEntry[]) content.toArray
        (new ConfigDescriptionEntry[content.size()]);
  }

  /**
   * Returns the length of the list.
   * 
   * @return the length of the list
   */
  public int getSize()
  {
    return content.size();
  }

⌨️ 快捷键说明

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