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

📄 tablegridlayout.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (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.
 *
 * --------------------
 * TableGridLayout.java
 * --------------------
 * (C)opyright 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: TableGridLayout.java,v 1.8.2.1 2003/12/21 23:28:46 taqua Exp $
 *
 * Changes
 * -------
 * 25-Jan-2003 : Initial version
 * 24-Feb-2003 : Fixed Checkstyle issues (DG);
 *
 */
package org.jfree.report.modules.output.table.base;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * The table grid layout is used to layout the collected TableCellData object from
 * the TableGrid into the table. The cells position is calculated by comparing the cell
 * bounds with the collected x and y-cuts.
 *
 * @author Thomas Morgner
 */
public class TableGridLayout
{
  /**
   * The Element class encapsulates all TableCellData-object within a single grid cell.
   * When the report is badly designed, cells may overlay. This will cause trouble when
   * the table is printed later, so we do not accept multiple non-background
   * TableCellData objects.
   * <p>
   * TableCellData backgrounds can be combined to create complex
   * background structures. Usually, the cell backgrounds must be merged before the
   * cellbackground can be applied to the generated table. As this is dependent
   * on the table implementation, we do not assume anything here, and just collect all
   * backgrounds in the list.
   *
   * @see TableCellData#isBackground
   */
  public static class Element
  {
    /** The root position, a data carrying TableCellData. */
    private TableGridPosition root;
    /** The backgrounds for the data cell. */
    private final ArrayList backGrounds;

    /**
     * Creates a new element. The element has no initial background or
     * cell data.
     */
    public Element()
    {
      backGrounds = new ArrayList();
    }

    /**
     * Adds a new TableGridPosition to the element.
     * <p>
     * If the new position contains a data cell: <br>
     * if no data cell has been set so far, the cell is added as new
     * data element. if there is a data cell already set, the new cell data
     * is ignored.
     * <p>
     * If the new position contains a background information: <br>
     * if no data cell has been set so far, the cell is added to the list of
     * available backgrounds. When a new data cell is added, the backgrounds
     * are validated. A cell background is valid, if it fully contains the
     * data cell area. <br>
     * If there is a data cell already set, the background is validated imediatly
     * and added if valid.
     * <p>
     * Invalid backgrounds or duplicate data cells are discarded.
     *
     * @param pos the new TableGridPosition to be added to this element.
     * @throws NullPointerException if the given position is null
     */
    public void add(final TableGridPosition pos)
    {
      if (pos == null)
      {
        throw new NullPointerException();
      }

      if (root == null)
      {
        if (pos.getElement().isBackground())
        {
          backGrounds.add(0, pos);
        }
        else
        {
          root = pos;
          final Iterator it = backGrounds.iterator();
          while (it.hasNext())
          {
            final TableGridPosition gpos = (TableGridPosition) it.next();
            if (gpos.contains(root) == false)
            {
              it.remove();
            }
          }
        }
      }
      else
      {
        if (pos.getElement().isBackground())
        {
          if (pos.contains(root))
          {
            backGrounds.add(0, pos);
          }
        }
        else
        {
          /*
          Log.warn (new Log.SimpleMessage("Root already added: " , pos.getElement().getBounds()));
          Log.warn (new Log.SimpleMessage("+            added: " , root.getElement().getBounds()));
          Log.warn (new Log.SimpleMessage("+            added: " , pos.getElement().debugChunk));
          Log.warn (new Log.SimpleMessage("+            added: Col=" , new Integer(root.getCol()),
                                                            "  Row=" , new Integer(root.getRow())));
          */
          pos.setInvalidCell(true);
        }
      }
    }

    /**
     * Returns the list of backgrounds of this element.
     *
     * @return the collected backgrounds of the element.
     */
    public List getBackground()
    {
      return backGrounds;
    }

    /**
     * Returns the data cell for this element. This can be null if no data cell
     * has been defined.
     *
     * @return the data cell or null, if this element does not have any data defined.
     */
    public TableGridPosition getRoot()
    {
      return root;
    }

    /**
     * Creates a string representation of this Element.
     *
     * @return a string representation of the element for debugging purposes.
     */
    public String toString()
    {
      final StringBuffer buffer = new StringBuffer();
      buffer.append("TableGridLayout.Element={root=");
      buffer.append(root);
      buffer.append(", backgrounds=");
      buffer.append(backGrounds);
      buffer.append("}");
      return buffer.toString();
    }
  }

  /** The table data as table. */
  private final Object[][] data;
  /** the collected xcuts, sorted. */
  private final int[] xCuts;
  /** the collected ycuts, sorted. */
  private final int[] yCuts;
  /** the maximum horizontal position the table ever reached. */
  private int maxX;
  /** the maximum vertical position the table ever reached. */
  private int maxY;

  /**
   * Creates a new TableGridLayout.
   *
   * @param pxCuts the collected horizontal cell bounds from the TableGrid,.
   * @param pyCuts the collected vertical cell bounds from the TableGrid.
   * @param positions the positions collected by the table grid.
   */
  public TableGridLayout(final int[] pxCuts, final int[] pyCuts, final TableCellData[] positions)
  {
    this.xCuts = new int[pxCuts.length];
    this.yCuts = new int[pyCuts.length];

    System.arraycopy(pxCuts, 0, xCuts, 0, pxCuts.length);
    System.arraycopy(pyCuts, 0, yCuts, 0, pyCuts.length);

    Arrays.sort(xCuts);
    Arrays.sort(yCuts);

    // +1 for outer boundry ...
    final int width = xCuts.length;
    final int height = yCuts.length;
//
//    Log.info ("Created GridLayout with " + width + ", " + height);
//    for (int i = 0; i < xCuts.length; i++)
//    {
//      Log.info ("X-Cuts: " + xCuts[i]);
//    }
//    for (int i = 0; i < yCuts.length; i++)
//    {
//      Log.info ("Y-Cuts: " + yCuts[i]);
//    }

    data = new Object[width][height];

    for (int i = 0; i < positions.length; i++)
    {
      final TableCellData pos = positions[i];
      add(pos);
    }
  }

  /**
   * Adds the table cell data position into the table grid. The coordinates are
   * calculated by using the sorted x- and y-cuts.
   *
   * @param pos the new position that should be added into the grid
   */
  protected void add(final TableCellData pos)
  {
    final Rectangle2D bounds = pos.getBounds();
    final int maxBoundsX = (int) (bounds.getX() + bounds.getWidth());
    final int maxBoundsY = (int) (bounds.getY() + bounds.getHeight());

    final TableGridPosition gPos;
    if (bounds.getWidth() == 0 && bounds.getHeight() == 0)
    {
      return;
    }
    if (bounds.getHeight() == 0)
    {
      final int col = findBoundary(xCuts, (int) bounds.getX());

⌨️ 快捷键说明

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