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

📄 reporttreemodel.java

📁 报表设计软件,很好的
💻 JAVA
字号:
/**
 * ========================================
 * 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.
 *
 * ------------------------------
 * ReportTreeModel.java
 * ------------------------------
 * (C)opyright 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: ReportTreeModel.java,v 1.2 2004/04/20 18:55:02 taqua Exp $
 *
 * Changes
 * -------------------------
 * 20-Oct-2003 : Initial version
 *
 */

package org.jfree.designer.visualeditor.treemodel;

import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.jfree.report.Band;
import org.jfree.report.Element;
import org.jfree.report.Group;
import org.jfree.report.JFreeReport;

public final class ReportTreeModel
        implements TreeModel
{
  private final ArrayList listeners;
  private final JFreeReport report;
  private final ReportDefinitionNode rootNode;

  public ReportTreeModel (final JFreeReport report)
  {
    listeners = new ArrayList();
    this.report = report;
    this.rootNode = new ReportDefinitionNode(report);
    initialize();
  }

  public final void initialize ()
  {
    rootNode.clear();
    final int groupCount = report.getGroupCount();
    rootNode.addNode(new BandNode(rootNode, report.getPageHeader(), "PageHeader"));
    rootNode.addNode(new BandNode(rootNode, report.getReportHeader(), "ReportHeader"));
    for (int i = 0; i < groupCount; i++)
    {
      final Group group = report.getGroup(i);
      rootNode.addNode(new BandNode(rootNode, group.getHeader(),
              "GroupHeader [" + group.getName() + "]"));
    }
    rootNode.addNode(new BandNode(rootNode, report.getItemBand(), "ItemBand"));
    for (int i = groupCount - 1; i >= 0; i--)
    {
      final Group group = report.getGroup(i);
      rootNode.addNode(new BandNode(rootNode, group.getFooter(),
              "GroupFooter [" + group.getName() + "]"));
    }
    rootNode.addNode(new BandNode(rootNode, report.getReportFooter(), "ReportFooter"));
    rootNode.addNode(new BandNode(rootNode, report.getPageFooter(), "PageFooter"));

    fireTreeModelChanged();
  }

  protected final void fireTreeModelChanged ()
  {
    for (int i = 0; i < listeners.size(); i++)
    {
      final TreeModelListener listener = (TreeModelListener) listeners.get(i);
      listener.treeStructureChanged(new TreeModelEvent(this, new TreePath(rootNode)));
    }
  }

  /**
   * Adds a listener for the <code>TreeModelEvent</code> posted after the tree changes.
   *
   * @param l the listener to add
   * @see #removeTreeModelListener
   */
  public final void addTreeModelListener (final TreeModelListener l)
  {
    if (l == null)
    {
      throw new NullPointerException();
    }
    listeners.add(l);
  }

  /**
   * Removes a listener previously added with <code>addTreeModelListener</code>.
   *
   * @param l the listener to remove
   * @see #addTreeModelListener
   */
  public final void removeTreeModelListener (final TreeModelListener l)
  {
    if (l == null)
    {
      throw new NullPointerException();
    }
    listeners.remove(l);
  }

  /**
   * Returns the child of <code>parent</code> at index <code>index</code> in the parent's
   * child array.  <code>parent</code> must be a node previously obtained from this data
   * source. This should not return <code>null</code> if <code>index</code> is a valid
   * index for <code>parent</code> (that is <code>index >= 0 && index <
   * getChildCount(parent</code>)).
   *
   * @param parent a node in the tree, obtained from this data source
   * @return the child of <code>parent</code> at index <code>index</code>
   */
  public final Object getChild (final Object parent, final int index)
  {
    final TreeNode node = (TreeNode) parent;
    return node.getChildAt(index);
  }

  /**
   * Returns the number of children of <code>parent</code>. Returns 0 if the node is a
   * leaf or if it has no children.  <code>parent</code> must be a node previously
   * obtained from this data source.
   *
   * @param parent a node in the tree, obtained from this data source
   * @return the number of children of the node <code>parent</code>
   */
  public final int getChildCount (final Object parent)
  {
    final TreeNode node = (TreeNode) parent;
    return node.getChildCount();
  }

  /**
   * Returns the index of child in parent.  If <code>parent</code> is <code>null</code> or
   * <code>child</code> is <code>null</code>, returns -1.
   *
   * @param parent a note in the tree, obtained from this data source
   * @param child  the node we are interested in
   * @return the index of the child in the parent, or -1 if either <code>child</code> or
   *         <code>parent</code> are <code>null</code>
   */
  public final int getIndexOfChild (final Object parent, final Object child)
  {
    final TreeNode node = (TreeNode) parent;
    return node.getIndex((TreeNode) child);
  }

  /**
   * Returns the root of the tree.  Returns <code>null</code> only if the tree has no
   * nodes.
   *
   * @return the root of the tree
   */
  public final Object getRoot ()
  {
    return rootNode;
  }

  /**
   * Returns <code>true</code> if <code>node</code> is a leaf. It is possible for this
   * method to return <code>false</code> even if <code>node</code> has no children. A
   * directory in a filesystem, for example, may contain no files; the node representing
   * the directory is not a leaf, but it also has no children.
   *
   * @param node a node in the tree, obtained from this data source
   * @return true if <code>node</code> is a leaf
   */
  public final boolean isLeaf (final Object node)
  {
    return ((TreeNode) node).isLeaf();
  }

  /**
   * Messaged when the user has altered the value for the item identified by
   * <code>path</code> to <code>newValue</code>. If <code>newValue</code> signifies a
   * truly new value the model should post a <code>treeNodesChanged</code> event.
   *
   * @param path     path to the node that the user has altered
   * @param newValue the new value from the TreeCellEditor
   */
  public final void valueForPathChanged (final TreePath path, final Object newValue)
  {
    // we dont support direct tree editing ...
  }

  public final TreePath getPathForElement (final Element e)
  {
    final ArrayList list = new ArrayList();
    list.add(e);
    Band parent = e.getParent();
    while (parent != null)
    {
      list.add(parent);
      parent = parent.getParent();
    }
    final ArrayList path = new ArrayList(list.size() + 1);
    path.add(rootNode);
    buildElementPath(list, list.size() - 1, rootNode, path);
    return new TreePath(path.toArray());
  }

  private void buildElementPath
          (final ArrayList elementList, final int index,
           final TreeNode currentNode, final ArrayList path)
  {
    if (index < 0)
    {
      return;
    }
    // root band from list of elements ...
    if (currentNode.getAllowsChildren() == false)
    {
      return;
    }
    final Element parent = (Element) elementList.get(index);
    final Enumeration enum = currentNode.children();
    while (enum.hasMoreElements())
    {
      final TreeNode node = (TreeNode) enum.nextElement();
      if (node instanceof ElementNode == false)
      {
        continue;
      }
      final ElementNode bnode = (ElementNode) node;
      if (bnode.getElement() == parent)
      {
        path.add(bnode);
        buildElementPath(elementList, index - 1, bnode, path);
        return;
      }
    }
  }
}

⌨️ 快捷键说明

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