use of org.eclipse.core.databinding.observable.Realm in project eclipse.platform.ui by eclipse-platform.
the class ViewerSupport method bind.
/**
* Binds the viewer to the specified input, using the specified children
* property to generate child nodes, and the specified label properties to
* generate labels.
*
* @param viewer
* the tree viewer to set up
* @param input
* the input to set on the viewer
* @param childrenProperty
* the property to use as the children of an element
* @param labelProperties
* the respective properties to use for labels in each of the
* viewer's columns
*/
@SafeVarargs
public static <E> void bind(AbstractTreeViewer viewer, E input, IListProperty<? super E, ? extends E> childrenProperty, IValueProperty<? super E, ?>... labelProperties) {
Realm realm = DisplayRealm.getRealm(viewer.getControl().getDisplay());
ObservableListTreeContentProvider<? extends E> contentProvider = new ObservableListTreeContentProvider<>(childrenProperty.listFactory(realm), null);
if (viewer.getInput() != null)
viewer.setInput(null);
viewer.setContentProvider(contentProvider);
viewer.setLabelProvider(new ObservableMapLabelProvider(Properties.observeEach(contentProvider.getKnownElements(), labelProperties)));
if (input != null)
viewer.setInput(input);
}
use of org.eclipse.core.databinding.observable.Realm in project eclipse.platform.ui by eclipse-platform.
the class TestMasterDetail method bind.
private void bind(Control parent) {
Realm realm = DisplayRealm.getRealm(parent.getDisplay());
TableViewer peopleViewer = new TableViewer(personsTable);
ViewerSupport.bind(peopleViewer, new WritableList<>(realm, model.getPersonList(), SimpleModel.class), BeanProperties.values(SimplePerson.class, "name", "state"));
IObservableValue<SimplePerson> selectedPerson = ViewerProperties.singleSelection(SimplePerson.class).observe(peopleViewer);
DataBindingContext dbc = new DataBindingContext(realm) {
@Override
protected <T, M> UpdateValueStrategy<T, M> createTargetToModelUpdateValueStrategy(IObservableValue<T> fromValue, IObservableValue<M> toValue) {
return new CustomUpdateValueStrategy<>();
}
};
IConverter<String, String> upperCaseConverter = new IConverter<>() {
@Override
public String convert(String fromObject) {
return fromObject.toUpperCase();
}
@Override
public Object getFromType() {
return String.class;
}
@Override
public Object getToType() {
return String.class;
}
};
IValidator<String> vowelValidator = value -> {
if (!value.matches("[aeiouAEIOU]*")) {
return ValidationStatus.error("only vowels allowed");
}
return Status.OK_STATUS;
};
Binding b = dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(name), BeanProperties.value(SimplePerson.class, "name", String.class).observeDetail(selectedPerson), new CustomUpdateValueStrategy<String, String>().setConverter(upperCaseConverter).setAfterGetValidator(vowelValidator), null);
// AggregateValidationStatus status = new AggregateValidationStatus(dbc
// .getBindings(), AggregateValidationStatus.MAX_SEVERITY);
dbc.bindValue(WidgetProperties.text().observe(validationStatus), b.getValidationStatus(), null, new UpdateValueStrategy<Object, String>().setConverter(new ObjectToStringConverter()));
dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(address), BeanProperties.value(SimplePerson.class, "address", String.class).observeDetail(selectedPerson));
dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(city), BeanProperties.value(SimplePerson.class, "city", String.class).observeDetail(selectedPerson));
dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(state), BeanProperties.value(SimplePerson.class, "state", String.class).observeDetail(selectedPerson));
TableViewer ordersViewer = new TableViewer(ordersTable);
ViewerSupport.bind(ordersViewer, BeanProperties.list(SimplePerson.class, "orders", SimpleOrder.class).observeDetail(selectedPerson), BeanProperties.values(SimpleOrder.class, "orderNumber", "date"));
}
use of org.eclipse.core.databinding.observable.Realm 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.core.databinding.observable.Realm in project eclipse.platform.ui by eclipse-platform.
the class TestBackgroundSaveEditor method createPartControl.
@Override
public void createPartControl(Composite parent) {
Realm realm = DisplayRealm.getRealm(parent.getDisplay());
final DataBindingContext dbc = new DataBindingContext(realm);
parent.addDisposeListener(e -> dbc.dispose());
final IObservableValue<?> inputObservable = BeansObservables.observeValue(realm, data, "input");
final IObservableValue<?> outputObservable = BeansObservables.observeValue(realm, data, "output");
createInputGroup(parent, dbc, inputObservable);
createOptionsGroup(parent, realm, dbc);
createOutputGroup(parent, dbc, outputObservable);
GridLayoutFactory.swtDefaults().numColumns(3).equalWidth(true).generateLayout(parent);
}
use of org.eclipse.core.databinding.observable.Realm in project go-dmd-clock by sker65.
the class EditorViewSWTTest method testCreateContents.
@Test
public void testCreateContents() throws Exception {
uut.vm = new ViewModel();
uut.shell = new Shell();
Display display = Display.getDefault();
Realm realm = SWTObservables.getRealm(display);
Realm.runWithDefault(realm, () -> uut.createContents());
}
Aggregations