001: package gnu.kawa.swtviews;
002:
003: import gnu.kawa.models.*;
004: import org.eclipse.swt.widgets.*;
005: import org.eclipse.swt.layout.*;
006: import org.eclipse.swt.SWT;
007: import org.eclipse.swt.events.SelectionEvent;
008:
009: public class SwtDisplay extends gnu.kawa.models.Display {
010: org.eclipse.swt.widgets.Display swtDisplay;
011:
012: static SwtDisplay instance;
013:
014: public SwtDisplay(org.eclipse.swt.widgets.Display swtDisplay) {
015: this .swtDisplay = swtDisplay;
016: }
017:
018: public static synchronized gnu.kawa.models.Display getInstance() {
019: if (instance == null) {
020: instance = new SwtDisplay(
021: new org.eclipse.swt.widgets.Display());
022: }
023: return instance;
024: }
025:
026: public Window makeWindow() {
027: SwtWindow window = new SwtWindow(this );
028: window.display = this ;
029: return window;
030: }
031:
032: public void addBox(Box model, Object where) {
033: new SwtBox(model, (Composite) where, this );
034: }
035:
036: public void addButton(gnu.kawa.models.Button model, Object where) {
037: new SwtButton(model, (Composite) where, this );
038: }
039:
040: public void addLabel(gnu.kawa.models.Label model, Object where) {
041: //new SwtLabel(model, (Composite) where, this);
042: org.eclipse.swt.widgets.Label swtLabel = new org.eclipse.swt.widgets.Label(
043: (Composite) where, 0);
044:
045: String text = model.getText();
046: if (text != null)
047: swtLabel.setText(text);
048:
049: LabelListener listener = new LabelListener(swtLabel, model);
050:
051: model.addListener(listener);
052: swtLabel.pack();
053: }
054:
055: public void addImage(DrawImage model, Object where) {
056: throw new Error("SwtDisplay.addImage not implemented!");
057: }
058:
059: public void addView(Object view, Object where) {
060: if (!((Control) view).setParent((Composite) where))
061: throw new Error("SwtDisplay.addView not implemented!");
062: }
063: }
064:
065: class SwtButton implements // ModelListener,
066: org.eclipse.swt.events.SelectionListener {
067: org.eclipse.swt.widgets.Button swtButton;
068:
069: gnu.kawa.models.Button model;
070:
071: public SwtButton(gnu.kawa.models.Button model, Composite parent,
072: SwtDisplay display) {
073: this .model = model;
074:
075: swtButton = new org.eclipse.swt.widgets.Button(parent, SWT.PUSH);
076: swtButton.setText(model.getText());
077: swtButton.addSelectionListener(this );
078: swtButton.pack();
079: }
080:
081: public void widgetSelected(SelectionEvent e) {
082: try {
083: ((gnu.mapping.Procedure) model.getAction()).apply1(e);
084: } catch (Throwable ex) {
085: throw new gnu.mapping.WrappedException(ex);
086: }
087: }
088:
089: public void widgetDefaultSelected(SelectionEvent e) {
090: System.err.println("(widgetDefaultSelected invoked)");
091: }
092: }
093:
094: /** A listener to update an SWT Label when the model has changed. */
095:
096: class LabelListener extends WeakListener {
097: public LabelListener(org.eclipse.swt.widgets.Label swtLabel,
098: gnu.kawa.models.Label model) {
099: super (swtLabel);
100: }
101:
102: public void update(Object view, Model model, Object key) {
103: org.eclipse.swt.widgets.Label swtLabel = (org.eclipse.swt.widgets.Label) view;
104: gnu.kawa.models.Label label = (gnu.kawa.models.Label) model;
105: if (key == "text")
106: swtLabel.setText(label.getText());
107: swtLabel.pack(true);
108: }
109: }
110:
111: class SwtBox extends Composite {
112: Box model;
113:
114: public SwtBox(Box model, Composite parent,
115: gnu.kawa.models.Display display) {
116: super (parent, 0);
117: this .model = model;
118:
119: Viewable cellSpacing = model.getCellSpacing();
120: int n = model.getComponentCount();
121: for (int i = 0; i < n; i++) {
122: if (i > 0 && cellSpacing != null)
123: cellSpacing.makeView(display, this );
124: model.getComponent(i).makeView(display, this );
125: }
126:
127: GridLayout layout = new GridLayout();
128: if (model.getAxis() == 0) { // Row
129: layout.numColumns = n;
130: } else { // Column
131: layout.numColumns = 1;
132: }
133: layout.horizontalSpacing = layout.verticalSpacing = 0;
134: layout.marginWidth = layout.marginHeight = 0;
135: setLayout(layout);
136: pack();
137: }
138: }
139:
140: class SwtWindow implements gnu.kawa.models.Window {
141: org.eclipse.swt.widgets.Shell shell;
142:
143: SwtDisplay display;
144:
145: public gnu.kawa.models.Display getDisplay() {
146: return display;
147: }
148:
149: public SwtWindow(SwtDisplay display) {
150: this .display = display;
151: shell = new Shell(display.swtDisplay, SWT.CLOSE | SWT.BORDER
152: | SWT.RESIZE);
153: }
154:
155: public String getTitle() {
156: return "(unknown title)"; // FIXME
157: }
158:
159: public void setTitle(String title) {
160: // FIXME
161: }
162:
163: public void setMenuBar(Object menubar) {
164: // FIXME
165: }
166:
167: public void setContent(Object content) {
168: if (content instanceof Viewable)
169: ((Viewable) content).makeView(getDisplay(), shell);
170: else
171: new Error("SwtDisplay.setContent:" + content)
172: .printStackTrace();
173: }
174:
175: public void open() {
176: shell.open();
177: while (!shell.isDisposed()) {
178: if (!display.swtDisplay.readAndDispatch())
179: display.swtDisplay.sleep();
180: }
181: display.swtDisplay.dispose();
182: }
183:
184: public void finalize() {
185: if (shell != null)
186: shell.dispose();
187: }
188: }
|