Author: tchemit Date: 2008-07-24 20:59:46 +0000 (Thu, 24 Jul 2008) New Revision: 791 Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/MyAbstractAction.java 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/ActionFactory.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactoryFromProvider.java Log: ActionFactory is typed with MyAbstractionAction Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/MyAbstractAction.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/MyAbstractAction.java 2008-07-24 19:18:03 UTC (rev 790) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/MyAbstractAction.java 2008-07-24 20:59:46 UTC (rev 791) @@ -18,8 +18,10 @@ import org.apache.commons.logging.LogFactory; import javax.swing.AbstractAction; +import javax.swing.Action; import javax.swing.JComponent; import java.awt.event.ActionEvent; +import java.beans.PropertyChangeListener; /** @author chemit */ public abstract class MyAbstractAction extends AbstractAction { @@ -38,13 +40,13 @@ super(name); } - protected MyAbstractAction(String name, MyAbstractAction delegate) { - super(name); + protected MyAbstractAction(MyAbstractAction delegate) { + super((String) delegate.getValue(Action.NAME)); this.delegate = delegate; } public void actionPerformed(java.awt.event.ActionEvent e) { - if (delegate != null) { + if (hasDelegate()) { // delegate to real action delegate.actionPerformed(e); return; @@ -75,21 +77,21 @@ } public String getI18nToolTipText() { - if (delegate != null) { + if (hasDelegate()) { return delegate.getI18nToolTipText(); } return getPrefix() + ".action." + getActionName() + ".tooltip"; } public void updateUI() { - if (delegate != null) { + if (hasDelegate()) { delegate.updateUI(); } // nothing by default } public void disposeUI() { - if (delegate != null) { + if (hasDelegate()) { delegate.disposeUI(); } // nothing by default @@ -99,20 +101,24 @@ return delegate; } + public boolean hasDelegate() { + return delegate != null; + } + protected String getActionName() { return (String) getValue(ACTION_COMMAND_KEY); } protected boolean beforeAction(ActionEvent evt) throws Exception { boolean enabled = isEnabled(); - if (enabled && delegate != null) { + if (enabled && hasDelegate()) { return delegate.beforeAction(evt); } return enabled; } protected void doAction(ActionEvent evt) throws Exception { - if (delegate != null) { + if (hasDelegate()) { delegate.doAction(evt); } // nothing by default @@ -126,7 +132,7 @@ } protected void clear() { - if (delegate != null) { + if (hasDelegate()) { delegate.clear(); } // nothing by default @@ -136,4 +142,89 @@ log.error(e); } + // ----------------------------------------------------------------------------- + // --- super classe delegate methods ------------------------------------------- + // ----------------------------------------------------------------------------- + + @Override + public Object getValue(String key) { + if (hasDelegate()) { + return delegate.getValue(key); + } + return super.getValue(key); + } + + @Override + public void putValue(String key, Object newValue) { + if (hasDelegate()) { + delegate.putValue(key, newValue); + } + super.putValue(key, newValue); + } + + @Override + public boolean isEnabled() { + if (hasDelegate()) { + return delegate.isEnabled(); + } + return super.isEnabled(); + } + + @Override + public void setEnabled(boolean newValue) { + if (hasDelegate()) { + delegate.setEnabled(newValue); + } + super.setEnabled(newValue); + } + + @Override + public Object[] getKeys() { + if (hasDelegate()) { + return getKeys(); + } + return super.getKeys(); + } + + @Override + protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + if (hasDelegate()) { + delegate.firePropertyChange(propertyName, oldValue, newValue); + } + super.firePropertyChange(propertyName, oldValue, newValue); + } + + @Override + public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { + if (hasDelegate()) { + delegate.addPropertyChangeListener(listener); + } + super.addPropertyChangeListener(listener); + } + + @Override + public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { + if (hasDelegate()) { + delegate.removePropertyChangeListener(listener); + } + super.removePropertyChangeListener(listener); + } + + @Override + public synchronized PropertyChangeListener[] getPropertyChangeListeners() { + if (hasDelegate()) { + return delegate.getPropertyChangeListeners(); + } + return super.getPropertyChangeListeners(); + } + + @Override + protected Object clone() throws CloneNotSupportedException { + if (hasDelegate()) { + return clone(); + } + return super.clone(); + } + + } Modified: 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-07-24 19:18:03 UTC (rev 790) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/AbstractActionFactory.java 2008-07-24 20:59:46 UTC (rev 791) @@ -37,9 +37,9 @@ import javax.swing.JComponent; import java.awt.event.ActionEvent; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; @@ -67,7 +67,7 @@ private Map<String, Class<? extends MyAbstractAction>> impls; /** dictionary of instanciated actions */ - private Map<String, MyAbstractAction> cache; + private Map<String, A> cache; protected final ActionConfigInitializer actionConfigInitializer; protected final ToggleActionConfigInitializer toggleActionConfigInitializer; @@ -79,7 +79,7 @@ protected AbstractActionFactory(Class<A> baseImpl) { this.baseImpl = baseImpl; this.impls = init(); - this.cache = new TreeMap<String, MyAbstractAction>(); + this.cache = new TreeMap<String, A>(); this.registredInitializers = new java.util.ArrayList<AbstractActionInitializer>(); this.toggleActionConfigInitializer = registerInitializer(ToggleActionConfigInitializer.class); @@ -95,15 +95,15 @@ cache.clear(); } - public MyAbstractAction get(String actionKey) { + public A get(String actionKey) { return cache.get(actionKey); } - public MyAbstractAction[] loadActions(JAXXObject ui) { + public void loadActions(JAXXObject ui) { if (log.isDebugEnabled()) { log.debug("for ui " + ui.getClass()); } - List<MyAbstractAction> result = new ArrayList<MyAbstractAction>(); + //List<A> result = new ArrayList<A>(); for (Map.Entry<String, Class<? extends MyAbstractAction>> entry : implsEntrySet()) { String actionKey = entry.getKey(); Object comp = ui.getObjectById(actionKey); @@ -116,7 +116,7 @@ } if (comp instanceof AbstractButton) { AbstractButton component = (AbstractButton) comp; - MyAbstractAction action = newAction(actionKey, component); + A action = newAction(actionKey, component); component.setAction(action); @@ -142,21 +142,21 @@ Boolean value = (Boolean) action.getValue("hideActionText"); component.setHideActionText(value != null && value); action.setEnabled(true); - result.add(action); + //result.add(action); continue; } // is JComboBox JComboBox component = (JComboBox) comp; - MyAbstractAction action = newAction(actionKey, component); + 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); } - result.add(action); + //result.add(action); } - return result.toArray(new MyAbstractAction[result.size()]); + //return (A[]) result.toArray(new MyAbstractAction[result.size()]); } /** @@ -165,9 +165,9 @@ * @param component le button o� rattacher l'action * @return une nouvelle instance de l'action associ�e � sa clef. */ - public MyAbstractAction newAction(String actionKey, JComponent component) { + public A newAction(String actionKey, JComponent component) { // try first in cache - MyAbstractAction result = getActionFromCache(actionKey); + A result = getActionFromCache(actionKey); if (result != null) { return result; } @@ -227,12 +227,12 @@ return impls.entrySet(); } - public Set<Map.Entry<String, MyAbstractAction>> cacheEntrySet() { + public Set<Entry<String, A>> cacheEntrySet() { return cache.entrySet(); } public void fireAction(String actionKey, Object source, JComponent component) { - AbstractAction action = newAction(actionKey, component); + A action = newAction(actionKey, component); fireAction0(actionKey, source, action); } @@ -244,14 +244,14 @@ * @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 MyAbstractAction getActionFromCache(String actionKey) { + 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 - MyAbstractAction action = cache.get(actionKey); + A action = cache.get(actionKey); if (log.isDebugEnabled()) { log.debug("use cache action " + action); } @@ -357,7 +357,7 @@ } } - protected void fireAction0(String actionKey, Object source, AbstractAction action) { + protected void fireAction0(String actionKey, Object source, A action) { if (action == null) { log.warn("could not find action " + actionKey); return; @@ -373,11 +373,16 @@ } - protected MyAbstractAction newActionInstance(String actionKey) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { + @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); - return result; + 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/ActionFactory.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactory.java 2008-07-24 19:18:03 UTC (rev 790) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactory.java 2008-07-24 20:59:46 UTC (rev 791) @@ -45,17 +45,17 @@ void resetCache(); - MyAbstractAction get(String actionKey); + A get(String actionKey); - MyAbstractAction[] loadActions(JAXXObject ui); + void loadActions(JAXXObject ui); - MyAbstractAction newAction(String actionKey, JComponent component); + A newAction(String actionKey, JComponent component); String[] getActionNames(); Set<Entry<String, Class<? extends MyAbstractAction>>> implsEntrySet(); - Set<Entry<String, MyAbstractAction>> cacheEntrySet(); + Set<Entry<String, A>> cacheEntrySet(); void fireAction(String actionKey, Object source, JComponent component); 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-07-24 19:18:03 UTC (rev 790) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/factory/ActionFactoryFromProvider.java 2008-07-24 20:59:46 UTC (rev 791) @@ -25,6 +25,7 @@ import java.util.TreeMap; /** + * TODO Do javadoc which is not up to date... * A simple implementation of {@link AbstractActionFactory} using a properties file to * load action mapping. * <p/>
participants (1)
-
tchemit@users.labs.libre-entreprise.org