|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 Matthew Hall and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Matthew Hall - initial API and implementation (bug 194734) |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.jface.databinding.swt; |
| 13 |
|
| 14 |
import org.eclipse.core.databinding.property.list.DelegatingListProperty; |
| 15 |
import org.eclipse.core.databinding.property.list.IListProperty; |
| 16 |
import org.eclipse.core.databinding.property.value.DelegatingValueProperty; |
| 17 |
import org.eclipse.core.databinding.property.value.IValueProperty; |
| 18 |
import org.eclipse.jface.internal.databinding.swt.ButtonSelectionProperty; |
| 19 |
import org.eclipse.jface.internal.databinding.swt.CComboItemsProperty; |
| 20 |
import org.eclipse.jface.internal.databinding.swt.CComboSelectionProperty; |
| 21 |
import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionIndexProperty; |
| 22 |
import org.eclipse.jface.internal.databinding.swt.CComboTextProperty; |
| 23 |
import org.eclipse.jface.internal.databinding.swt.CLabelTextProperty; |
| 24 |
import org.eclipse.jface.internal.databinding.swt.CTabItemTooltipTextProperty; |
| 25 |
import org.eclipse.jface.internal.databinding.swt.ComboItemsProperty; |
| 26 |
import org.eclipse.jface.internal.databinding.swt.ComboSelectionProperty; |
| 27 |
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionIndexProperty; |
| 28 |
import org.eclipse.jface.internal.databinding.swt.ComboTextProperty; |
| 29 |
import org.eclipse.jface.internal.databinding.swt.ControlBackgroundProperty; |
| 30 |
import org.eclipse.jface.internal.databinding.swt.ControlBoundsProperty; |
| 31 |
import org.eclipse.jface.internal.databinding.swt.ControlEnabledProperty; |
| 32 |
import org.eclipse.jface.internal.databinding.swt.ControlFocusedProperty; |
| 33 |
import org.eclipse.jface.internal.databinding.swt.ControlFontProperty; |
| 34 |
import org.eclipse.jface.internal.databinding.swt.ControlForegroundProperty; |
| 35 |
import org.eclipse.jface.internal.databinding.swt.ControlLocationProperty; |
| 36 |
import org.eclipse.jface.internal.databinding.swt.ControlSizeProperty; |
| 37 |
import org.eclipse.jface.internal.databinding.swt.ControlTooltipTextProperty; |
| 38 |
import org.eclipse.jface.internal.databinding.swt.ControlVisibleProperty; |
| 39 |
import org.eclipse.jface.internal.databinding.swt.ItemTextProperty; |
| 40 |
import org.eclipse.jface.internal.databinding.swt.LabelTextProperty; |
| 41 |
import org.eclipse.jface.internal.databinding.swt.LinkTextProperty; |
| 42 |
import org.eclipse.jface.internal.databinding.swt.ListItemsProperty; |
| 43 |
import org.eclipse.jface.internal.databinding.swt.ListSelectionProperty; |
| 44 |
import org.eclipse.jface.internal.databinding.swt.ListSingleSelectionIndexProperty; |
| 45 |
import org.eclipse.jface.internal.databinding.swt.ScaleMaximumProperty; |
| 46 |
import org.eclipse.jface.internal.databinding.swt.ScaleMinimumProperty; |
| 47 |
import org.eclipse.jface.internal.databinding.swt.ScaleSelectionProperty; |
| 48 |
import org.eclipse.jface.internal.databinding.swt.ShellTextProperty; |
| 49 |
import org.eclipse.jface.internal.databinding.swt.SpinnerMaximumProperty; |
| 50 |
import org.eclipse.jface.internal.databinding.swt.SpinnerMinimumProperty; |
| 51 |
import org.eclipse.jface.internal.databinding.swt.SpinnerSelectionProperty; |
| 52 |
import org.eclipse.jface.internal.databinding.swt.StyledTextTextProperty; |
| 53 |
import org.eclipse.jface.internal.databinding.swt.TabItemTooltipTextProperty; |
| 54 |
import org.eclipse.jface.internal.databinding.swt.TableColumnTooltipTextProperty; |
| 55 |
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionIndexProperty; |
| 56 |
import org.eclipse.jface.internal.databinding.swt.TextEditableProperty; |
| 57 |
import org.eclipse.jface.internal.databinding.swt.TextTextProperty; |
| 58 |
import org.eclipse.jface.internal.databinding.swt.ToolItemTooltipTextProperty; |
| 59 |
import org.eclipse.jface.internal.databinding.swt.TrayItemTooltipTextProperty; |
| 60 |
import org.eclipse.jface.internal.databinding.swt.TreeItemTooltipTextProperty; |
| 61 |
import org.eclipse.swt.SWT; |
| 62 |
import org.eclipse.swt.custom.CCombo; |
| 63 |
import org.eclipse.swt.custom.CLabel; |
| 64 |
import org.eclipse.swt.custom.CTabItem; |
| 65 |
import org.eclipse.swt.custom.StyledText; |
| 66 |
import org.eclipse.swt.widgets.Button; |
| 67 |
import org.eclipse.swt.widgets.Combo; |
| 68 |
import org.eclipse.swt.widgets.Control; |
| 69 |
import org.eclipse.swt.widgets.Item; |
| 70 |
import org.eclipse.swt.widgets.Label; |
| 71 |
import org.eclipse.swt.widgets.Link; |
| 72 |
import org.eclipse.swt.widgets.List; |
| 73 |
import org.eclipse.swt.widgets.Scale; |
| 74 |
import org.eclipse.swt.widgets.Shell; |
| 75 |
import org.eclipse.swt.widgets.Spinner; |
| 76 |
import org.eclipse.swt.widgets.TabItem; |
| 77 |
import org.eclipse.swt.widgets.Table; |
| 78 |
import org.eclipse.swt.widgets.TableColumn; |
| 79 |
import org.eclipse.swt.widgets.Text; |
| 80 |
import org.eclipse.swt.widgets.ToolItem; |
| 81 |
import org.eclipse.swt.widgets.TrayItem; |
| 82 |
import org.eclipse.swt.widgets.TreeColumn; |
| 83 |
import org.eclipse.swt.widgets.TreeItem; |
| 84 |
import org.eclipse.swt.widgets.Widget; |
| 85 |
|
| 86 |
/** |
| 87 |
* A factory for creating properties of SWT {@link Widget widgets}. |
| 88 |
* |
| 89 |
* @since 1.3 |
| 90 |
*/ |
| 91 |
public class WidgetProperties { |
| 92 |
private static RuntimeException notSupported(Object source) { |
| 93 |
return new IllegalArgumentException( |
| 94 |
"Widget [" + source.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns a value property for observing the background color of a |
| 99 |
* {@link Control}. |
| 100 |
* |
| 101 |
* @return a value property for observing the background color of a |
| 102 |
* {@link Control}. |
| 103 |
*/ |
| 104 |
public static IValueProperty background() { |
| 105 |
return new ControlBackgroundProperty(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns a value property for observing the bounds of a {@link Control}. |
| 110 |
* |
| 111 |
* @return a value property for observing the bounds of a {@link Control}. |
| 112 |
*/ |
| 113 |
public static IValueProperty bounds() { |
| 114 |
return new ControlBoundsProperty(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns a value property for observing the editable state of a |
| 119 |
* {@link Text}. |
| 120 |
* |
| 121 |
* @return a value property for observing the editable state of a |
| 122 |
* {@link Text}. |
| 123 |
*/ |
| 124 |
public static IValueProperty editable() { |
| 125 |
return new DelegatingValueProperty(Boolean.TYPE) { |
| 126 |
IValueProperty text = new TextEditableProperty(); |
| 127 |
|
| 128 |
protected IValueProperty doGetDelegate(Object source) { |
| 129 |
if (source instanceof Text) |
| 130 |
return text; |
| 131 |
throw notSupported(source); |
| 132 |
} |
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns a value property for observing the enablement state of a |
| 138 |
* {@link Control}. |
| 139 |
* |
| 140 |
* @return a value property for observing the enablement state of a |
| 141 |
* {@link Control}. |
| 142 |
*/ |
| 143 |
public static IValueProperty enabled() { |
| 144 |
return new ControlEnabledProperty(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns a value property for observing the focus state of a |
| 149 |
* {@link Control}. |
| 150 |
* |
| 151 |
* @return a value property for observing the focus state of a |
| 152 |
* {@link Control}. |
| 153 |
*/ |
| 154 |
public static IValueProperty focused() { |
| 155 |
return new ControlFocusedProperty(); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns a value property for observing the font of a {@link Control}. |
| 160 |
* |
| 161 |
* @return a value property for observing the font of a {@link Control}. |
| 162 |
*/ |
| 163 |
public static IValueProperty font() { |
| 164 |
return new ControlFontProperty(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns a value property for observing the foreground color of a |
| 169 |
* {@link Control}. |
| 170 |
* |
| 171 |
* @return a value property for observing the foreground color of a |
| 172 |
* {@link Control}. |
| 173 |
*/ |
| 174 |
public static IValueProperty foreground() { |
| 175 |
return new ControlForegroundProperty(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns a list property for observing the items of a {@link CCombo}, |
| 180 |
* {@link Combo} or {@link List}. |
| 181 |
* |
| 182 |
* @return a list property for observing the items of a {@link CCombo}, |
| 183 |
* {@link Combo} or {@link List}. |
| 184 |
*/ |
| 185 |
public static IListProperty items() { |
| 186 |
return new DelegatingListProperty(String.class) { |
| 187 |
private IListProperty cCombo = new CComboItemsProperty(); |
| 188 |
private IListProperty combo = new ComboItemsProperty(); |
| 189 |
private IListProperty list = new ListItemsProperty(); |
| 190 |
|
| 191 |
protected IListProperty doGetDelegate(Object source) { |
| 192 |
if (source instanceof CCombo) |
| 193 |
return cCombo; |
| 194 |
if (source instanceof Combo) |
| 195 |
return combo; |
| 196 |
if (source instanceof List) |
| 197 |
return list; |
| 198 |
throw notSupported(source); |
| 199 |
} |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Returns a value property for observing the location of a {@link Control}. |
| 205 |
* |
| 206 |
* @return a value property for observing the location of a {@link Control}. |
| 207 |
*/ |
| 208 |
public static IValueProperty location() { |
| 209 |
return new ControlLocationProperty(); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Returns a value property for observing the maximum value of a |
| 214 |
* {@link Scale} or {@link Spinner}. |
| 215 |
* |
| 216 |
* @return a value property for observing the maximum value of a |
| 217 |
* {@link Scale} or {@link Spinner}. |
| 218 |
*/ |
| 219 |
public static IValueProperty maximum() { |
| 220 |
return new DelegatingValueProperty(Integer.TYPE) { |
| 221 |
private IValueProperty scale = new ScaleMaximumProperty(); |
| 222 |
private IValueProperty spinner = new SpinnerMaximumProperty(); |
| 223 |
|
| 224 |
protected IValueProperty doGetDelegate(Object source) { |
| 225 |
if (source instanceof Scale) |
| 226 |
return scale; |
| 227 |
if (source instanceof Spinner) |
| 228 |
return spinner; |
| 229 |
throw notSupported(source); |
| 230 |
} |
| 231 |
}; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Returns a value property for observing the minimum value of a |
| 236 |
* {@link Scale} or {@link Spinner}. |
| 237 |
* |
| 238 |
* @return a value property for observing the minimum value of a |
| 239 |
* {@link Scale} or {@link Spinner}. |
| 240 |
*/ |
| 241 |
public static IValueProperty minimum() { |
| 242 |
return new DelegatingValueProperty(Integer.TYPE) { |
| 243 |
private IValueProperty scale = new ScaleMinimumProperty(); |
| 244 |
private IValueProperty spinner = new SpinnerMinimumProperty(); |
| 245 |
|
| 246 |
protected IValueProperty doGetDelegate(Object source) { |
| 247 |
if (source instanceof Scale) |
| 248 |
return scale; |
| 249 |
if (source instanceof Spinner) |
| 250 |
return spinner; |
| 251 |
throw notSupported(source); |
| 252 |
} |
| 253 |
}; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Returns a value property for observing the selection state of a |
| 258 |
* {@link Button}, {@link CCombo}, {@link Combo}, {@link List}, |
| 259 |
* {@link Scale} or {@link Spinner}. |
| 260 |
* |
| 261 |
* @return a value property for observing the selection state of a |
| 262 |
* {@link Button}, {@link CCombo}, {@link Combo}, {@link List}, |
| 263 |
* {@link Scale} or {@link Spinner}. |
| 264 |
*/ |
| 265 |
public static IValueProperty selection() { |
| 266 |
return new DelegatingValueProperty() { |
| 267 |
private IValueProperty button = new ButtonSelectionProperty(); |
| 268 |
private IValueProperty cCombo = new CComboSelectionProperty(); |
| 269 |
private IValueProperty combo = new ComboSelectionProperty(); |
| 270 |
private IValueProperty list = new ListSelectionProperty(); |
| 271 |
private IValueProperty scale = new ScaleSelectionProperty(); |
| 272 |
private IValueProperty spinner = new SpinnerSelectionProperty(); |
| 273 |
|
| 274 |
protected IValueProperty doGetDelegate(Object source) { |
| 275 |
if (source instanceof Button) |
| 276 |
return button; |
| 277 |
if (source instanceof CCombo) |
| 278 |
return cCombo; |
| 279 |
if (source instanceof Combo) |
| 280 |
return combo; |
| 281 |
if (source instanceof List) |
| 282 |
return list; |
| 283 |
if (source instanceof Scale) |
| 284 |
return scale; |
| 285 |
if (source instanceof Spinner) |
| 286 |
return spinner; |
| 287 |
throw notSupported(source); |
| 288 |
} |
| 289 |
}; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Returns a value property for observing the single selection index of a |
| 294 |
* {@link CCombo}, {@link Combo}, {@link List} or {@link Table}. |
| 295 |
* |
| 296 |
* @return a value property for the single selection index of a SWT Combo. |
| 297 |
*/ |
| 298 |
public static IValueProperty singleSelectionIndex() { |
| 299 |
return new DelegatingValueProperty(Integer.TYPE) { |
| 300 |
private IValueProperty cCombo = new CComboSingleSelectionIndexProperty(); |
| 301 |
private IValueProperty combo = new ComboSingleSelectionIndexProperty(); |
| 302 |
private IValueProperty list = new ListSingleSelectionIndexProperty(); |
| 303 |
private IValueProperty table = new TableSingleSelectionIndexProperty(); |
| 304 |
|
| 305 |
protected IValueProperty doGetDelegate(Object source) { |
| 306 |
if (source instanceof CCombo) |
| 307 |
return cCombo; |
| 308 |
if (source instanceof Combo) |
| 309 |
return combo; |
| 310 |
if (source instanceof List) |
| 311 |
return list; |
| 312 |
if (source instanceof Table) |
| 313 |
return table; |
| 314 |
throw notSupported(source); |
| 315 |
} |
| 316 |
}; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Returns a value property for observing the size of a {@link Control}. |
| 321 |
* |
| 322 |
* @return a value property for observing the size of a {@link Control}. |
| 323 |
*/ |
| 324 |
public static IValueProperty size() { |
| 325 |
return new ControlSizeProperty(); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns a value property for observing the text of a {@link CCombo}, |
| 330 |
* {@link CLabel}, {@link Combo}, {@link Label}, {@link Item}, {@link Link}, |
| 331 |
* {@link Shell} or {@link Text}. |
| 332 |
* |
| 333 |
* @return a value property for observing the text of a {@link CCombo}, |
| 334 |
* {@link CLabel}, {@link Combo}, {@link Label}, {@link Item}, |
| 335 |
* {@link Link}, {@link Shell} or {@link Text}. |
| 336 |
*/ |
| 337 |
public static IValueProperty text() { |
| 338 |
return new DelegatingValueProperty(String.class) { |
| 339 |
private IValueProperty cCombo = new CComboTextProperty(); |
| 340 |
private IValueProperty cLabel = new CLabelTextProperty(); |
| 341 |
private IValueProperty combo = new ComboTextProperty(); |
| 342 |
private IValueProperty item = new ItemTextProperty(); |
| 343 |
private IValueProperty label = new LabelTextProperty(); |
| 344 |
private IValueProperty link = new LinkTextProperty(); |
| 345 |
private IValueProperty shell = new ShellTextProperty(); |
| 346 |
private IValueProperty text = new TextTextProperty(SWT.None); |
| 347 |
|
| 348 |
protected IValueProperty doGetDelegate(Object source) { |
| 349 |
if (source instanceof CCombo) |
| 350 |
return cCombo; |
| 351 |
if (source instanceof CLabel) |
| 352 |
return cLabel; |
| 353 |
if (source instanceof Combo) |
| 354 |
return combo; |
| 355 |
if (source instanceof Item) |
| 356 |
return item; |
| 357 |
if (source instanceof Label) |
| 358 |
return label; |
| 359 |
if (source instanceof Link) |
| 360 |
return link; |
| 361 |
if (source instanceof Shell) |
| 362 |
return shell; |
| 363 |
if (source instanceof Text) |
| 364 |
return text; |
| 365 |
throw notSupported(source); |
| 366 |
} |
| 367 |
}; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Returns a value property for observing the text of a {@link StyledText} |
| 372 |
* or {@link Text}. |
| 373 |
* |
| 374 |
* @param event |
| 375 |
* the SWT event type to register for change events. May be |
| 376 |
* {@link SWT#None}, {@link SWT#Modify} or {@link SWT#FocusOut}. |
| 377 |
* |
| 378 |
* @return a value property for observing the text of a {@link StyledText} |
| 379 |
* or {@link Text}. |
| 380 |
*/ |
| 381 |
public static IValueProperty text(final int event) { |
| 382 |
return new DelegatingValueProperty(String.class) { |
| 383 |
private IValueProperty styledText = new StyledTextTextProperty( |
| 384 |
event); |
| 385 |
private IValueProperty text = new TextTextProperty(event); |
| 386 |
|
| 387 |
protected IValueProperty doGetDelegate(Object source) { |
| 388 |
if (source instanceof StyledText) |
| 389 |
return styledText; |
| 390 |
if (source instanceof Text) |
| 391 |
return text; |
| 392 |
throw notSupported(source); |
| 393 |
} |
| 394 |
}; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Returns a value property for observing the tooltip text of a |
| 399 |
* {@link CTabItem}, {@link Control}, {@link TabItem}, {@link TableColumn}, |
| 400 |
* {@link ToolItem}, {@link TrayItem}, {@link TreeColumn} or |
| 401 |
* {@link TreeItem}. |
| 402 |
* |
| 403 |
* @return a value property for observing the tooltip text of a |
| 404 |
* {@link CTabItem}, {@link Control}, {@link TabItem}, |
| 405 |
* {@link TableColumn}, {@link ToolItem}, {@link TrayItem}, |
| 406 |
* {@link TreeColumn} or {@link TreeItem}. |
| 407 |
*/ |
| 408 |
public static IValueProperty tooltipText() { |
| 409 |
return new DelegatingValueProperty(String.class) { |
| 410 |
private IValueProperty cTabItem = new CTabItemTooltipTextProperty(); |
| 411 |
private IValueProperty control = new ControlTooltipTextProperty(); |
| 412 |
private IValueProperty tabItem = new TabItemTooltipTextProperty(); |
| 413 |
private IValueProperty tableColumn = new TableColumnTooltipTextProperty(); |
| 414 |
private IValueProperty toolItem = new ToolItemTooltipTextProperty(); |
| 415 |
private IValueProperty trayItem = new TrayItemTooltipTextProperty(); |
| 416 |
private IValueProperty treeItem = new TreeItemTooltipTextProperty(); |
| 417 |
|
| 418 |
protected IValueProperty doGetDelegate(Object source) { |
| 419 |
if (source instanceof CTabItem) |
| 420 |
return cTabItem; |
| 421 |
if (source instanceof Control) |
| 422 |
return control; |
| 423 |
if (source instanceof TabItem) |
| 424 |
return tabItem; |
| 425 |
if (source instanceof TableColumn) |
| 426 |
return tableColumn; |
| 427 |
if (source instanceof ToolItem) |
| 428 |
return toolItem; |
| 429 |
if (source instanceof TrayItem) |
| 430 |
return trayItem; |
| 431 |
if (source instanceof TreeItem) |
| 432 |
return treeItem; |
| 433 |
throw notSupported(source); |
| 434 |
} |
| 435 |
}; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Returns a value property for observing the visibility state of a |
| 440 |
* {@link Control}. |
| 441 |
* |
| 442 |
* @return a value property for observing the visibility state of a |
| 443 |
* {@link Control}. |
| 444 |
*/ |
| 445 |
public static IValueProperty visible() { |
| 446 |
return new ControlVisibleProperty(); |
| 447 |
} |
| 448 |
} |