Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 156264 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/databinding/swt/SWTObservables.java (-3 / +3 lines)
Lines 86-96 Link Here
86
86
87
	/**
87
	/**
88
	 * @param text
88
	 * @param text
89
	 * @param event
89
	 * @param observableStyle as specified in {@link TextObservableValue}
90
	 * @return
90
	 * @return
91
	 */
91
	 */
92
	public static ISWTObservableValue getText(Text text, int event) {
92
	public static ISWTObservableValue getText(Text text, int observableStyle) {
93
		return new TextObservableValue(text, event);
93
		return new TextObservableValue(text, observableStyle);
94
	}
94
	}
95
95
96
	/**
96
	/**
(-)src/org/eclipse/jface/internal/databinding/internal/swt/TextObservableValue.java (-23 / +67 lines)
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.NONE;
127
		}
142
		}
128
		if (!eventValid) {
143
		else {
129
			throw new IllegalArgumentException(
144
			throw new IllegalArgumentException(
130
					"UpdateEventType [" + updateEventType + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
145
					"Invalid ObservableStyle was supplied."); //$NON-NLS-1$
131
		}
146
		}
132
		this.text = text;
147
		
133
		this.updateEventType = updateEventType;
134
		if (updateEventType != SWT.None) {
148
		if (updateEventType != SWT.None) {
135
			text.addListener(updateEventType, updateListener);
149
			text.addListener(updateEventType, updateListener);
136
		}
150
		}
151
		
137
		// If the update policy is SWT.Modify then the model is notified of
152
		// If the update policy is SWT.Modify then the model is notified of
138
		// changed on key stroke by key stroke
153
		// changed on key stroke by key stroke
139
		// When escape is pressed we need to rollback to the previous value
154
		// When escape is pressed we need to rollback to the previous value
Lines 149-154 Link Here
149
				}
164
				}
150
			});
165
			});
151
		}
166
		}
167
		
152
		verifyListener = new VerifyListener() {
168
		verifyListener = new VerifyListener() {
153
			public void verifyText(VerifyEvent e) {
169
			public void verifyText(VerifyEvent e) {
154
				if (!updating) {
170
				if (!updating) {
Lines 164-181 Link Here
164
			}
180
			}
165
		};
181
		};
166
		text.addVerifyListener(verifyListener);
182
		text.addVerifyListener(verifyListener);
183
		
167
		keyListener = new KeyListener() {
184
		keyListener = new KeyListener() {
168
			public void keyPressed(KeyEvent e) {
185
			public void keyPressed(KeyEvent e) {
169
				if (e.character == SWT.ESC && bufferedValue != null) {
186
				
187
				// If character is escape
188
				if ((e.character == SWT.ESC) && (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 (((observableStyle == SWT.NONE) || 
197
                		(observableStyleContains(SWT.CR))) && 
198
            		(e.character == SWT.CR)) {
199
                		bufferedValue = text.getText();
200
                        if (!newValue.equals(oldValue)) {
201
                        		// Propagate text field value to the model
202
                                fireValueChange(Diffs.createValueDiff(oldValue,
203
                                                newValue));
204
                        }
205
206
                }
173
			}
207
			}
174
208
175
			public void keyReleased(KeyEvent e) {
209
			public void keyReleased(KeyEvent e) {
176
			}
210
			}
177
		};
211
		};
178
		text.addKeyListener(keyListener);
212
		text.addKeyListener(keyListener);
213
		
179
		shellListener = new ShellAdapter() {
214
		shellListener = new ShellAdapter() {
180
			public void shellClosed(ShellEvent e) {
215
			public void shellClosed(ShellEvent e) {
181
				if (!text.isDisposed()) {
216
				if (!text.isDisposed()) {
Lines 241-244 Link Here
241
		}
276
		}
242
		super.dispose();
277
		super.dispose();
243
	}
278
	}
279
	
280
	/**
281
	 * Indicates if observableStyle contains a particular SWT style
282
	 * @param style SWT style
283
	 * @return true if observableStyle contains SWT style or false otherwise
284
	 */
285
	private boolean observableStyleContains(int style) {
286
		return (observableStyle & style) == style;
287
	}
244
}
288
}

Return to bug 156264