Author: tchemit Date: 2008-02-19 15:09:24 +0000 (Tue, 19 Feb 2008) New Revision: 216 Added: trunk/jaxx/src/main/java/jaxx/runtime/builder/SimpleActionFactoryImpl.java Modified: trunk/jaxx/src/main/java/jaxx/runtime/builder/ActionFactory.java Log: add a simple ActionFactory implementation with a properties file as source Modified: trunk/jaxx/src/main/java/jaxx/runtime/builder/ActionFactory.java =================================================================== --- trunk/jaxx/src/main/java/jaxx/runtime/builder/ActionFactory.java 2008-02-14 06:17:57 UTC (rev 215) +++ trunk/jaxx/src/main/java/jaxx/runtime/builder/ActionFactory.java 2008-02-19 15:09:24 UTC (rev 216) @@ -51,7 +51,7 @@ */ public abstract class ActionFactory { - private static Log log = LogFactory.getLog(ActionFactory.class); + protected static Log log = LogFactory.getLog(ActionFactory.class); /** dictionary of known actions implementations */ private Map<String, Class<? extends AbstractAction>> impls; @@ -123,6 +123,7 @@ Boolean value = (Boolean) action.getValue("hideActionText"); component.setHideActionText(value != null && value); + action.setEnabled(true); result.add(action); continue; } @@ -142,6 +143,19 @@ return result.toArray(new AbstractAction[result.size()]); } + public AbstractAction newAction0(String actionKey) { + // on v�rifie que l'action existe bien + checkRegistredAction(actionKey); + + // try in cache + if (cache.containsKey(actionKey)) { + // use cached action + AbstractAction action = cache.get(actionKey); + log.info("use cache action "+action); + return action; + } + return null; + } /** * @param actionKey le nom de l'action tel que d�finie dans le fichier * de mapping (sans le prefix action.) @@ -150,22 +164,19 @@ */ public AbstractAction newAction(String actionKey, AbstractButton component) { - // on v�rifie que l'action existe bien - checkRegistredAction(actionKey); - - // try in cache - if (cache.containsKey(actionKey)) { - // use cached action - return cache.get(actionKey); + // try first in cache + AbstractAction result =newAction0(actionKey); + if (result!=null) { + return result; } // on r�cup�re la classe d'implantation de l'action Class<? extends AbstractAction> klazz = impls.get(actionKey); try { - AbstractAction result = klazz.getConstructor(String.class).newInstance(actionKey); + result = klazz.getConstructor(String.class).newInstance(actionKey); result.putValue(Action.ACTION_COMMAND_KEY, actionKey); - log.debug(actionKey + " : " + result); + log.info("create action "+actionKey + " : " + result); // recherche de l'annotation de configuration ActionConfig anno = initActionConfig(component, result); @@ -216,20 +227,17 @@ */ public AbstractAction newAction(String actionKey, JComboBox component) { - // on v�rifie que l'action existe bien - checkRegistredAction(actionKey); - - // try in cache - if (cache.containsKey(actionKey)) { - // use cached action - return cache.get(actionKey); + // try first in cache + AbstractAction result =newAction0(actionKey); + if (result!=null) { + return result; } // on r�cup�re la classe d'implantation de l'action Class<? extends AbstractAction> klazz = impls.get(actionKey); try { - AbstractAction result = klazz.getConstructor(String.class).newInstance(actionKey); + result = klazz.getConstructor(String.class).newInstance(actionKey); result.putValue(Action.ACTION_COMMAND_KEY, actionKey); log.debug(actionKey + " : " + result); @@ -316,7 +324,7 @@ result.putValue("hideActionText", anno.hideActionText()); result.putValue(Action.SELECTED_KEY, anno.selected()); - result.setEnabled(component!=null && (component.isEnabled() || anno.enabled())); + result.setEnabled(anno.enabled()); } return anno; Added: trunk/jaxx/src/main/java/jaxx/runtime/builder/SimpleActionFactoryImpl.java =================================================================== --- trunk/jaxx/src/main/java/jaxx/runtime/builder/SimpleActionFactoryImpl.java (rev 0) +++ trunk/jaxx/src/main/java/jaxx/runtime/builder/SimpleActionFactoryImpl.java 2008-02-19 15:09:24 UTC (rev 216) @@ -0,0 +1,118 @@ +/* +* ##% Copyright (C) 2007, 2008 Code Lutin, Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package jaxx.runtime.builder; + +import static org.codelutin.i18n.I18n._; +import org.codelutin.util.Resource; + +import javax.swing.AbstractAction; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.TreeMap; + +/** + * A simple implementation of {@link ActionFactory} using a properties file to + * load action mapping. + * <p/> + * An entry is in that form : <code>action.actionName=fqn</code> where + * <p/> + * <code>actionName</code> is the key of action used in factory, and + * <code>fqn</code> is the fully qualified name of the implemented action class. + * + * @author chemit + */ +public class SimpleActionFactoryImpl extends ActionFactory { + /** default file where to load action mapping */ + protected static final String ACTIONS_FILE_PATH = "/actions.properties"; + + /** default prefix for an entryin mapping file. */ + protected static final String ACTION_KEY_PREFIX = "action."; + + /** real file where to load actions mapping */ + protected String actionFilePath; + + public SimpleActionFactoryImpl() { + this(ACTIONS_FILE_PATH); + } + + public SimpleActionFactoryImpl(String actionFilePath) { + this.actionFilePath = actionFilePath; + } + + @SuppressWarnings({"unchecked"}) + protected Map<String, Class<? extends AbstractAction>> init() { + Properties properties = new Properties(); + List<URL> urls = Resource.getURLs(".*jaxx/.+-actions\\.properties"); + for (URL url : urls) { + InputStream inputStream=null; + try { + inputStream = url.openStream(); + log.info("load " + url); + properties.load(inputStream); + } catch (IOException e) { + log.warn(_("jaxx.error.load.actions.file", e.getMessage())); + //throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); + } finally { + if (inputStream!=null) { + try { + inputStream.close(); + } catch (IOException e) { + log.warn(_("jaxx.error.close.actions.file", e.getMessage())); + //throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); + } + } + } + } + Map<String, Class<? extends AbstractAction>> cache = new TreeMap<String, Class<? extends AbstractAction>>(); + int prefix = ACTION_KEY_PREFIX.length(); + for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) { + String key = objectObjectEntry.getKey() + ""; + String qfn = objectObjectEntry.getValue() + ""; + try { + Class<? extends AbstractAction> implCass; + implCass = (Class<? extends AbstractAction>) Class.forName(qfn); + log.debug("found action " + implCass); + cache.put(key.substring(prefix), implCass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn)); + } + } + return cache; + } + + String[] getFilenames(Locale l) { + String prefix = ".*jaxx/.+"; + String postfix = "actions\\.properties"; + java.util.List<String> result = new ArrayList<String>(); + if (!"".equals(l.getLanguage())) { + if (!"".equals(l.getCountry())) { + result.add(prefix + "-" + l.getLanguage() + "_" + + l.getCountry() + postfix); + } + result.add(prefix + "-" + l.getLanguage() + postfix); + } + result.add(prefix + postfix); + return result.toArray(new String[result.size()]); + } +}