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: */package org.jfree.report.modules.gui.swing.common;
031:
032: import java.util.HashMap;
033: import java.util.Iterator;
034:
035: import org.jfree.util.Configuration;
036: import org.jfree.util.Log;
037: import org.jfree.util.ObjectUtilities;
038:
039: /**
040: * Creation-Date: 16.11.2006, 16:28:03
041: *
042: * @author Thomas Morgner
043: */
044: public class DefaultActionFactory implements ActionFactory {
045: private static final String PREFIX = "org.jfree.report.modules.gui.swing.actions.";
046:
047: public DefaultActionFactory() {
048: }
049:
050: public ActionPlugin[] getActions(final SwingGuiContext context,
051: final String category) {
052: final Configuration configuration = context.getConfiguration();
053: final String prefix = PREFIX + category;
054: final Iterator keys = configuration.findPropertyKeys(prefix);
055: if (keys.hasNext() == false) {
056: Log.debug("Returning : NO actions for " + category);
057: return new ActionPlugin[0];
058: }
059:
060: final HashMap plugins = new HashMap();
061: while (keys.hasNext()) {
062: final String key = (String) keys.next();
063: final String base = key.substring(prefix.length());
064: if (isPluginKey(base) == false) {
065: continue;
066: }
067:
068: final String clazz = configuration.getConfigProperty(key);
069: final Object maybeActionPlugin = ObjectUtilities
070: .loadAndInstantiate(clazz,
071: DefaultActionFactory.class,
072: ActionPlugin.class);
073: if (maybeActionPlugin == null) {
074: Log.debug("Error : " + category + ": " + clazz
075: + " is no action plugin");
076: continue;
077: }
078:
079: final ActionPlugin plugin = (ActionPlugin) maybeActionPlugin;
080: if (plugin.initialize(context) == false) {
081: continue;
082: }
083: final String role = plugin.getRole();
084: if (role == null) {
085: plugins.put(plugin, plugin);
086: } else {
087: final ActionPlugin otherPlugin = (ActionPlugin) plugins
088: .get(role);
089: if (otherPlugin != null) {
090: if (plugin.getRolePreference() > otherPlugin
091: .getRolePreference()) {
092: plugins.put(role, plugin);
093: } else {
094: Log.debug("Loaded : " + category + ": " + clazz
095: + ": Overridden by "
096: + otherPlugin.getClass().getName());
097: }
098: } else {
099: plugins.put(role, plugin);
100: }
101: }
102: }
103:
104: Log.debug("Returning : " + plugins.size() + " actions for "
105: + category);
106:
107: return (ActionPlugin[]) plugins.values().toArray(
108: new ActionPlugin[plugins.size()]);
109: }
110:
111: private boolean isPluginKey(final String base) {
112: if (base.length() < 1) {
113: return false;
114: }
115: if (base.indexOf('.', 1) > 0) {
116: return false;
117: }
118: return true;
119: }
120: }
|