Author: tchemit Date: 2008-04-20 12:23:38 +0000 (Sun, 20 Apr 2008) New Revision: 607 Modified: trunk/lutinui/src/main/java/org/codelutin/ui/AbstractUIAction.java trunk/lutinui/src/main/java/org/codelutin/ui/DialogUI.java Log: generic action factory in DialogUI.newAction Modified: trunk/lutinui/src/main/java/org/codelutin/ui/AbstractUIAction.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/AbstractUIAction.java 2008-04-20 10:49:03 UTC (rev 606) +++ trunk/lutinui/src/main/java/org/codelutin/ui/AbstractUIAction.java 2008-04-20 12:23:38 UTC (rev 607) @@ -23,32 +23,25 @@ protected static Log log = LogFactory.getLog(AbstractUIAction.class); protected transient DialogUI<? extends H> ui; - protected transient H handler; private static final long serialVersionUID = 1L; - protected AbstractUIAction(String name, javax.swing.Icon icon) { + protected AbstractUIAction(String name, javax.swing.Icon icon, DialogUI<? extends H> ui) { super(name, icon); + this.ui = ui; } - public AbstractUIAction() { - } - protected H getHandler() { checkInit(); - if (handler == null) { - handler = ui.getHandler(); - } - return handler; + return ui.getHandler(); } protected void setUi(DialogUI<? extends H> ui) { this.ui = ui; - this.handler = null; } protected void checkInit() throws IllegalStateException { - if (handler == null && ui == null) { + if (ui == null) { throw new IllegalStateException("no handler, nor ui referenced in " + this); } } Modified: trunk/lutinui/src/main/java/org/codelutin/ui/DialogUI.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/DialogUI.java 2008-04-20 10:49:03 UTC (rev 606) +++ trunk/lutinui/src/main/java/org/codelutin/ui/DialogUI.java 2008-04-20 12:23:38 UTC (rev 607) @@ -23,6 +23,7 @@ import javax.swing.JDialog; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; +import java.lang.reflect.Constructor; /** * A abstract dialog contract to be realised by a dialogUI (WindowEvent adapter) @@ -35,16 +36,44 @@ protected static Log log = LogFactory.getLog(DialogUI.class); + public javax.swing.AbstractAction newAction(Class<? extends AbstractUIAction> actionClass, Object... params) { + Constructor<?> constructor = null; + for (Constructor<?> cons : actionClass.getConstructors()) { + Class<?>[] prototype = cons.getParameterTypes(); + if (prototype.length > 0 && DialogUI.class.isAssignableFrom(prototype[0])) { + // use this constructor + constructor = cons; + break; + } + } + if (constructor == null) { + throw new IllegalStateException("could not find a matching constructor for " + actionClass); + } + + // wrap params + Object[] parameters = new Object[1 + params.length]; + parameters[0] = this; + System.arraycopy(params, 0, parameters, 1, params.length); + try { + AbstractAction action = (AbstractAction) constructor.newInstance(parameters); + if (log.isInfoEnabled()) { + log.info(action); + } + return action; + } catch (Exception e) { + throw new IllegalStateException("could not init the action " + actionClass + " for reason : " + e.getMessage()); + } + } + private H handler; public abstract AbstractButton getHelp(); - //TODO will be handled by jaxx with javax.help... - protected abstract AbstractAction createHelpAction(); - protected DialogUI() { UIHelper.setQuitAction(this); addWindowListener(this); + //TODO will be handled by jaxx with javax.help... + //getHelp().setAction(newAction(HelpAction.class)); } public H getHandler() {