001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * PageSetupPlugin.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.print;
030:
031: import java.awt.print.PageFormat;
032: import java.awt.print.PrinterJob;
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035: import java.util.Locale;
036: import javax.swing.Icon;
037: import javax.swing.KeyStroke;
038:
039: import org.jfree.report.JFreeReport;
040: import org.jfree.report.JFreeReportBoot;
041: import org.jfree.report.PageDefinition;
042: import org.jfree.report.SimplePageDefinition;
043: import org.jfree.report.modules.gui.base.PreviewPane;
044: import org.jfree.report.modules.gui.base.actions.ControlActionPlugin;
045: import org.jfree.report.modules.gui.commonswing.AbstractActionPlugin;
046: import org.jfree.report.modules.gui.commonswing.ReportEventSource;
047: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
048: import org.jfree.report.util.PageFormatFactory;
049: import org.jfree.util.ResourceBundleSupport;
050:
051: /**
052: * An export control plugin that handles the setup of page format objects for the report.
053: *
054: * @author Thomas Morgner
055: */
056: public class PageSetupPlugin extends AbstractActionPlugin implements
057: ControlActionPlugin {
058: private class ReportJobListener implements PropertyChangeListener {
059: protected ReportJobListener() {
060: }
061:
062: public void propertyChange(final PropertyChangeEvent evt) {
063: setEnabled(eventSource.getReportJob() != null);
064: }
065: }
066:
067: /**
068: * Localised resources.
069: */
070: private ResourceBundleSupport resources;
071: private ReportEventSource eventSource;
072:
073: /**
074: * Default Constructor.
075: */
076: public PageSetupPlugin() {
077: resources = new ResourceBundleSupport(
078: PrintingPlugin.BASE_RESOURCE_CLASS);
079: }
080:
081: public boolean initialize(final SwingGuiContext context) {
082: if (super .initialize(context) == false) {
083: return false;
084: }
085: eventSource = context.getEventSource();
086: eventSource.addPropertyChangeListener(
087: "reportJob", new ReportJobListener()); //$NON-NLS-1$
088: setEnabled(eventSource.getReportJob() != null);
089:
090: if (JFreeReportBoot.getInstance().isModuleAvailable(
091: AWTPrintingGUIModule.class.getName()) == false) {
092: return false;
093: }
094: return true;
095: }
096:
097: /**
098: * Returns the display name for the export action.
099: *
100: * @return The display name.
101: */
102: public String getDisplayName() {
103: return (resources.getString("action.page-setup.name")); //$NON-NLS-1$
104: }
105:
106: /**
107: * Returns the short description for the export action.
108: *
109: * @return The short description.
110: */
111: public String getShortDescription() {
112: return (resources.getString("action.page-setup.description")); //$NON-NLS-1$
113: }
114:
115: /**
116: * Returns the small icon for the export action.
117: *
118: * @return The icon.
119: */
120: public Icon getSmallIcon() {
121: final Locale locale = getContext().getLocale();
122: return getIconTheme().getSmallIcon(locale,
123: "action.page-setup.small-icon"); //$NON-NLS-1$
124: }
125:
126: /**
127: * Returns the large icon for the export action.
128: *
129: * @return The icon.
130: */
131: public Icon getLargeIcon() {
132: final Locale locale = getContext().getLocale();
133: return getIconTheme().getLargeIcon(locale,
134: "action.page-setup.icon"); //$NON-NLS-1$
135: }
136:
137: /**
138: * Returns the accelerator key for the export action.
139: *
140: * @return The accelerator key.
141: */
142: public KeyStroke getAcceleratorKey() {
143: return null;
144: }
145:
146: /**
147: * Returns the mnemonic key code.
148: *
149: * @return The code.
150: */
151: public Integer getMnemonicKey() {
152: return resources.getMnemonic("action.page-setup.mnemonic"); //$NON-NLS-1$
153: }
154:
155: /**
156: * Returns the resourcebundle to be used to translate strings into localized content.
157: *
158: * @return the resourcebundle for the localisation.
159: */
160: protected ResourceBundleSupport getResources() {
161: return resources;
162: }
163:
164: protected String getConfigurationPrefix() {
165: return "org.jfree.report.modules.gui.print.page-setup."; //$NON-NLS-1$
166: }
167:
168: public boolean configure(final PreviewPane pane) {
169: final JFreeReport report = pane.getReportJob();
170:
171: final PrinterJob pj = PrinterJob.getPrinterJob();
172: final PageFormat original = report.getPageDefinition()
173: .getPageFormat(0);
174: final PageFormat pf = pj.validatePage(pj.pageDialog(original));
175: if (PageFormatFactory.isEqual(pf, original)) {
176: return false;
177: }
178:
179: final PageDefinition pageDefinition = report
180: .getPageDefinition();
181: if (pageDefinition instanceof SimplePageDefinition) {
182: final SimplePageDefinition spd = (SimplePageDefinition) pageDefinition;
183: report.setPageDefinition(new SimplePageDefinition(pf, spd
184: .getPageCountHorizontal(), spd
185: .getPageCountVertical()));
186: } else {
187: report.setPageDefinition(new SimplePageDefinition(pf));
188: }
189: pane.setReportJob(report);
190: return true;
191: }
192: }
|