001: package fr.aliacom.form.swt;
002:
003: import java.beans.PropertyChangeListener;
004: import java.beans.PropertyChangeSupport;
005:
006: import org.apache.log4j.Logger;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.DisposeEvent;
009: import org.eclipse.swt.events.DisposeListener;
010: import org.eclipse.swt.graphics.Image;
011: import org.eclipse.swt.layout.FillLayout;
012: import org.eclipse.swt.widgets.Display;
013: import org.eclipse.swt.widgets.Shell;
014:
015: import fr.aliacom.commands.Command;
016: import fr.aliacom.common.ui.IFrame;
017: import fr.aliacom.common.ui.IIcon;
018: import fr.aliacom.form.common.ApplyChangeException;
019: import fr.aliacom.form.common.FormContext;
020: import fr.aliacom.form.common.FormLoader;
021: import fr.aliacom.form.common.FormManager;
022: import fr.aliacom.form.common.IForm;
023: import fr.aliacom.form.common.LoadException;
024: import fr.aliacom.form.common.ToolkitManager;
025:
026: /**
027: * @author tom
028: *
029: * (C) 2001, 2003 Thomas Cataldo
030: */
031: public final class SWTForm implements IForm, DisposeListener {
032:
033: private FormContext ctx;
034: private FormLoader loader;
035: private PropertyChangeSupport pcs;
036: private IFrame frame;
037: private Shell shell;
038: private Command load;
039: private Command save;
040: private boolean isClosing;
041: private boolean dlLock;
042:
043: private static final Logger log = Logger.getLogger(SWTForm.class);
044:
045: public SWTForm(FormContext ctx, IForm parent) {
046: this .ctx = ctx;
047: pcs = new PropertyChangeSupport(this );
048: if (parent == null) {
049: shell = new Shell((Display) ToolkitManager.getToolkit()
050: .getDisplay());
051: } else {
052: shell = new Shell((Shell) parent.getNativeWidget(),
053: SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
054: }
055: shell.setLayout(new FillLayout());
056: isClosing = false;
057: dlLock = false;
058: shell.addDisposeListener(this );
059: }
060:
061: /**
062: * @see fr.aliacom.form.common.IForm#addPropertyChangeListener(String, PropertyChangeListener)
063: */
064: public void addPropertyChangeListener(String string,
065: PropertyChangeListener pcl) {
066: pcs.addPropertyChangeListener(string, pcl);
067: }
068:
069: /**
070: * @see fr.aliacom.form.common.IForm#save()
071: */
072: public void save() throws ApplyChangeException {
073: if (save != null) {
074: save.run();
075: }
076: }
077:
078: /**
079: * @see fr.aliacom.form.common.IForm#close()
080: */
081: public void close() {
082: log.debug("SWTForm::close()");
083: PropertyChangeListener[] lters = pcs
084: .getPropertyChangeListeners();
085: for (int i = lters.length - 1; i >= 0; i--) {
086: pcs.removePropertyChangeListener(lters[i]);
087: }
088: dlLock = true;
089: FormManager.getInstance().dispose(this );
090: }
091:
092: /**
093: * @see fr.aliacom.form.common.IForm#getFormContext()
094: */
095: public FormContext getFormContext() {
096: return ctx;
097: }
098:
099: /**
100: * @see fr.aliacom.form.common.IForm#setManager(FormManager)
101: */
102: public void setManager(FormManager formManager) {
103: }
104:
105: /**
106: * @see fr.aliacom.form.common.IForm#setFrame(IFrame)
107: */
108: public void setFrame(IFrame jif) {
109: this .frame = jif;
110: }
111:
112: /**
113: * @see fr.aliacom.form.common.IForm#getFrame()
114: */
115: public IFrame getFrame() {
116: return frame;
117: }
118:
119: /**
120: * @see fr.aliacom.form.common.IForm#getLoader()
121: */
122: public FormLoader getLoader() {
123: return loader;
124: }
125:
126: public void setLoader(FormLoader loader) {
127: this .loader = loader;
128: }
129:
130: /**
131: * @see fr.aliacom.form.common.IForm#load()
132: */
133: public void load() throws LoadException {
134: if (load != null) {
135: load.run();
136: }
137: }
138:
139: /**
140: * @see fr.aliacom.form.common.IFormComponent#reset()
141: */
142: public void reset() {
143: }
144:
145: /**
146: * @see fr.aliacom.form.common.IFormComponent#setValueBean(Object)
147: */
148: public void setValueBean(Object bean) {
149: }
150:
151: /**
152: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
153: */
154: public Object getNativeWidget() {
155: return shell;
156: }
157:
158: /**
159: * @see fr.aliacom.form.common.IForm#setContext(FormContext)
160: */
161: public void setContext(FormContext ctx) {
162: this .ctx = ctx;
163: }
164:
165: /**
166: * @see fr.aliacom.form.common.IForm#setLoadCommand(fr.aliacom.commands.Command)
167: */
168: public void setLoadCommand(Command c) {
169: load = c;
170: }
171:
172: /**
173: * @see fr.aliacom.form.common.IForm#setSaveCommand(fr.aliacom.commands.Command)
174: */
175: public void setSaveCommand(Command c) {
176: save = c;
177: }
178:
179: /**
180: * @see fr.aliacom.form.common.IForm#setIcon(fr.aliacom.common.ui.IIcon)
181: */
182: public void setIcon(IIcon icon) {
183: shell.setImage((Image) icon.getNativeWidget());
184: }
185:
186: /**
187: * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
188: */
189: public void widgetDisposed(DisposeEvent arg0) {
190: log.debug("Shell disposed. Closing form.");
191: if (!dlLock) {
192: isClosing = true;
193: close();
194: }
195: }
196:
197: public boolean isClosing() {
198: return isClosing;
199: }
200:
201: }
|