use of org.eclipse.jface.examples.databinding.model.Catalog 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.Catalog 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.Catalog 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);
}
use of org.eclipse.jface.examples.databinding.model.Catalog 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.Catalog in project eclipse.platform.ui by eclipse-platform.
the class MasterDetailScenarios method testScenario03.
@Test
public void testScenario03() {
// List adventures and for the selected adventure allow its default
// lodging�s name and description to be changed in text controls. If
// there is no selected adventure or the default lodging is null the
// text controls are disabled. This is a nested property. The default
// lodging can be changed elsewhere, and the list
final Catalog catalog = SampleData.CATALOG_2005;
final ListViewer categoryListViewer = new ListViewer(getComposite(), SWT.BORDER);
categoryListViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
IObservableList categories = BeansObservables.observeList(realm, catalog, "categories");
ViewerSupport.bind(categoryListViewer, categories, BeanProperties.value(Category.class, "name"));
assertArrayEquals(catalog.getCategories(), getViewerContent(categoryListViewer).toArray());
final IObservableValue selectedCategoryObservable = ViewersObservables.observeSingleSelection(categoryListViewer);
final ListViewer adventureListViewer = new ListViewer(getComposite(), SWT.BORDER);
adventureListViewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
IObservableList adventures = BeansObservables.observeDetailList(selectedCategoryObservable, "adventures", Adventure.class);
ViewerSupport.bind(adventureListViewer, adventures, BeanProperties.value(Adventure.class, "name"));
ComputedValue categorySelectionExistsObservable = new ComputedValue() {
@Override
protected Object calculate() {
return Boolean.valueOf(selectedCategoryObservable.getValue() != null);
}
};
getDbc().bindValue(SWTObservables.observeEnabled(adventureListViewer.getList()), categorySelectionExistsObservable);
final IObservableValue selectedAdventureObservable = ViewersObservables.observeSingleSelection(adventureListViewer);
ComputedValue adventureSelectionExistsObservable = new ComputedValue() {
@Override
protected Object calculate() {
return Boolean.valueOf(selectedAdventureObservable.getValue() != null);
}
};
final Text txtName = new Text(getComposite(), SWT.BORDER);
getDbc().bindValue(SWTObservables.observeEnabled(txtName), adventureSelectionExistsObservable);
getDbc().bindValue(SWTObservables.observeText(txtName, SWT.Modify), BeansObservables.observeDetailValue(selectedAdventureObservable, "name", String.class));
assertEquals(txtName.getText(), "");
assertFalse(txtName.getEnabled());
final Text txtDescription = new Text(getComposite(), SWT.BORDER);
getDbc().bindValue(SWTObservables.observeEnabled(txtDescription), adventureSelectionExistsObservable);
getDbc().bindValue(SWTObservables.observeText(txtDescription, SWT.Modify), BeansObservables.observeDetailValue(selectedAdventureObservable, "description", String.class));
assertFalse(adventureListViewer.getList().isEnabled());
categoryListViewer.setSelection(new StructuredSelection(SampleData.SUMMER_CATEGORY));
assertTrue(adventureListViewer.getList().isEnabled());
assertFalse(txtName.getEnabled());
adventureListViewer.setSelection(new StructuredSelection(SampleData.RAFTING_HOLIDAY));
assertEquals(Boolean.TRUE, adventureSelectionExistsObservable.getValue());
assertTrue(txtName.getEnabled());
assertEquals(SampleData.RAFTING_HOLIDAY.getName(), txtName.getText());
categoryListViewer.setSelection(new StructuredSelection(SampleData.WINTER_CATEGORY));
assertTrue(adventureListViewer.getList().isEnabled());
assertFalse(txtName.getEnabled());
}
Aggregations