Search in sources :

Example 1 with Lodging

use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.

the class ComboScenarios method test_ROCombo_Scenario03_collectionBindings.

/**
 * This test case deal with the 3rd scenario, and focuses on the collection
 * binding to the combo. It will bind a collection, add/remove/change
 * elements in the collection, and change element's properties to ensure
 * that the combo's labels were updated appropriatly.
 *
 * it also induce null values in properties, and elments.
 *
 * This test does not deal with the combo's selection.
 */
@Test
public void test_ROCombo_Scenario03_collectionBindings() {
    // column binding
    // Bind the ComboViewer's content to the available lodging
    IObservableList<Lodging> lodgings = BeanProperties.<Catalog, Lodging>list("lodgings").observe(Realm.getDefault(), catalog);
    ViewerSupport.bind(cviewer, lodgings, BeanProperties.value(Lodging.class, "name"));
    // Ensure that cv's content now has the catalog's lodgings
    assertArrayEquals(catalog.getLodgings(), getViewerContent(cviewer).toArray());
    // Ensure that the cv's labels are the same as the lodging descriptions
    assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    // Add a lodging in the middle (not supported by the model right now)
    // Lodging lodging = SampleData.FACTORY.createLodging();
    // lodging.setName("Middle Lodging");
    // catalog.addLodging(lodging);
    // assertEquals(getViewerContent(cviewer).get(2), lodging);
    // Add a lodging at the end
    Lodging lodging = SampleData.FACTORY.createLodging();
    lodging.setName("End Lodging");
    catalog.addLodging(lodging);
    int index = getComboContent().size() - 1;
    assertEquals(getViewerContent(cviewer).get(index), lodging);
    // Delete the first Lodging
    catalog.removeLodging(catalog.getLodgings()[0]);
    // Ensure that the cv's labels are the same as the lodging descriptions
    assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    // Delete middle Lodging
    catalog.removeLodging(catalog.getLodgings()[2]);
    // Ensure that the cv's labels are the same as the lodging descriptions
    assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    // Change the names of all Lodging
    for (int i = 0; i < catalog.getLodgings().length; i++) {
        Lodging l = catalog.getLodgings()[i];
        l.setName("Changed: " + l.getName());
    }
    // force Async. efforts
    spinEventLoop(0);
    assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    // Set to null value
    Lodging l = catalog.getLodgings()[0];
    assertEquals(combo.getItem(0), l.getName());
    l.setName(null);
    assertEquals("", combo.getItem(0));
    // set to empty list
    while (catalog.getLodgings().length > 0) {
        catalog.removeLodging(catalog.getLodgings()[0]);
        assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    }
}
Also used : Lodging(org.eclipse.jface.examples.databinding.model.Lodging) Test(org.junit.Test)

Example 2 with Lodging

use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.

the class ComboScenarios method test_ROCombo_Scenario03_vanilla.

/**
 * This test case deal with the 3rd scenario, using vanilla bindings: Ensure
 * a valid content and selection are bounded correctly Bind a collection of
 * Lodgings to a ComboViewer Bind the ComboViewer's selection to the
 * defaultLodging of an Adventure
 *
 * This test does not deal with null values, empty content, changed content,
 * property change of content elements, etc.
 */
@Test
public void test_ROCombo_Scenario03_vanilla() {
    IObservableList<Lodging> lodgings = BeanProperties.list(Catalog.class, "lodgings", Lodging.class).observe(Realm.getDefault(), catalog);
    ViewerSupport.bind(cviewer, lodgings, BeanProperties.value(Lodging.class, "name"));
    // selection will
    Adventure skiAdventure = SampleData.WINTER_HOLIDAY;
    // Ensure that cv's content now has the catalog's lodgings
    assertArrayEquals(catalog.getLodgings(), getViewerContent(cviewer).toArray());
    // Ensure that the cv's labels are the same as the lodging descriptions
    assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    getDbc().bindValue(ViewerProperties.singleSelection().observe(cviewer), BeanProperties.value("defaultLodging").observe(skiAdventure));
    // Check to see that the initial selection is the currentDefault Lodging
    assertEquals(getViewerSelection(), skiAdventure.getDefaultLodging());
    // verify that skiAdventure's default lodging was changed accordingly
    for (int i = 0; i < catalog.getLodgings().length; i++) {
        Object selection = catalog.getLodgings()[i];
        cviewer.setSelection(new StructuredSelection(selection));
        assertEquals(selection, skiAdventure.getDefaultLodging());
        assertEquals(getViewerSelection(), skiAdventure.getDefaultLodging());
    }
}
Also used : Adventure(org.eclipse.jface.examples.databinding.model.Adventure) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Lodging(org.eclipse.jface.examples.databinding.model.Lodging) Catalog(org.eclipse.jface.examples.databinding.model.Catalog) Test(org.junit.Test)

Example 3 with Lodging

use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.

the class ComboScenarios method test_ROCombo_multipleBindings.

/**
 * This scenario tests a simple SWT combo with a set item list where the
 * selection is bouded to a String property
 */
// @Test
// public void test_ROCombo_Scenario01() {
// 
// // Read-Only Combo will not change its text property on a call to
// // setText()
// 
// String[] items = new String[] { "FairyLand", "TuneLand", "NoWereLand",
// "TinkerLand", "DreamLand" };
// combo.setItems(items);
// Account account = catalog.getAccounts()[0];
// 
// // simple Combo's selection bound to the Account's country property
// getDbc().bind(new Property(combo, SWTProperties.SELECTION),
// new Property(account, "country"), null);
// 
// // Drive the combo selection
// int index = 3;
// combo.setText(items[index]); // this should drive the selection
// assertEquals(account.getCountry(), items[index]);
// 
// // Set the country, and ensure selection is set property
// index = 1;
// account.setCountry(items[index]);
// assertEquals(index, combo.getSelectionIndex());
// assertEquals(combo.getText(), items[index]);
// 
// index = combo.getSelectionIndex();
// String txt = combo.getText();
// // Set the country to something that is not in the Combo's list
// account.setCountry("FooBar");
// // Combo's selection will not Change
// assertEquals(combo.getSelectionIndex(), index);
// assertEquals(combo.getText(), txt);
// 
// }
/**
 * This scenario tests a simple SWT combo that is bound to a list of Country
 * objects. The Country object's name property is listed in the Combo.
 *
 * The Combo's selection is bounded to the Country property of an Account.
 */
// @Test
// public void test_ROCombo_Scenario02_SWTCombo() {
// 
// // Create a list of Strings for the countries
// IObservableList list = new WritableList();
// for (int i = 0; i < catalog.getAccounts().length; i++)
// list.add(catalog.getAccounts()[i].getCountry());
// 
// // Bind the combo's content to that of the String based list
// getDbc().bind(combo, list, null);
// assertEquals(Arrays.asList(combo.getItems()), list);
// 
// Account account = catalog.getAccounts()[0];
// 
// // simple Combo's selection bound to the Account's country property
// getDbc().bind(new Property(combo, SWTProperties.SELECTION),
// new Property(account, "country"), null);
// 
// // Drive the combo selection
// String selection = (String) list.get(2);
// combo.setText(selection); // this should drive the selection
// assertEquals(account.getCountry(), selection);
// 
// }
/**
 * This scenario tests a simple SWT combo that is bound to a list of Country
 * objects. The Country object's name property is listed in the Combo.
 *
 * The Combo's selection is bounded to the Country property of an Account.
 */
// @Test
// public void test_ROCombo_Scenario02_ComboViewer() {
// 
// // Account label provider will fill the combo with the country
// cviewer.setLabelProvider(accountLabelProvider);
// // Bind the ComboViewer's content to the available accounts
// getDbc().bind(
// cviewer,
// new ListModelDescription(new Property(catalog, "accounts"),
// "country"), null);
// 
// // Ensure that cv's content now has the catalog's accounts
// assertArrayEquals(catalog.getAccounts(), getViewerContent(cviewer)
// .toArray());
// // Ensure that the cv's labels are the same as the account countries
// assertEquals(getColumn(catalog.getAccounts(), "country"),
// getComboContent());
// 
// Account account = SampleData.FACTORY.createAccount();
// 
// // Use the Viewers visual Combo (Strings) to set the account's country
// getDbc().bind(new Property(combo, SWTProperties.SELECTION),
// new Property(account, "country"), null);
// 
// // Change the selection of the ComboViewer to all possible accounts, and
// // verify that the account's Country is being changed correctly.
// for (int i = 0; i < catalog.getAccounts().length; i++) {
// Account selection = catalog.getAccounts()[i];
// cviewer.setSelection(new StructuredSelection(selection));
// assertEquals(selection.getCountry(), account.getCountry());
// }
// 
// }
/**
 * This test ensure that multiple combos can be bound to the same deomain
 * model
 */
@Test
public void test_ROCombo_multipleBindings() {
    // for selection
    Adventure skiAdventure = SampleData.WINTER_HOLIDAY;
    // Bind the ComboViewer's content to the available lodging
    IObservableList<Lodging> lodgings = BeanProperties.<Catalog, Lodging>list("lodgings").observe(Realm.getDefault(), catalog);
    ViewerSupport.bind(cviewer, lodgings, BeanProperties.value(Lodging.class, "name"));
    // Ensure that cv's content now has the catalog's lodgings
    assertArrayEquals(catalog.getLodgings(), getViewerContent(cviewer).toArray());
    // Ensure that the cv's labels are the same as the lodging descriptions
    assertEquals(getColumn(catalog.getLodgings(), "name"), getComboContent());
    ComboViewer otherViewer = new ComboViewer(getComposite(), SWT.NONE);
    ViewerSupport.bind(otherViewer, lodgings, BeanProperties.value(Lodging.class, "name"));
    // Ensure that cv's content now has the catalog's lodgings
    assertArrayEquals(catalog.getLodgings(), getViewerContent(otherViewer).toArray());
    // Bind both selections to the same thing
    IObservableValue<Object> selection = ViewerProperties.singleSelection().observe(cviewer);
    getDbc().bindValue(selection, BeanProperties.value("defaultLodging").observe(skiAdventure));
    IObservableValue<Object> otherSelection = ViewerProperties.singleSelection().observe(otherViewer);
    getDbc().bindValue(otherSelection, BeanProperties.value("defaultLodging").observe(skiAdventure));
    Lodging lodging = catalog.getLodgings()[0];
    // Ensure that setting the selection is driven forward to the other
    // combo
    cviewer.setSelection(new StructuredSelection(lodging));
    assertEquals(cviewer.getStructuredSelection().getFirstElement(), otherViewer.getStructuredSelection().getFirstElement());
    // Change the list of one combo, and ensure it updates the other combo
    catalog.removeLodging(lodging);
    assertEquals(getViewerContent(cviewer), getViewerContent(otherViewer));
}
Also used : Adventure(org.eclipse.jface.examples.databinding.model.Adventure) ComboViewer(org.eclipse.jface.viewers.ComboViewer) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Lodging(org.eclipse.jface.examples.databinding.model.Lodging) Test(org.junit.Test)

Example 4 with Lodging

use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.

the class ComboViewerScenario method testScenario01.

@Test
public void testScenario01() {
    // Bind the catalog's lodgings to the combo
    IObservableList<Lodging> lodgings = BeanProperties.list(Catalog.class, "lodgings", Lodging.class).observe(realm, catalog);
    ViewerSupport.bind(comboViewer, 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], comboViewer.getElementAt(i));
    }
    // Verify that the String being shown in the combo 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, combo.getItems());
    // Verify that the combo has no selected item
    assertEquals(null, comboViewer.getStructuredSelection().getFirstElement());
    // Now bind the selection of the combo to the "defaultLodging" property
    // of an adventure
    final Adventure adventure = SampleData.WINTER_HOLIDAY;
    IObservableValue<Lodging> selection = ViewerProperties.singleSelection(Lodging.class).observe(comboViewer);
    getDbc().bindValue(selection, BeanProperties.value("defaultLodging").observe(adventure));
    // Verify that the combo selection is the default lodging
    assertEquals(comboViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
    // Change the model and verify that the combo selection changes
    adventure.setDefaultLodging(SampleData.CAMP_GROUND);
    assertEquals(adventure.getDefaultLodging(), SampleData.CAMP_GROUND);
    assertEquals(comboViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
    // Change the combo selection and verify that the model changes
    comboViewer.getCombo().select(3);
    assertEquals(comboViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
    adventure.setDefaultLodging(SampleData.YOUTH_HOSTEL);
    spinEventLoop(0);
    assertEquals(comboViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
}
Also used : Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Lodging(org.eclipse.jface.examples.databinding.model.Lodging) Catalog(org.eclipse.jface.examples.databinding.model.Catalog) Test(org.junit.Test)

Example 5 with Lodging

use of org.eclipse.jface.examples.databinding.model.Lodging in project eclipse.platform.ui by eclipse-platform.

the class MasterDetailScenarios method testScenario02.

@Test
public void testScenario02() {
    // Selecting from the list of lodgings for an adventure and editing the
    // properties of the selected lodging in text widgets. If no lodging is
    // selected the input controls for name and adventure are disabled.
    // There are two buttons "Add" and "Remove"; clicking on "Add" creates a
    // new lodging and selects it so it can be edited, clicking on "Remove"
    // removes the currently selected lodging from the list.
    final ListViewer listViewer = new ListViewer(getComposite(), SWT.BORDER);
    listViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
    final 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());
    final IObservableValue selectedLodgingObservable = ViewersObservables.observeSingleSelection(listViewer);
    selectedLodgingObservable.setValue(null);
    assertTrue(listViewer.getStructuredSelection().isEmpty());
    ComputedValue selectionExistsObservable = new ComputedValue(boolean.class) {

        @Override
        protected Object calculate() {
            return Boolean.valueOf(selectedLodgingObservable.getValue() != null);
        }
    };
    assertFalse(((Boolean) selectionExistsObservable.getValue()).booleanValue());
    final Text txtName = new Text(getComposite(), SWT.BORDER);
    getDbc().bindValue(SWTObservables.observeEnabled(txtName), selectionExistsObservable);
    getDbc().bindValue(SWTObservables.observeText(txtName, SWT.Modify), BeansObservables.observeDetailValue(selectedLodgingObservable, "name", String.class));
    assertEquals(txtName.getText(), "");
    assertFalse(txtName.getEnabled());
    final Text txtDescription = new Text(getComposite(), SWT.BORDER);
    getDbc().bindValue(SWTObservables.observeEnabled(txtDescription), selectionExistsObservable);
    getDbc().bindValue(SWTObservables.observeText(txtDescription, SWT.Modify), MasterDetailObservables.detailValue(selectedLodgingObservable, BeansObservables.valueFactory(realm, "description"), String.class));
    assertEquals(txtDescription.getText(), "");
    assertFalse(txtDescription.getEnabled());
    Button addButton = new Button(getComposite(), SWT.PUSH);
    addButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Lodging selectedLodging = (Lodging) selectedLodgingObservable.getValue();
            int insertionIndex = 0;
            if (selectedLodging != null) {
                insertionIndex = Arrays.asList(catalog.getLodgings()).indexOf(selectedLodging);
                assertTrue(insertionIndex >= 0);
            }
            Lodging newLodging = SampleData.FACTORY.createLodging();
            int itemCount = listViewer.getList().getItemCount();
            newLodging.setName("new lodging name " + itemCount);
            newLodging.setDescription("new lodging description " + itemCount);
            catalog.addLodging(newLodging);
            assertEquals(itemCount + 1, listViewer.getList().getItemCount());
            listViewer.setSelection(new StructuredSelection(newLodging));
            assertSame(newLodging, selectedLodgingObservable.getValue());
            assertTrue(txtName.getEnabled());
            assertTrue(txtDescription.getEnabled());
            assertEquals(newLodging.getName(), txtName.getText());
            assertEquals(newLodging.getDescription(), txtDescription.getText());
        }
    });
    Button removeButton = new Button(getComposite(), SWT.PUSH);
    removeButton.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Lodging selectedLodging = (Lodging) selectedLodgingObservable.getValue();
            assertNotNull(selectedLodging);
            int deletionIndex = Arrays.asList(catalog.getLodgings()).indexOf(selectedLodging);
            assertTrue(deletionIndex >= 0);
            int itemCount = listViewer.getList().getItemCount();
            catalog.removeLodging(selectedLodging);
            assertEquals(itemCount - 1, listViewer.getList().getItemCount());
            assertNull(selectedLodgingObservable.getValue());
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }
    });
    pushButtonWithEvents(addButton);
    pushButtonWithEvents(removeButton);
    pushButtonWithEvents(addButton);
    pushButtonWithEvents(addButton);
    pushButtonWithEvents(removeButton);
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) ComputedValue(org.eclipse.core.databinding.observable.value.ComputedValue) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) 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) Catalog(org.eclipse.jface.examples.databinding.model.Catalog) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) SelectionListener(org.eclipse.swt.events.SelectionListener) 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