1 package gnu.kawa.swtviews; 2 import gnu.kawa.models.*; 3 import org.eclipse.swt.widgets.*; 4 import org.eclipse.swt.layout.*; 5 import org.eclipse.swt.SWT; 6 import org.eclipse.swt.events.SelectionEvent; 7 8 public class SwtDisplay extends gnu.kawa.models.Display 9 { 10 org.eclipse.swt.widgets.Display swtDisplay; 11 12 static SwtDisplay instance; 13 14 public SwtDisplay (org.eclipse.swt.widgets.Display swtDisplay) 15 { 16 this.swtDisplay = swtDisplay; 17 } 18 19 public static synchronized gnu.kawa.models.Display getInstance() 20 { 21 if (instance == null) 22 { 23 instance = new SwtDisplay(new org.eclipse.swt.widgets.Display()); 24 } 25 return instance; 26 } 27 28 public Window makeWindow () 29 { 30 SwtWindow window = new SwtWindow(this); 31 window.display = this; 32 return window; 33 } 34 35 public void addBox (Box model, Object where) 36 { 37 new SwtBox(model, (Composite) where, this); 38 } 39 40 public void addButton (gnu.kawa.models.Button model, Object where) 41 { 42 new SwtButton(model, (Composite) where, this); 43 } 44 45 public void addLabel (gnu.kawa.models.Label model, Object where) 46 { 47 org.eclipse.swt.widgets.Label swtLabel 49 = new org.eclipse.swt.widgets.Label((Composite) where, 0); 50 51 String text = model.getText(); 52 if (text != null) 53 swtLabel.setText(text); 54 55 LabelListener listener = new LabelListener(swtLabel, model); 56 57 model.addListener(listener); 58 swtLabel.pack(); 59 } 60 61 public void addImage (DrawImage model, Object where) 62 { 63 throw new Error ("SwtDisplay.addImage not implemented!"); 64 } 65 66 public void addView (Object view, Object where) 67 { 68 if (! ((Control) view).setParent((Composite) where)) 69 throw new Error ("SwtDisplay.addView not implemented!"); 70 } 71 } 72 73 class SwtButton 74 implements org.eclipse.swt.events.SelectionListener 76 { 77 org.eclipse.swt.widgets.Button swtButton; 78 79 gnu.kawa.models.Button model; 80 81 public SwtButton (gnu.kawa.models.Button model, 82 Composite parent, SwtDisplay display) 83 { 84 this.model = model; 85 86 swtButton = new org.eclipse.swt.widgets.Button(parent, SWT.PUSH); 87 swtButton.setText(model.getText()); 88 swtButton.addSelectionListener(this); 89 swtButton.pack(); 90 } 91 92 public void widgetSelected(SelectionEvent e) 93 { 94 try 95 { 96 ((gnu.mapping.Procedure) model.getAction()).apply1(e); 97 } 98 catch (Throwable ex) 99 { 100 throw new gnu.mapping.WrappedException(ex); 101 } 102 } 103 104 public void widgetDefaultSelected (SelectionEvent e) 105 { 106 System.err.println("(widgetDefaultSelected invoked)"); 107 } 108 } 109 110 111 112 class LabelListener extends WeakListener 113 { 114 public LabelListener(org.eclipse.swt.widgets.Label swtLabel, 115 gnu.kawa.models.Label model) 116 { 117 super(swtLabel); 118 } 119 120 public void update (Object view, Model model, Object key) 121 { 122 org.eclipse.swt.widgets.Label swtLabel 123 = (org.eclipse.swt.widgets.Label) view; 124 gnu.kawa.models.Label label = (gnu.kawa.models.Label) model; 125 if (key == "text") 126 swtLabel.setText(label.getText()); 127 swtLabel.pack(true); 128 } 129 } 130 131 class SwtBox extends Composite 132 { 133 Box model; 134 135 public SwtBox (Box model, Composite parent, gnu.kawa.models.Display display) 136 { 137 super(parent, 0); 138 this.model = model; 139 140 Viewable cellSpacing = model.getCellSpacing(); 141 int n = model.getComponentCount(); 142 for (int i = 0; i < n; i++) 143 { 144 if (i > 0 && cellSpacing != null) 145 cellSpacing.makeView(display, this); 146 model.getComponent(i).makeView(display, this); 147 } 148 149 GridLayout layout = new GridLayout(); 150 if (model.getAxis() == 0) 151 { layout.numColumns = n; 153 } 154 else 155 { layout.numColumns = 1; 157 } 158 layout.horizontalSpacing = layout.verticalSpacing = 0; 159 layout.marginWidth = layout.marginHeight = 0; 160 setLayout(layout); 161 pack(); 162 } 163 } 164 165 class SwtWindow implements gnu.kawa.models.Window 166 { 167 org.eclipse.swt.widgets.Shell shell; 168 169 SwtDisplay display; 170 171 public gnu.kawa.models.Display getDisplay () { return display; } 172 173 public SwtWindow (SwtDisplay display) 174 { 175 this.display = display; 176 shell = new Shell(display.swtDisplay, SWT.CLOSE|SWT.BORDER|SWT.RESIZE); 177 } 178 179 public String getTitle () 180 { 181 return "(unknown title)"; } 183 184 public void setTitle (String title) 185 { 186 } 188 189 public void setMenuBar (Object menubar) 190 { 191 } 193 194 public void setContent (Object content) 195 { 196 if (content instanceof Viewable) 197 ((Viewable) content).makeView(getDisplay(), shell); 198 else 199 new Error ("SwtDisplay.setContent:"+ content).printStackTrace(); 200 } 201 202 public void open () 203 { 204 shell.open(); 205 while (!shell.isDisposed ()) 206 { 207 if (!display.swtDisplay.readAndDispatch()) 208 display.swtDisplay.sleep(); 209 } 210 display.swtDisplay.dispose(); 211 } 212 213 public void finalize () 214 { 215 if (shell != null) 216 shell.dispose(); 217 } 218 } | Popular Tags |