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: DefaultActionFactory.java 3525 2007-10-16 11:43:48Z tmorgner $
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.util.HashMap;
035    import java.util.Iterator;
036    
037    import org.jfree.util.Configuration;
038    import org.jfree.util.Log;
039    import org.jfree.util.ObjectUtilities;
040    
041    /**
042     * Creation-Date: 16.11.2006, 16:28:03
043     *
044     * @author Thomas Morgner
045     */
046    public class DefaultActionFactory implements ActionFactory
047    {
048      private static final String PREFIX =
049          "org.jfree.report.modules.gui.swing.actions.";
050    
051      public DefaultActionFactory()
052      {
053      }
054    
055      public ActionPlugin[] getActions(final SwingGuiContext context, final String category)
056      {
057        final Configuration configuration = context.getConfiguration();
058        final String prefix = PREFIX + category;
059        final Iterator keys = configuration.findPropertyKeys(prefix);
060        if (keys.hasNext() == false)
061        {
062          Log.debug("Returning : NO actions for " + category);
063          return new ActionPlugin[0];
064        }
065    
066        final HashMap plugins = new HashMap();
067        while (keys.hasNext())
068        {
069          final String key = (String) keys.next();
070          final String base = key.substring(prefix.length());
071          if (isPluginKey(base) == false)
072          {
073            continue;
074          }
075    
076          final String clazz = configuration.getConfigProperty(key);
077          final Object maybeActionPlugin = ObjectUtilities.loadAndInstantiate
078              (clazz, DefaultActionFactory.class, ActionPlugin.class);
079          if (maybeActionPlugin == null)
080          {
081            Log.debug("Error : " + category + ": " + clazz + " is no action plugin");
082            continue;
083          }
084    
085          final ActionPlugin plugin = (ActionPlugin) maybeActionPlugin;
086          if (plugin.initialize(context) == false)
087          {
088            continue;
089          }
090          final String role = plugin.getRole();
091          if (role == null)
092          {
093            plugins.put(plugin, plugin);
094          }
095          else
096          {
097            final ActionPlugin otherPlugin = (ActionPlugin) plugins.get(role);
098            if (otherPlugin != null)
099            {
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    }