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