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