|
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 32-45
Link Here
|
| 32 |
* on construction. |
33 |
* on construction. |
| 33 |
* |
34 |
* |
| 34 |
* <dl> |
35 |
* <dl> |
| 35 |
* <dt>Events:</dt> |
36 |
* <dt>Observable Styles:</dt> |
| 36 |
* <dd> If the update event type (specified on construction) is |
37 |
* <dd> If the observable style (specified on construction) contains |
| 37 |
* <code>SWT.Modify</code> a value change event will be fired on every key |
38 |
* <code>UPDATE_ON_MODIFY</code> a value change event will be fired on every |
| 38 |
* stroke. If the update event type is <code>SWT.FocusOut</code> a value |
39 |
* key stroke. If the observable style contains |
| 39 |
* change event will be fired on focus out. When in either mode if the user is |
40 |
* <code>UPDATE_ON_FOCUS_OUT</code>, a value change event will be fired on focus |
| 40 |
* entering text and presses [Escape] the value will be reverted back to the |
41 |
* out. If the observable style contains <code>UPDATE_ON_ENTER</code>, a value |
| 41 |
* last value set using doSetValue(). Regardless of the update event type a |
42 |
* change event will be fired when pressing [Enter]. If the observable style is |
| 42 |
* value changing event will fire on verify to enable vetoing of changes.</dd> |
43 |
* <code>NONE</code>, no value change event will ever fire. |
|
|
44 |
* If the observable style <code>TextObservableStyle.REVERT_ON_ESC</code> is |
| 45 |
* specified, then if the user is entering text and presses [Escape], the value |
| 46 |
* will be reverted back to the last value set using doSetValue(). |
| 47 |
* Regardless of the observable style a value changing event will fire on |
| 48 |
* verify to enable vetoing of changes. |
| 49 |
* </dd> |
| 43 |
* </dl> |
50 |
* </dl> |
| 44 |
* |
51 |
* |
| 45 |
* @since 1.0 |
52 |
* @since 1.0 |
|
Lines 47-52
Link Here
|
| 47 |
public class TextObservableValue extends AbstractSWTVetoableValue { |
54 |
public class TextObservableValue extends AbstractSWTVetoableValue { |
| 48 |
|
55 |
|
| 49 |
/** |
56 |
/** |
|
|
57 |
* No value change event must ever fire. |
| 58 |
*/ |
| 59 |
public static final int NONE = 0; |
| 60 |
|
| 61 |
/** |
| 62 |
* A value change event must be fired on every key stroke. |
| 63 |
*/ |
| 64 |
public static final int UPDATE_ON_MODIFY = 1<<0; |
| 65 |
|
| 66 |
/** |
| 67 |
* A value change event must be fired when pressing [Enter]. |
| 68 |
*/ |
| 69 |
public static final int UPDATE_ON_ENTER = 1<<1; |
| 70 |
|
| 71 |
/** |
| 72 |
* A value change event must be fired on focus out. |
| 73 |
*/ |
| 74 |
public static final int UPDATE_ON_FOCUS_OUT = 1<<2; |
| 75 |
|
| 76 |
/** |
| 77 |
* If the user is entering text and presses [Escape], the value must be |
| 78 |
* reverted back to the last value set. |
| 79 |
*/ |
| 80 |
public static final int REVERT_ON_ESC = 1<<3; |
| 81 |
|
| 82 |
/** |
| 50 |
* {@link Text} widget that this is being observed. |
83 |
* {@link Text} widget that this is being observed. |
| 51 |
*/ |
84 |
*/ |
| 52 |
private final Text text; |
85 |
private final Text text; |
|
Lines 59-74
Link Here
|
| 59 |
private boolean updating = false; |
92 |
private boolean updating = false; |
| 60 |
|
93 |
|
| 61 |
/** |
94 |
/** |
| 62 |
* SWT event that on firing this observable will fire change events to its |
95 |
* Observable style for specifying whether Enter should be handled, |
| 63 |
* listeners. |
96 |
* whether to use FocusOut or Modify as the time of updating, and whether |
|
|
97 |
* to support Escape to undo edits. |
| 64 |
*/ |
98 |
*/ |
| 65 |
private int updateEventType; |
99 |
private int observableStyle; |
| 66 |
|
100 |
|
| 67 |
/** |
101 |
/** |
| 68 |
* Valid types for the {@link #updateEventType}. |
102 |
* SWT event that on firing this observable will fire change events to its |
|
|
103 |
* listeners. |
| 69 |
*/ |
104 |
*/ |
| 70 |
private static final int[] validUpdateEventTypes = new int[] { SWT.Modify, |
105 |
private int updateEventType; |
| 71 |
SWT.FocusOut, SWT.NONE }; |
|
|
| 72 |
|
106 |
|
| 73 |
/** |
107 |
/** |
| 74 |
* Last value set using doSetValue(), or null. This is maintained so that |
108 |
* Last value set using doSetValue(), or null. This is maintained so that |
|
Lines 112-139
Link Here
|
| 112 |
* <code>updateEventType</code>. |
146 |
* <code>updateEventType</code>. |
| 113 |
* |
147 |
* |
| 114 |
* @param text |
148 |
* @param text |
| 115 |
* @param updateEventType |
149 |
* @param observableStyle |
| 116 |
* SWT event constant as to what SWT event to update the model in |
150 |
* SWT event constant as to what SWT event to update the model in |
| 117 |
* response to. Appropriate values are: <code>SWT.Modify</code>, |
151 |
* response to. Appropriate values are: <code>UPDATE_ON_MODIFY</code>, |
| 118 |
* <code>SWT.FocusOut</code>, <code>SWT.NONE</code>. |
152 |
* <code>SWT.FocusOut</code>, <code>UPDATE_ON_ENTER</code>, |
|
|
153 |
* <code>NONE</code>. |
| 119 |
* @throws IllegalArgumentException |
154 |
* @throws IllegalArgumentException |
| 120 |
* if <code>updateEventType</code> is an incorrect type. |
155 |
* if <code>updateEventType</code> is an incorrect type. |
| 121 |
*/ |
156 |
*/ |
| 122 |
public TextObservableValue(final Text text, int updateEventType) { |
157 |
public TextObservableValue(final Text text, final int observableStyle) { |
| 123 |
super(text); |
158 |
super(text); |
| 124 |
boolean eventValid = false; |
159 |
this.text = text; |
| 125 |
for (int i = 0; !eventValid && i < validUpdateEventTypes.length; i++) { |
160 |
this.observableStyle = observableStyle; |
| 126 |
eventValid = (updateEventType == validUpdateEventTypes[i]); |
161 |
if (observableStyleContains(UPDATE_ON_MODIFY)) { |
| 127 |
} |
162 |
updateEventType = SWT.Modify; |
| 128 |
if (!eventValid) { |
163 |
} else if (observableStyleContains(UPDATE_ON_FOCUS_OUT)) { |
|
|
164 |
updateEventType = SWT.FocusOut; |
| 165 |
// If equal to NONE after filtering remaining styles |
| 166 |
} else if ((observableStyle & |
| 167 |
~UPDATE_ON_ENTER & |
| 168 |
~REVERT_ON_ESC) == NONE) { |
| 169 |
updateEventType = SWT.NONE; |
| 170 |
} else { |
| 129 |
throw new IllegalArgumentException( |
171 |
throw new IllegalArgumentException( |
| 130 |
"UpdateEventType [" + updateEventType + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ |
172 |
"Invalid ObservableStyle was supplied."); //$NON-NLS-1$ |
| 131 |
} |
173 |
} |
| 132 |
this.text = text; |
174 |
|
| 133 |
this.updateEventType = updateEventType; |
175 |
if (updateEventType != SWT.NONE) { |
| 134 |
if (updateEventType != SWT.None) { |
|
|
| 135 |
text.addListener(updateEventType, updateListener); |
176 |
text.addListener(updateEventType, updateListener); |
| 136 |
} |
177 |
} |
|
|
178 |
|
| 137 |
// If the update policy is SWT.Modify then the model is notified of |
179 |
// If the update policy is SWT.Modify then the model is notified of |
| 138 |
// changed on key stroke by key stroke |
180 |
// changed on key stroke by key stroke |
| 139 |
// When escape is pressed we need to rollback to the previous value |
181 |
// When escape is pressed we need to rollback to the previous value |
|
Lines 149-154
Link Here
|
| 149 |
} |
191 |
} |
| 150 |
}); |
192 |
}); |
| 151 |
} |
193 |
} |
|
|
194 |
|
| 152 |
verifyListener = new VerifyListener() { |
195 |
verifyListener = new VerifyListener() { |
| 153 |
public void verifyText(VerifyEvent e) { |
196 |
public void verifyText(VerifyEvent e) { |
| 154 |
if (!updating) { |
197 |
if (!updating) { |
|
Lines 164-181
Link Here
|
| 164 |
} |
207 |
} |
| 165 |
}; |
208 |
}; |
| 166 |
text.addVerifyListener(verifyListener); |
209 |
text.addVerifyListener(verifyListener); |
|
|
210 |
|
| 167 |
keyListener = new KeyListener() { |
211 |
keyListener = new KeyListener() { |
| 168 |
public void keyPressed(KeyEvent e) { |
212 |
public void keyPressed(KeyEvent e) { |
| 169 |
if (e.character == SWT.ESC && bufferedValue != null) { |
213 |
|
|
|
214 |
// If character is escape |
| 215 |
if ((observableStyleContains(REVERT_ON_ESC)) && |
| 216 |
(e.character == SWT.ESC) && |
| 217 |
(bufferedValue != null)) { |
| 170 |
// Revert the value in the text field to the model value |
218 |
// Revert the value in the text field to the model value |
| 171 |
text.setText(bufferedValue); |
219 |
text.setText(bufferedValue); |
| 172 |
} |
220 |
} |
|
|
221 |
|
| 222 |
// If character is carriage return |
| 223 |
String oldValue = bufferedValue; |
| 224 |
String newValue = text.getText(); |
| 225 |
if ((observableStyleContains(UPDATE_ON_ENTER)) && |
| 226 |
(e.character == SWT.CR)) { |
| 227 |
bufferedValue = text.getText(); |
| 228 |
if (!newValue.equals(oldValue)) { |
| 229 |
// Fire value change event |
| 230 |
fireValueChange(Diffs.createValueDiff(oldValue, |
| 231 |
newValue)); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 173 |
} |
235 |
} |
| 174 |
|
236 |
|
| 175 |
public void keyReleased(KeyEvent e) { |
237 |
public void keyReleased(KeyEvent e) { |
| 176 |
} |
238 |
} |
| 177 |
}; |
239 |
}; |
| 178 |
text.addKeyListener(keyListener); |
240 |
text.addKeyListener(keyListener); |
|
|
241 |
|
| 179 |
shellListener = new ShellAdapter() { |
242 |
shellListener = new ShellAdapter() { |
| 180 |
public void shellClosed(ShellEvent e) { |
243 |
public void shellClosed(ShellEvent e) { |
| 181 |
if (!text.isDisposed()) { |
244 |
if (!text.isDisposed()) { |
|
Lines 241-244
Link Here
|
| 241 |
} |
304 |
} |
| 242 |
super.dispose(); |
305 |
super.dispose(); |
| 243 |
} |
306 |
} |
|
|
307 |
|
| 308 |
/** |
| 309 |
* Indicates if observableStyle contains a particular SWT style |
| 310 |
* @param style SWT style |
| 311 |
* @return true if observableStyle contains SWT style or false otherwise |
| 312 |
*/ |
| 313 |
private boolean observableStyleContains(int style) { |
| 314 |
return (observableStyle & style) == style; |
| 315 |
} |
| 244 |
} |
316 |
} |