001: /*******************************************************************************
002: * Copyright (c) 2005, 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: * Brad Reynolds - bug 160000
012: *******************************************************************************/package org.eclipse.jface.tests.databinding.scenarios;
013:
014: import org.eclipse.core.databinding.beans.BeansObservables;
015: import org.eclipse.core.databinding.observable.list.IObservableList;
016: import org.eclipse.core.databinding.observable.map.IObservableMap;
017: import org.eclipse.core.databinding.observable.value.IObservableValue;
018: import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
019: import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
020: import org.eclipse.jface.databinding.viewers.ViewersObservables;
021: import org.eclipse.jface.examples.databinding.model.Adventure;
022: import org.eclipse.jface.examples.databinding.model.Catalog;
023: import org.eclipse.jface.examples.databinding.model.Lodging;
024: import org.eclipse.jface.examples.databinding.model.SampleData;
025: import org.eclipse.jface.viewers.ComboViewer;
026: import org.eclipse.jface.viewers.IStructuredSelection;
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.widgets.Combo;
029:
030: /**
031: * To run the tests in this class, right-click and select "Run As JUnit Plug-in
032: * Test". This will also start an Eclipse instance. To clean up the launch
033: * configuration, open up its "Main" tab and select "[No Application] - Headless
034: * Mode" as the application to run.
035: */
036:
037: public class ComboViewerScenario extends ScenariosTestCase {
038:
039: private Catalog catalog;
040:
041: private Combo combo;
042:
043: private ComboViewer comboViewer;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: // do any setup work here
048: combo = new Combo(getComposite(), SWT.READ_ONLY | SWT.DROP_DOWN);
049: comboViewer = new ComboViewer(combo);
050: catalog = SampleData.CATALOG_2005; // Lodging source
051: }
052:
053: protected void tearDown() throws Exception {
054: combo.dispose();
055: combo = null;
056: comboViewer = null;
057: super .tearDown();
058: }
059:
060: public void testScenario01() {
061: // Bind the catalog's lodgings to the combo
062: IObservableList lodgings = BeansObservables.observeList(realm,
063: catalog, "lodgings");
064: ObservableListContentProvider contentProvider = new ObservableListContentProvider();
065:
066: IObservableMap attributeMap = BeansObservables.observeMap(
067: contentProvider.getKnownElements(), Lodging.class,
068: "name");
069: comboViewer.setLabelProvider(new ObservableMapLabelProvider(
070: attributeMap));
071: comboViewer.setContentProvider(contentProvider);
072: comboViewer.setInput(lodgings);
073:
074: // Verify that the combo's items are the lodgings
075: for (int i = 0; i < catalog.getLodgings().length; i++) {
076: assertEquals(catalog.getLodgings()[i], comboViewer
077: .getElementAt(i));
078: }
079: // Verify that the String being shown in the combo viewer is the
080: // "toString" of the combo viewer
081: String[] lodgingStrings = new String[catalog.getLodgings().length];
082: for (int i = 0; i < catalog.getLodgings().length; i++) {
083: lodgingStrings[i] = catalog.getLodgings()[i].getName();
084: }
085: assertArrayEquals(lodgingStrings, combo.getItems());
086:
087: // Verify that the combo has no selected item
088: assertEquals(null, ((IStructuredSelection) comboViewer
089: .getSelection()).getFirstElement());
090:
091: // Now bind the selection of the combo to the "defaultLodging" property
092: // of an adventure
093: final Adventure adventure = SampleData.WINTER_HOLIDAY;
094: IObservableValue selection = ViewersObservables
095: .observeSingleSelection(comboViewer);
096: getDbc().bindValue(
097: selection,
098: BeansObservables.observeValue(adventure,
099: "defaultLodging"), null, null);
100:
101: // Verify that the combo selection is the default lodging
102: assertEquals(
103: ((IStructuredSelection) comboViewer.getSelection())
104: .getFirstElement(), adventure
105: .getDefaultLodging());
106:
107: // Change the model and verify that the combo selection changes
108: adventure.setDefaultLodging(SampleData.CAMP_GROUND);
109: assertEquals(adventure.getDefaultLodging(),
110: SampleData.CAMP_GROUND);
111: assertEquals(
112: ((IStructuredSelection) comboViewer.getSelection())
113: .getFirstElement(), adventure
114: .getDefaultLodging());
115:
116: // Change the combo selection and verify that the model changes
117: comboViewer.getCombo().select(3);
118: assertEquals(
119: ((IStructuredSelection) comboViewer.getSelection())
120: .getFirstElement(), adventure
121: .getDefaultLodging());
122:
123: adventure.setDefaultLodging(SampleData.YOUTH_HOSTEL);
124: spinEventLoop(0);
125: assertEquals(
126: ((IStructuredSelection) comboViewer.getSelection())
127: .getFirstElement(), adventure
128: .getDefaultLodging());
129: }
130:
131: }
|