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: * AbstractExportActionPlugin.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.commonswing;
030:
031: import java.awt.Dialog;
032: import java.awt.Frame;
033: import java.awt.Window;
034: import java.beans.PropertyChangeEvent;
035: import java.beans.PropertyChangeListener;
036: import java.lang.reflect.Constructor;
037:
038: import org.jfree.report.JFreeReport;
039: import org.jfree.report.util.i18n.Messages;
040: import org.jfree.util.Configuration;
041: import org.jfree.util.Log;
042: import org.jfree.util.ObjectUtilities;
043:
044: /**
045: * Creation-Date: 02.12.2006, 14:21:07
046: *
047: * @author Thomas Morgner
048: */
049: public abstract class AbstractExportActionPlugin extends
050: AbstractActionPlugin implements ExportActionPlugin {
051: private class ReportJobListener implements PropertyChangeListener {
052: protected ReportJobListener() {
053: }
054:
055: public void propertyChange(final PropertyChangeEvent evt) {
056: setEnabled(eventSource.getReportJob() != null);
057: }
058: }
059:
060: private ReportEventSource eventSource;
061:
062: private Messages messages;
063:
064: protected AbstractExportActionPlugin() {
065: messages = new Messages(SwingCommonModule.BUNDLE_NAME);
066: }
067:
068: public boolean initialize(final SwingGuiContext context) {
069: if (super .initialize(context) == false) {
070: return false;
071: }
072: messages = new Messages(context.getLocale(),
073: SwingCommonModule.BUNDLE_NAME);
074: eventSource = context.getEventSource();
075: eventSource.addPropertyChangeListener(
076: "reportJob", new ReportJobListener()); //$NON-NLS-1$
077: setEnabled(eventSource.getReportJob() != null);
078: return true;
079: }
080:
081: /**
082: * Creates a progress dialog, and tries to assign a parent based on the given
083: * preview proxy.
084: *
085: * @return the progress dialog.
086: */
087: protected ExportDialog createExportDialog(final String className)
088: throws InstantiationException {
089: final Window proxy = getContext().getWindow();
090: if (proxy instanceof Frame) {
091: final ClassLoader classLoader = ObjectUtilities
092: .getClassLoader(AbstractActionPlugin.class);
093: try {
094: final Class aClass = classLoader.loadClass(className);
095: final Constructor constructor = aClass
096: .getConstructor(new Class[] { Frame.class });
097: return (ExportDialog) constructor
098: .newInstance(new Object[] { proxy });
099: } catch (Exception e) {
100: Log
101: .error(messages
102: .getErrorString(
103: "AbstractExportActionPlugin.ERROR_0001_FAILED_EXPORT_DIALOG_CREATION", className)); //$NON-NLS-1$
104: }
105: } else if (proxy instanceof Dialog) {
106: final ClassLoader classLoader = ObjectUtilities
107: .getClassLoader(AbstractActionPlugin.class);
108: try {
109: final Class aClass = classLoader.loadClass(className);
110: final Constructor constructor = aClass
111: .getConstructor(new Class[] { Dialog.class });
112: return (ExportDialog) constructor
113: .newInstance(new Object[] { proxy });
114: } catch (Exception e) {
115: Log
116: .error(
117: messages
118: .getErrorString(
119: "AbstractExportActionPlugin.ERROR_0002_FAILED_EXPORT_DIALOG_CREATION", className), e); //$NON-NLS-1$
120: }
121: }
122:
123: final Object fallBack = ObjectUtilities.loadAndInstantiate(
124: className, AbstractActionPlugin.class,
125: ExportDialog.class);
126: if (fallBack != null) {
127: return (ExportDialog) fallBack;
128: }
129:
130: Log
131: .error(messages
132: .getErrorString(
133: "AbstractExportActionPlugin.ERROR_0003_FAILED_EXPORT_DIALOG_CREATION", className)); //$NON-NLS-1$
134: throw new InstantiationException(
135: messages
136: .getErrorString("AbstractExportActionPlugin.ERROR_0004_FAILED_EXPORT_DIALOG_CREATION")); //$NON-NLS-1$
137: }
138:
139: /**
140: * Exports a report.
141: *
142: * @param job the report.
143: * @return A boolean.
144: */
145: public boolean performShowExportDialog(final JFreeReport job,
146: final String configKey) {
147: try {
148: final Configuration configuration = job.getConfiguration();
149: final String dialogClassName = configuration
150: .getConfigProperty(configKey);
151: final ExportDialog dialog = createExportDialog(dialogClassName);
152:
153: return dialog.performQueryForExport(job, getContext());
154: } catch (InstantiationException e) {
155: Log
156: .error(messages
157: .getErrorString("AbstractExportActionPlugin.ERROR_0005_UNABLE_TO_CONFIGURE")); //$NON-NLS-1$
158: getContext()
159: .getStatusListener()
160: .setStatus(
161: StatusType.ERROR,
162: messages
163: .getString("AbstractExportActionPlugin.ERROR_0005_UNABLE_TO_CONFIGURE")); //$NON-NLS-1$
164: return false;
165: }
166: }
167:
168: protected boolean isProgressDialogEnabled(final JFreeReport report,
169: final String configKey) {
170: return getConfig().getBoolProperty(configKey);
171: }
172:
173: }
|