Author: tchemit
Date: 2008-01-22 01:03:39 +0000 (Tue, 22 Jan 2008)
New Revision: 349
Modified:
trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerActionManager.java
Log:
utilisation de la factory d'actions
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-22 01:02:59 UTC (rev 348)
+++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerActionManager.java 2008-01-22 01:03:39 UTC (rev 349)
@@ -20,18 +20,14 @@
import fr.cemagref.simexplorer.is.ui.SimExplorerRuntimeException;
import fr.cemagref.simexplorer.is.ui.swing.action.SimExplorerAbstractAction;
-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 jaxx.runtime.builder.ActionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.codelutin.i18n.I18n._;
+import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
-import javax.swing.Action;
-import javax.swing.Icon;
import javax.swing.JComboBox;
import java.io.IOException;
import java.util.Map;
@@ -40,11 +36,9 @@
/**
* La classe responsable de l'enregistrement des actions disponibles
- * dans l'application ({@link #init()} .
+ * dans l'application en délégeant à une ActionFactory.
* <p/>
- * C'est aussi une usine d'actions ({@link #newAction(String,javax.swing.AbstractButton)} .
- * <p/>
- * On peut de plus via la méthode {@link #loadActions(JAXXObject)} de charger
+ * On peut de plus via la méthode {@link #loadActions(JAXXObject,SimExplorerTabManager)} de charger
* dans l'ui les actions connues.
*
* @author tony
@@ -52,301 +46,94 @@
*/
public class SimExplorerActionManager {
- static private Log log = LogFactory.getLog(SimExplorerActionManager.class);
+ private static final Log log = LogFactory.getLog(SimExplorerActionManager.class);
/** path in classpath where to find actions mapping */
protected static final String ACTIONS_FILE_PATH = "/actions.properties";
protected static final String ACTION_KEY_PREFIX = "action.";
- static Map<String, Class<? extends SimExplorerAbstractAction>> impls;
+ /** l'usine d'actions */
+ private static ActionFactory factory;
- @SuppressWarnings({"unchecked"})
- public static void init() {
- if (impls == null) {
- // first init load actions
- Map<String, Class<? extends SimExplorerAbstractAction>> cache = new TreeMap<String, Class<? extends SimExplorerAbstractAction>>();
- Properties properties;
- try {
- properties = new Properties();
- properties.load(Class.class.getResourceAsStream(ACTIONS_FILE_PATH));
- } catch (IOException e) {
- throw new SimExplorerRuntimeException(_("could not load actions.properties file for reason {0} ", e.getMessage()));
- }
- 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 SimExplorerAbstractAction> implCass;
- implCass = (Class<? extends SimExplorerAbstractAction>) Class.forName(qfn);
- log.debug("found action " + implCass);
- cache.put(key.substring(prefix), implCass);
- } catch (ClassNotFoundException e) {
- impls = cache;
- throw new SimExplorerRuntimeException(_("could not found for action key {0} class {1} ", key, qfn));
- }
- }
- impls = cache;
- }
- }
-
- public static String[] getActionNames() {
- checkInit();
- return impls.keySet().toArray(new String[impls.size()]);
- }
-
- public static void loadActions(JAXXObject ui) {
- checkInit();
- 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 || comp instanceof JComboBox)) {
- continue;
- }
- if (comp instanceof AbstractButton) {
- AbstractButton component = (AbstractButton) comp;
-
- SimExplorerAbstractAction action = newAction(actionKey, component);
-
- 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);
- }
- 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);
- 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);
- }
- }
-
- }
- }
-
/**
- * @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.
+ * la tab en cours de chargement, utilisé dans l'init des actions
+ * de type <code>SimExplorerAbstractTabAction</code> pour savoir à quel tab
+ * elles se rattachent.
*/
- public static SimExplorerAbstractAction newAction(String actionKey, AbstractButton component) {
+ private static SimExplorerTabManager loadingTab;
- // 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
- ActionConfig anno = initActionConfig(component, klazz, result);
-
- if (anno == null) {
- GlueActionConfig anno2 = initGlueActionConfig(component, klazz, result);
- if (anno2 == null && component != null) {
- result.putValue(Action.ACTION_COMMAND_KEY, component.getName());
- result.putValue(Action.SHORT_DESCRIPTION, component.getToolTipText());
- result.putValue(Action.SMALL_ICON, component.getIcon());
- result.putValue(Action.NAME, component.getText());
- result.putValue(Action.MNEMONIC_KEY, component.getMnemonic());
- 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 + 2, glueComponent.getGlueTooltipText());
- result.putValue(Action.NAME + 2, glueComponent.getGlueText());
- result.putValue(Action.SMALL_ICON + 2, glueComponent.getSelectedIcon());
- result.putValue(Action.MNEMONIC_KEY + 2, glueComponent.getGlueMnemonic());
- }
- }
- }
-
- String text = (String) result.getValue(Action.NAME);
- Integer mnemo = (Integer) result.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()));
- }
- result.putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, pos);
- }
- return result;
- } catch (Exception e) {
- throw new SimExplorerRuntimeException(e);
- }
- }
-
/**
- * @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.
+ * @param ui l'ui à charger
+ * @param fromTab la tab en cours de chargement ou <code>null</code< si l'ui
+ * n'est pas une tab
+ * @return le tableau des actions instanciées ou récupérées du cache de
+ * la factory
*/
- 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);
-
+ public static AbstractAction[] loadActions(JAXXObject ui, SimExplorerTabManager fromTab) {
+ loadingTab = fromTab;
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);
+ AbstractAction[] actions;
+ actions = getFactory().loadActions(ui);
+ return actions;
+ } finally {
+ loadingTab = null;
}
}
- private static GlueActionConfig initGlueActionConfig(AbstractButton component, Class<? extends SimExplorerAbstractAction> klazz, SimExplorerAbstractAction result) {
- GlueActionConfig anno = klazz.getAnnotation(GlueActionConfig.class);
- if (anno != null) {
- // inject les données
- if (!anno.name().isEmpty()) {
- //System.out.println("found action with name : " + anno.name());
- result.putValue(Action.NAME, _(anno.name()));
- }
- if (!anno.name2().isEmpty()) {
- //System.out.println("found action with name2 : " + anno.name2());
- result.putValue(Action.NAME + "2", _(anno.name2()));
- }
-
- if (!anno.shortDescription().isEmpty()) {
- result.putValue(Action.SHORT_DESCRIPTION, _(anno.shortDescription()));
- }
- if (!anno.shortDescription2().isEmpty()) {
- result.putValue(Action.SHORT_DESCRIPTION + "2", _(anno.shortDescription2()));
- }
-
- if (!anno.smallIcon().isEmpty()) {
- result.putValue(Action.SMALL_ICON, fr.cemagref.simexplorer.is.ui.swing.util.UIHelper.createImageIcon(anno.smallIcon()));
- }
- if (!anno.smallIcon2().isEmpty()) {
- result.putValue(Action.SMALL_ICON + "2", fr.cemagref.simexplorer.is.ui.swing.util.UIHelper.createImageIcon(anno.smallIcon2()));
- }
-
- if (anno.mnemonic() != '\0') {
- result.putValue(Action.MNEMONIC_KEY, anno.mnemonic());
- } else if (component != null) {
- result.putValue(Action.MNEMONIC_KEY, component.getMnemonic());
- }
- if (anno.mnemonic2() != '\0') {
- result.putValue(Action.MNEMONIC_KEY + "2", anno.mnemonic2());
- }
- //TODO Convert it from String result.putValue(Action.ACCELERATOR_KEY, anno.accelerator());
-
-
- result.putValue("hideActionText", anno.hideActionText());
- result.putValue(Action.SELECTED_KEY, anno.selected());
- result.setEnabled(anno.enabled());
- }
- return anno;
+ /** @return la tab en cours de chargement ou bien <code>null</code> */
+ public static SimExplorerTabManager getLoadingTab() {
+ return loadingTab;
}
- private static ActionConfig initActionConfig(AbstractButton component, Class<? extends SimExplorerAbstractAction> klazz, SimExplorerAbstractAction result) {
- ActionConfig anno = klazz.getAnnotation(ActionConfig.class);
- if (anno != null) {
- // inject les données
- if (!anno.name().isEmpty()) {
- //System.out.println("found action with name : " + anno.name());
- result.putValue(Action.NAME, _(anno.name()));
- }
- //if (!anno.shortDescription().isEmpty()) {
- result.putValue(Action.SHORT_DESCRIPTION, _(anno.shortDescription()));
- //}
- if (!anno.smallIcon().isEmpty()) {
- result.putValue(Action.SMALL_ICON, fr.cemagref.simexplorer.is.ui.swing.util.UIHelper.createImageIcon(anno.smallIcon()));
- }
- if (anno.mnemonic() != '\0') {
- result.putValue(Action.MNEMONIC_KEY, anno.mnemonic());
- } else if (component != null) {
- result.putValue(Action.MNEMONIC_KEY, component.getMnemonic());
- }
- //TODO Convert it from String result.putValue(Action.ACCELERATOR_KEY, anno.accelerator());
-
-
- result.putValue("hideActionText", anno.hideActionText());
- result.putValue(Action.SELECTED_KEY, anno.selected());
- result.setEnabled(anno.enabled());
+ /**
+ * Retourne (et instancie la première fois l'usine d'actions)
+ *
+ * @return l'usine d'actions
+ * @see jaxx.runtime.builder.ActionFactory
+ */
+ protected static ActionFactory getFactory() {
+ if (factory == null) {
+ factory = new ActionFactory() {
+ @SuppressWarnings({"unchecked"})
+ protected Map<String, Class<? extends AbstractAction>> init() {
+ // first init load actions
+ Properties properties = new Properties();
+ try {
+ properties.load(Class.class.getResourceAsStream(ACTIONS_FILE_PATH));
+ } catch (IOException e) {
+ throw new SimExplorerRuntimeException(_("simexplorer.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 SimExplorerAbstractAction>) Class.forName(qfn);
+ log.debug("found action " + implCass);
+ cache.put(key.substring(prefix), implCass);
+ } catch (ClassNotFoundException e) {
+ throw new SimExplorerRuntimeException(_("simexplorer.error.load.actions.class", key, qfn));
+ }
+ }
+ return cache;
+ }
+ };
}
- return anno;
+ return factory;
}
- 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;
+ public static AbstractAction newAction(String actionKey, AbstractButton component) {
+ return getFactory().newAction(actionKey, component);
}
- protected static void checkInit() {
- if (impls == null) {
- throw new SimExplorerRuntimeException("you must init first the " + SimExplorerActionManager.class.getName() + " class via init method");
- }
+ public static AbstractAction newAction(String actionKey, JComboBox component) {
+ return getFactory().newAction(actionKey, component);
}
- protected static void checkRegistredAction(String actionKey) {
- checkInit();
- if (!impls.containsKey(actionKey)) {
- throw new SimExplorerRuntimeException(_("can not find a registered action for key {0} ", actionKey));
- }
+ public static void resetCache() {
+ getFactory().resetCache();
}
}