levelledexpressionlist.java

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

JAVA
1,180
字号
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (C) Copyright 2000-2002, 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.
 *
 * --------------------------
 * LevelledExpressionList.java
 * --------------------------
 * (C)opyright 2000-2002, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: LevelledExpressionList.java,v 1.8 2003/11/07 18:33:48 taqua Exp $
 *
 * Changes
 * -------
 * 05-Dec-2002 : Updates Javadocs (DG);
 * 06-Jul-2003 : Only PageEventListener will receive page events.
 */

package org.jfree.report.function;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.jfree.report.DataRow;
import org.jfree.report.event.LayoutEvent;
import org.jfree.report.event.LayoutListener;
import org.jfree.report.event.PageEventListener;
import org.jfree.report.event.PrepareEventListener;
import org.jfree.report.event.ReportEvent;
import org.jfree.report.event.ReportListener;
import org.jfree.report.util.LevelList;

/**
 * A list of expressions/functions and associated levels.  This class listens for report events,
 * then passes these events on to the expressions and functions in *descending* level order.
 *
 * @author Thomas Morgner
 */
public final class LevelledExpressionList implements ReportListener,
    Cloneable, LayoutListener, PageEventListener
{
  /** error list stores the errors that occur during the event dispatching. */
  private ArrayList errorList;

  /** The current processing level. */
  private int level;

  /** The levels (in descending order). */
  private int[] levels;

  /** The dataRow for all functions. */
  private DataRow dataRow;

  /** The expressions sorted by levels. */
  private Expression[][] levelData;
  /** all data as flat list. */
  private Expression[] flatData;

  /** The number of functions and expressions in this list. */
  private int size;

  /**
   * DefaultConstructor.
   */
  protected LevelledExpressionList()
  {
    errorList = new ArrayList();
    levels = new int[0];
  }

  /**
   * Creates a new list.
   *
   * @param ec  the expressions from the report definition.
   * @param fc  the functions from the report definition.
   */
  public LevelledExpressionList(final ExpressionCollection ec, final ExpressionCollection fc)
  {
    this();
    initialize(ec, fc);
  }

  /**
   * Builds the list of all levels. This is done once after the initialisation,
   * as the functions level is not expected to change after the function was
   * initialized.
   * 
   * @param expressionList the level list from where to build the data.
   * @return the function levels.
   */
  private int[] buildLevels(final LevelList expressionList)
  {
    // copy all levels from the collections to the cache ...
    // we assume, that the collections do not change anymore!
    final ArrayList al = new ArrayList();
    final Iterator it = expressionList.getLevelsDescending();
    while (it.hasNext())
    {
      final Integer level = (Integer) it.next();
      al.add(level);
    }
    final int[] levels = new int[al.size()];
    for (int i = 0; i < levels.length; i++)
    {
      final Integer level = (Integer) al.get(i);
      levels[i] = level.intValue();
    }
    return levels;
  }

  /**
   * Receives notification that report generation has started.
   * <P>
   * The event carries a ReportState.Started state.
   * Use this to prepare the report header.
   *
   * @param event  the event.
   */
  public void reportStarted(final ReportEvent event)
  {
//    clearError(); is done in the prepare event ...

    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          final Function f = (Function) e;
          try
          {
            f.reportStarted(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }

  }

  /**
   * Receives notification that report generation has started.
   * <P>
   * The event carries a ReportState.Started state.  Use this to initialize the report.
   *
   * @param event  the event.
   */
  public void reportInitialized(final ReportEvent event)
  {
    clearError(); // has no prepare event ...

    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          final Function f = (Function) e;
          try
          {
            f.reportInitialized(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that report generation has finished (the last record is read and all
   * groups are closed).
   *
   * @param event  the event.
   */
  public void reportFinished(final ReportEvent event)
  {
    // clearError(); done in the prepare event ...

    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          final Function f = (Function) e;
          try
          {
            f.reportFinished(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that a new page is being started.
   *
   * @param event  the event.
   */
  public void pageStarted(final ReportEvent event)
  {
    // this is an internal event, don't fire prepare or clear the errors.
    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          if (e instanceof PageEventListener)
          {
            final PageEventListener f = (PageEventListener) e;
            try
            {
              f.pageStarted(event);
            }
            catch (Exception ex)
            {
              addError(ex);
            }
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that a new page is being started.
   *
   * @param event  the event.
   */
  public void pageCanceled(final ReportEvent event)
  {
    // this is an internal event, don't fire prepare or clear the errors.
    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof PageEventListener)
        {
          final PageEventListener f = (PageEventListener) e;
          try
          {
            f.pageCanceled(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }


  /**
   * Receives notification that a page is completed.
   *
   * @param event  the event.
   */
  public void pageFinished(final ReportEvent event)
  {
    // this is an internal event, don't fire prepare or clear the errors.
    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          if (e instanceof PageEventListener)
          {
            final PageEventListener f = (PageEventListener) e;
            try
            {
              f.pageFinished(event);
            }
            catch (Exception ex)
            {
              addError(ex);
            }
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that a new group has started.
   * <P>
   * The group can be determined by the report state's getCurrentGroup() function.
   *
   * @param event  the event.
   */
  public void groupStarted(final ReportEvent event)
  {
    // clearError(); done in the prepare event ...

    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          final Function f = (Function) e;
          try
          {
            f.groupStarted(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that a group is finished.
   * <P>
   * The group can be determined by the report state's getCurrentGroup() function.
   *
   * @param event  the event.
   */
  public void groupFinished(final ReportEvent event)
  {
    // clearError(); done in the prepare event ...

    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          final Function f = (Function) e;
          try
          {
            f.groupFinished(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that a group of item bands is about to be processed.
   * <P>
   * The next events will be itemsAdvanced events until the itemsFinished event is raised.
   *
   * @param event  the event.
   */
  public void itemsStarted(final ReportEvent event)
  {
    // clearError(); done in the prepare event ...

    for (int i = 0; i < levels.length; i++)
    {
      final int level = levels[i];
      if (level < getLevel())
      {
        break;
      }
      final Object[] itLevel = levelData[i];
      for (int l = 0; l < itLevel.length; l++)
      {
        final Expression e = (Expression) itLevel[l];
        if (e instanceof Function)
        {
          final Function f = (Function) e;
          try
          {
            f.itemsStarted(event);
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
        else
        {
          try
          {
            if (e.isActive())
            {
              e.getValue();
            }
          }
          catch (Exception ex)
          {
            addError(ex);
          }
        }
      }
    }
  }

  /**
   * Receives notification that a group of item bands has been completed.
   * <P>
   * The itemBand is finished, the report starts to close open groups.

⌨️ 快捷键说明

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