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