001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 116920
011: *******************************************************************************/package org.eclipse.jface.tests.databinding.scenarios;
012:
013: import java.beans.PropertyChangeListener;
014: import java.beans.PropertyChangeSupport;
015: import java.util.ArrayList;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.eclipse.core.databinding.beans.BeansObservables;
020: import org.eclipse.core.databinding.observable.list.IObservableList;
021: import org.eclipse.core.databinding.observable.list.WritableList;
022: import org.eclipse.jface.databinding.swt.SWTObservables;
023: import org.eclipse.jface.tests.databinding.BindingTestSuite;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.layout.FillLayout;
026: import org.eclipse.swt.widgets.Combo;
027:
028: /**
029: * @since 3.2
030: */
031: public class ComboUpdatingTest extends ScenariosTestCase {
032:
033: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
034: this );
035:
036: public void addPropertyChangeListener(
037: PropertyChangeListener listener) {
038: propertyChangeSupport.addPropertyChangeListener(listener);
039: }
040:
041: public void addPropertyChangeListener(String propertyName,
042: PropertyChangeListener listener) {
043: propertyChangeSupport.addPropertyChangeListener(propertyName,
044: listener);
045: }
046:
047: public void removePropertyChangeListener(
048: PropertyChangeListener listener) {
049: propertyChangeSupport.removePropertyChangeListener(listener);
050: }
051:
052: public void removePropertyChangeListener(String propertyName,
053: PropertyChangeListener listener) {
054: propertyChangeSupport.removePropertyChangeListener(
055: propertyName, listener);
056: }
057:
058: protected void firePropertyChange(String propertyName,
059: Object oldValue, Object newValue) {
060: propertyChangeSupport.firePropertyChange(propertyName,
061: oldValue, newValue);
062: }
063:
064: private Combo comboEditable;
065: //private Combo comboReadOnly;
066:
067: private static final String PROP_TEXT = "text";
068: private String text = "Hello, world";
069:
070: public String getText() {
071: return text;
072: }
073:
074: public void setText(String text) {
075: this .text = text;
076: }
077:
078: private static final String PROP_CHOICES = "choices";
079: private List choices = new ArrayList();
080: {
081: choices.add("Banana");
082: choices.add("Apple");
083: choices.add("Mango");
084: }
085:
086: public List getChoices() {
087: return choices;
088: }
089:
090: public void setChoices(List choices) {
091: this .choices = choices;
092: firePropertyChange(PROP_CHOICES, null, null);
093: }
094:
095: protected void setUp() throws Exception {
096: super .setUp();
097: getComposite().setLayout(new FillLayout());
098: comboEditable = new Combo(getComposite(), SWT.DROP_DOWN);
099: // comboReadOnly = new Combo(getComposite(), SWT.DROP_DOWN | SWT.READ_ONLY);
100: }
101:
102: //-------------------------------------------------------------------------
103:
104: private static final String NEXT = "Next";
105:
106: public void testBindText() throws Exception {
107: getDbc()
108: .bindValue(SWTObservables.observeText(comboEditable),
109: BeansObservables.observeValue(this , "text"),
110: null, null);
111: spinEventLoop(0);
112: assertEquals("Should find value of text", text, comboEditable
113: .getText());
114: comboEditable.setText(NEXT);
115: spinEventLoop(0);
116: assertEquals("Should find new value in text", NEXT, text);
117: }
118:
119: public void testBindItems_listHasSameItems_editable()
120: throws Exception {
121: if (BindingTestSuite.failingTestsDisabled(this )) {
122: return;
123: }
124: text = "Apple";
125:
126: getDbc().bindValue(SWTObservables.observeText(comboEditable),
127: BeansObservables.observeValue(this , PROP_TEXT), null,
128: null);
129:
130: spinEventLoop(0);
131: assertEquals("Should find value of text", text, comboEditable
132: .getText());
133:
134: IObservableList list = new WritableList(getChoices(), null);
135: getDbc().bindList(SWTObservables.observeItems(comboEditable),
136: list, null, null);
137:
138: spinEventLoop(0);
139: int position = 0;
140: for (Iterator choicesIter = choices.iterator(); choicesIter
141: .hasNext();) {
142: String element = (String) choicesIter.next();
143: assertEquals(element, comboEditable.getItem(position));
144: ++position;
145: }
146: // assertEquals("Should find value of text", "Apple", text);
147: assertEquals("Should find value of combo.getText()", "",
148: comboEditable.getText());
149: comboEditable.setText("Banana");
150: spinEventLoop(0);
151: assertEquals("Should find value of text", "Banana", text);
152: }
153:
154: // public void testBindItems_listHasSameItems_readOnly() throws Exception {
155: // text = "Apple";
156: // ComboObservableValue value = (ComboObservableValue) getDbc().createObservable(new Property(comboReadOnly, PROP_TEXT));
157: // getDbc().bind(value.getItems(), new Property(this, PROP_CHOICES), null);
158: //// getDbc().bind(combo, new Property(this, PROP_CHOICES, String.class, Boolean.TRUE), null);
159: // spinEventLoop(0);
160: // assertEquals("Should find value of text", "Apple", text);
161: // getDbc().bind(value, new Property(this, PROP_TEXT), null);
162: // spinEventLoop(0);
163: // assertEquals("Should find value of text", "Apple", text);
164: // assertEquals("Should find value of combo.getText()", "Apple", comboReadOnly.getText());
165: // int position = 0;
166: // for (Iterator choicesIter = choices.iterator(); choicesIter.hasNext();) {
167: // String element = (String) choicesIter.next();
168: // assertEquals(element, comboReadOnly.getItem(position));
169: // ++position;
170: // }
171: // assertEquals("Should find value of text", "Apple", text);
172: // assertEquals("Should find value of combo.getText()", "Apple", comboReadOnly.getText());
173: // comboReadOnly.setText("Banana");
174: // spinEventLoop(0);
175: // assertEquals("Should find value of text", "Banana", text);
176: // }
177:
178: public void testBindItems_listHasDifferentItems_editable()
179: throws Exception {
180: if (BindingTestSuite.failingTestsDisabled(this )) {
181: return;
182: }
183:
184: getDbc().bindValue(SWTObservables.observeText(comboEditable),
185: BeansObservables.observeValue(this , PROP_TEXT), null,
186: null);
187:
188: spinEventLoop(0);
189: assertEquals("Should find value of text", text, comboEditable
190: .getText());
191:
192: IObservableList list = new WritableList(new ArrayList(),
193: String.class);
194: list.addAll(getChoices());
195: getDbc().bindList(SWTObservables.observeItems(comboEditable),
196: list, null, null);
197:
198: spinEventLoop(0);
199: int position = 0;
200: for (Iterator choicesIter = choices.iterator(); choicesIter
201: .hasNext();) {
202: String element = (String) choicesIter.next();
203: assertEquals(element, comboEditable.getItem(position));
204: ++position;
205: }
206: // assertEquals("Should find value of text", "Hello, world", text);
207: assertEquals("Should find value of combo.getText()", "",
208: comboEditable.getText());
209: comboEditable.setText("Banana");
210: spinEventLoop(0);
211: assertEquals("Should find value of text", "Banana", text);
212: }
213:
214: // public void testBindItems_listHasDifferentItems_readOnly() throws Exception {
215: // ComboObservableValue value = (ComboObservableValue) getDbc().createObservable(new Property(comboReadOnly, PROP_TEXT));
216: // getDbc().bind(value, new Property(this, PROP_TEXT), null);
217: // spinEventLoop(0);
218: // getDbc().bind(value.getItems(), new Property(this, PROP_CHOICES), null);
219: //// getDbc().bind(combo, new Property(this, PROP_CHOICES, String.class, Boolean.TRUE), null);
220: // spinEventLoop(0);
221: // int position = 0;
222: // for (Iterator choicesIter = choices.iterator(); choicesIter.hasNext();) {
223: // String element = (String) choicesIter.next();
224: // assertEquals(element, comboReadOnly.getItem(position));
225: // ++position;
226: // }
227: //// assertEquals("Should find value of text", "Hello, world", text);
228: // assertEquals("Should find value of combo.getText()", "", comboReadOnly.getText());
229: // comboReadOnly.setText("Banana");
230: // spinEventLoop(0);
231: // assertEquals("Should find value of text", "Banana", text);
232: // }
233:
234: }
|