Source for org.jfree.report.modules.gui.swing.common.AbstractActionPlugin

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: AbstractActionPlugin.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.modules.gui.swing.common;
  33: 
  34: import java.awt.Dialog;
  35: import java.awt.Frame;
  36: import java.awt.Window;
  37: import java.beans.PropertyChangeListener;
  38: import java.beans.PropertyChangeSupport;
  39: 
  40: import org.jfree.report.modules.gui.common.IconTheme;
  41: import org.jfree.util.ExtendedConfiguration;
  42: import org.jfree.util.ExtendedConfigurationWrapper;
  43: import org.jfree.util.ResourceBundleSupport;
  44: 
  45: /**
  46:  * The AbstractExportPlugin provides a basic implementation of the ExportPlugin
  47:  * interface.
  48:  *
  49:  * @author Thomas Morgner
  50:  */
  51: public abstract class AbstractActionPlugin implements ActionPlugin
  52: {
  53:   private PropertyChangeSupport propertyChangeSupport;
  54: 
  55:   private boolean enabled;
  56: 
  57:   /**
  58:    * Localised resources.
  59:    */
  60:   private ResourceBundleSupport baseResources;
  61:   private IconTheme iconTheme;
  62:   private String statusText;
  63: 
  64:   /**
  65:    * The base resource class.
  66:    */
  67:   public static final String BASE_RESOURCE_CLASS =
  68:       "org.jfree.report.modules.gui.common.resources";
  69:   private SwingGuiContext context;
  70:   private ExtendedConfiguration configuration;
  71: 
  72:   protected AbstractActionPlugin()
  73:   {
  74:     propertyChangeSupport = new PropertyChangeSupport(this);
  75:   }
  76: 
  77:   public boolean initialize(final SwingGuiContext context)
  78:   {
  79:     this.context = context;
  80:     this.baseResources = new ResourceBundleSupport
  81:         (context.getLocale(), BASE_RESOURCE_CLASS);
  82:     this.iconTheme = context.getIconTheme();
  83:     this.configuration = new ExtendedConfigurationWrapper
  84:         (context.getConfiguration());
  85:     return true;
  86:   }
  87: 
  88:   protected PropertyChangeSupport getPropertyChangeSupport()
  89:   {
  90:     return propertyChangeSupport;
  91:   }
  92: 
  93:   public SwingGuiContext getContext()
  94:   {
  95:     return context;
  96:   }
  97: 
  98:   public ExtendedConfiguration getConfig()
  99:   {
 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:     final 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: }