Author: tchemit Date: 2008-01-21 04:44:12 +0000 (Mon, 21 Jan 2008) New Revision: 329 Modified: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerActionManager.java Log: ajout annotation de configuration pour ComboBox Modified: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerActionManager.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerActionManager.java 2008-01-21 04:43:52 UTC (rev 328) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerActionManager.java 2008-01-21 04:44:12 UTC (rev 329) @@ -23,6 +23,7 @@ import fr.cemagref.simexplorer.is.ui.swing.util.ActionConfig; import fr.cemagref.simexplorer.is.ui.swing.util.GlueActionConfig; import fr.cemagref.simexplorer.is.ui.swing.util.MyToggleButton; +import fr.cemagref.simexplorer.is.ui.swing.util.SelectActionConfig; import jaxx.runtime.JAXXObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -31,6 +32,7 @@ import javax.swing.AbstractButton; import javax.swing.Action; import javax.swing.Icon; +import javax.swing.JComboBox; import java.io.IOException; import java.util.Map; import java.util.Properties; @@ -99,37 +101,52 @@ for (Map.Entry<String, Class<? extends SimExplorerAbstractAction>> entry : impls.entrySet()) { String actionKey = entry.getKey(); Object comp = ui.getObjectById(actionKey); - if (comp == null || !(comp instanceof AbstractButton)) { + if (comp == null || !(comp instanceof AbstractButton || comp instanceof JComboBox)) { continue; } - AbstractButton component = (AbstractButton) comp; + if (comp instanceof AbstractButton) { + AbstractButton component = (AbstractButton) comp; - SimExplorerAbstractAction action = newAction(actionKey, component); + SimExplorerAbstractAction action = newAction(actionKey, component); - component.setAction(action); + component.setAction(action); - if (component instanceof MyToggleButton) { - MyToggleButton glueComponent = (MyToggleButton) component; - glueComponent.setIcon((Icon) action.getValue(Action.SMALL_ICON )); - Integer integer = (Integer) action.getValue(Action.MNEMONIC_KEY); - if (integer!=null) { - glueComponent.setNormalMnemonic(integer); + if (component instanceof MyToggleButton) { + MyToggleButton glueComponent = (MyToggleButton) 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)); } - glueComponent.setSelectedIcon((Icon) action.getValue(Action.SMALL_ICON + 2)); - integer = (Integer) action.getValue(Action.MNEMONIC_KEY + 2); - if (integer!=null) { - glueComponent.setGlueMnemonic(integer); + + Boolean value = (Boolean) action.getValue("hideActionText"); + + component.setHideActionText(value != null && value); + continue; + } + if (comp instanceof JComboBox) { + JComboBox component = (JComboBox) comp; + + SimExplorerAbstractAction action = newAction(actionKey, component); + + component.setAction(action); + Integer val = (Integer) action.getValue("selectedIndex"); + if (val != null && val<component.getItemCount()) { + component.setSelectedIndex(val); } - 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); } } @@ -167,10 +184,10 @@ result.putValue("hideActionText", component.getHideActionText()); if (component instanceof MyToggleButton) { MyToggleButton glueComponent = (MyToggleButton) component; - result.putValue(Action.SHORT_DESCRIPTION , glueComponent.getNormalTooltipText()); - result.putValue(Action.NAME , glueComponent.getNormalText()); - result.putValue(Action.SMALL_ICON , glueComponent.getIcon()); - result.putValue(Action.MNEMONIC_KEY , glueComponent.getNormalMnemonic()); + result.putValue(Action.SHORT_DESCRIPTION, glueComponent.getNormalTooltipText()); + result.putValue(Action.NAME, glueComponent.getNormalText()); + result.putValue(Action.SMALL_ICON, glueComponent.getIcon()); + result.putValue(Action.MNEMONIC_KEY, glueComponent.getNormalMnemonic()); result.putValue(Action.SHORT_DESCRIPTION + 2, glueComponent.getGlueTooltipText()); result.putValue(Action.NAME + 2, glueComponent.getGlueText()); result.putValue(Action.SMALL_ICON + 2, glueComponent.getSelectedIcon()); @@ -194,6 +211,39 @@ } } + /** + * @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 static SimExplorerAbstractAction newAction(String actionKey, JComboBox component) { + + // on vérifie que l'action existe bien + checkRegistredAction(actionKey); + + // on récupère la classe d'implantation de l'action + Class<? extends SimExplorerAbstractAction> klazz = impls.get(actionKey); + + try { + SimExplorerAbstractAction result = klazz.getConstructor(String.class).newInstance(actionKey); + //result.putValue(Action.ACTION_COMMAND_KEY, actionKey); + + log.debug(actionKey + " : " + result); + // recherche de l'annotation de configuration + SelectActionConfig anno = initSelectActionConfig(component, klazz, result); + if (anno == null) { + result.putValue(Action.ACTION_COMMAND_KEY, component.getName()); + result.putValue(Action.SHORT_DESCRIPTION, component.getToolTipText()); + result.putValue("selectedIndex", component.getSelectedIndex()); + + } + return result; + } catch (Exception e) { + throw new SimExplorerRuntimeException(e); + } + } + private static GlueActionConfig initGlueActionConfig(AbstractButton component, Class<? extends SimExplorerAbstractAction> klazz, SimExplorerAbstractAction result) { GlueActionConfig anno = klazz.getAnnotation(GlueActionConfig.class); if (anno != null) { @@ -268,6 +318,25 @@ return anno; } + private static SelectActionConfig initSelectActionConfig(JComboBox component, Class<? extends SimExplorerAbstractAction> klazz, SimExplorerAbstractAction result) { + SelectActionConfig anno = klazz.getAnnotation(SelectActionConfig.class); + if (anno != null) { + // inject les données + if (!anno.name().isEmpty()) { + result.putValue(Action.NAME, _(anno.name())); + } + if (!anno.shortDescription().isEmpty()) { + result.putValue(Action.SHORT_DESCRIPTION, _(anno.shortDescription())); + } else { + result.putValue(Action.SHORT_DESCRIPTION, _(component.getToolTipText())); + } + result.putValue("selectedIndex", anno.selectedIndex()); + //TODO Convert it from String result.putValue(Action.ACCELERATOR_KEY, anno.accelerator()); + result.setEnabled(anno.enabled()); + } + return anno; + } + protected static void checkInit() { if (impls == null) { throw new SimExplorerRuntimeException("you must init first the " + SimExplorerActionManager.class.getName() + " class via init method");