Search in sources :

Example 1 with SimplePerson

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

the class CompositeMapTest method testChangeInSecondMap.

@Test
public void testChangeInSecondMap() {
    SimplePerson person0 = new SimplePerson("p0", "a0", "c0", "s0");
    person0.getCart().setNumItems(13);
    persons.add(person0);
    MapChangeEventTracker<Object, Object> tracker = new MapChangeEventTracker<>();
    composedMap.addMapChangeListener(tracker);
    assertEquals(0, tracker.count);
    person0.getCart().setNumItems(42);
    assertEquals(1, tracker.count);
    assertEquals(0, tracker.event.diff.getAddedKeys().size());
    assertEquals(0, tracker.event.diff.getRemovedKeys().size());
    assertEquals(Collections.singleton(person0), tracker.event.diff.getChangedKeys());
    assertEquals(Integer.valueOf(13), tracker.event.diff.getOldValue(person0));
    assertEquals(Integer.valueOf(42), tracker.event.diff.getNewValue(person0));
    assertEquals(Integer.valueOf(42), composedMap.get(person0));
}
Also used : MapChangeEventTracker(org.eclipse.jface.databinding.conformance.util.MapChangeEventTracker) SimplePerson(org.eclipse.jface.examples.databinding.model.SimplePerson) Test(org.junit.Test)

Example 2 with SimplePerson

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

the class CompositeMapTest method testRemoveSharedFromFirstMap.

@Test
public void testRemoveSharedFromFirstMap() {
    SimplePerson person1 = new SimplePerson("p1", "a1", "c1", "s1");
    person1.getCart().setNumItems(42);
    persons.add(person1);
    SimplePerson person2 = new SimplePerson("p1", "a1", "c1", "s1");
    person2.setCart(person1.getCart());
    persons.add(person2);
    assertTrue("person2 should be added", composedMap.containsKey(person2));
    MapChangeEventTracker<Object, Object> tracker = new MapChangeEventTracker<>();
    composedMap.addMapChangeListener(tracker);
    assertEquals(0, tracker.count);
    persons.remove(person2);
    assertEquals(1, tracker.count);
    assertEquals(0, tracker.event.diff.getAddedKeys().size());
    assertEquals(0, tracker.event.diff.getChangedKeys().size());
    assertEquals(Collections.singleton(person2), tracker.event.diff.getRemovedKeys());
    assertEquals(Integer.valueOf(42), tracker.event.diff.getOldValue(person2));
    assertFalse("person2 should be removed", composedMap.containsKey(person2));
    assertEquals(Integer.valueOf(42), composedMap.get(person1));
}
Also used : MapChangeEventTracker(org.eclipse.jface.databinding.conformance.util.MapChangeEventTracker) SimplePerson(org.eclipse.jface.examples.databinding.model.SimplePerson) Test(org.junit.Test)

Example 3 with SimplePerson

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

the class CompositeMapTest method testChangeInFirstMapFromShared.

@Test
public void testChangeInFirstMapFromShared() {
    SimplePerson person0 = new SimplePerson("p0", "a0", "c0", "s0");
    person0.getCart().setNumItems(13);
    persons.add(person0);
    SimplePerson person1 = new SimplePerson("p1", "a1", "c1", "s1");
    person1.setCart(person0.getCart());
    persons.add(person1);
    MapChangeEventTracker<Object, Object> tracker = new MapChangeEventTracker<>();
    composedMap.addMapChangeListener(tracker);
    assertEquals(0, tracker.count);
    person1.setCart(new SimpleCart());
    assertEquals(1, tracker.count);
    assertEquals(0, tracker.event.diff.getAddedKeys().size());
    assertEquals(0, tracker.event.diff.getRemovedKeys().size());
    assertEquals(Collections.singleton(person1), tracker.event.diff.getChangedKeys());
    assertEquals(Integer.valueOf(13), tracker.event.diff.getOldValue(person1));
    assertEquals(Integer.valueOf(0), tracker.event.diff.getNewValue(person1));
    assertEquals(Integer.valueOf(0), composedMap.get(person1));
}
Also used : MapChangeEventTracker(org.eclipse.jface.databinding.conformance.util.MapChangeEventTracker) SimpleCart(org.eclipse.jface.examples.databinding.model.SimpleCart) SimplePerson(org.eclipse.jface.examples.databinding.model.SimplePerson) Test(org.junit.Test)

Example 4 with SimplePerson

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

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

the class ListDetailValueObservableListTest method testUnmodifiability.

@Test
public void testUnmodifiability() {
    WritableList masterObservableList = new WritableList();
    masterObservableList.add(new SimplePerson());
    masterObservableList.add(new SimplePerson());
    ListDetailValueObservableList ldol = new ListDetailValueObservableList(masterObservableList, BeansObservables.valueFactory("name"), null);
    try {
        ldol.add("name");
        fail("ListDetailValueObservableList must not be modifiable.");
    } catch (UnsupportedOperationException e) {
    // expected exception
    }
    try {
        ldol.remove(masterObservableList.get(0));
        fail("ListDetailValueObservableList must not be modifiable.");
    } catch (UnsupportedOperationException e) {
    // expected exception
    }
    try {
        ldol.removeAll(Collections.singleton(masterObservableList.get(0)));
        fail("ListDetailValueObservableList must not be modifiable.");
    } catch (UnsupportedOperationException e) {
    // expected exception
    }
    try {
        ldol.retainAll(Collections.EMPTY_LIST);
        fail("ListDetailValueObservableList must not be modifiable.");
    } catch (UnsupportedOperationException e) {
    // expected exception
    }
    try {
        ldol.move(0, 1);
        fail("ListDetailValueObservableList must not be modifiable.");
    } catch (UnsupportedOperationException e) {
    // expected exception
    }
}
Also used : WritableList(org.eclipse.core.databinding.observable.list.WritableList) ListDetailValueObservableList(org.eclipse.core.internal.databinding.observable.masterdetail.ListDetailValueObservableList) SimplePerson(org.eclipse.jface.examples.databinding.model.SimplePerson) Test(org.junit.Test) ObservableListContractTest(org.eclipse.jface.databinding.conformance.ObservableListContractTest)

Aggregations

SimplePerson (org.eclipse.jface.examples.databinding.model.SimplePerson)33 Test (org.junit.Test)32 MapChangeEventTracker (org.eclipse.jface.databinding.conformance.util.MapChangeEventTracker)10 WritableList (org.eclipse.core.databinding.observable.list.WritableList)8 WritableMap (org.eclipse.core.databinding.observable.map.WritableMap)7 WritableSet (org.eclipse.core.databinding.observable.set.WritableSet)7 ListDetailValueObservableList (org.eclipse.core.internal.databinding.observable.masterdetail.ListDetailValueObservableList)7 MapDetailValueObservableMap (org.eclipse.core.internal.databinding.observable.masterdetail.MapDetailValueObservableMap)7 SetDetailValueObservableMap (org.eclipse.core.internal.databinding.observable.masterdetail.SetDetailValueObservableMap)7 ObservableListContractTest (org.eclipse.jface.databinding.conformance.ObservableListContractTest)7 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)2 ListChangeEventTracker (org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker)2 SimpleCart (org.eclipse.jface.examples.databinding.model.SimpleCart)2 PropertyChangeListener (java.beans.PropertyChangeListener)1 Binding (org.eclipse.core.databinding.Binding)1 DataBindingContext (org.eclipse.core.databinding.DataBindingContext)1 UpdateValueStrategy (org.eclipse.core.databinding.UpdateValueStrategy)1 BeanProperties (org.eclipse.core.databinding.beans.typed.BeanProperties)1 IConverter (org.eclipse.core.databinding.conversion.IConverter)1 AbstractObservable (org.eclipse.core.databinding.observable.AbstractObservable)1