001: package fr.aliacom.form.swt;
002:
003: import java.io.InputStream;
004:
005: import org.eclipse.swt.SWT;
006: import org.eclipse.swt.custom.CLabel;
007: import org.eclipse.swt.custom.StyledText;
008: import org.eclipse.swt.dnd.Clipboard;
009: import org.eclipse.swt.dnd.TextTransfer;
010: import org.eclipse.swt.dnd.Transfer;
011: import org.eclipse.swt.events.SelectionAdapter;
012: import org.eclipse.swt.events.SelectionEvent;
013: import org.eclipse.swt.graphics.Device;
014: import org.eclipse.swt.graphics.FontData;
015: import org.eclipse.swt.graphics.GC;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.layout.RowLayout;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.DirectoryDialog;
023: import org.eclipse.swt.widgets.Display;
024: import org.eclipse.swt.widgets.FileDialog;
025: import org.eclipse.swt.widgets.FontDialog;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Shell;
028:
029: import fr.aliacom.common.ui.IColor;
030: import fr.aliacom.common.ui.IIcon;
031: import fr.aliacom.common.ui.IInternalFrame;
032: import fr.aliacom.common.ui.ISplashScreen;
033: import fr.aliacom.common.ui.ITopLevelFrame;
034: import fr.aliacom.common.ui.IconFactory;
035: import fr.aliacom.form.common.IApplication;
036: import fr.aliacom.form.common.IForm;
037: import fr.aliacom.form.common.IFormComponent;
038: import fr.aliacom.form.common.IFormParser;
039: import fr.aliacom.form.common.Toolkit;
040: import fr.aliacom.form.storage.IFormRepository;
041: import fr.aliacom.form.storage.IIconRepository;
042: import fr.aliacom.form.storage.IScriptRepository;
043: import fr.aliacom.form.swt.ui.SWTColor;
044: import fr.aliacom.form.swt.ui.SWTFrame;
045: import fr.aliacom.form.swt.ui.SWTIcon;
046: import fr.aliacom.form.swt.utils.SWTUtils;
047:
048: /**
049: * @author tom
050: *
051: * (C) 2001, 2002 Thomas Cataldo
052: */
053: public final class SWTToolkit extends Toolkit {
054:
055: private Display display;
056: private boolean stopped;
057: private IFormRepository formsStore;
058: private IIconRepository iconsStore;
059: private IScriptRepository scriptsStore;
060: private IForm mainWindow;
061: private IApplication app;
062:
063: /**
064: * @see fr.aliacom.form.common.Toolkit#init()
065: */
066: public void init() {
067: display = new Display();
068: }
069:
070: /**
071: * TODO: Move this code in its own class
072: * @see fr.aliacom.form.common.Toolkit#handleError(IFormComponent, String)
073: */
074: public void handleError(IFormComponent parent, String s) {
075: Shell sh = (Shell) parent.getNativeWidget();
076: final Shell dialog = new Shell(sh, SWT.DIALOG_TRIM
077: | SWT.APPLICATION_MODAL);
078: dialog.setText("Error message");
079: GridLayout layout = new GridLayout();
080: layout.makeColumnsEqualWidth = false;
081: layout.numColumns = 2;
082: dialog.setLayout(layout);
083: GridData data;
084:
085: /* Create the error image */
086: Image error = (Image) IconFactory.get("Error")
087: .getNativeWidget();
088: CLabel label = new CLabel(dialog, SWT.NULL);
089: data = new GridData(GridData.VERTICAL_ALIGN_FILL);
090: label.setImage(error);
091: SWTUtils.addDisposeHandler(label, error);
092: label.setLayoutData(data);
093:
094: /* Create the message */
095: final Clipboard cb = new Clipboard(display);
096: final StyledText st = new StyledText(dialog, SWT.MULTI);
097: st.setText(s);
098: st.setEditable(false);
099: st.setEnabled(false);
100: st.setSize(st.computeSize(300, st.getLineCount()
101: * st.getLineHeight()));
102: st.setBackground(dialog.getBackground());
103: data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
104: | GridData.VERTICAL_ALIGN_FILL);
105: st.setLayoutData(data);
106:
107: /* Add a separator */
108: Label sep = new Label(dialog, SWT.SEPARATOR | SWT.HORIZONTAL);
109: data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
110: data.horizontalSpan = 2;
111: sep.setLayoutData(data);
112:
113: /* Create the buttons */
114: Composite buttons = new Composite(dialog, SWT.NO_FOCUS);
115: RowLayout rl = new RowLayout();
116: rl.marginLeft = 0;
117: rl.marginRight = 0;
118: buttons.setLayout(rl);
119: rl.marginTop = 0;
120: rl.marginBottom = 0;
121: data = new GridData(GridData.HORIZONTAL_ALIGN_END);
122: data.horizontalSpan = 2;
123: buttons.setLayoutData(data);
124:
125: Button button = new Button(buttons, SWT.PUSH);
126: button.setText("Copy to clipboard");
127: button.addSelectionListener(new SelectionAdapter() {
128: public void widgetSelected(SelectionEvent se) {
129: String textData = st.getText();
130: TextTransfer textTransfer = TextTransfer.getInstance();
131: cb.setContents(new Object[] { textData },
132: new Transfer[] { textTransfer });
133: }
134: });
135:
136: button = new Button(buttons, SWT.PUSH);
137: button.setText("Close");
138: button.addSelectionListener(new SelectionAdapter() {
139: public void widgetSelected(SelectionEvent se) {
140: dialog.close();
141: }
142: });
143: dialog.setDefaultButton(button);
144:
145: dialog.pack();
146: dialog.open();
147: while (!dialog.isDisposed()) {
148: if (!display.readAndDispatch())
149: display.sleep();
150: }
151: cb.dispose();
152: }
153:
154: /**
155: * @see fr.aliacom.form.common.Toolkit#getNewParser()
156: */
157: public IFormParser getNewParser() {
158: return new SWTFormParser();
159: }
160:
161: /**
162: * @see fr.aliacom.form.common.Toolkit#newInternalFrame(IForm)
163: */
164: public IInternalFrame newInternalFrame(IForm f) {
165: return new SWTFrame();
166: }
167:
168: /**
169: * @see fr.aliacom.form.common.Toolkit#newTopLevelFrame(IForm)
170: */
171: public ITopLevelFrame newTopLevelFrame(IForm f) {
172: return new SWTFrame();
173: }
174:
175: /**
176: * @see fr.aliacom.form.common.Toolkit#confirm(IFormComponent, String)
177: */
178: public boolean confirm(IFormComponent form, String confirmMessage) {
179: return false;
180: }
181:
182: /**
183: * The swt implementation cannot cache its icons as
184: * the swing one.
185: * The icon must be disposed the IFormComponent
186: * using it.
187: *
188: * @see fr.aliacom.form.common.Toolkit#getIcon(String)
189: */
190: public IIcon getIcon(String iconName) {
191: return new SWTIcon(iconName, this );
192: }
193:
194: /**
195: * @see fr.aliacom.form.common.Toolkit#asyncExec(Runnable)
196: */
197: public void asyncExec(Runnable asyncCommandWorker) {
198: //BusyIndicator.showWhile(display, asyncCommandWorker);
199: display.asyncExec(asyncCommandWorker);
200: }
201:
202: /**
203: * @see fr.aliacom.form.common.Toolkit#getDisplay()
204: */
205: public Object getDisplay() {
206: return display;
207: }
208:
209: /**
210: * @see fr.aliacom.form.common.Toolkit#newSplashScreen(String, int)
211: */
212: public ISplashScreen newSplashScreen(String iconName, int i) {
213: return null;
214: }
215:
216: /**
217: * @see fr.aliacom.form.common.Toolkit#syncExec(Runnable)
218: */
219: public void syncExec(Runnable syncCommandWorker) {
220: syncCommandWorker.run();
221: }
222:
223: /**
224: * @see fr.aliacom.form.common.Toolkit#start(IApplication)
225: */
226: public void start(IApplication app) {
227: this .app = app;
228: stopped = false;
229: iconsStore = app.getIconRepository();
230: formsStore = app.getFormRepository();
231: scriptsStore = app.getScriptRepository();
232: ISplashScreen splash = newSplashScreen(app.getSplashIconName(),
233: app.getSplashSteps());
234: asyncExec(new SWTAppRunner(app, splash));
235: while (true) {
236: if (!display.readAndDispatch()) {
237: display.sleep();
238: }
239: }
240: }
241:
242: /**
243: * @see fr.aliacom.form.common.Toolkit#stop()
244: */
245: public void stop() {
246: app.stop();
247: System.exit(0);
248: }
249:
250: /**
251: * @see fr.aliacom.form.common.Toolkit#newColor(int, int, int)
252: */
253: public IColor newColor(int r, int g, int b) {
254: return new SWTColor((Device) getDisplay(), r, g, b);
255: }
256:
257: /**
258: * @see fr.aliacom.form.common.Toolkit#getMainWindow()
259: */
260: public IForm getMainWindow() {
261: return mainWindow;
262: }
263:
264: /**
265: * @see fr.aliacom.form.common.Toolkit#setMainWindow(fr.aliacom.form.common.IForm)
266: */
267: public void setMainWindow(IForm mainWindow) {
268: this .mainWindow = mainWindow;
269: }
270:
271: /**
272: * @see fr.aliacom.form.common.Toolkit#showDirectoryDialog()
273: */
274: public String showDirectoryDialog() {
275: DirectoryDialog d = new DirectoryDialog((Shell) mainWindow
276: .getNativeWidget());
277: d.setText("Choose a directory...");
278: return d.open();
279: }
280:
281: /**
282: * @see fr.aliacom.form.common.Toolkit#showFileDialog(int)
283: */
284: public String showFileDialog(int mode) {
285: FileDialog d = new FileDialog((Shell) mainWindow
286: .getNativeWidget(),
287: (mode == OPEN ? SWT.OPEN : SWT.SAVE));
288: d.setText("Choose a file...");
289: return d.open();
290: }
291:
292: /* (non-Javadoc)
293: * @see fr.aliacom.form.common.Toolkit#showFontDialog()
294: */
295: public String showFontDialog() {
296: FontDialog d = new FontDialog((Shell) mainWindow
297: .getNativeWidget());
298: FontData fd = d.open();
299: return (fd == null ? null : fd.getName());
300: }
301:
302: /**
303: * When an icon is not available, this icon is used instead.
304: */
305: public IIcon getBrokenIcon() {
306: final Image img = new Image(display, 16, 16);
307: GC gc = new GC(img);
308: gc.drawRectangle(0, 0, 16, 16);
309: gc.dispose();
310: return new SWTIcon(img);
311: }
312:
313: public InputStream loadIcon(String iconName) {
314: return iconsStore.loadIcon(iconName);
315: }
316:
317: public InputStream loadForm(String formName) {
318: return formsStore.loadForm(formName);
319: }
320:
321: public InputStream loadScript(String scriptName) {
322: return scriptsStore.loadScript(scriptName);
323: }
324:
325: }
|