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