01: /*
02: * Created on May 5, 2003
03: *
04: * Dbmjui is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU General Public License version 2 as
06: * published by the Free Software Foundation.
07: *
08: * Dbmjui is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public
14: * License along with dbmjui; see the file COPYING. If not,
15: * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16: * Boston, MA 02111-1307, USA.
17: *
18: */
19: package fr.aliacom.form.swt;
20:
21: import java.lang.reflect.Constructor;
22:
23: import org.w3c.dom.Element;
24:
25: import fr.aliacom.commands.Command;
26: import fr.aliacom.commands.CommandPool;
27: import fr.aliacom.form.common.FormContext;
28: import fr.aliacom.form.common.IForm;
29: import fr.aliacom.form.common.events.CommandEvent;
30: import fr.aliacom.form.common.events.ICommandListener;
31: import fr.aliacom.form.swt.events.CommandSelectionListener;
32:
33: /**
34: * @author tom
35: *
36: * (c) 2001, 2003 Thomas Cataldo
37: */
38: public abstract class SWTCommandBuilder extends SWTBuilder {
39:
40: protected void initFromPythonAction(IItem item, Element elem,
41: IForm form, FormContext ctx) {
42: item.setText(getNodeText(elem));
43: addListener(item, CommandPool.getInstance().getCommand(form,
44: ctx, elem.getAttribute("pythonAction")));
45: }
46:
47: protected void initFromJavaAction(IItem item, Element elem,
48: IForm form) {
49: Command c = null;
50: try {
51: Class klass = Class
52: .forName(elem.getAttribute("javaAction"));
53: Constructor ctor = klass
54: .getConstructor(new Class[] { IForm.class });
55: c = (Command) ctor.newInstance(new Object[] { form });
56: } catch (Exception e) {
57: log.fatal("Error while loading command ", e);
58: return;
59: }
60: item.setText(getNodeText(elem));
61: addListener(item, c);
62: }
63:
64: private void addListener(final IItem item, Command c) {
65: // execute command on click
66: item.addSelectionListener(new CommandSelectionListener(c));
67: // disable the item while the command runs...
68: c.addCommandListener(new ICommandListener() {
69: public void stateChanged(CommandEvent ce) {
70: switch (ce.getState()) {
71: case Command.STATE_ENABLED:
72: item.setEnabled(true);
73: break;
74: case Command.STATE_DISABLED:
75: item.setEnabled(false);
76: break;
77: default:
78: break;
79: }
80: }
81: });
82: }
83:
84: }
|