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

   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: DefaultActionFactory.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.util.HashMap;
  35: import java.util.Iterator;
  36: 
  37: import org.jfree.util.Configuration;
  38: import org.jfree.util.Log;
  39: import org.jfree.util.ObjectUtilities;
  40: 
  41: /**
  42:  * Creation-Date: 16.11.2006, 16:28:03
  43:  *
  44:  * @author Thomas Morgner
  45:  */
  46: public class DefaultActionFactory implements ActionFactory
  47: {
  48:   private static final String PREFIX =
  49:       "org.jfree.report.modules.gui.swing.actions.";
  50: 
  51:   public DefaultActionFactory()
  52:   {
  53:   }
  54: 
  55:   public ActionPlugin[] getActions(final SwingGuiContext context, final String category)
  56:   {
  57:     final Configuration configuration = context.getConfiguration();
  58:     final String prefix = PREFIX + category;
  59:     final Iterator keys = configuration.findPropertyKeys(prefix);
  60:     if (keys.hasNext() == false)
  61:     {
  62:       Log.debug("Returning : NO actions for " + category);
  63:       return new ActionPlugin[0];
  64:     }
  65: 
  66:     final HashMap plugins = new HashMap();
  67:     while (keys.hasNext())
  68:     {
  69:       final String key = (String) keys.next();
  70:       final String base = key.substring(prefix.length());
  71:       if (isPluginKey(base) == false)
  72:       {
  73:         continue;
  74:       }
  75: 
  76:       final String clazz = configuration.getConfigProperty(key);
  77:       final Object maybeActionPlugin = ObjectUtilities.loadAndInstantiate
  78:           (clazz, DefaultActionFactory.class, ActionPlugin.class);
  79:       if (maybeActionPlugin == null)
  80:       {
  81:         Log.debug("Error : " + category + ": " + clazz + " is no action plugin");
  82:         continue;
  83:       }
  84: 
  85:       final ActionPlugin plugin = (ActionPlugin) maybeActionPlugin;
  86:       if (plugin.initialize(context) == false)
  87:       {
  88:         continue;
  89:       }
  90:       final String role = plugin.getRole();
  91:       if (role == null)
  92:       {
  93:         plugins.put(plugin, plugin);
  94:       }
  95:       else
  96:       {
  97:         final ActionPlugin otherPlugin = (ActionPlugin) plugins.get(role);
  98:         if (otherPlugin != null)
  99:         {
 100:           if (plugin.getRolePreference() > otherPlugin.getRolePreference())
 101:           {
 102:             plugins.put(role, plugin);
 103:           }
 104:           else
 105:           {
 106:             Log.debug("Loaded : " + category + ": " + clazz + ": Overridden by " + otherPlugin.getClass().getName());
 107:           }
 108:         }
 109:         else
 110:         {
 111:           plugins.put(role, plugin);
 112:         }
 113:       }
 114:     }
 115: 
 116:     Log.debug("Returning : " + plugins.size() + " actions for " + category);
 117: 
 118:     return (ActionPlugin[]) plugins.values().toArray
 119:         (new ActionPlugin[plugins.size()]);
 120:   }
 121: 
 122:   private boolean isPluginKey(final String base)
 123:   {
 124:     if (base.length() < 1)
 125:     {
 126:       return false;
 127:     }
 128:     if (base.indexOf('.', 1) > 0)
 129:     {
 130:       return false;
 131:     }
 132:     return true;
 133:   }
 134: }