comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/CComboObservableValue.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 * Eric Rizzo - bug 134884
14 *******************************************************************************/
15 module org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
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.custom.CCombo;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26
27 /**
28 * @since 3.2
29 *
30 */
31 public class CComboObservableValue : AbstractSWTObservableValue {
32
33 /**
34 *
35 */
36
37 private final CCombo ccombo;
38
39 private final String attribute;
40
41 private bool updating = false;
42
43 private String currentValue;
44
45 private ModifyListener modifyListener;
46
47 /**
48 * @param ccombo
49 * @param attribute
50 */
51 public this(CCombo ccombo, String attribute) {
52 super(ccombo);
53 this.ccombo = ccombo;
54 this.attribute = attribute;
55 init();
56 }
57
58 /**
59 * @param realm
60 * @param ccombo
61 * @param attribute
62 */
63 public this(Realm realm, CCombo ccombo, String attribute) {
64 super(realm, ccombo);
65 this.ccombo = ccombo;
66 this.attribute = attribute;
67 init();
68 }
69
70 private void init() {
71 if (attribute.equals(SWTProperties.SELECTION)
72 || attribute.equals(SWTProperties.TEXT)) {
73 this.currentValue = ccombo.getText();
74 modifyListener = new class() ModifyListener {
75
76 public void modifyText(ModifyEvent e) {
77 if (!updating) {
78 String oldValue = currentValue;
79 currentValue = this.outer.ccombo
80 .getText();
81
82 notifyIfChanged(oldValue, currentValue);
83 }
84 }
85 };
86 ccombo.addModifyListener(modifyListener);
87 } else
88 throw new IllegalArgumentException();
89 }
90
91 public void doSetValue(Object value) {
92 String oldValue = ccombo.getText();
93 try {
94 updating = true;
95 if (attribute.equals(SWTProperties.TEXT)) {
96 String stringValue = value !is null ? value.toString() : ""; //$NON-NLS-1$
97 ccombo.setText(stringValue);
98 } else if (attribute.equals(SWTProperties.SELECTION)) {
99 String items[] = ccombo.getItems();
100 int index = -1;
101 if (value is null) {
102 ccombo.select(-1);
103 } else if (items !is null) {
104 for (int i = 0; i < items.length; i++) {
105 if (value.equals(items[i])) {
106 index = i;
107 break;
108 }
109 }
110 if (index is -1) {
111 ccombo.setText(cast(String) value);
112 } else {
113 ccombo.select(index); // -1 will not "unselect"
114 }
115 }
116 }
117 } finally {
118 updating = false;
119 currentValue = ccombo.getText();
120 }
121
122 notifyIfChanged(oldValue, currentValue);
123 }
124
125 public Object doGetValue() {
126 if (attribute.equals(SWTProperties.TEXT))
127 return ccombo.getText();
128
129 Assert.isTrue(attribute.equals(SWTProperties.SELECTION),
130 "unexpected attribute: " + attribute); //$NON-NLS-1$
131 // The problem with a ccombo, is that it changes the text and
132 // fires before it update its selection index
133 return ccombo.getText();
134 }
135
136 public Object getValueType() {
137 Assert.isTrue(attribute.equals(SWTProperties.TEXT)
138 || attribute.equals(SWTProperties.SELECTION),
139 "unexpected attribute: " + attribute); //$NON-NLS-1$
140 return String.classinfo;
141 }
142
143 /**
144 * @return attribute being observed
145 */
146 public String getAttribute() {
147 return attribute;
148 }
149
150 /*
151 * (non-Javadoc)
152 *
153 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
154 */
155 public synchronized void dispose() {
156 super.dispose();
157
158 if (modifyListener !is null && !ccombo.isDisposed()) {
159 ccombo.removeModifyListener(modifyListener);
160 }
161 }
162
163 private void notifyIfChanged(String oldValue, String newValue) {
164 if (!oldValue.equals(newValue)) {
165 fireValueChange(Diffs.createValueDiff(oldValue, ccombo.getText()));
166 }
167 }
168 }