78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2005, 2008 IBM Corporation 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 * IBM Corporation - initial API and implementation
|
|
10 * Brad Reynolds - bug 164653
|
|
11 *******************************************************************************/
|
|
12 module org.eclipse.jface.internal.databinding.swt.SingleSelectionObservableValue;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import org.eclipse.core.databinding.observable.Diffs;
|
|
17 import org.eclipse.core.databinding.observable.Realm;
|
|
18 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
|
|
19 import org.eclipse.swt.widgets.Control;
|
|
20
|
|
21 /**
|
|
22 * @since 1.0
|
|
23 *
|
|
24 */
|
|
25 abstract public class SingleSelectionObservableValue :
|
|
26 AbstractSWTObservableValue {
|
|
27
|
|
28 private bool updating = false;
|
|
29
|
|
30 private int currentSelection;
|
|
31
|
|
32 /**
|
|
33 * @param control
|
|
34 * the control
|
|
35 */
|
|
36 public this(Control control) {
|
|
37 super(control);
|
|
38 init();
|
|
39 }
|
|
40
|
|
41 /**
|
|
42 * @param realm
|
|
43 * @param control
|
|
44 */
|
|
45 public this(Realm realm, Control control) {
|
|
46 super(realm, control);
|
|
47 init();
|
|
48 }
|
|
49
|
|
50 private void init() {
|
|
51 currentSelection = doGetSelectionIndex();
|
|
52 doAddSelectionListener(new class() Runnable{
|
|
53 public void run() {
|
|
54 if (!updating) {
|
|
55 int newSelection = doGetSelectionIndex();
|
|
56 notifyIfChanged(currentSelection, newSelection);
|
|
57 currentSelection = newSelection;
|
|
58 }
|
|
59 }
|
|
60 });
|
|
61 }
|
|
62
|
|
63 /**
|
|
64 * @param runnable
|
|
65 */
|
|
66 protected abstract void doAddSelectionListener(Runnable runnable);
|
|
67
|
|
68 public void doSetValue(Object value) {
|
|
69 try {
|
|
70 updating = true;
|
|
71 int intValue = (cast(Integer) value).intValue();
|
|
72 doSetSelectionIndex(intValue);
|
|
73 notifyIfChanged(currentSelection, intValue);
|
|
74 currentSelection = intValue;
|
|
75 } finally {
|
|
76 updating = false;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * @param intValue
|
|
82 * the selection index
|
|
83 */
|
|
84 protected abstract void doSetSelectionIndex(int intValue);
|
|
85
|
|
86 public Object doGetValue() {
|
|
87 return new Integer(doGetSelectionIndex());
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 * @return the selection index
|
|
92 */
|
|
93 protected abstract int doGetSelectionIndex();
|
|
94
|
|
95 public Object getValueType() {
|
|
96 return Integer.TYPE;
|
|
97 }
|
|
98
|
|
99 private void notifyIfChanged(int oldValue, int newValue) {
|
|
100 if (oldValue !is newValue) {
|
|
101 fireValueChange(Diffs.createValueDiff(new Integer(
|
|
102 oldValue), new Integer(newValue)));
|
|
103 }
|
|
104 }
|
|
105 }
|