|
Lines 8-13
Link Here
|
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
| 10 |
* Brad Reynolds (bug 135446) |
10 |
* Brad Reynolds (bug 135446) |
|
|
11 |
* Obtiva Corporation (bug 156264) |
| 11 |
*******************************************************************************/ |
12 |
*******************************************************************************/ |
| 12 |
package org.eclipse.jface.internal.databinding.internal.swt; |
13 |
package org.eclipse.jface.internal.databinding.internal.swt; |
| 13 |
|
14 |
|
|
Lines 33-45
Link Here
|
| 33 |
* |
34 |
* |
| 34 |
* <dl> |
35 |
* <dl> |
| 35 |
* <dt>Events:</dt> |
36 |
* <dt>Events:</dt> |
| 36 |
* <dd> If the update event type (specified on construction) is |
37 |
* <dd> The update event type (specified on construction) can contain either |
|
|
38 |
* SWT.Modify or SWT.FocusOut, but not both. If the update event type contains |
| 37 |
* <code>SWT.Modify</code> a value change event will be fired on every key |
39 |
* <code>SWT.Modify</code> a value change event will be fired on every key |
| 38 |
* stroke. If the update event type is <code>SWT.FocusOut</code> a value |
40 |
* stroke. If the update event type contains <code>SWT.FocusOut</code>, a value |
| 39 |
* change event will be fired on focus out. When in either mode if the user is |
41 |
* change event will be fired on focus out. If the update event type contains |
| 40 |
* entering text and presses [Escape] the value will be reverted back to the |
42 |
* <code>SWT.CR</code> in addition to <code>SWT.FocusOut</code>, a value change |
| 41 |
* last value set using doSetValue(). Regardless of the update event type a |
43 |
* event will be fired when pressing [Enter]. The default <code>SWT.NONE</code> |
| 42 |
* value changing event will fire on verify to enable vetoing of changes.</dd> |
44 |
* is equivalent to <code>SWT.FocusOut | SWT.CR</code> |
|
|
45 |
* Regardless of the update event type a value changing event will fire on |
| 46 |
* verify to enable vetoing of changes. Moreover, if the user is entering |
| 47 |
* text and presses [Escape], the value will be reverted back to the last value |
| 48 |
* set using doSetValue(). |
| 49 |
* </dd> |
| 43 |
* </dl> |
50 |
* </dl> |
| 44 |
* |
51 |
* |
| 45 |
* @since 1.0 |
52 |
* @since 1.0 |
|
Lines 59-74
Link Here
|
| 59 |
private boolean updating = false; |
66 |
private boolean updating = false; |
| 60 |
|
67 |
|
| 61 |
/** |
68 |
/** |
| 62 |
* SWT event that on firing this observable will fire change events to its |
69 |
* Observable style for specifying whether Enter should be handled, |
| 63 |
* listeners. |
70 |
* whether to use FocusOut or Modify as the time of updating, and whether |
|
|
71 |
* to support Escape to undo edits. |
| 64 |
*/ |
72 |
*/ |
| 65 |
private int updateEventType; |
73 |
private int observableStyle; |
| 66 |
|
74 |
|
| 67 |
/** |
75 |
/** |
| 68 |
* Valid types for the {@link #updateEventType}. |
76 |
* SWT event that on firing this observable will fire change events to its |
|
|
77 |
* listeners. |
| 69 |
*/ |
78 |
*/ |
| 70 |
private static final int[] validUpdateEventTypes = new int[] { SWT.Modify, |
79 |
private int updateEventType; |
| 71 |
SWT.FocusOut, SWT.NONE }; |
|
|
| 72 |
|
80 |
|
| 73 |
/** |
81 |
/** |
| 74 |
* Last value set using doSetValue(), or null. This is maintained so that |
82 |
* Last value set using doSetValue(), or null. This is maintained so that |
|
Lines 112-139
Link Here
|
| 112 |
* <code>updateEventType</code>. |
120 |
* <code>updateEventType</code>. |
| 113 |
* |
121 |
* |
| 114 |
* @param text |
122 |
* @param text |
| 115 |
* @param updateEventType |
123 |
* @param observableStyle |
| 116 |
* SWT event constant as to what SWT event to update the model in |
124 |
* SWT event constant as to what SWT event to update the model in |
| 117 |
* response to. Appropriate values are: <code>SWT.Modify</code>, |
125 |
* response to. Appropriate values are: <code>SWT.Modify</code>, |
| 118 |
* <code>SWT.FocusOut</code>, <code>SWT.NONE</code>. |
126 |
* <code>SWT.FocusOut</code>, <code>SWT.CR</code>, |
|
|
127 |
* <code>SWT.NONE</code>. |
| 119 |
* @throws IllegalArgumentException |
128 |
* @throws IllegalArgumentException |
| 120 |
* if <code>updateEventType</code> is an incorrect type. |
129 |
* if <code>updateEventType</code> is an incorrect type. |
| 121 |
*/ |
130 |
*/ |
| 122 |
public TextObservableValue(final Text text, int updateEventType) { |
131 |
public TextObservableValue(final Text text, final int observableStyle) { |
| 123 |
super(text); |
132 |
super(text); |
| 124 |
boolean eventValid = false; |
133 |
this.text = text; |
| 125 |
for (int i = 0; !eventValid && i < validUpdateEventTypes.length; i++) { |
134 |
this.observableStyle = observableStyle; |
| 126 |
eventValid = (updateEventType == validUpdateEventTypes[i]); |
135 |
if (observableStyleContains(SWT.Modify) && |
|
|
136 |
!observableStyleContains(SWT.CR)) { // CR conflicts with Modify |
| 137 |
updateEventType = SWT.Modify; |
| 138 |
} else if (observableStyleContains(SWT.FocusOut)) { |
| 139 |
updateEventType = SWT.FocusOut; |
| 140 |
} else if (observableStyle == SWT.NONE) { |
| 141 |
updateEventType = SWT.FocusOut; |
| 142 |
this.observableStyle = SWT.FocusOut | SWT.CR; // Defaults |
| 127 |
} |
143 |
} |
| 128 |
if (!eventValid) { |
144 |
else { |
| 129 |
throw new IllegalArgumentException( |
145 |
throw new IllegalArgumentException( |
| 130 |
"UpdateEventType [" + updateEventType + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ |
146 |
"Invalid ObservableStyle was supplied."); //$NON-NLS-1$ |
| 131 |
} |
|
|
| 132 |
this.text = text; |
| 133 |
this.updateEventType = updateEventType; |
| 134 |
if (updateEventType != SWT.None) { |
| 135 |
text.addListener(updateEventType, updateListener); |
| 136 |
} |
147 |
} |
|
|
148 |
|
| 149 |
text.addListener(updateEventType, updateListener); |
| 150 |
|
| 137 |
// If the update policy is SWT.Modify then the model is notified of |
151 |
// If the update policy is SWT.Modify then the model is notified of |
| 138 |
// changed on key stroke by key stroke |
152 |
// changed on key stroke by key stroke |
| 139 |
// When escape is pressed we need to rollback to the previous value |
153 |
// When escape is pressed we need to rollback to the previous value |
|
Lines 149-154
Link Here
|
| 149 |
} |
163 |
} |
| 150 |
}); |
164 |
}); |
| 151 |
} |
165 |
} |
|
|
166 |
|
| 152 |
verifyListener = new VerifyListener() { |
167 |
verifyListener = new VerifyListener() { |
| 153 |
public void verifyText(VerifyEvent e) { |
168 |
public void verifyText(VerifyEvent e) { |
| 154 |
if (!updating) { |
169 |
if (!updating) { |
|
Lines 164-181
Link Here
|
| 164 |
} |
179 |
} |
| 165 |
}; |
180 |
}; |
| 166 |
text.addVerifyListener(verifyListener); |
181 |
text.addVerifyListener(verifyListener); |
|
|
182 |
|
| 167 |
keyListener = new KeyListener() { |
183 |
keyListener = new KeyListener() { |
| 168 |
public void keyPressed(KeyEvent e) { |
184 |
public void keyPressed(KeyEvent e) { |
| 169 |
if (e.character == SWT.ESC && bufferedValue != null) { |
185 |
|
|
|
186 |
// If character is escape |
| 187 |
if ((e.character == SWT.ESC) && |
| 188 |
(bufferedValue != null)) { |
| 170 |
// Revert the value in the text field to the model value |
189 |
// Revert the value in the text field to the model value |
| 171 |
text.setText(bufferedValue); |
190 |
text.setText(bufferedValue); |
| 172 |
} |
191 |
} |
|
|
192 |
|
| 193 |
// If character is carriage return |
| 194 |
String oldValue = bufferedValue; |
| 195 |
String newValue = text.getText(); |
| 196 |
if ((observableStyleContains(SWT.CR)) && |
| 197 |
(e.character == SWT.CR)) { |
| 198 |
bufferedValue = text.getText(); |
| 199 |
if (!newValue.equals(oldValue)) { |
| 200 |
// Propagate text field value to the model |
| 201 |
fireValueChange(Diffs.createValueDiff(oldValue, |
| 202 |
newValue)); |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 173 |
} |
206 |
} |
| 174 |
|
207 |
|
| 175 |
public void keyReleased(KeyEvent e) { |
208 |
public void keyReleased(KeyEvent e) { |
| 176 |
} |
209 |
} |
| 177 |
}; |
210 |
}; |
| 178 |
text.addKeyListener(keyListener); |
211 |
text.addKeyListener(keyListener); |
|
|
212 |
|
| 179 |
shellListener = new ShellAdapter() { |
213 |
shellListener = new ShellAdapter() { |
| 180 |
public void shellClosed(ShellEvent e) { |
214 |
public void shellClosed(ShellEvent e) { |
| 181 |
if (!text.isDisposed()) { |
215 |
if (!text.isDisposed()) { |
|
Lines 241-244
Link Here
|
| 241 |
} |
275 |
} |
| 242 |
super.dispose(); |
276 |
super.dispose(); |
| 243 |
} |
277 |
} |
|
|
278 |
|
| 279 |
/** |
| 280 |
* Indicates if observableStyle contains a particular SWT style |
| 281 |
* @param style SWT style |
| 282 |
* @return true if observableStyle contains SWT style or false otherwise |
| 283 |
*/ |
| 284 |
private boolean observableStyleContains(int style) { |
| 285 |
return (observableStyle & style) == style; |
| 286 |
} |
| 244 |
} |
287 |
} |