Search in sources :

Example 6 with Lodging

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());
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) GridData(org.eclipse.swt.layout.GridData) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Lodging(org.eclipse.jface.examples.databinding.model.Lodging) Text(org.eclipse.swt.widgets.Text) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) Realm(org.eclipse.core.databinding.observable.Realm) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm) Catalog(org.eclipse.jface.examples.databinding.model.Catalog) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) Test(org.junit.Test)

Example 7 with Lodging

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());
}
Also used : Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Lodging(org.eclipse.jface.examples.databinding.model.Lodging) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) Test(org.junit.Test)

Aggregations

Lodging (org.eclipse.jface.examples.databinding.model.Lodging)7 Test (org.junit.Test)7 Adventure (org.eclipse.jface.examples.databinding.model.Adventure)4 Catalog (org.eclipse.jface.examples.databinding.model.Catalog)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 IObservableList (org.eclipse.core.databinding.observable.list.IObservableList)3 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 ListViewer (org.eclipse.jface.viewers.ListViewer)2 GridData (org.eclipse.swt.layout.GridData)2 Text (org.eclipse.swt.widgets.Text)2 Realm (org.eclipse.core.databinding.observable.Realm)1 ComputedValue (org.eclipse.core.databinding.observable.value.ComputedValue)1 DisplayRealm (org.eclipse.jface.databinding.swt.DisplayRealm)1 ComboViewer (org.eclipse.jface.viewers.ComboViewer)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 SelectionListener (org.eclipse.swt.events.SelectionListener)1 Button (org.eclipse.swt.widgets.Button)1