From ed9e5c672fdfacf93e02fad4b3c635d016919a20 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Mon, 10 Jul 2006 19:56:49 +0000 Subject: [PATCH] Bugzilla #39709: Some clean-up in the AWT Renderer/Preview. Submitted by: Stefan Ziel Original patch slightly modified for (hopefully) better naming. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@420623 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/render/awt/AWTRenderer.java | 76 ++++++------------- .../fop/render/awt/viewer/PreviewDialog.java | 52 +++++++++++-- 2 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/java/org/apache/fop/render/awt/AWTRenderer.java b/src/java/org/apache/fop/render/awt/AWTRenderer.java index f04158b83..a52e75470 100644 --- a/src/java/org/apache/fop/render/awt/AWTRenderer.java +++ b/src/java/org/apache/fop/render/awt/AWTRenderer.java @@ -28,9 +28,6 @@ package org.apache.fop.render.awt; // Java import java.awt.Color; import java.awt.Dimension; -import java.awt.Toolkit; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; import java.awt.geom.Rectangle2D; import java.awt.print.PageFormat; import java.awt.print.Pageable; @@ -44,8 +41,8 @@ import org.apache.fop.apps.MimeConstants; import org.apache.fop.area.Area; import org.apache.fop.area.PageViewport; import org.apache.fop.render.awt.viewer.PreviewDialog; +import org.apache.fop.render.awt.viewer.StatusListener; import org.apache.fop.render.awt.viewer.Renderable; -import org.apache.fop.render.awt.viewer.Translator; import org.apache.fop.render.java2d.Java2DRenderer; /** @@ -58,39 +55,34 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { /** The MIME type for AWT-Rendering */ public static final String MIME_TYPE = MimeConstants.MIME_FOP_AWT_PREVIEW; - /** The resource bundle used for AWT messages. */ - protected Translator translator = null; - /** flag for debugging */ public boolean debug; /** If true, preview dialog is shown. */ public boolean dialogDisplay = true; - /** - * The preview dialog frame used for display of the documents. Also used as - * the AWT Component for FontSetup in generating valid font measures. - */ - protected PreviewDialog frame; - /** * Renderable instance that can be used to reload and re-render a document after * modifications. */ protected Renderable renderable; + + /** + * Will be notified when rendering progresses + */ + protected StatusListener statusListener = null; /** * Creates a new AWTRenderer instance. */ public AWTRenderer() { - translator = new Translator(); } /** @see org.apache.fop.render.Renderer#setUserAgent(org.apache.fop.apps.FOUserAgent) */ public void setUserAgent(FOUserAgent foUserAgent) { super.setUserAgent(foUserAgent); if (dialogDisplay) { - createPreviewDialog(); + setStatusListener(PreviewDialog.createPreviewDialog(userAgent, this.renderable)); } } @@ -118,17 +110,16 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { public void renderPage(PageViewport pageViewport) throws IOException { super.renderPage(pageViewport); - if (frame != null) { - frame.setInfo(); + if (statusListener != null) { + statusListener.notifyPageRendered(); } } /** @see org.apache.fop.render.Renderer#stopRenderer() */ public void stopRenderer() throws IOException { super.stopRenderer(); - if (frame != null) { - frame.setStatus(translator.getString("Status.Show")); - frame.reload(); // Refreshes view of page + if (statusListener != null) { + statusListener.notifyRendererStopped(); // Refreshes view of page } } @@ -149,32 +140,7 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { / userAgent.getTargetPixelUnitToMillimeter(); int bitmapWidth = (int) ((pageWidth * scaleX) + 0.5); int bitmapHeight = (int) ((pageHeight * scaleY) + 0.5); - return new Dimension(bitmapWidth, bitmapHeight); - } - - /** Creates and initialize the AWT Viewer main window */ - private PreviewDialog createPreviewDialog() { - frame = new PreviewDialog(userAgent, this.renderable); - frame.addWindowListener(new WindowAdapter() { - public void windowClosed(WindowEvent we) { - System.exit(0); - } - }); - - // Centers the window - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - Dimension frameSize = frame.getSize(); - if (frameSize.height > screenSize.height) { - frameSize.height = screenSize.height; - } - if (frameSize.width > screenSize.width) { - frameSize.width = screenSize.width; - } - frame.setLocation((screenSize.width - frameSize.width) / 2, - (screenSize.height - frameSize.height) / 2); - frame.setStatus(translator.getString("Status.Build.FO.tree")); - frame.setVisible(true); - return frame; + return new Dimension(bitmapWidth, bitmapHeight); } /** @see java.awt.print.Pageable#getPageFormat(int) */ @@ -222,11 +188,6 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { return true; // TODO true? } - /** @return the Translator for this renderer */ - public Translator getTranslator() { - return translator; - } - /** @see org.apache.fop.render.AbstractRenderer */ public String getMimeType() { return MIME_TYPE; @@ -272,4 +233,17 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { restoreGraphicsState(); } + /** @return the StatusListener. */ + public StatusListener getStatusListener() { + return statusListener; + } + + /** + * Sets a StatusListener this renderer uses to notify about events. + * @param statusListener The StatusListener to set. + */ + public void setStatusListener(StatusListener statusListener) { + this.statusListener = statusListener; + } + } diff --git a/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java index 5c0b29faf..31221664f 100644 --- a/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java +++ b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java @@ -33,6 +33,8 @@ import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; @@ -60,7 +62,7 @@ import org.apache.fop.render.awt.AWTRenderer; * AWT Viewer main window. * Surrounds a PreviewPanel with a bunch of pretty buttons and controls. */ -public class PreviewDialog extends JFrame { +public class PreviewDialog extends JFrame implements StatusListener { /** The Translator for localization */ protected Translator translator; @@ -99,7 +101,7 @@ public class PreviewDialog extends JFrame { renderer = (AWTRenderer) foUserAgent.getRendererOverride(); this.foUserAgent = foUserAgent; this.renderable = renderable; - translator = renderer.getTranslator(); + translator = new Translator(); //Commands aka Actions Command printAction = new Command(translator.getString("Menu.Print"), "Print") { @@ -238,6 +240,38 @@ public class PreviewDialog extends JFrame { new Insets(0, 0, 0, 0), 0, 0)); getContentPane().add(statusBar, BorderLayout.SOUTH); } + + /** + * Creates and initialize the AWT Viewer main window. + * @param foUserAgent the FO user agent + * @param renderable the target for the rendering + * @return the newly initialized preview dialog + */ + public static PreviewDialog createPreviewDialog(FOUserAgent foUserAgent, + Renderable renderable) { + PreviewDialog frame = new PreviewDialog(foUserAgent, renderable); + + frame.addWindowListener(new WindowAdapter() { + public void windowClosed(WindowEvent we) { + System.exit(0); + } + }); + + // Centers the window + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + Dimension frameSize = frame.getSize(); + if (frameSize.height > screenSize.height) { + frameSize.height = screenSize.height; + } + if (frameSize.width > screenSize.width) { + frameSize.width = screenSize.width; + } + frame.setLocation((screenSize.width - frameSize.width) / 2, + (screenSize.height - frameSize.height) / 2); + frame.setStatus(frame.translator.getString("Status.Build.FO.tree")); + frame.setVisible(true); + return frame; + } /** * Creates a new PreviewDialog that uses the given renderer. @@ -396,10 +430,16 @@ public class PreviewDialog extends JFrame { return menuBar; } - public void reload() { - previewPanel.reload(); + /** @see org.apache.fop.render.awt.viewer.StatusListener#notifyRendererStopped() */ + public void notifyRendererStopped() { + reload(); } + private void reload() { + setStatus(translator.getString("Status.Show")); + previewPanel.reload(); + } + /** * Changes the current visible page * @param number the page number to go to @@ -407,7 +447,7 @@ public class PreviewDialog extends JFrame { public void goToPage(int number) { if (number != previewPanel.getPage()) { previewPanel.setPage(number); - setInfo(); + notifyPageRendered(); } } @@ -570,7 +610,7 @@ public class PreviewDialog extends JFrame { /** * Updates the message to be shown in the info bar in a thread safe way. */ - public void setInfo() { + public void notifyPageRendered() { SwingUtilities.invokeLater(new ShowInfo()); } -- 2.39.5