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 131435 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/tests/databinding/swt/SWTObservablesTest.java (-2 / +16 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 Brad Reynolds and others.
2
 * Copyright (c) 2007, 2008 Brad Reynolds and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.jface.tests.databinding.swt;
13
package org.eclipse.jface.tests.databinding.swt;
Lines 27-32 Link Here
27
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
28
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
28
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.StyledTextObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
34
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
Lines 34-39 Link Here
34
import org.eclipse.swt.SWT;
36
import org.eclipse.swt.SWT;
35
import org.eclipse.swt.custom.CCombo;
37
import org.eclipse.swt.custom.CCombo;
36
import org.eclipse.swt.custom.CLabel;
38
import org.eclipse.swt.custom.CLabel;
39
import org.eclipse.swt.custom.StyledText;
37
import org.eclipse.swt.graphics.Color;
40
import org.eclipse.swt.graphics.Color;
38
import org.eclipse.swt.graphics.Font;
41
import org.eclipse.swt.graphics.Font;
39
import org.eclipse.swt.widgets.Button;
42
import org.eclipse.swt.widgets.Button;
Lines 146-152 Link Here
146
		} catch (IllegalArgumentException e) {
149
		} catch (IllegalArgumentException e) {
147
		}
150
		}
148
	}
151
	}
149
152
	
150
	public void testObserveTextOfText() throws Exception {
153
	public void testObserveTextOfText() throws Exception {
151
		Text text = new Text(shell, SWT.NONE);
154
		Text text = new Text(shell, SWT.NONE);
152
		assertFalse(text.isListening(SWT.FocusOut));
155
		assertFalse(text.isListening(SWT.FocusOut));
Lines 157-162 Link Here
157
		assertTrue(value instanceof TextObservableValue);
160
		assertTrue(value instanceof TextObservableValue);
158
		assertTrue(text.isListening(SWT.FocusOut));
161
		assertTrue(text.isListening(SWT.FocusOut));
159
	}
162
	}
163
	
164
	public void testObserveTextOfStyledText() throws Exception {
165
		StyledText text = new StyledText(shell, SWT.NONE);
166
		assertFalse(text.isListening(SWT.FocusOut));
167
168
		ISWTObservableValue value = SWTObservables.observeText(text,
169
				SWT.FocusOut);
170
		assertNotNull(value);
171
		assertTrue(value instanceof StyledTextObservableValue);
172
		assertTrue(text.isListening(SWT.FocusOut));
173
	}
160
174
161
	public void testObserveTextWithEventOfUnsupportedControl() throws Exception {
175
	public void testObserveTextWithEventOfUnsupportedControl() throws Exception {
162
		Label label = new Label(shell, SWT.NONE);
176
		Label label = new Label(shell, SWT.NONE);
(-)src/org/eclipse/jface/tests/databinding/BindingTestSuite.java (-1 / +8 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005-2008 IBM Corporation and others.
2
 * Copyright (c) 2005, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 15-20 Link Here
15
 *                    212518, 215531, 221351, 184830, 213145, 218269, 239015,
15
 *                    212518, 215531, 221351, 184830, 213145, 218269, 239015,
16
 *                    237703, 237718
16
 *                    237703, 237718
17
 *     Ovidio Mallo - bug 237163, bug 235195
17
 *     Ovidio Mallo - bug 237163, bug 235195
18
 *     Chris Aniszczyk - bug 131435
18
 *******************************************************************************/
19
 *******************************************************************************/
19
package org.eclipse.jface.tests.databinding;
20
package org.eclipse.jface.tests.databinding;
20
21
Lines 163-168 Link Here
163
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueMinTest;
164
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueMinTest;
164
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueSelectionTest;
165
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueSelectionTest;
165
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueTest;
166
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueTest;
167
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueFocusOutTest;
168
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueModifyTest;
169
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueTest;
166
import org.eclipse.jface.tests.internal.databinding.swt.TableObservableValueTest;
170
import org.eclipse.jface.tests.internal.databinding.swt.TableObservableValueTest;
167
import org.eclipse.jface.tests.internal.databinding.swt.TableSingleSelectionObservableValueTest;
171
import org.eclipse.jface.tests.internal.databinding.swt.TableSingleSelectionObservableValueTest;
168
import org.eclipse.jface.tests.internal.databinding.swt.TextEditableObservableValueTest;
172
import org.eclipse.jface.tests.internal.databinding.swt.TextEditableObservableValueTest;
Lines 386-391 Link Here
386
		addTest(TextObservableValueFocusOutTest.suite());
390
		addTest(TextObservableValueFocusOutTest.suite());
387
		addTest(TextObservableValueModifyTest.suite());
391
		addTest(TextObservableValueModifyTest.suite());
388
		addTestSuite(TextObservableValueTest.class);
392
		addTestSuite(TextObservableValueTest.class);
393
		addTest(StyledTextObservableValueFocusOutTest.suite());
394
		addTest(StyledTextObservableValueModifyTest.suite());
395
		addTestSuite(StyledTextObservableValueTest.class);
389
		
396
		
390
		//org.eclipse.jface.tests.internal.databinding.internal.viewers
397
		//org.eclipse.jface.tests.internal.databinding.internal.viewers
391
		addTest(ObservableViewerElementSetTest.suite());
398
		addTest(ObservableViewerElementSetTest.suite());
(-)src/org/eclipse/jface/tests/internal/databinding/swt/StyledTextObservableValueTest.java (+102 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
16
import org.eclipse.jface.internal.databinding.swt.StyledTextObservableValue;
17
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.custom.StyledText;
20
import org.eclipse.swt.widgets.Shell;
21
22
/**
23
 * Tests to assert the inputs of the StyledTextObservableValue constructor.
24
 */
25
public class StyledTextObservableValueTest extends AbstractDefaultRealmTestCase {
26
	private StyledText text;
27
	private ValueChangeEventTracker listener;
28
29
	protected void setUp() throws Exception {
30
		super.setUp();
31
32
		Shell shell = new Shell();
33
		text = new StyledText(shell, SWT.NONE);
34
		
35
		listener = new ValueChangeEventTracker();
36
	}
37
38
	/**
39
	 * Asserts that only valid SWT event types are accepted on construction of
40
	 * StyledTextObservableValue.
41
	 */
42
	public void testConstructorUpdateEventTypes() {
43
		try {
44
			new StyledTextObservableValue(text, SWT.NONE);
45
			new StyledTextObservableValue(text, SWT.FocusOut);
46
			new StyledTextObservableValue(text, SWT.Modify);
47
			assertTrue(true);
48
		} catch (IllegalArgumentException e) {
49
			fail();
50
		}
51
52
		try {
53
			new StyledTextObservableValue(text, SWT.Verify);
54
			fail();
55
		} catch (IllegalArgumentException e) {
56
			assertTrue(true);
57
		}
58
	}
59
	
60
	/**
61
	 * @throws Exception
62
	 */
63
	public void testGetValueBeforeFocusOutChangeEventsFire() throws Exception {
64
		StyledTextObservableValue observableValue = new StyledTextObservableValue(text, SWT.FocusOut);
65
		observableValue.addValueChangeListener(listener);
66
		
67
		String a = "a";
68
		String b = "b";
69
		
70
		text.setText(a);
71
		assertEquals(a, observableValue.getValue()); //fetch the value updating the buffered value
72
		
73
		text.setText(b);
74
		text.notifyListeners(SWT.FocusOut, null);
75
		
76
		assertEquals(1, listener.count);
77
		assertEquals(a, listener.event.diff.getOldValue());
78
		assertEquals(b, listener.event.diff.getNewValue());
79
	}
80
81
	public void testDispose() throws Exception {
82
		StyledTextObservableValue observableValue = new StyledTextObservableValue(text,
83
				SWT.Modify);
84
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
85
		observableValue.addValueChangeListener(testCounterValueChangeListener);
86
87
		String expected1 = "Test123";
88
		text.setText(expected1);
89
90
		assertEquals(1, testCounterValueChangeListener.count);
91
		assertEquals(expected1, text.getText());
92
		assertEquals(expected1, observableValue.getValue());
93
94
		observableValue.dispose();
95
96
		String expected2 = "NewValue123";
97
		text.setText(expected2);
98
99
		assertEquals(1, testCounterValueChangeListener.count);
100
		assertEquals(expected2, text.getText());
101
	}
102
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/StyledTextObservableValueModifyTest.java (+74 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import junit.framework.Test;
16
import junit.framework.TestCase;
17
import junit.framework.TestSuite;
18
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.StyledTextObservableValue;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.custom.StyledText;
27
import org.eclipse.swt.widgets.Shell;
28
29
/**
30
 * Tests for the Modify version of StyledTextObservableValue.
31
 */
32
public class StyledTextObservableValueModifyTest extends TestCase {
33
	public static Test suite() {
34
		TestSuite suite = new TestSuite(StyledTextObservableValueModifyTest.class.toString());
35
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
36
		return suite;
37
	}
38
39
	/* package */static class Delegate extends
40
			AbstractObservableValueContractDelegate {
41
		private Shell shell;
42
43
		private StyledText text;
44
		
45
		public void setUp() {
46
			shell = new Shell();
47
			text = new StyledText(shell, SWT.NONE);
48
		}
49
50
		public void tearDown() {
51
			shell.dispose();
52
		}
53
54
		public IObservableValue createObservableValue(Realm realm) {
55
			return new StyledTextObservableValue(realm, text, SWT.Modify);
56
		}
57
58
		public Object getValueType(IObservableValue observable) {
59
			return String.class;
60
		}
61
62
		public void change(IObservable observable) {
63
			text.setFocus();
64
			
65
			IObservableValue observableValue = (IObservableValue) observable;
66
			text.setText((String) createValue(observableValue));
67
		}
68
69
		public Object createValue(IObservableValue observable) {
70
			String value = (String) observable.getValue();
71
			return value + "a";
72
		}
73
	}
74
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/StyledTextObservableValueFocusOutTest.java (+76 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import junit.framework.Test;
16
import junit.framework.TestCase;
17
import junit.framework.TestSuite;
18
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.StyledTextObservableValue;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.custom.StyledText;
27
import org.eclipse.swt.widgets.Shell;
28
29
/**
30
 * Tests for the FocusOut version of StyledTextObservableValue.
31
 */
32
public class StyledTextObservableValueFocusOutTest extends TestCase {
33
	public static Test suite() {
34
		TestSuite suite = new TestSuite(StyledTextObservableValueFocusOutTest.class.toString());
35
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
36
		return suite;
37
	}
38
39
	/* package */static class Delegate extends
40
			AbstractObservableValueContractDelegate {
41
		private Shell shell;
42
43
		private StyledText text;
44
		
45
		public void setUp() {
46
			shell = new Shell();
47
			text = new StyledText(shell, SWT.NONE);
48
		}
49
50
		public void tearDown() {
51
			shell.dispose();
52
		}
53
54
		public IObservableValue createObservableValue(Realm realm) {
55
			return new StyledTextObservableValue(realm, text, SWT.FocusOut);
56
		}
57
58
		public Object getValueType(IObservableValue observable) {
59
			return String.class;
60
		}
61
62
		public void change(IObservable observable) {
63
			text.setFocus();
64
			
65
			IObservableValue observableValue = (IObservableValue) observable;
66
			text.setText((String) createValue(observableValue));
67
			
68
			text.notifyListeners(SWT.FocusOut, null);
69
		}
70
71
		public Object createValue(IObservableValue observable) {
72
			String value = (String) observable.getValue();
73
			return value + "a";
74
		}
75
	}
76
}
(-)src/org/eclipse/jface/databinding/swt/SWTObservables.java (-2 / +6 lines)
Lines 19-24 Link Here
19
19
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.list.IObservableList;
21
import org.eclipse.core.databinding.observable.list.IObservableList;
22
import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue;
22
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
23
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
23
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
24
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
24
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
25
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
Lines 26-32 Link Here
26
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
28
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
28
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
29
import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
Lines 38-48 Link Here
38
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
38
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
39
import org.eclipse.jface.internal.databinding.swt.ShellObservableValue;
39
import org.eclipse.jface.internal.databinding.swt.ShellObservableValue;
40
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
40
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
41
import org.eclipse.jface.internal.databinding.swt.StyledTextObservableValue;
41
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
42
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
42
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
43
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
43
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
44
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
44
import org.eclipse.swt.custom.CCombo;
45
import org.eclipse.swt.custom.CCombo;
45
import org.eclipse.swt.custom.CLabel;
46
import org.eclipse.swt.custom.CLabel;
47
import org.eclipse.swt.custom.StyledText;
46
import org.eclipse.swt.widgets.Button;
48
import org.eclipse.swt.widgets.Button;
47
import org.eclipse.swt.widgets.Combo;
49
import org.eclipse.swt.widgets.Combo;
48
import org.eclipse.swt.widgets.Control;
50
import org.eclipse.swt.widgets.Control;
Lines 230-238 Link Here
230
	 * <code>control</code>. The supported types are:
232
	 * <code>control</code>. The supported types are:
231
	 * <ul>
233
	 * <ul>
232
	 * <li>org.eclipse.swt.widgets.Text</li>
234
	 * <li>org.eclipse.swt.widgets.Text</li>
235
	 * <li>org.eclipse.swt.custom.StyledText (as of 1.3)</li>
233
	 * </ul>
236
	 * </ul>
234
	 * 
237
	 * 
235
	 * <li>org.eclipse.swt.widgets.Label</li>
236
	 * @param control
238
	 * @param control
237
	 * @param event event type to register for change events
239
	 * @param event event type to register for change events
238
	 * @return observable value
240
	 * @return observable value
Lines 242-247 Link Here
242
	public static ISWTObservableValue observeText(Control control, int event) {
244
	public static ISWTObservableValue observeText(Control control, int event) {
243
		if (control instanceof Text) {
245
		if (control instanceof Text) {
244
			return new TextObservableValue((Text) control, event);
246
			return new TextObservableValue((Text) control, event);
247
		} else if (control instanceof StyledText) {
248
			return new StyledTextObservableValue((StyledText) control, event);
245
		}
249
		}
246
250
247
		throw new IllegalArgumentException(
251
		throw new IllegalArgumentException(
(-)src/org/eclipse/jface/internal/databinding/swt/StyledTextObservableValue.java (+197 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *******************************************************************************/
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.IObservable;
16
import org.eclipse.core.databinding.observable.Realm;
17
import org.eclipse.jface.databinding.swt.SWTObservables;
18
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTVetoableValue;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.custom.StyledText;
21
import org.eclipse.swt.events.VerifyEvent;
22
import org.eclipse.swt.events.VerifyListener;
23
import org.eclipse.swt.widgets.Event;
24
import org.eclipse.swt.widgets.Listener;
25
26
/**
27
 * {@link IObservable} implementation that wraps a {@link StyledText} widget. The time
28
 * at which listeners should be notified about changes to the text is specified
29
 * on construction.
30
 * 
31
 * <dl>
32
 * <dt>Events:</dt>
33
 * <dd> If the update event type (specified on construction) is
34
 * <code>SWT.Modify</code> a value change event will be fired on every key
35
 * stroke. If the update event type is <code>SWT.FocusOut</code> a value
36
 * change event will be fired on focus out. When in either mode if the user is
37
 * entering text and presses [Escape] the value will be reverted back to the
38
 * last value set using doSetValue(). Regardless of the update event type a
39
 * value changing event will fire on verify to enable vetoing of changes.</dd>
40
 * </dl>
41
 * 
42
 * @since 1.3
43
 */
44
public class StyledTextObservableValue extends AbstractSWTVetoableValue {
45
46
	/**
47
	 * {@link StyledText} widget that this is being observed.
48
	 */
49
	private final StyledText text;
50
51
	/**
52
	 * Flag to track when the model is updating the widget. When
53
	 * <code>true</code> the handlers for the SWT events should not process
54
	 * the event as this would cause an infinite loop.
55
	 */
56
	private boolean updating = false;
57
58
	/**
59
	 * SWT event that on firing this observable will fire change events to its
60
	 * listeners.
61
	 */
62
	private final int updateEventType;
63
64
	/**
65
	 * Valid types for the {@link #updateEventType}.
66
	 */
67
	private static final int[] validUpdateEventTypes = new int[] { SWT.Modify,
68
			SWT.FocusOut, SWT.None };
69
70
	/**
71
	 * Previous value of the Text.
72
	 */
73
	private String oldValue;
74
75
	private Listener updateListener = new Listener() {
76
		public void handleEvent(Event event) {
77
			if (!updating) {
78
				String newValue = text.getText();
79
80
				if (!newValue.equals(oldValue)) {
81
					fireValueChange(Diffs.createValueDiff(oldValue, newValue));					
82
					oldValue = newValue;
83
				}
84
			}
85
		}
86
	};
87
88
	private VerifyListener verifyListener;
89
90
	/**
91
	 * Constructs a new instance bound to the given <code>StyledText</code> widget
92
	 * and configured to fire change events to its listeners at the time of the
93
	 * <code>updateEventType</code>.
94
	 * 
95
	 * @param text
96
	 * @param updateEventType
97
	 *            SWT event constant as to what SWT event to update the model in
98
	 *            response to. Appropriate values are: <code>SWT.Modify</code>,
99
	 *            <code>SWT.FocusOut</code>, <code>SWT.None</code>.
100
	 * @throws IllegalArgumentException
101
	 *             if <code>updateEventType</code> is an incorrect type.
102
	 */
103
	public StyledTextObservableValue(final StyledText text, int updateEventType) {
104
		this(SWTObservables.getRealm(text.getDisplay()), text, updateEventType);
105
	}
106
	
107
	/**
108
	 * Constructs a new instance.
109
	 * 
110
	 * @param realm can not be <code>null</code>
111
	 * @param text
112
	 * @param updateEventType
113
	 */
114
	public StyledTextObservableValue(final Realm realm, StyledText text, int updateEventType) {
115
		super(realm, text);
116
		
117
		boolean eventValid = false;
118
		for (int i = 0; !eventValid && i < validUpdateEventTypes.length; i++) {
119
			eventValid = (updateEventType == validUpdateEventTypes[i]);
120
		}
121
		if (!eventValid) {
122
			throw new IllegalArgumentException(
123
					"UpdateEventType [" + updateEventType + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
124
		}
125
		this.text = text;
126
		this.updateEventType = updateEventType;
127
		if (updateEventType != SWT.None) {
128
			text.addListener(updateEventType, updateListener);
129
		}
130
		
131
		oldValue = text.getText();
132
		
133
		verifyListener = new VerifyListener() {
134
			public void verifyText(VerifyEvent e) {
135
				if (!updating) {
136
					String currentText = StyledTextObservableValue.this.text
137
							.getText();
138
					String newText = currentText.substring(0, e.start) + e.text
139
							+ currentText.substring(e.end);
140
					if (!fireValueChanging(Diffs.createValueDiff(currentText,
141
							newText))) {
142
						e.doit = false;
143
					}
144
				}
145
			}
146
		};
147
		text.addVerifyListener(verifyListener);
148
	}
149
150
	/**
151
	 * Sets the bound {@link StyledText Text's} text to the passed <code>value</code>.
152
	 * 
153
	 * @param value
154
	 *            new value, String expected
155
	 * @see org.eclipse.core.databinding.observable.value.AbstractVetoableValue#doSetApprovedValue(java.lang.Object)
156
	 * @throws ClassCastException
157
	 *             if the value is anything other than a String
158
	 */
159
	protected void doSetApprovedValue(final Object value) {
160
		try {
161
			updating = true;
162
			text.setText(value == null ? "" : value.toString()); //$NON-NLS-1$
163
			oldValue = text.getText();
164
		} finally {
165
			updating = false;
166
		}
167
	}
168
169
	/**
170
	 * Returns the current value of the {@link StyledText}.
171
	 * 
172
	 * @see org.eclipse.core.databinding.observable.value.AbstractVetoableValue#doGetValue()
173
	 */
174
	public Object doGetValue() {
175
		return oldValue = text.getText();
176
	}
177
178
	/**
179
	 * Returns the type of the value from {@link #doGetValue()}, i.e.
180
	 * String.class
181
	 * 
182
	 * @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType()
183
	 */
184
	public Object getValueType() {
185
		return String.class;
186
	}
187
188
	public void dispose() {
189
		if (!text.isDisposed()) {
190
			if (updateEventType != SWT.None) {
191
				text.removeListener(updateEventType, updateListener);
192
			}
193
			text.removeVerifyListener(verifyListener);
194
		}
195
		super.dispose();
196
	}
197
}

Return to bug 131435