Author: tchemit Date: 2008-08-06 10:15:00 +0000 (Wed, 06 Aug 2008) New Revision: 815 Removed: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/AbstractActionFactory.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/AbstractActionProvider.java Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactoryFromProvider.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java Log: simplification du code : on supprime la classe abstraite car pour le moment y'a qu'une seule implantation :) Deleted: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/AbstractActionFactory.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/AbstractActionFactory.java 2008-08-05 22:30:47 UTC (rev 814) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/AbstractActionFactory.java 2008-08-06 10:15:00 UTC (rev 815) @@ -1,387 +0,0 @@ -/* -* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 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 org.codelutin.jaxx.action.factory; - -import jaxx.runtime.JAXXObject; -import jaxx.runtime.swing.JAXXToggleButton; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codelutin.jaxx.action.MyAbstractAction; -import org.codelutin.jaxx.action.initializer.AbstractActionConfigurationResolver; -import org.codelutin.jaxx.action.initializer.ActionConfigConfigurationResolver; -import org.codelutin.jaxx.action.initializer.ActionConfigurationResolver; -import org.codelutin.jaxx.action.initializer.SelectActionConfigConfigurationResolver; -import org.codelutin.jaxx.action.initializer.ToggleActionConfigConfigurationResolver; - -import javax.swing.AbstractButton; -import javax.swing.Action; -import javax.swing.Icon; -import javax.swing.JComboBox; -import javax.swing.JComponent; -import java.awt.event.ActionEvent; -import java.lang.reflect.InvocationTargetException; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; - -/** - * Action factory using the ActionConfig annotations to configure the action. - * <p/> - * The factory is abstract, implements method {@link #init()} to fill the - * dictonary of known action implementations. - * <p/> - * Use after the {@link #loadActions(jaxx.runtime.JAXXObject)} to instanciate - * actions in ui with id equals a known action... - * <p/> - * TODO Finish doc - * - * @author chemit - */ -public abstract class AbstractActionFactory<A extends MyAbstractAction> implements ActionFactory<A> { - - protected static Log log = LogFactory.getLog(AbstractActionFactory.class); - - /** class of encapsuling action */ - protected Class<A> baseImpl; - - /** dictionary of known actions implementations */ - private Map<String, Class<? extends MyAbstractAction>> impls; - - /** dictionary of instanciated actions */ - private Map<String, A> cache; - - protected final ActionConfigConfigurationResolver actionConfigInitializer; - protected final ToggleActionConfigConfigurationResolver toggleActionConfigInitializer; - protected final SelectActionConfigConfigurationResolver selectActionConfigInitializer; - - protected List<AbstractActionConfigurationResolver> configurationResolvers; - - - protected AbstractActionFactory(Class<A> baseImpl) { - this.baseImpl = baseImpl; - this.impls = init(); - this.cache = new TreeMap<String, A>(); - this.configurationResolvers = new java.util.ArrayList<AbstractActionConfigurationResolver>(); - - this.toggleActionConfigInitializer = registerInitializer(ToggleActionConfigConfigurationResolver.class); - this.actionConfigInitializer = registerInitializer(ActionConfigConfigurationResolver.class); - this.selectActionConfigInitializer = registerInitializer(SelectActionConfigConfigurationResolver.class); - } - - public Class<A> getBaseImpl() { - return baseImpl; - } - - public void resetCache() { - cache.clear(); - } - - public A get(String actionKey) { - return cache.get(actionKey); - } - - public void loadActions(JAXXObject ui) { - if (log.isDebugEnabled()) { - log.debug("for ui " + ui.getClass()); - } - for (Map.Entry<String, Class<? extends MyAbstractAction>> entry : implsEntrySet()) { - String actionKey = entry.getKey(); - Object comp = ui.getObjectById(actionKey); - if (comp == null || !(comp instanceof AbstractButton || comp instanceof JComboBox)) { - // nothing to do - continue; - } - if (log.isTraceEnabled()) { - log.trace("detect action " + actionKey); - } - if (comp instanceof AbstractButton) { - AbstractButton component = (AbstractButton) comp; - A action = newAction(actionKey, component); - - component.setAction(action); - - if (component instanceof JAXXToggleButton) { - JAXXToggleButton glueComponent = (JAXXToggleButton) component; - glueComponent.setIcon((Icon) action.getValue(Action.SMALL_ICON)); - Integer integer = (Integer) action.getValue(Action.MNEMONIC_KEY); - if (integer != null) { - glueComponent.setNormalMnemonic(integer); - } - glueComponent.setSelectedIcon((Icon) action.getValue(Action.SMALL_ICON + 2)); - integer = (Integer) action.getValue(Action.MNEMONIC_KEY + 2); - if (integer != null) { - glueComponent.setGlueMnemonic(integer); - } - glueComponent.setGlueText((String) action.getValue(Action.NAME + 2)); - glueComponent.setGlueTooltipText((String) action.getValue(Action.SHORT_DESCRIPTION + 2)); - - glueComponent.setNormalText((String) action.getValue(Action.NAME)); - glueComponent.setNormalTooltipText((String) action.getValue(Action.SHORT_DESCRIPTION)); - } - - Boolean value = (Boolean) action.getValue("hideActionText"); - component.setHideActionText(value != null && value); - action.setEnabled(true); - continue; - } - // is JComboBox - JComboBox component = (JComboBox) comp; - A action = newAction(actionKey, component); - - component.setAction(action); - Integer val = (Integer) action.getValue("selectedIndex"); - if (val != null && val != -1 && val < component.getItemCount() && val != component.getSelectedIndex()) { - component.setSelectedIndex(val); - } - } - } - - /** - * @param actionKey le nom de l'action tel que définie dans le fichier - * de mapping (sans le prefix action.) - * @param component le button où rattacher l'action - * @return une nouvelle instance de l'action associée à sa clef. - */ - public A newAction(String actionKey, JComponent component) { - // try first in cache - A result = getActionFromCache(actionKey); - if (result != null) { - return result; - } - - try { - result = newActionInstance(actionKey); - } catch (Exception e) { - throw new RuntimeException(e); - } - - if (log.isDebugEnabled()) { - log.debug("create <" + actionKey + " : " + result + ">"); - } - - // recherche de l'annotation de configuration - ActionConfigurationResolver<?, ?> configurationResolver = resolveActionConfiguration(result); - - if (configurationResolver != null) { - configurationResolver.applyConfiguration(component, result); - } - - try { - - if (configurationResolver != null) { - if (AbstractButton.class.isAssignableFrom(configurationResolver.getComponentImpl())) { - finalizeNewAction((AbstractButton) component, result, configurationResolver); - } - - if (JComboBox.class.isAssignableFrom(configurationResolver.getComponentImpl())) { - finalizeNewAction((JComboBox) component, result, configurationResolver); - } - - return result; - } - - if (component == null || component instanceof AbstractButton) { - finalizeNewAction((AbstractButton) component, result, configurationResolver); - return result; - } - - if (component instanceof JComboBox) { - finalizeNewAction((JComboBox) component, result, configurationResolver); - } - } finally { - // save result in cache - cache.put(actionKey, result); - } - - return result; - } - - public A newAction(String actionKey) { - return newAction(actionKey, null); - } - - public String[] getActionNames() { - return impls.keySet().toArray(new String[impls.size()]); - } - - public Set<Map.Entry<String, Class<? extends MyAbstractAction>>> implsEntrySet() { - return impls.entrySet(); - } - - public Set<Entry<String, A>> cacheEntrySet() { - return cache.entrySet(); - } - - public void fireAction(String actionKey, Object source, JComponent component) { - A action = newAction(actionKey, component); - fireAction0(actionKey, source, action); - } - - public void fireAction(String actionKey, Object source) { - fireAction(actionKey, source, null); - } - - /** - * @param actionKey la clef de l'action - * @return l'action deja stockee dans le cache d'action, ou <code>null</code> si non trouvée. - */ - public A getActionFromCache(String actionKey) { - // on vérifie que l'action existe bien - checkRegistredAction(actionKey); - - // try in cache - if (cache.containsKey(actionKey)) { - // use cached action - A action = cache.get(actionKey); - if (log.isDebugEnabled()) { - log.debug("use cache action " + action); - } - return action; - } - return null; - } - - public void dispose() { - if (log.isInfoEnabled()) { - log.info(this); - } - for (String actionKey : getActionNames()) { - MyAbstractAction action = getActionFromCache(actionKey); - if (action != null) { - action.disposeUI(); - } - } - resetCache(); - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - resetCache(); - impls.clear(); - } - - /** - * @param component le button où rattacher l'action - * @param action action - * @param configurationResolver initializer - */ - protected void finalizeNewAction(AbstractButton component, MyAbstractAction action, ActionConfigurationResolver<?, ?> configurationResolver) { - - if (configurationResolver == null) { - // no configurationResolver matching, - if (component != null) { - action.putValue(Action.ACTION_COMMAND_KEY, component.getName()); - action.putValue(Action.SHORT_DESCRIPTION, component.getToolTipText()); - action.putValue(Action.SMALL_ICON, component.getIcon()); - action.putValue(Action.NAME, component.getText()); - action.putValue(Action.MNEMONIC_KEY, component.getMnemonic()); - action.putValue("hideActionText", component.getHideActionText()); - if (component instanceof JAXXToggleButton) { - JAXXToggleButton glueComponent = (JAXXToggleButton) component; - action.putValue(Action.SHORT_DESCRIPTION, glueComponent.getNormalTooltipText()); - action.putValue(Action.NAME, glueComponent.getNormalText()); - action.putValue(Action.SMALL_ICON, glueComponent.getIcon()); - action.putValue(Action.MNEMONIC_KEY, glueComponent.getNormalMnemonic()); - action.putValue(Action.SHORT_DESCRIPTION + 2, glueComponent.getGlueTooltipText()); - action.putValue(Action.NAME + 2, glueComponent.getGlueText()); - action.putValue(Action.SMALL_ICON + 2, glueComponent.getSelectedIcon()); - action.putValue(Action.MNEMONIC_KEY + 2, glueComponent.getGlueMnemonic()); - } - } - - } - - String text = (String) action.getValue(Action.NAME); - Integer mnemo = (Integer) action.getValue(Action.MNEMONIC_KEY); - if (mnemo != null && mnemo != '\0') { - int pos = text.indexOf((char) mnemo.intValue()); - if (pos == -1) { - pos = text.indexOf(Character.toLowerCase((char) mnemo.intValue())); - } - action.putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, pos); - } - - } - - /** - * @param component le select box où rattacher l'action - * @param action action - * @param configurationResolver initializer - */ - protected void finalizeNewAction(JComboBox component, MyAbstractAction action, ActionConfigurationResolver<?, ?> configurationResolver) { - - if (configurationResolver == null) { - action.putValue(Action.ACTION_COMMAND_KEY, component.getName()); - action.putValue(Action.SHORT_DESCRIPTION, component.getToolTipText()); - //result.putValue("selectedIndex", component.getSelectedIndex()); - } - - } - - protected ActionConfigurationResolver resolveActionConfiguration(MyAbstractAction action) { - for (ActionConfigurationResolver resolver : configurationResolvers) { - if (resolver.resolveConfiguration(action) != null) { - return resolver; - } - } - return null; - } - - protected <I extends AbstractActionConfigurationResolver> I registerInitializer(Class<I> initizalizer) { - try { - I instance = initizalizer.newInstance(); - configurationResolvers.add(instance); - return instance; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public void fireAction0(String actionKey, Object source, A action) { - if (action == null) { - log.warn("could not find action " + actionKey); - return; - } - ActionEvent event = new ActionEvent(source, ActionEvent.ACTION_FIRST, actionKey); - action.actionPerformed(event); - } - - protected void checkRegistredAction(String actionKey) { - if (!impls.containsKey(actionKey)) { - throw new IllegalStateException("can not find a registered action for key " + actionKey); - } - } - - - @SuppressWarnings({"unchecked"}) - protected A newActionInstance(String actionKey) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { - Class<? extends MyAbstractAction> klazz = impls.get(actionKey); - MyAbstractAction result; - result = klazz.getConstructor(String.class).newInstance(actionKey); - result.putValue(Action.ACTION_COMMAND_KEY, actionKey); - if (!getBaseImpl().isAssignableFrom(klazz)) { - // the instanciated action must be boxed in the base Action of the factory - result = getBaseImpl().getConstructor(MyAbstractAction.class).newInstance(result); - } - return (A) result; - } -} \ No newline at end of file Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactoryFromProvider.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactoryFromProvider.java 2008-08-05 22:30:47 UTC (rev 814) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactoryFromProvider.java 2008-08-06 10:15:00 UTC (rev 815) @@ -17,16 +17,35 @@ * ##% */ package org.codelutin.jaxx.action.factory; +import jaxx.runtime.JAXXObject; +import jaxx.runtime.swing.JAXXToggleButton; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.codelutin.jaxx.action.MyAbstractAction; +import org.codelutin.jaxx.action.initializer.AbstractActionConfigurationResolver; +import org.codelutin.jaxx.action.initializer.ActionConfigConfigurationResolver; +import org.codelutin.jaxx.action.initializer.ActionConfigurationResolver; +import org.codelutin.jaxx.action.initializer.SelectActionConfigConfigurationResolver; +import org.codelutin.jaxx.action.initializer.ToggleActionConfigConfigurationResolver; import org.codelutin.jaxx.action.provider.ActionProvider; +import javax.swing.AbstractButton; +import javax.swing.Action; +import javax.swing.Icon; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import java.awt.event.ActionEvent; +import java.lang.reflect.InvocationTargetException; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.ServiceLoader; +import java.util.Set; import java.util.TreeMap; /** * TODO Do javadoc which is not up to date... - * A simple implementation of {@link AbstractActionFactory} using a properties file to + * 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 @@ -36,16 +55,340 @@ * * @author chemit */ -public class ActionFactoryFromProvider<A extends MyAbstractAction> extends AbstractActionFactory<A> { +public class ActionFactoryFromProvider<A extends MyAbstractAction> implements ActionFactory<A> { - public static <A extends MyAbstractAction> ActionFactoryFromProvider<A> newInstance(Class<A> klazz) { + protected static Log log = LogFactory.getLog(ActionFactoryFromProvider.class); + + public static <A extends MyAbstractAction> ActionFactory<A> newInstance(Class<A> klazz) { return new ActionFactoryFromProvider<A>(klazz); } - protected ActionFactoryFromProvider(Class<A> baseClass) { - super(baseClass); + /** class of encapsuling action */ + protected Class<A> baseImpl; + + /** dictionary of known actions implementations */ + private Map<String, Class<? extends MyAbstractAction>> impls; + + /** dictionary of instanciated actions */ + private Map<String, A> cache; + + protected final ActionConfigConfigurationResolver actionConfigInitializer; + protected final ToggleActionConfigConfigurationResolver toggleActionConfigInitializer; + protected final SelectActionConfigConfigurationResolver selectActionConfigInitializer; + + protected List<AbstractActionConfigurationResolver> configurationResolvers; + + protected ActionFactoryFromProvider(Class<A> baseImpl) { + this.baseImpl = baseImpl; + this.impls = init(); + this.cache = new TreeMap<String, A>(); + this.configurationResolvers = new java.util.ArrayList<AbstractActionConfigurationResolver>(); + + this.toggleActionConfigInitializer = registerInitializer(ToggleActionConfigConfigurationResolver.class); + this.actionConfigInitializer = registerInitializer(ActionConfigConfigurationResolver.class); + this.selectActionConfigInitializer = registerInitializer(SelectActionConfigConfigurationResolver.class); } + public Class<A> getBaseImpl() { + return baseImpl; + } + + public void resetCache() { + cache.clear(); + } + + public A get(String actionKey) { + return cache.get(actionKey); + } + + public void loadActions(JAXXObject ui) { + if (log.isDebugEnabled()) { + log.debug("for ui " + ui.getClass()); + } + for (Map.Entry<String, Class<? extends MyAbstractAction>> entry : implsEntrySet()) { + String actionKey = entry.getKey(); + Object comp = ui.getObjectById(actionKey); + if (comp == null || !(comp instanceof AbstractButton || comp instanceof JComboBox)) { + // nothing to do + continue; + } + if (log.isTraceEnabled()) { + log.trace("detect action " + actionKey); + } + if (comp instanceof AbstractButton) { + AbstractButton component = (AbstractButton) comp; + A action = newAction(actionKey, component); + + component.setAction(action); + + if (component instanceof JAXXToggleButton) { + JAXXToggleButton glueComponent = (JAXXToggleButton) component; + glueComponent.setIcon((Icon) action.getValue(Action.SMALL_ICON)); + Integer integer = (Integer) action.getValue(Action.MNEMONIC_KEY); + if (integer != null) { + glueComponent.setNormalMnemonic(integer); + } + glueComponent.setSelectedIcon((Icon) action.getValue(Action.SMALL_ICON + 2)); + integer = (Integer) action.getValue(Action.MNEMONIC_KEY + 2); + if (integer != null) { + glueComponent.setGlueMnemonic(integer); + } + glueComponent.setGlueText((String) action.getValue(Action.NAME + 2)); + glueComponent.setGlueTooltipText((String) action.getValue(Action.SHORT_DESCRIPTION + 2)); + + glueComponent.setNormalText((String) action.getValue(Action.NAME)); + glueComponent.setNormalTooltipText((String) action.getValue(Action.SHORT_DESCRIPTION)); + } + + Boolean value = (Boolean) action.getValue("hideActionText"); + component.setHideActionText(value != null && value); + action.setEnabled(true); + continue; + } + // is JComboBox + JComboBox component = (JComboBox) comp; + A action = newAction(actionKey, component); + + component.setAction(action); + Integer val = (Integer) action.getValue("selectedIndex"); + if (val != null && val != -1 && val < component.getItemCount() && val != component.getSelectedIndex()) { + component.setSelectedIndex(val); + } + } + } + + /** + * @param actionKey le nom de l'action tel que définie dans le fichier + * de mapping (sans le prefix action.) + * @param component le button où rattacher l'action + * @return une nouvelle instance de l'action associée à sa clef. + */ + public A newAction(String actionKey, JComponent component) { + // try first in cache + A result = getActionFromCache(actionKey); + if (result != null) { + return result; + } + + try { + result = newActionInstance(actionKey); + } catch (Exception e) { + throw new RuntimeException(e); + } + + if (log.isDebugEnabled()) { + log.debug("create <" + actionKey + " : " + result + ">"); + } + + // recherche de l'annotation de configuration + ActionConfigurationResolver<?, ?> configurationResolver = resolveActionConfiguration(result); + + if (configurationResolver != null) { + configurationResolver.applyConfiguration(component, result); + } + + try { + + if (configurationResolver != null) { + if (AbstractButton.class.isAssignableFrom(configurationResolver.getComponentImpl())) { + finalizeNewAction((AbstractButton) component, result, configurationResolver); + } + + if (JComboBox.class.isAssignableFrom(configurationResolver.getComponentImpl())) { + finalizeNewAction((JComboBox) component, result, configurationResolver); + } + + return result; + } + + if (component == null || component instanceof AbstractButton) { + finalizeNewAction((AbstractButton) component, result, configurationResolver); + return result; + } + + if (component instanceof JComboBox) { + finalizeNewAction((JComboBox) component, result, configurationResolver); + } + } finally { + // save result in cache + cache.put(actionKey, result); + } + + return result; + } + + public A newAction(String actionKey) { + return newAction(actionKey, null); + } + + public String[] getActionNames() { + return impls.keySet().toArray(new String[impls.size()]); + } + + public Set<Entry<String, Class<? extends MyAbstractAction>>> implsEntrySet() { + return impls.entrySet(); + } + + public Set<Entry<String, A>> cacheEntrySet() { + return cache.entrySet(); + } + + public void fireAction(String actionKey, Object source, JComponent component) { + A action = newAction(actionKey, component); + fireAction0(actionKey, source, action); + } + + public void fireAction(String actionKey, Object source) { + fireAction(actionKey, source, null); + } + + /** + * @param actionKey la clef de l'action + * @return l'action deja stockee dans le cache d'action, ou <code>null</code> si non trouvée. + */ + public A getActionFromCache(String actionKey) { + // on vérifie que l'action existe bien + checkRegistredAction(actionKey); + + // try in cache + if (cache.containsKey(actionKey)) { + // use cached action + A action = cache.get(actionKey); + if (log.isDebugEnabled()) { + log.debug("use cache action " + action); + } + return action; + } + return null; + } + + public void dispose() { + if (log.isInfoEnabled()) { + log.info(this); + } + for (String actionKey : getActionNames()) { + MyAbstractAction action = getActionFromCache(actionKey); + if (action != null) { + action.disposeUI(); + } + } + resetCache(); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + resetCache(); + impls.clear(); + } + + /** + * @param component le button où rattacher l'action + * @param action action + * @param configurationResolver initializer + */ + protected void finalizeNewAction(AbstractButton component, MyAbstractAction action, ActionConfigurationResolver<?, ?> configurationResolver) { + + if (configurationResolver == null) { + // no configurationResolver matching, + if (component != null) { + action.putValue(Action.ACTION_COMMAND_KEY, component.getName()); + action.putValue(Action.SHORT_DESCRIPTION, component.getToolTipText()); + action.putValue(Action.SMALL_ICON, component.getIcon()); + action.putValue(Action.NAME, component.getText()); + action.putValue(Action.MNEMONIC_KEY, component.getMnemonic()); + action.putValue("hideActionText", component.getHideActionText()); + if (component instanceof JAXXToggleButton) { + JAXXToggleButton glueComponent = (JAXXToggleButton) component; + action.putValue(Action.SHORT_DESCRIPTION, glueComponent.getNormalTooltipText()); + action.putValue(Action.NAME, glueComponent.getNormalText()); + action.putValue(Action.SMALL_ICON, glueComponent.getIcon()); + action.putValue(Action.MNEMONIC_KEY, glueComponent.getNormalMnemonic()); + action.putValue(Action.SHORT_DESCRIPTION + 2, glueComponent.getGlueTooltipText()); + action.putValue(Action.NAME + 2, glueComponent.getGlueText()); + action.putValue(Action.SMALL_ICON + 2, glueComponent.getSelectedIcon()); + action.putValue(Action.MNEMONIC_KEY + 2, glueComponent.getGlueMnemonic()); + } + } + + } + + String text = (String) action.getValue(Action.NAME); + Integer mnemo = (Integer) action.getValue(Action.MNEMONIC_KEY); + if (mnemo != null && mnemo != '\0') { + int pos = text.indexOf((char) mnemo.intValue()); + if (pos == -1) { + pos = text.indexOf(Character.toLowerCase((char) mnemo.intValue())); + } + action.putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, pos); + } + + } + + /** + * @param component le select box où rattacher l'action + * @param action action + * @param configurationResolver initializer + */ + protected void finalizeNewAction(JComboBox component, MyAbstractAction action, ActionConfigurationResolver<?, ?> configurationResolver) { + + if (configurationResolver == null) { + action.putValue(Action.ACTION_COMMAND_KEY, component.getName()); + action.putValue(Action.SHORT_DESCRIPTION, component.getToolTipText()); + //result.putValue("selectedIndex", component.getSelectedIndex()); + } + + } + + protected ActionConfigurationResolver resolveActionConfiguration(MyAbstractAction action) { + for (ActionConfigurationResolver resolver : configurationResolvers) { + if (resolver.resolveConfiguration(action) != null) { + return resolver; + } + } + return null; + } + + protected <I extends AbstractActionConfigurationResolver> I registerInitializer(Class<I> initizalizer) { + try { + I instance = initizalizer.newInstance(); + configurationResolvers.add(instance); + return instance; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public void fireAction0(String actionKey, Object source, A action) { + if (action == null) { + log.warn("could not find action " + actionKey); + return; + } + ActionEvent event = new ActionEvent(source, ActionEvent.ACTION_FIRST, actionKey); + action.actionPerformed(event); + } + + protected void checkRegistredAction(String actionKey) { + if (!impls.containsKey(actionKey)) { + throw new IllegalStateException("can not find a registered action for key " + actionKey); + } + } + + + @SuppressWarnings({"unchecked"}) + protected A newActionInstance(String actionKey) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { + Class<? extends MyAbstractAction> klazz = impls.get(actionKey); + MyAbstractAction result; + result = klazz.getConstructor(String.class).newInstance(actionKey); + result.putValue(Action.ACTION_COMMAND_KEY, actionKey); + if (!getBaseImpl().isAssignableFrom(klazz)) { + // the instanciated action must be boxed in the base Action of the factory + result = getBaseImpl().getConstructor(MyAbstractAction.class).newInstance(result); + } + return (A) result; + } + + public Map<String, Class<? extends MyAbstractAction>> init() { if (log.isDebugEnabled()) { log.debug("start loading " + this); Deleted: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/AbstractActionProvider.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/AbstractActionProvider.java 2008-08-05 22:30:47 UTC (rev 814) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/AbstractActionProvider.java 2008-08-06 10:15:00 UTC (rev 815) @@ -1,73 +0,0 @@ -/** - * # #% Copyright (C) 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 org.codelutin.jaxx.action.provider; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codelutin.jaxx.action.MyAbstractAction; - -import java.util.Map; - -/** @author chemit */ -public abstract class AbstractActionProvider<A extends MyAbstractAction> implements ActionProvider<A> { - - protected static Log log = LogFactory.getLog(AbstractActionProvider.class); - - protected String name; - - protected Class<A> baseClass; - - protected Map<String, Class<? extends A>> actions; - - protected abstract Map<String, Class<? extends A>> initCache(); - - protected AbstractActionProvider(String name, Class<A> baseClass, boolean doInitCache) { - this.name = name; - this.baseClass = baseClass; - if (doInitCache) { - this.actions = initCache(); - } - } - - public String getName() { - return name; - } - - public Class<A> getBaseClass() { - return baseClass; - } - - public Map<String, Class<? extends A>> getClasses() { - return actions; - } - - @Override - public String toString() { - return super.toString() + "<name:" + name + ", baseClass:" + baseClass.getSimpleName() + ">"; - } - - protected void clearCache() { - if (actions != null) { - actions.clear(); - actions = null; - } - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - clearCache(); - } -} Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java 2008-08-05 22:30:47 UTC (rev 814) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java 2008-08-06 10:15:00 UTC (rev 815) @@ -14,6 +14,8 @@ */ package org.codelutin.jaxx.action.provider; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import static org.codelutin.i18n.I18n._; import org.codelutin.jaxx.action.ActionNameHelper.RuntimeActionNameProvider; import org.codelutin.jaxx.action.MyAbstractAction; @@ -25,19 +27,61 @@ import java.util.TreeMap; /** @author chemit */ -public class ActionProviderFromProperties<A extends MyAbstractAction> extends AbstractActionProvider<A> { +public class ActionProviderFromProperties<A extends MyAbstractAction> implements ActionProvider<A> { /** default prefix for an entryin mapping file. */ protected static final String ACTION_KEY_PREFIX = "action."; protected String propertiesPath; + protected static Log log = LogFactory.getLog(ActionProviderFromProperties.class); + + protected String name; + + protected Class<A> baseClass; + + protected Map<String, Class<? extends A>> actions; + + protected ActionProviderFromProperties(String name, Class<A> baseClass, String propertiesPath) { - super(name, baseClass, false); + + this.name = name; + this.baseClass = baseClass; + this.propertiesPath = propertiesPath; this.actions = initCache(); } + public String getName() { + return name; + } + + public Class<A> getBaseClass() { + return baseClass; + } + + public Map<String, Class<? extends A>> getClasses() { + return actions; + } + + @Override + public String toString() { + return super.toString() + "<name:" + name + ", baseClass:" + baseClass.getSimpleName() + ">"; + } + + protected void clearCache() { + if (actions != null) { + actions.clear(); + actions = null; + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + clearCache(); + } + @SuppressWarnings({"unchecked"}) protected Map<String, Class<? extends A>> initCache() {