use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.
the class MasterDetailScenarios method testScenario01.
@Test
public void testScenario01() {
// Displaying the catalog's list of Lodging objects in a list viewer,
// using their names. The name of the currently selected Lodging can
// be edited in a text widget. There is always a selected Lodging
// object.
ListViewer listViewer = new ListViewer(getComposite(), SWT.BORDER);
Realm realm = DisplayRealm.getRealm(listViewer.getControl().getDisplay());
listViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
Catalog catalog = SampleData.CATALOG_2005;
IObservableList lodgings = BeansObservables.observeList(realm, catalog, "lodgings");
ViewerSupport.bind(listViewer, lodgings, BeanProperties.value(Lodging.class, "name"));
assertArrayEquals(catalog.getLodgings(), getViewerContent(listViewer).toArray());
IObservableValue selectedLodging = ViewersObservables.observeSingleSelection(listViewer);
selectedLodging.setValue(SampleData.CAMP_GROUND);
assertEquals(SampleData.CAMP_GROUND, getViewerSelection(listViewer));
Text txtName = new Text(getComposite(), SWT.BORDER);
getDbc().bindValue(SWTObservables.observeText(txtName, SWT.Modify), BeansObservables.observeDetailValue(selectedLodging, "name", String.class));
assertEquals(txtName.getText(), SampleData.CAMP_GROUND.getName());
enterText(txtName, "foobar");
assertEquals("foobar", SampleData.CAMP_GROUND.getName());
listViewer.setSelection(new StructuredSelection(SampleData.FIVE_STAR_HOTEL));
assertEquals(SampleData.FIVE_STAR_HOTEL, selectedLodging.getValue());
assertEquals(SampleData.FIVE_STAR_HOTEL.getName(), txtName.getText());
SampleData.FIVE_STAR_HOTEL.setName("barfoo");
assertEquals("barfoo", txtName.getText());
// Now make sure that the event listeners get removed on dispose()
// Values should no longer be updated
selectedLodging.dispose();
// selectedLodging.setValue(SampleData.CAMP_GROUND);
// assertNotSame(SampleData.CAMP_GROUND,
// getViewerSelection(listViewer));
// assertNotSame(txtName.getText(), SampleData.CAMP_GROUND.getName());
// enterText(txtName, "foobar");
// assertNotSame("foobar", SampleData.CAMP_GROUND.getName());
// listViewer.setSelection(new StructuredSelection(
// SampleData.FIVE_STAR_HOTEL));
// assertNotSame(SampleData.FIVE_STAR_HOTEL,
// selectedLodging.getValue());
// assertNotSame(SampleData.FIVE_STAR_HOTEL.getName(),
// txtName.getText());
// SampleData.FIVE_STAR_HOTEL.setName("barfoo");
// assertNotSame("barfoo", txtName.getText());
}
use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.
the class ListViewerScenario method testScenario01.
@Test
public void testScenario01() {
// Bind the catalog's lodgings to the combo
IObservableList lodgings = BeansObservables.observeList(Realm.getDefault(), catalog, "lodgings");
ViewerSupport.bind(listViewer, lodgings, BeanProperties.value(Lodging.class, "name"));
// Verify that the combo's items are the lodgings
for (int i = 0; i < catalog.getLodgings().length; i++) {
assertEquals(catalog.getLodgings()[i], listViewer.getElementAt(i));
}
// Verify that the String being shown in the list viewer is the
// "toString" of the combo viewer
String[] lodgingStrings = new String[catalog.getLodgings().length];
for (int i = 0; i < catalog.getLodgings().length; i++) {
lodgingStrings[i] = catalog.getLodgings()[i].getName();
}
assertArrayEquals(lodgingStrings, list.getItems());
// Verify that the list has no selected item
assertEquals(null, listViewer.getStructuredSelection().getFirstElement());
// Now bind the selection of the combo to the "defaultLodging" property
// of an adventure
final Adventure adventure = SampleData.WINTER_HOLIDAY;
IObservableValue selection = ViewersObservables.observeSingleSelection(listViewer);
getDbc().bindValue(selection, BeansObservables.observeValue(adventure, "defaultLodging"));
// Verify that the list selection is the default lodging
assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
// Change the model and verify that the list selection changes
adventure.setDefaultLodging(SampleData.CAMP_GROUND);
assertEquals(adventure.getDefaultLodging(), SampleData.CAMP_GROUND);
assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
// Change the list selection and verify that the model changes
listViewer.getList().select(3);
assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
adventure.setDefaultLodging(SampleData.YOUTH_HOSTEL);
spinEventLoop(0);
assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
}
Aggregations