From e3d79c1a636ec9a35082db9ea151b5953d3e9cad Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Tue, 8 Jun 2010 19:01:56 +0000 Subject: [PATCH] Bugzilla #42306: Fix for AWT viewer to correctly track page numbers in continuous display mode. Submitted by: Richard Wheeldon Modifications to original patch: - Small adjustments for out code conventions. - Added some missing Javadocs. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@952770 13f79535-47bb-0310-9956-ffa450edef68 --- .../render/awt/viewer/PageChangeEvent.java | 61 +++++++++++++++++++ .../render/awt/viewer/PageChangeListener.java | 36 +++++++++++ .../fop/render/awt/viewer/PreviewDialog.java | 21 ++++++- .../fop/render/awt/viewer/PreviewPanel.java | 60 ++++++++++++++++++ status.xml | 3 + 5 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 src/java/org/apache/fop/render/awt/viewer/PageChangeEvent.java create mode 100644 src/java/org/apache/fop/render/awt/viewer/PageChangeListener.java diff --git a/src/java/org/apache/fop/render/awt/viewer/PageChangeEvent.java b/src/java/org/apache/fop/render/awt/viewer/PageChangeEvent.java new file mode 100644 index 000000000..045ce7fe4 --- /dev/null +++ b/src/java/org/apache/fop/render/awt/viewer/PageChangeEvent.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.awt.viewer; + +import java.util.EventObject; + +/** + * Swing event fired whenever the current page selection of a + * {@link PreviewPanel} changes. Page numbers are 0-based. + */ +public class PageChangeEvent extends EventObject { + + private int oldPage; + private int newPage; + + /** + * Creates an new page change event. + * @param panel the preview panel the event is produced for. + * @param oldPage the old page (zero based) + * @param newPage the new page (zero based) + */ + public PageChangeEvent(PreviewPanel panel, int oldPage, int newPage) { + super(panel); + this.oldPage = oldPage; + this.newPage = newPage; + } + + /** + * Returns the new page. + * @return the new page (zero based) + */ + public int getNewPage() { + return newPage; + } + + /** + * Returns the old page. + * @return the old page (zero based) + */ + public int getOldPage() { + return oldPage; + } + +} diff --git a/src/java/org/apache/fop/render/awt/viewer/PageChangeListener.java b/src/java/org/apache/fop/render/awt/viewer/PageChangeListener.java new file mode 100644 index 000000000..b350f891d --- /dev/null +++ b/src/java/org/apache/fop/render/awt/viewer/PageChangeListener.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.awt.viewer; + +import java.util.EventListener; + +/** + * Swing listener interface for classes which wish to receive + * notification of page change events. + */ +public interface PageChangeListener extends EventListener { + + /** + * Called whenever the current page is changed. + * @param pce the page change event + */ + void pageChanged(PageChangeEvent pce); + +} 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 e3f510853..031021a6f 100644 --- a/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java +++ b/src/java/org/apache/fop/render/awt/viewer/PreviewDialog.java @@ -171,6 +171,11 @@ public class PreviewDialog extends JFrame implements StatusListener { //Page view stuff previewPanel = new PreviewPanel(foUserAgent, renderable, renderer); getContentPane().add(previewPanel, BorderLayout.CENTER); + previewPanel.addPageChangeListener(new PageChangeListener() { + public void pageChanged(PageChangeEvent pce) { + new ShowInfo().run(); + } + }); // Keyboard shortcuts - pgup/pgdn InputMap im = previewPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); @@ -260,6 +265,7 @@ public class PreviewDialog extends JFrame implements StatusListener { * Creates and initialize the AWT Viewer main window. * @param foUserAgent the FO user agent * @param renderable the target for the rendering + * @param asMainWindow true if the window shall act as the main application window. * @return the newly initialized preview dialog */ public static PreviewDialog createPreviewDialog(FOUserAgent foUserAgent, @@ -530,12 +536,18 @@ public class PreviewDialog extends JFrame implements StatusListener { goToPage(currentPage); } - /** Scales page image */ + /** + * Scales page image. + * @param scaleFactor the scale factor + */ public void setScale(double scaleFactor) { scale.setSelectedItem(percentFormat.format(scaleFactor) + "%"); previewPanel.setScaleFactor(scaleFactor / 100d); } + /** + * Sets the scaling so the contents fit into the window. + */ public void setScaleToFitWindow() { try { setScale(previewPanel.getScaleToFitWindow() * 100); @@ -544,6 +556,9 @@ public class PreviewDialog extends JFrame implements StatusListener { } } + /** + * Sets the scaling so the contents are spread over the whole width available. + */ public void setScaleToFitWidth() { try { setScale(previewPanel.getScaleToFitWidth() * 100); @@ -569,7 +584,7 @@ public class PreviewDialog extends JFrame implements StatusListener { //Restore originally configured target resolution float saveResolution = foUserAgent.getTargetResolution(); foUserAgent.setTargetResolution(this.configuredTargetResolution); - + PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPageable(renderer); if (!showDialog || pj.printDialog()) { @@ -579,7 +594,7 @@ public class PreviewDialog extends JFrame implements StatusListener { e.printStackTrace(); } } - + foUserAgent.setTargetResolution(saveResolution); } diff --git a/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java b/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java index f8152a978..2303c0e8e 100644 --- a/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java +++ b/src/java/org/apache/fop/render/awt/viewer/PreviewPanel.java @@ -19,11 +19,14 @@ package org.apache.fop.render.awt.viewer; +import java.awt.Adjustable; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Point; import java.awt.Toolkit; +import java.awt.event.AdjustmentEvent; +import java.awt.event.AdjustmentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; @@ -166,6 +169,8 @@ public class PreviewPanel extends JPanel { previewArea = new JScrollPane(gridPanel); previewArea.getViewport().setBackground(Color.gray); + previewArea.getVerticalScrollBar().addAdjustmentListener(new PageNumberListener()); + // FIXME should add scroll wheel support here at some point. scroller = new ViewportScroller(previewArea.getViewport()); previewArea.addMouseListener(scroller); @@ -188,6 +193,7 @@ public class PreviewPanel extends JPanel { * @param number the page number */ public void setPage(int number) { + int oldPage = currentPage; if (displayMode == CONTINUOUS || displayMode == CONT_FACING) { currentPage = number; gridPanel.scrollRectToVisible(pagePanels[currentPage].getBounds()); @@ -196,6 +202,7 @@ public class PreviewPanel extends JPanel { firstPage = currentPage; } showPage(); + firePageChange(oldPage, currentPage); } /** @@ -236,6 +243,42 @@ public class PreviewPanel extends JPanel { reload(); } + /** + * Add a listener to receive notification of page change events. Events will + * be fired whenever the currentPage value is changed. The values recorded + * are 0-based. + * @param l the page change listener to add + */ + public void addPageChangeListener(PageChangeListener l) { + listenerList.add(PageChangeListener.class, l); + } + + /** + * Removes a page change listener. + * @param l the page change listener to remove + */ + public void removePageChangeListener(PageChangeListener l) { + listenerList.remove(PageChangeListener.class, l); + } + + /** + * Notify all registered listeners of a page change event. + * @param oldPage the old page + * @param newPage the new page + */ + protected void firePageChange(int oldPage, int newPage) { + Object[] listeners = listenerList.getListenerList(); + PageChangeEvent e = null; + for (int i = listeners.length - 2; i >= 0; i -= 2) { + if (listeners[i] == PageChangeListener.class) { + if (e == null) { + e = new PageChangeEvent(this, newPage, oldPage); + } + ((PageChangeListener)listeners[i + 1]).pageChanged(e); + } + } + } + /** * Allows any mouse drag on the page area to scroll the display window. */ @@ -352,6 +395,23 @@ public class PreviewPanel extends JPanel { } } + private class PageNumberListener implements AdjustmentListener { + public void adjustmentValueChanged(AdjustmentEvent e) { + if (displayMode == PreviewPanel.CONTINUOUS || displayMode == PreviewPanel.CONT_FACING) { + Adjustable a = e.getAdjustable(); + int value = +e.getValue(); + int min = a.getMinimum(); + int max = a.getMaximum(); + int page = ( (renderer.getNumberOfPages() * value) / (max - min) ); + if (page != currentPage) { + int oldPage = currentPage; + currentPage = page; + firePageChange(oldPage, currentPage); + } + } + } + } + /** * Scales page image * @param scale [0;1] diff --git a/status.xml b/status.xml index f309c8f45..acc40d878 100644 --- a/status.xml +++ b/status.xml @@ -58,6 +58,9 @@ documents. Example: the fix of marks layering will be such a case when it's done. --> + + Fix for AWT viewer to correctly track page numbers in continuous display mode. + Bugfix for formatting of floating point numbers which could lead to invalid PDFs. -- 2.39.5