001 /**
002 * ========================================
003 * JFreeReport : a free Java report library
004 * ========================================
005 *
006 * Project Info: http://reporting.pentaho.org/
007 *
008 * (C) Copyright 2000-2007, by Object Refinery Limited, 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 * $Id: AbstractActionPlugin.java,v 1.5 2007/06/10 15:54:22 taqua Exp $
027 * ------------
028 * (C) Copyright 2000-2005, by Object Refinery Limited.
029 * (C) Copyright 2005-2007, by Pentaho Corporation.
030 */
031
032 package org.jfree.report.modules.gui.swing.common;
033
034 import java.awt.Dialog;
035 import java.awt.Frame;
036 import java.awt.Window;
037 import java.beans.PropertyChangeListener;
038 import java.beans.PropertyChangeSupport;
039
040 import org.jfree.report.modules.gui.common.IconTheme;
041 import org.jfree.util.ExtendedConfiguration;
042 import org.jfree.util.ExtendedConfigurationWrapper;
043 import org.jfree.util.ResourceBundleSupport;
044
045 /**
046 * The AbstractExportPlugin provides a basic implementation of the ExportPlugin
047 * interface.
048 *
049 * @author Thomas Morgner
050 */
051 public abstract class AbstractActionPlugin implements ActionPlugin
052 {
053 private PropertyChangeSupport propertyChangeSupport;
054
055 private boolean enabled;
056
057 /**
058 * Localised resources.
059 */
060 private ResourceBundleSupport baseResources;
061 private IconTheme iconTheme;
062 private String statusText;
063
064 /**
065 * The base resource class.
066 */
067 public static final String BASE_RESOURCE_CLASS =
068 "org.jfree.report.modules.gui.common.resources";
069 private SwingGuiContext context;
070 private ExtendedConfiguration configuration;
071
072 protected AbstractActionPlugin()
073 {
074 propertyChangeSupport = new PropertyChangeSupport(this);
075 }
076
077 public boolean initialize(final SwingGuiContext context)
078 {
079 this.context = context;
080 this.baseResources = new ResourceBundleSupport
081 (context.getLocale(), BASE_RESOURCE_CLASS);
082 this.iconTheme = context.getIconTheme();
083 this.configuration = new ExtendedConfigurationWrapper
084 (context.getConfiguration());
085 return true;
086 }
087
088 protected PropertyChangeSupport getPropertyChangeSupport()
089 {
090 return propertyChangeSupport;
091 }
092
093 public SwingGuiContext getContext()
094 {
095 return context;
096 }
097
098 public ExtendedConfiguration getConfig()
099 {
100 return configuration;
101 }
102
103 /**
104 * Returns true if the action is separated, and false otherwise. A separated
105 * action starts a new action group and will be spearated from previous
106 * actions on the menu and toolbar.
107 *
108 * @return true, if the action should be separated from previous actions,
109 * false otherwise.
110 */
111 public boolean isSeparated()
112 {
113 return getConfig().getBoolProperty
114 (getConfigurationPrefix() + "separated");
115 }
116
117 /**
118 * Returns an error description for the last operation. This implementation
119 * provides a basic default failure description text and should be overriden
120 * to give a more detailed explaination.
121 *
122 * @return returns a error description.
123 */
124 public String getFailureDescription()
125 {
126 return baseResources.formatMessage
127 ("statusline.export.generic-failure-description", getDisplayName());
128 }
129
130 public String getStatusText()
131 {
132 return statusText;
133 }
134
135 public void setStatusText(final String statusText)
136 {
137 String oldText = this.statusText;
138 this.statusText = statusText;
139 propertyChangeSupport.firePropertyChange("statusText", oldText, statusText);
140 }
141
142 /**
143 * Returns true if the action should be added to the toolbar, and false
144 * otherwise.
145 *
146 * @return true, if the plugin should be added to the toolbar, false
147 * otherwise.
148 */
149 public boolean isAddToToolbar()
150 {
151 return getConfig().getBoolProperty
152 (getConfigurationPrefix() + "add-to-toolbar");
153 }
154
155 /**
156 * Returns true if the action should be added to the menu, and false
157 * otherwise.
158 *
159 * @return A boolean.
160 */
161 public boolean isAddToMenu()
162 {
163 final String name = getConfigurationPrefix() + "add-to-menu";
164 return getConfig().getBoolProperty(name);
165 }
166
167 /**
168 * Creates a progress dialog, and tries to assign a parent based on the given
169 * preview proxy.
170 *
171 * @return the progress dialog.
172 */
173 protected ReportProgressDialog createProgressDialog()
174 {
175 final Window proxy = context.getWindow();
176 if (proxy instanceof Frame)
177 {
178 return new ReportProgressDialog((Frame) proxy);
179 }
180 else if (proxy instanceof Dialog)
181 {
182 return new ReportProgressDialog((Dialog) proxy);
183 }
184 else
185 {
186 return new ReportProgressDialog();
187 }
188 }
189
190 public void addPropertyChangeListener(final PropertyChangeListener l)
191 {
192 propertyChangeSupport.addPropertyChangeListener(l);
193 }
194
195 public void addPropertyChangeListener(final String property,
196 final PropertyChangeListener l)
197 {
198 propertyChangeSupport.addPropertyChangeListener(property, l);
199 }
200
201 public void removePropertyChangeListener(final PropertyChangeListener l)
202 {
203 propertyChangeSupport.removePropertyChangeListener(l);
204 }
205
206 public void setEnabled(final boolean enabled)
207 {
208 final boolean oldEnabled = this.enabled;
209 this.enabled = enabled;
210 propertyChangeSupport.firePropertyChange("enabled", oldEnabled, enabled);
211 }
212
213 public boolean isEnabled()
214 {
215 return enabled;
216 }
217
218 public IconTheme getIconTheme()
219 {
220 return iconTheme;
221 }
222
223 protected abstract String getConfigurationPrefix();
224
225
226 /**
227 * A sort key used to enforce a certain order within the actions.
228 *
229 * @return
230 */
231 public int getMenuOrder()
232 {
233 return getConfig().getIntProperty
234 (getConfigurationPrefix() + "menu-order", 0);
235 }
236
237 public int getToolbarOrder()
238 {
239 return getConfig().getIntProperty
240 (getConfigurationPrefix() + "toolbar-order", 0);
241 }
242
243 public String getRole()
244 {
245 return getConfig().getConfigProperty
246 (getConfigurationPrefix() + "role");
247 }
248
249 public int getRolePreference()
250 {
251 return getConfig().getIntProperty
252 (getConfigurationPrefix() + "role-preference", 0);
253 }
254 }