reportpane.java

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

JAVA
1,021
字号

  /**
   * Returns the preferred size of the pane - used by the layout manager.
   * @return The preferred size of the pane;
   *
   */
  public Dimension getPreferredSize()
  {
    return this.getSize();
  }

  /**
   * Returns the minimum size of the pane - used by the layout manager.
   * @return The minimum size of the pane;
   *
   */
  public Dimension getMinimumSize()
  {
    return this.getSize();
  }

  /**
   * Paints the component, which means drawing the current page of the report.
   *
   * @param g  the graphics device.
   *
   */
  public void paintComponent(final Graphics g)
  {
    if (paginateLock.isPaginating())
    {
      return;
    }
    try
    {
      final Graphics2D g2org = (Graphics2D) g;

      if (graphCache == null)
      {
        final PageFormat pageFormat = getPageFormat();

        final Dimension size = getSize();

        final float outerX = 0;
        final float outerY = 0;

        final float realouterW = (float) size.getWidth() - 1;
        final float realouterH = (float) size.getHeight() - 1;

        final float outerW = (float) pageFormat.getWidth();
        final float outerH = (float) pageFormat.getHeight();

        final float innerX = (float) pageFormat.getImageableX();
        final float innerY = (float) pageFormat.getImageableY();
        final float innerW = (float) pageFormat.getImageableWidth();
        final float innerH = (float) pageFormat.getImageableHeight();

        //double paperBorder = paperBorderPixel * zoomFactor;

        final Graphics2D g2;
        if (realouterW > 1500 || realouterH > 1500)
        {
          graphCache = null;
          g2 = g2org;
        }
        else
        {
          graphCache =
              g2org.getDeviceConfiguration().createCompatibleImage(
                  (int) (realouterW),
                  (int) (realouterH));
          g2 = graphCache.createGraphics();
        }


        g2.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        /** Prepare background **/
        Rectangle2D pageArea = new Rectangle2D.Float(outerX, outerY, realouterW, realouterH);
        g2.setPaint(getBackground());
        g2.fill(pageArea);
        g2.transform(AffineTransform.getScaleInstance(zoomFactor, zoomFactor));

        /** Prepare background **/
        pageArea = new Rectangle2D.Float(outerX, outerY, outerW - 2, outerH - 2);

        g2.setPaint(Color.white);
        g2.fill(pageArea);

        /** Generate the page ... */
        performGeneratePage(g2);

        /**
         * The border around the printable area is painted when the corresponding property is
         * set to true.
         */
        final Rectangle2D printingArea = new Rectangle2D.Float(innerX, innerY, innerW, innerH);

        /** Paint Page Shadow */
        final Rectangle2D southborder =
            new Rectangle2D.Float(
                outerX + PAPERBORDER_PIXEL - 2,
                outerY + outerH - 2,
                outerW,
                PAPERBORDER_PIXEL);

        g2.setPaint(UIManager.getColor("controlShadow"));

        g2.fill(southborder);
        final Rectangle2D eastborder =
            new Rectangle2D.Float(
                outerW - 2,
                outerY - 2 + PAPERBORDER_PIXEL,
                PAPERBORDER_PIXEL,
                outerH);

        g2.fill(eastborder);
        final Rectangle2D transPageArea;
        if (zoomFactor > 1.49)
        {
          transPageArea = new Rectangle2D.Float(outerX, outerY, outerW - 1, outerH - 1);
        }
        else
        {
          transPageArea = new Rectangle2D.Float(outerX, outerY, outerW - 2, outerH - 2);
        }

        g2.setPaint(Color.black);
        g2.draw(transPageArea);
        if (isBorderPainted())
        {
          g2.setPaint(Color.gray);
          g2.draw(printingArea);
        }

        // Dispose the temporary graphics object, it is no longer needed
        // if graphcache is null, the image is not cached, so do not dispose...
        if (graphCache != null)
        {
          g2.dispose();
        }
      }

      if (graphCache != null)
      {
        g2org.drawImage(
            graphCache,
            new AffineTransformOp(
                g2org.getDeviceConfiguration().getDefaultTransform(),
                g2org.getRenderingHints()),
            0,
            0);
      }
      else
      {
        super.paintComponent(g);
      }
    }
    catch (Exception e)
    {
      setError(e);
      super.paintComponent(g);
    }
  }

  /**
   * Generates the page and draws that page on the given Graphics2D object.
   * @param g2 the target graphics.
   * @throws OutputTargetException if an error occured.
   */
  private void performGeneratePage(final Graphics2D g2)
      throws OutputTargetException
  {
    final G2OutputTarget target = new G2OutputTarget(g2, getPageFormat());
    target.open();
    getProcessor().setOutputTarget(target);

    try
    {
      if (!isPaginated())
      {
        repaginate();
      }
    }
    catch (ReportProcessingException rpe)
    {
      Log.error("Repaginate failed: ", rpe);
      setError(rpe);
    }

    final int pageNumber = getPageNumber();
    if (pageNumber > 0)
    {
      final ReportState state = getPageStateList().get(pageNumber - 1);
      try
      {
        getProcessor().processPage(state, target);
      }
      catch (ReportProcessingException rpe)
      {
        Log.error("Repaginate failed: ", rpe);
        setError(rpe);
      }
    }
    else
    {
      Log.error("PageNumber is invalid after repaginating: " + pageNumber);
    }

    getProcessor().setOutputTarget(null);
    target.close();
  }

  /**
   * Supports the Printable interface by drawing a report on a single page.
   *
   * @param g  the graphics device.
   * @param pf  the page format.
   * @param pageIndex  the page index.
   *
   * @return PAGE_EXISTS if the page is rendered successfully
   *         or NO_SUCH_PAGE if <code>pageIndex</code> specifies a non-existent page.
   */
  public int print(final Graphics g, final PageFormat pf, final int pageIndex)
  {
    final Graphics2D g2 = (Graphics2D) g;

    try
    {
      final G2OutputTarget target = new G2OutputTarget(g2, pf);
      getProcessor().setOutputTarget(target);
      target.open();
      if (!isPaginated())
      {
        repaginate();
      }

      if (pageIndex > pageCount - 1)
      {
        return NO_SUCH_PAGE;
      }

      final ReportState state = getPageStateList().get(pageIndex);
      getProcessor().processPage(state, target);
      getProcessor().setOutputTarget(null);
      target.close();
    }
    catch (OutputTargetException oe)
    {
      Log.error("Report generated an error", oe);
      setError(oe);
    }
    catch (ReportProcessingException rpe)
    {
      Log.error("Report generated an error", rpe);
      setError(rpe);
    }
    return PAGE_EXISTS;
  }

  /**
   * Returns true, if this ReportPane is currently paginating.
   *
   * @return true if the report pane is paginating.
   */
  public boolean isPaginating()
  {
    return paginateLock.isPaginating();
  }

  /**
   * Repaginates this report according to the OutputTarget given.
   *
   * This function synchronizes on the paginateLock. While a ReportPane is paginating,
   * no other pane may print.
   *
   * @throws ReportProcessingException if there is a problem processing the report.
   */
  public void repaginate()
      throws ReportProcessingException
  {
    if (isPaginated())
    {
      // Is already done
      return;
    }
    if (isPaginating() || isPrinting())
    {
      throw new IllegalStateException("Already paginating");
    }

    synchronized (paginateLock)
    {
      setPageStateList(null);
      //paginateLock.setPaginateState(PAGINATE_PAGINATING);
      setPaginating(true);

      boolean addedOutputTarget = false;
      if (getProcessor().getOutputTarget() == null)
      {
        try
        {
          final OutputTarget target = new DummyOutputTarget(
              new G2OutputTarget(G2OutputTarget.createEmptyGraphics(), getPageFormat()));
          target.open();
          getProcessor().setOutputTarget(target);
        }
        catch (OutputTargetException oe)
        {
          // does not happen when using the dummy target, but just in case
          Log.error("Unable to repaginate: Error", oe);
        }
        addedOutputTarget = true;
      }

      try
      {
        final ReportStateList list = processor.repaginate();
        int pageCount = 0;
        int pageNr = 0;
        if (list.size() > 0)
        {
          // the report state list stores one state for every page.
          pageNr = 1;
          pageCount = list.size();
        }
        setCurrentPageCount(pageCount);
        setPageNumber(pageNr);
        setPageStateList(list);

      }
      finally
      {
        if (addedOutputTarget)
        {
          getProcessor().setOutputTarget(null);
        }
        //paginateLock.setPaginateState(PAGINATE_IDLE);
        setPaginating(false);
      }
    }
  }

  public void setPrinting(boolean printing)
  {
    boolean oldPrinting = isPrinting();
    synchronized (paginateLock)
    {
      if (printing)
      {
        paginateLock.setPaginateState(PAGINATE_PRINTING);
      }
      else
      {
        paginateLock.setPaginateState(PAGINATE_IDLE);
      }
    }
    firePropertyChange(PRINTING_PROPERTY, oldPrinting, printing);
  }

  protected void setPaginating (boolean paginating)
  {
    boolean oldPaginating = isPaginating();
    synchronized (paginateLock)
    {
      if (paginating)
      {
        paginateLock.setPaginateState(PAGINATE_PAGINATING);
      }
      else
      {
        paginateLock.setPaginateState(PAGINATE_IDLE);
      }
    }
    firePropertyChange(PAGINATING_PROPERTY, oldPaginating, paginating);
  }

  public boolean isPrinting()
  {
    return paginateLock.isPrinting();
  }

  /**
   * sets the last error occurred. The error can be cleared with the clearError() function.
   *
   * @param error  the error.
   */
  public void setError(final Exception error)
  {
    final Exception oldError = this.error;
    this.error = error;
    firePropertyChange(ERROR_PROPERTY, oldError, error);
  }

  /**
   * Checks whether an error occurred since the last call to clearError().
   *
   * @return a flag indicating whether or not here is an error.
   */
  public boolean hasError()
  {
    return (error != null);
  }

  /**
   * Returns the report for this ReportPane.
   *
   * @return the report.
   */
  public JFreeReport getReport()
  {
    return report;
  }

  /**
   * Clears the error state.
   */
  public void clearError()
  {
    setError(null);
  }

  /**
   * Queries the error state for this ReportPane.
   *
   * @return the last error.
   */
  public Exception getError()
  {
    return error;
  }

  /**
   * Returns the report processor.
   *
   * @return the report processor.
   */
  protected PageableReportProcessor getProcessor()
  {
    return processor;
  }

  /**
   * Free some of the used memory. It is the duty of the caller to
   * interrupt a possible pagination process.
   */
  public void dispose()
  {
    // Log.debug ("Dispose the report pane ...");
    // clean up a little bit
    // this is safe, the report is repaginated if needed
    setPageStateList(null);
    // is regenerated on next repaint
    graphCache = null;
  }

  /**
   * Receives notification of a repagination update. This method is public
   * as an implementation side effect.
   *
   * @param state  the state.
   */
  public void repaginationUpdate(final RepaginationState state)
  {
    if (repaginationListenersCache == null)
    {
      repaginationListenersCache = repaginationListeners.toArray();
    }

    for (int i = 0; i < repaginationListenersCache.length; i++)
    {
      final RepaginationListener l = (RepaginationListener) repaginationListenersCache[i];
      l.repaginationUpdate(state);
    }
  }

  /**
   * Adds a repagination listener to this component. The listener will be
   * informed about the pagination progress.
   *
   * @param listener the listener to be added.
   */
  public void addRepaginationListener(final RepaginationListener listener)
  {
    if (listener == null)
    {
      throw new NullPointerException("Listener must not be null.");
    }
    repaginationListenersCache = null;
    repaginationListeners.add(listener);
  }

  /**
   * Removes the specified repagination listener from this component.
   *
   * @param listener the listener to be removed.
   */
  public void removeRepaginationListener(final RepaginationListener listener)
  {
    if (listener == null)
    {
      throw new NullPointerException("Listener must not be null.");
    }
    repaginationListenersCache = null;
    repaginationListeners.remove(listener);
  }
}

⌨️ 快捷键说明

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