Search in sources :

Example 1 with Realm

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);
}
Also used : Realm(org.eclipse.core.databinding.observable.Realm) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm)

Example 2 with Realm

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"));
}
Also used : SimpleModel(org.eclipse.jface.examples.databinding.model.SimpleModel) TableViewer(org.eclipse.jface.viewers.TableViewer) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) ObjectToStringConverter(org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter) TableColumn(org.eclipse.swt.widgets.TableColumn) IValidator(org.eclipse.core.databinding.validation.IValidator) SimpleOrder(org.eclipse.jface.examples.databinding.model.SimpleOrder) DataBindingContext(org.eclipse.core.databinding.DataBindingContext) WidgetProperties(org.eclipse.jface.databinding.swt.typed.WidgetProperties) ViewerSupport(org.eclipse.jface.databinding.viewers.ViewerSupport) Table(org.eclipse.swt.widgets.Table) WritableList(org.eclipse.core.databinding.observable.list.WritableList) SimpleModel(org.eclipse.jface.examples.databinding.model.SimpleModel) IObserving(org.eclipse.core.databinding.observable.IObserving) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IConverter(org.eclipse.core.databinding.conversion.IConverter) IStatus(org.eclipse.core.runtime.IStatus) BeanProperties(org.eclipse.core.databinding.beans.typed.BeanProperties) GridData(org.eclipse.swt.layout.GridData) Realm(org.eclipse.core.databinding.observable.Realm) Text(org.eclipse.swt.widgets.Text) Shell(org.eclipse.swt.widgets.Shell) Status(org.eclipse.core.runtime.Status) Display(org.eclipse.swt.widgets.Display) Binding(org.eclipse.core.databinding.Binding) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm) ViewerProperties(org.eclipse.jface.databinding.viewers.typed.ViewerProperties) SWT(org.eclipse.swt.SWT) SimplePerson(org.eclipse.jface.examples.databinding.model.SimplePerson) Label(org.eclipse.swt.widgets.Label) Control(org.eclipse.swt.widgets.Control) GridLayout(org.eclipse.swt.layout.GridLayout) Binding(org.eclipse.core.databinding.Binding) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) SimpleOrder(org.eclipse.jface.examples.databinding.model.SimpleOrder) DataBindingContext(org.eclipse.core.databinding.DataBindingContext) SWT(org.eclipse.swt.SWT) ObjectToStringConverter(org.eclipse.core.internal.databinding.conversion.ObjectToStringConverter) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) Realm(org.eclipse.core.databinding.observable.Realm) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm) TableViewer(org.eclipse.jface.viewers.TableViewer) IConverter(org.eclipse.core.databinding.conversion.IConverter) SimplePerson(org.eclipse.jface.examples.databinding.model.SimplePerson)

Example 3 with Realm

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());
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) GridData(org.eclipse.swt.layout.GridData) 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) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) Realm(org.eclipse.core.databinding.observable.Realm) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm) Catalog(org.eclipse.jface.examples.databinding.model.Catalog) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) Test(org.junit.Test)

Example 4 with Realm

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);
}
Also used : DataBindingContext(org.eclipse.core.databinding.DataBindingContext) Realm(org.eclipse.core.databinding.observable.Realm) DisplayRealm(org.eclipse.jface.databinding.swt.DisplayRealm)

Example 5 with Realm

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());
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ViewModel(com.rinke.solutions.pinball.view.model.ViewModel) Realm(org.eclipse.core.databinding.observable.Realm) Display(org.eclipse.swt.widgets.Display) Test(org.junit.Test)

Aggregations

Realm (org.eclipse.core.databinding.observable.Realm)22 Test (org.junit.Test)10 CurrentRealm (org.eclipse.jface.databinding.conformance.util.CurrentRealm)8 Event (org.eclipse.swt.widgets.Event)7 Listener (org.eclipse.swt.widgets.Listener)7 DisplayRealm (org.eclipse.jface.databinding.swt.DisplayRealm)6 Button (org.eclipse.swt.widgets.Button)6 Group (org.eclipse.swt.widgets.Group)6 Text (org.eclipse.swt.widgets.Text)3 DataBindingContext (org.eclipse.core.databinding.DataBindingContext)2 IObservableMap (org.eclipse.core.databinding.observable.map.IObservableMap)2 WritableSet (org.eclipse.core.databinding.observable.set.WritableSet)2 ComputedValue (org.eclipse.core.databinding.observable.value.ComputedValue)2 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)2 IValueChangeListener (org.eclipse.core.databinding.observable.value.IValueChangeListener)2 ValueChangeEvent (org.eclipse.core.databinding.observable.value.ValueChangeEvent)2 ThreadRealm (org.eclipse.core.tests.databinding.observable.ThreadRealm)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 GridData (org.eclipse.swt.layout.GridData)2 Control (org.eclipse.swt.widgets.Control)2