comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/SpinnerObservableValue.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
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;
15
16 import java.lang.all;
17
18 import org.eclipse.core.databinding.observable.Diffs;
19 import org.eclipse.core.databinding.observable.Realm;
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.widgets.Spinner;
25
26 /**
27 * @since 1.0
28 *
29 */
30 public class SpinnerObservableValue : AbstractSWTObservableValue {
31
32 private final Spinner spinner;
33
34 private final String attribute;
35
36 private bool updating = false;
37
38 private int currentSelection;
39
40 private ModifyListener modifyListener;
41
42 /**
43 * @param spinner
44 * @param attribute
45 */
46 public this(Spinner spinner, String attribute) {
47 super(spinner);
48 this.spinner = spinner;
49 this.attribute = attribute;
50 init();
51 }
52
53 /**
54 * @param realm
55 * @param spinner
56 * @param attribute
57 */
58 public this(Realm realm, Spinner spinner, String attribute) {
59 super(realm, spinner);
60 this.spinner = spinner;
61 this.attribute = attribute;
62 init();
63 }
64
65 private void init() {
66 if (attribute.equals(SWTProperties.SELECTION)) {
67 currentSelection = spinner.getSelection();
68 modifyListener = new class() ModifyListener {
69 public void modifyText(ModifyEvent e) {
70 if (!updating) {
71 int newSelection = this.outer.spinner
72 .getSelection();
73 notifyIfChanged(currentSelection, newSelection);
74 currentSelection = newSelection;
75 }
76 }
77 };
78 spinner.addModifyListener(modifyListener);
79 } else if (!attribute.equals(SWTProperties.MIN)
80 && !attribute.equals(SWTProperties.MAX)) {
81 throw new IllegalArgumentException(
82 Format("Attribute name not valid: {}", attribute)); //$NON-NLS-1$
83 }
84 }
85
86 public void doSetValue(Object value) {
87 int oldValue;
88 int newValue;
89 try {
90 updating = true;
91 newValue = (cast(Integer) value).intValue();
92 if (attribute.equals(SWTProperties.SELECTION)) {
93 oldValue = spinner.getSelection();
94 spinner.setSelection(newValue);
95 currentSelection = newValue;
96 } else if (attribute.equals(SWTProperties.MIN)) {
97 oldValue = spinner.getMinimum();
98 spinner.setMinimum(newValue);
99 } else if (attribute.equals(SWTProperties.MAX)) {
100 oldValue = spinner.getMaximum();
101 spinner.setMaximum(newValue);
102 } else {
103 Assert.isTrue(false, "invalid attribute name:" + attribute); //$NON-NLS-1$
104 return;
105 }
106 notifyIfChanged(oldValue, newValue);
107 } finally {
108 updating = false;
109 }
110 }
111
112 public Object doGetValue() {
113 int value = 0;
114 if (attribute.equals(SWTProperties.SELECTION)) {
115 value = spinner.getSelection();
116 } else if (attribute.equals(SWTProperties.MIN)) {
117 value = spinner.getMinimum();
118 } else if (attribute.equals(SWTProperties.MAX)) {
119 value = spinner.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 /*
136 * (non-Javadoc)
137 *
138 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
139 */
140 public synchronized void dispose() {
141 super.dispose();
142 if (modifyListener !is null && !spinner.isDisposed()) {
143 spinner.removeModifyListener(modifyListener);
144 }
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 }