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());
}
}
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));
}
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());
}
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());
}
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());
}
Aggregations