Search in sources :

Example 1 with Adventure

use of org.eclipse.jface.examples.databinding.model.Adventure 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 2 with Adventure

use of org.eclipse.jface.examples.databinding.model.Adventure 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 3 with Adventure

use of org.eclipse.jface.examples.databinding.model.Adventure 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 4 with Adventure

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

the class CustomScenarios method testScenario01.

@Test
public void testScenario01() {
    // Binding the name property of an Adventure object to the contents of
    // Text controls, no conversion, no validation.
    Adventure adventure = SampleData.WINTER_HOLIDAY;
    Text text = new Text(getComposite(), SWT.BORDER);
    IObservableValue descriptionObservable = BeansObservables.observeValue(adventure, "description");
    IObservableValue nameObservable = BeansObservables.observeValue(adventure, "name");
    AggregateObservableValue customObservable_comma = new AggregateObservableValue(new IObservableValue[] { descriptionObservable, nameObservable }, ",");
    getDbc().bindValue(SWTObservables.observeText(text, SWT.Modify), customObservable_comma);
    // spinEventLoop(1);
    // Make sure that the description on the model match the widget
    assertEquals(adventure.getDescription() + "," + adventure.getName(), text.getText());
    // Change the widget to newDescription,newName and ensure the model is
    // updated
    text.setText("newDescription,newName");
    assertEquals("newDescription", adventure.getDescription());
    assertEquals("newName", adventure.getName());
    // Change the model to newDescription_0 and newName_0 and ensure the GUI
    // is updated
    adventure.setDescription("newDescription_0");
    adventure.setName("newName_0");
    assertEquals("newDescription_0,newName_0", text.getText());
    // Change text to newDescription_1 with no comma and ensure the model is
    // updated correctly with no name
    text.setText("newDescription_1");
    assertEquals("newDescription_1", adventure.getDescription());
    assertEquals(null, adventure.getName());
// Change text to newName with a preceeding comma and ensure the model
// is updated correctly with no description
// TODO - Get this test working + Add the one where we have two
// aggregates and update one and
// check that the other is updated - currently this fails on the GUI -
// JRW
// text.setText(",newName_1");
// assertEquals(null, adventure.getDescription());
// assertEquals("newName_1", adventure.getName());
}
Also used : AggregateObservableValue(org.eclipse.jface.examples.databinding.model.AggregateObservableValue) Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Text(org.eclipse.swt.widgets.Text) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) Test(org.junit.Test)

Example 5 with Adventure

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

the class PropertyScenarios method testScenario06.

@Test
public void testScenario06() {
    // Binding the name property of an Adventure object to the contents of
    // Text controls where validation occurs and the name cannot be longer
    // than 15 characters and cannot contain spaces
    Text text = new Text(getComposite(), SWT.BORDER);
    final String noSpacesMessage = "Name must not contain spaces.";
    final String max15CharactersMessage = "Maximum length for name is 15 characters.";
    adventure.setName("ValidValue");
    IValidator validator = value -> {
        String stringValue = (String) value;
        if (stringValue.length() > 15) {
            return ValidationStatus.error(max15CharactersMessage);
        } else if (stringValue.indexOf(' ') != -1) {
            return ValidationStatus.cancel(noSpacesMessage);
        } else {
            return Status.OK_STATUS;
        }
    };
    // BindSpec bindSpec = new DefaultBindSpec().setModelToTargetConverter(new IdentityConverter(String.class))
    // .setTargetToModelConverter(new IdentityConverter(String.class))
    // .addTargetValidator(BindingEvent.PIPELINE_VALUE_CHANGING, validator);
    Binding binding = getDbc().bindValue(SWTObservables.observeText(text, SWT.Modify), BeansObservables.observeValue(adventure, "name"), new UpdateValueStrategy().setConverter(new IdentityConverter(String.class)).setAfterGetValidator(validator), new UpdateValueStrategy().setConverter(new IdentityConverter(String.class)));
    // no validation message
    assertTrue(binding.getValidationStatus().getValue().isOK());
    enterText(text, "Invalid Value");
    assertEquals(noSpacesMessage, binding.getValidationStatus().getValue().getMessage());
    assertEquals("ValidValue", adventure.getName());
    text.setText("InvalidValueBecauseTooLong");
    assertEquals(max15CharactersMessage, binding.getValidationStatus().getValue().getMessage());
    assertEquals("ValidValue", adventure.getName());
    enterText(text, "anothervalid");
    assertTrue(binding.getValidationStatus().getValue().isOK());
    assertEquals("anothervalid", adventure.getName());
}
Also used : Converter(org.eclipse.core.databinding.conversion.Converter) Date(java.util.Date) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) Spinner(org.eclipse.swt.widgets.Spinner) FocusEvent(org.eclipse.swt.events.FocusEvent) IdentityConverter(org.eclipse.core.internal.databinding.conversion.IdentityConverter) NumberFormat(java.text.NumberFormat) FocusListener(org.eclipse.swt.events.FocusListener) Event(org.eclipse.swt.widgets.Event) NumberToStringConverter(org.eclipse.core.databinding.conversion.text.NumberToStringConverter) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IConverter(org.eclipse.core.databinding.conversion.IConverter) Locale(java.util.Locale) After(org.junit.After) BeansObservables(org.eclipse.core.databinding.beans.BeansObservables) SampleData(org.eclipse.jface.examples.databinding.model.SampleData) AggregateValidationStatus(org.eclipse.core.databinding.AggregateValidationStatus) ParseException(java.text.ParseException) Before(org.junit.Before) SWTObservables(org.eclipse.jface.databinding.swt.SWTObservables) Text(org.eclipse.swt.widgets.Text) Button(org.eclipse.swt.widgets.Button) Assert.assertTrue(org.junit.Assert.assertTrue) Status(org.eclipse.core.runtime.Status) StringToNumberConverter(org.eclipse.core.databinding.conversion.text.StringToNumberConverter) Test(org.junit.Test) Account(org.eclipse.jface.examples.databinding.model.Account) Binding(org.eclipse.core.databinding.Binding) Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) Cart(org.eclipse.jface.examples.databinding.model.Cart) Assert.assertEquals(org.junit.Assert.assertEquals) Binding(org.eclipse.core.databinding.Binding) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) IdentityConverter(org.eclipse.core.internal.databinding.conversion.IdentityConverter) Text(org.eclipse.swt.widgets.Text) Test(org.junit.Test)

Aggregations

Adventure (org.eclipse.jface.examples.databinding.model.Adventure)9 Test (org.junit.Test)9 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)6 Text (org.eclipse.swt.widgets.Text)5 Lodging (org.eclipse.jface.examples.databinding.model.Lodging)4 NumberFormat (java.text.NumberFormat)3 ParseException (java.text.ParseException)3 Date (java.util.Date)3 Locale (java.util.Locale)3 AggregateValidationStatus (org.eclipse.core.databinding.AggregateValidationStatus)3 Binding (org.eclipse.core.databinding.Binding)3 UpdateValueStrategy (org.eclipse.core.databinding.UpdateValueStrategy)3 BeansObservables (org.eclipse.core.databinding.beans.BeansObservables)3 Converter (org.eclipse.core.databinding.conversion.Converter)3 IConverter (org.eclipse.core.databinding.conversion.IConverter)3 NumberToStringConverter (org.eclipse.core.databinding.conversion.text.NumberToStringConverter)3 StringToNumberConverter (org.eclipse.core.databinding.conversion.text.StringToNumberConverter)3 IValidator (org.eclipse.core.databinding.validation.IValidator)3 ValidationStatus (org.eclipse.core.databinding.validation.ValidationStatus)3 IdentityConverter (org.eclipse.core.internal.databinding.conversion.IdentityConverter)3