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 * Ashley Cambrell - bug 198904
|
|
12 * Matthew Hall - bug 118516
|
|
13 *******************************************************************************/
|
|
14 module org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
|
85
|
15 import org.eclipse.jface.internal.databinding.swt.SWTProperties;
|
78
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import org.eclipse.core.databinding.observable.Diffs;
|
|
20 import org.eclipse.core.databinding.observable.Realm;
|
|
21 import org.eclipse.core.runtime.Assert;
|
|
22 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
|
|
23 import org.eclipse.swt.events.ModifyEvent;
|
|
24 import org.eclipse.swt.events.ModifyListener;
|
|
25 import org.eclipse.swt.widgets.Spinner;
|
|
26
|
|
27 /**
|
|
28 * @since 1.0
|
|
29 *
|
|
30 */
|
|
31 public class SpinnerObservableValue : AbstractSWTObservableValue {
|
|
32
|
|
33 private final Spinner spinner;
|
|
34
|
|
35 private final String attribute;
|
|
36
|
|
37 private bool updating = false;
|
|
38
|
|
39 private int currentSelection;
|
|
40
|
|
41 private ModifyListener modifyListener;
|
|
42
|
|
43 /**
|
|
44 * @param spinner
|
|
45 * @param attribute
|
|
46 */
|
|
47 public this(Spinner spinner, String attribute) {
|
|
48 super(spinner);
|
|
49 this.spinner = spinner;
|
|
50 this.attribute = attribute;
|
|
51 init();
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * @param realm
|
|
56 * @param spinner
|
|
57 * @param attribute
|
|
58 */
|
|
59 public this(Realm realm, Spinner spinner, String attribute) {
|
|
60 super(realm, spinner);
|
|
61 this.spinner = spinner;
|
|
62 this.attribute = attribute;
|
|
63 init();
|
|
64 }
|
|
65
|
|
66 private void init() {
|
|
67 if (attribute.equals(SWTProperties.SELECTION)) {
|
|
68 currentSelection = spinner.getSelection();
|
|
69 modifyListener = new class() ModifyListener {
|
|
70 public void modifyText(ModifyEvent e) {
|
|
71 if (!updating) {
|
|
72 int newSelection = this.outer.spinner
|
|
73 .getSelection();
|
|
74 notifyIfChanged(currentSelection, newSelection);
|
|
75 currentSelection = newSelection;
|
|
76 }
|
|
77 }
|
|
78 };
|
|
79 spinner.addModifyListener(modifyListener);
|
|
80 } else if (!attribute.equals(SWTProperties.MIN)
|
|
81 && !attribute.equals(SWTProperties.MAX)) {
|
|
82 throw new IllegalArgumentException(
|
|
83 Format("Attribute name not valid: {}", attribute)); //$NON-NLS-1$
|
|
84 }
|
|
85 }
|
|
86
|
|
87 public void doSetValue(Object value) {
|
|
88 int oldValue;
|
|
89 int newValue;
|
|
90 try {
|
|
91 updating = true;
|
|
92 newValue = (cast(Integer) value).intValue();
|
|
93 if (attribute.equals(SWTProperties.SELECTION)) {
|
|
94 oldValue = spinner.getSelection();
|
|
95 spinner.setSelection(newValue);
|
|
96 currentSelection = newValue;
|
|
97 } else if (attribute.equals(SWTProperties.MIN)) {
|
|
98 oldValue = spinner.getMinimum();
|
|
99 spinner.setMinimum(newValue);
|
|
100 } else if (attribute.equals(SWTProperties.MAX)) {
|
|
101 oldValue = spinner.getMaximum();
|
|
102 spinner.setMaximum(newValue);
|
|
103 } else {
|
85
|
104 Assert.isTrue(false, "invalid attribute name:" ~ attribute); //$NON-NLS-1$
|
78
|
105 return;
|
|
106 }
|
|
107 notifyIfChanged(oldValue, newValue);
|
|
108 } finally {
|
|
109 updating = false;
|
|
110 }
|
|
111 }
|
|
112
|
|
113 public Object doGetValue() {
|
|
114 int value = 0;
|
|
115 if (attribute.equals(SWTProperties.SELECTION)) {
|
|
116 value = spinner.getSelection();
|
|
117 } else if (attribute.equals(SWTProperties.MIN)) {
|
|
118 value = spinner.getMinimum();
|
|
119 } else if (attribute.equals(SWTProperties.MAX)) {
|
|
120 value = spinner.getMaximum();
|
|
121 }
|
|
122 return new Integer(value);
|
|
123 }
|
|
124
|
|
125 public Object getValueType() {
|
|
126 return Integer.TYPE;
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * @return attribute being observed
|
|
131 */
|
|
132 public String getAttribute() {
|
|
133 return attribute;
|
|
134 }
|
|
135
|
|
136 /*
|
|
137 * (non-Javadoc)
|
|
138 *
|
|
139 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
|
|
140 */
|
|
141 public synchronized void dispose() {
|
|
142 super.dispose();
|
|
143 if (modifyListener !is null && !spinner.isDisposed()) {
|
|
144 spinner.removeModifyListener(modifyListener);
|
|
145 }
|
|
146 }
|
|
147
|
|
148 private void notifyIfChanged(int oldValue, int newValue) {
|
|
149 if (oldValue !is newValue) {
|
|
150 fireValueChange(Diffs.createValueDiff(new Integer(oldValue),
|
|
151 new Integer(newValue)));
|
|
152 }
|
|
153 }
|
|
154 }
|