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