📄 reportpane.java
字号:
final int w = (int) ((pageFormat.getWidth() + PAPERBORDER_PIXEL) * zoomFactor);
final int h = (int) ((pageFormat.getHeight() + PAPERBORDER_PIXEL) * zoomFactor);
super.setSize(w, h);
firePropertyChange(ZOOMFACTOR_PROPERTY, new Double(oldzoom), new Double(factor));
}
/**
* Gets the currently set zoom factor.
*
* @return the zoom factor.
*/
public float getZoomFactory()
{
return zoomFactor;
}
/**
* 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);
/**
* 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);
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();
/** 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);
}
}
/**
* 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())
{
throw new IllegalStateException("Already paginating");
}
synchronized (paginateLock)
{
setPageStateList(null);
paginateLock.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.setPaginating(false);
}
}
}
/**
* 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. */
public void dispose()
{
// 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.
*
* @param state the state.
*/
public void repaginationUpdate(RepaginationState state)
{
if (repaginationListenersCache == null)
{
repaginationListenersCache = repaginationListeners.toArray();
}
for (int i = 0; i < repaginationListenersCache.length; i++)
{
RepaginationListener l = (RepaginationListener) repaginationListenersCache[i];
l.repaginationUpdate(state);
}
}
public void addRepaginationListener (RepaginationListener listener)
{
if (listener == null)
{
throw new NullPointerException("Listener must not be null.");
}
repaginationListenersCache = null;
repaginationListeners.add(listener);
}
public void removeRepaginationListener (RepaginationListener listener)
{
if (listener == null)
{
throw new NullPointerException("Listener must not be null.");
}
repaginationListenersCache = null;
repaginationListeners.remove(listener);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -