1 13 package org.eclipse.jface.databinding.swt; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.databinding.observable.Realm; 19 import org.eclipse.core.databinding.observable.list.IObservableList; 20 import org.eclipse.jface.internal.databinding.internal.swt.ButtonObservableValue; 21 import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableList; 22 import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableValue; 23 import org.eclipse.jface.internal.databinding.internal.swt.CComboSingleSelectionObservableValue; 24 import org.eclipse.jface.internal.databinding.internal.swt.CLabelObservableValue; 25 import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableList; 26 import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableValue; 27 import org.eclipse.jface.internal.databinding.internal.swt.ComboSingleSelectionObservableValue; 28 import org.eclipse.jface.internal.databinding.internal.swt.ControlObservableValue; 29 import org.eclipse.jface.internal.databinding.internal.swt.LabelObservableValue; 30 import org.eclipse.jface.internal.databinding.internal.swt.ListObservableList; 31 import org.eclipse.jface.internal.databinding.internal.swt.ListObservableValue; 32 import org.eclipse.jface.internal.databinding.internal.swt.ListSingleSelectionObservableValue; 33 import org.eclipse.jface.internal.databinding.internal.swt.SWTProperties; 34 import org.eclipse.jface.internal.databinding.internal.swt.ScaleObservableValue; 35 import org.eclipse.jface.internal.databinding.internal.swt.SpinnerObservableValue; 36 import org.eclipse.jface.internal.databinding.internal.swt.TableSingleSelectionObservableValue; 37 import org.eclipse.jface.internal.databinding.internal.swt.TextEditableObservableValue; 38 import org.eclipse.jface.internal.databinding.internal.swt.TextObservableValue; 39 import org.eclipse.swt.custom.CCombo; 40 import org.eclipse.swt.custom.CLabel; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Combo; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Display; 45 import org.eclipse.swt.widgets.Label; 46 import org.eclipse.swt.widgets.List; 47 import org.eclipse.swt.widgets.Scale; 48 import org.eclipse.swt.widgets.Spinner; 49 import org.eclipse.swt.widgets.Table; 50 import org.eclipse.swt.widgets.Text; 51 52 58 public class SWTObservables { 59 60 private static java.util.List realms = new ArrayList (); 61 62 68 public static Realm getRealm(final Display display) { 69 synchronized (realms) { 70 for (Iterator it = realms.iterator(); it.hasNext();) { 71 DisplayRealm displayRealm = (DisplayRealm) it.next(); 72 if (displayRealm.display == display) { 73 return displayRealm; 74 } 75 } 76 DisplayRealm result = new DisplayRealm(display); 77 realms.add(result); 78 return result; 79 } 80 } 81 82 87 public static ISWTObservableValue observeEnabled(Control control) { 88 return new ControlObservableValue(control, SWTProperties.ENABLED); 89 } 90 91 96 public static ISWTObservableValue observeVisible(Control control) { 97 return new ControlObservableValue(control, SWTProperties.VISIBLE); 98 } 99 100 105 public static ISWTObservableValue observeTooltipText(Control control) { 106 return new ControlObservableValue(control, SWTProperties.TOOLTIP_TEXT); 107 } 108 109 126 public static ISWTObservableValue observeSelection(Control control) { 127 if (control instanceof Spinner) { 128 return new SpinnerObservableValue((Spinner) control, 129 SWTProperties.SELECTION); 130 } else if (control instanceof Button) { 131 return new ButtonObservableValue((Button) control); 132 } else if (control instanceof Combo) { 133 return new ComboObservableValue((Combo) control, 134 SWTProperties.SELECTION); 135 } else if (control instanceof CCombo) { 136 return new CComboObservableValue((CCombo) control, 137 SWTProperties.SELECTION); 138 } else if (control instanceof List ) { 139 return new ListObservableValue((List ) control); 140 } else if (control instanceof Scale) { 141 return new ScaleObservableValue((Scale) control, 142 SWTProperties.SELECTION); 143 } 144 145 throw new IllegalArgumentException ( 146 "Widget [" + control.getClass().getName() + "] is not supported."); } 148 149 162 public static ISWTObservableValue observeMin(Control control) { 163 if (control instanceof Spinner) { 164 return new SpinnerObservableValue((Spinner) control, 165 SWTProperties.MIN); 166 } else if (control instanceof Scale) { 167 return new ScaleObservableValue((Scale) control, SWTProperties.MIN); 168 } 169 170 throw new IllegalArgumentException ( 171 "Widget [" + control.getClass().getName() + "] is not supported."); } 173 174 187 public static ISWTObservableValue observeMax(Control control) { 188 if (control instanceof Spinner) { 189 return new SpinnerObservableValue((Spinner) control, 190 SWTProperties.MAX); 191 } else if (control instanceof Scale) { 192 return new ScaleObservableValue((Scale) control, SWTProperties.MAX); 193 } 194 195 throw new IllegalArgumentException ( 196 "Widget [" + control.getClass().getName() + "] is not supported."); } 198 199 212 public static ISWTObservableValue observeText(Control control, int event) { 213 if (control instanceof Text) { 214 return new TextObservableValue((Text) control, event); 215 } 216 217 throw new IllegalArgumentException ( 218 "Widget [" + control.getClass().getName() + "] is not supported."); } 220 221 236 public static ISWTObservableValue observeText(Control control) { 237 if (control instanceof Label) { 238 return new LabelObservableValue((Label) control); 239 } else if (control instanceof CLabel) { 240 return new CLabelObservableValue((CLabel) control); 241 } else if (control instanceof Combo) { 242 return new ComboObservableValue((Combo) control, SWTProperties.TEXT); 243 } else if (control instanceof CCombo) { 244 return new CComboObservableValue((CCombo) control, 245 SWTProperties.TEXT); 246 } 247 248 throw new IllegalArgumentException ( 249 "Widget [" + control.getClass().getName() + "] is not supported."); } 251 252 266 public static IObservableList observeItems(Control control) { 267 if (control instanceof Combo) { 268 return new ComboObservableList((Combo) control); 269 } else if (control instanceof CCombo) { 270 return new CComboObservableList((CCombo) control); 271 } else if (control instanceof List ) { 272 return new ListObservableList((List ) control); 273 } 274 275 throw new IllegalArgumentException ( 276 "Widget [" + control.getClass().getName() + "] is not supported."); } 278 279 294 public static ISWTObservableValue observeSingleSelectionIndex( 295 Control control) { 296 if (control instanceof Table) { 297 return new TableSingleSelectionObservableValue((Table) control); 298 } else if (control instanceof Combo) { 299 return new ComboSingleSelectionObservableValue((Combo) control); 300 } else if (control instanceof CCombo) { 301 return new CComboSingleSelectionObservableValue((CCombo) control); 302 } else if (control instanceof List ) { 303 return new ListSingleSelectionObservableValue((List ) control); 304 } 305 306 throw new IllegalArgumentException ( 307 "Widget [" + control.getClass().getName() + "] is not supported."); } 309 310 315 public static ISWTObservableValue observeForeground(Control control) { 316 return new ControlObservableValue(control, SWTProperties.FOREGROUND); 317 } 318 319 324 public static ISWTObservableValue observeBackground(Control control) { 325 return new ControlObservableValue(control, SWTProperties.BACKGROUND); 326 } 327 328 332 public static ISWTObservableValue observeFont(Control control) { 333 return new ControlObservableValue(control, SWTProperties.FONT); 334 } 335 336 348 public static ISWTObservableValue observeEditable(Control control) { 349 if (control instanceof Text) { 350 return new TextEditableObservableValue((Text) control); 351 } 352 353 throw new IllegalArgumentException ( 354 "Widget [" + control.getClass().getName() + "] is not supported."); } 356 357 private static class DisplayRealm extends Realm { 358 private Display display; 359 360 363 private DisplayRealm(Display display) { 364 this.display = display; 365 } 366 367 public boolean isCurrent() { 368 return Display.getCurrent() == display; 369 } 370 371 public void asyncExec(final Runnable runnable) { 372 Runnable safeRunnable = new Runnable () { 373 public void run() { 374 safeRun(runnable); 375 } 376 }; 377 if (!display.isDisposed()) { 378 display.asyncExec(safeRunnable); 379 } 380 } 381 382 387 public int hashCode() { 388 return (display == null) ? 0 : display.hashCode(); 389 } 390 391 396 public boolean equals(Object obj) { 397 if (this == obj) 398 return true; 399 if (obj == null) 400 return false; 401 if (getClass() != obj.getClass()) 402 return false; 403 final DisplayRealm other = (DisplayRealm) obj; 404 if (display == null) { 405 if (other.display != null) 406 return false; 407 } else if (!display.equals(other.display)) 408 return false; 409 return true; 410 } 411 } 412 } 413 | Popular Tags |