001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * DefaultActionFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.commonswing;
030:
031: import java.util.HashMap;
032: import java.util.Iterator;
033:
034: import org.jfree.report.util.i18n.Messages;
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 ActionPlugin[] EMPTY_ACTIONS = new ActionPlugin[0];
046: private static final String PREFIX = "org.jfree.report.modules.gui.swing.actions."; //$NON-NLS-1$
047:
048: private static final Messages MESSAGES = new Messages(
049: SwingCommonModule.BUNDLE_NAME);
050:
051: public DefaultActionFactory() {
052: }
053:
054: public ActionPlugin[] getActions(final SwingGuiContext context,
055: final String category) {
056: final Configuration configuration = context.getConfiguration();
057: final String prefix = PREFIX + category;
058: final Iterator keys = configuration.findPropertyKeys(prefix);
059: if (keys.hasNext() == false) {
060: Log.debug(MESSAGES.getString(
061: "DefaultActionFactory.DEBUG_NO_ACTIONS", category)); //$NON-NLS-1$
062: return EMPTY_ACTIONS;
063: }
064:
065: final HashMap plugins = new HashMap();
066: while (keys.hasNext()) {
067: final String key = (String) keys.next();
068: final String base = key.substring(prefix.length());
069: if (isPluginKey(base) == false) {
070: // Maybe an invalid key or a key for a sub-category ..
071: continue;
072: }
073:
074: final String clazz = configuration.getConfigProperty(key);
075: final Object maybeActionPlugin = ObjectUtilities
076: .loadAndInstantiate(clazz,
077: DefaultActionFactory.class,
078: ActionPlugin.class);
079: if (maybeActionPlugin == null) {
080: Log
081: .debug(MESSAGES
082: .getString(
083: "DefaultActionFactory.DEBUG_NOT_ACTION_PLUGIN", category, clazz)); //$NON-NLS-1$
084: continue;
085: }
086:
087: final ActionPlugin plugin = (ActionPlugin) maybeActionPlugin;
088: if (plugin.initialize(context) == false) {
089: continue;
090: }
091: final String role = plugin.getRole();
092: if (role == null) {
093: plugins.put(plugin, plugin);
094: } else {
095: final ActionPlugin otherPlugin = (ActionPlugin) plugins
096: .get(role);
097: if (otherPlugin != null) {
098: if (plugin.getRolePreference() > otherPlugin
099: .getRolePreference()) {
100: plugins.put(role, plugin);
101: } else {
102: Log
103: .debug(MESSAGES
104: .getString(
105: "DefaultActionFactory.DEBUG_PLUGIN_OVERRIDE", category, clazz, otherPlugin.getClass().getName())); //$NON-NLS-1$
106: }
107: } else {
108: plugins.put(role, plugin);
109: }
110: }
111: }
112:
113: Log
114: .debug(MESSAGES
115: .getString(
116: "DefaultActionFactory.DEBUG_RETURNING_PLUGINS", String.valueOf(plugins.size()), category)); //$NON-NLS-1$ //$NON-NLS-2$
117:
118: return (ActionPlugin[]) plugins.values().toArray(
119: new ActionPlugin[plugins.size()]);
120: }
121:
122: private boolean isPluginKey(final String base) {
123: if (base.length() < 1) {
124: return false;
125: }
126: if (base.indexOf('.', 1) > 0) {
127: return false;
128: }
129: return true;
130: }
131: }
|