Search in sources :

Example 6 with Adventure

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

the class PropertyScenarios method testScenario07.

@Test
public void testScenario07() {
    // Binding the price property of an Adventure to a Text control. Price
    // is a double and Text accepts String so conversion will have to occur.
    // Validation ensure that the value is positive
    Text text = new Text(getComposite(), SWT.BORDER);
    adventure.setPrice(5.0);
    final String cannotBeNegativeMessage = "Price cannot be negative.";
    final String mustBeCurrencyMessage = "Price must be a currency.";
    IValidator validator = value -> {
        String stringValue = (String) value;
        try {
            double doubleValue = Double.parseDouble(stringValue);
            if (doubleValue < 0.0) {
                return ValidationStatus.error(cannotBeNegativeMessage);
            }
            return Status.OK_STATUS;
        } catch (NumberFormatException ex) {
            return ValidationStatus.error(mustBeCurrencyMessage);
        }
    };
    // Create a number formatter that will display one decimal position.
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setMinimumFractionDigits(1);
    IConverter targetToModelConverter = StringToNumberConverter.toDouble(numberFormat, true);
    IConverter modelToTargetConverter = NumberToStringConverter.fromDouble(numberFormat, true);
    getDbc().bindValue(SWTObservables.observeText(text, SWT.Modify), BeansObservables.observeValue(adventure, "price"), new UpdateValueStrategy().setAfterGetValidator(validator).setConverter(targetToModelConverter), new UpdateValueStrategy().setConverter(modelToTargetConverter));
    String expected = numberFormat.format(adventure.getPrice());
    assertEquals(expected, text.getText());
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
    String toEnter = numberFormat.format(0.65);
    enterText(text, toEnter);
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
    assertEquals(0.65, adventure.getPrice(), 0.0001);
    adventure.setPrice(42.24);
    expected = numberFormat.format(adventure.getPrice());
    assertEquals(expected, text.getText());
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
    enterText(text, "jygt");
    assertEquals(mustBeCurrencyMessage, AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).getMessage());
    toEnter = numberFormat.format(-23.9);
    enterText(text, toEnter);
    assertEquals(cannotBeNegativeMessage, AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).getMessage());
    assertEquals(42.24, adventure.getPrice(), 0.0001);
    adventure.setPrice(0.0);
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
}
Also used : Converter(org.eclipse.core.databinding.conversion.Converter) Date(java.util.Date) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) Spinner(org.eclipse.swt.widgets.Spinner) FocusEvent(org.eclipse.swt.events.FocusEvent) IdentityConverter(org.eclipse.core.internal.databinding.conversion.IdentityConverter) NumberFormat(java.text.NumberFormat) FocusListener(org.eclipse.swt.events.FocusListener) Event(org.eclipse.swt.widgets.Event) NumberToStringConverter(org.eclipse.core.databinding.conversion.text.NumberToStringConverter) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IConverter(org.eclipse.core.databinding.conversion.IConverter) Locale(java.util.Locale) After(org.junit.After) BeansObservables(org.eclipse.core.databinding.beans.BeansObservables) SampleData(org.eclipse.jface.examples.databinding.model.SampleData) AggregateValidationStatus(org.eclipse.core.databinding.AggregateValidationStatus) ParseException(java.text.ParseException) Before(org.junit.Before) SWTObservables(org.eclipse.jface.databinding.swt.SWTObservables) Text(org.eclipse.swt.widgets.Text) Button(org.eclipse.swt.widgets.Button) Assert.assertTrue(org.junit.Assert.assertTrue) Status(org.eclipse.core.runtime.Status) StringToNumberConverter(org.eclipse.core.databinding.conversion.text.StringToNumberConverter) Test(org.junit.Test) Account(org.eclipse.jface.examples.databinding.model.Account) Binding(org.eclipse.core.databinding.Binding) Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) Cart(org.eclipse.jface.examples.databinding.model.Cart) Assert.assertEquals(org.junit.Assert.assertEquals) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) Text(org.eclipse.swt.widgets.Text) IConverter(org.eclipse.core.databinding.conversion.IConverter) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 7 with Adventure

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

the class ListViewerScenario method testScenario01.

@Test
public void testScenario01() {
    // Bind the catalog's lodgings to the combo
    IObservableList lodgings = BeansObservables.observeList(Realm.getDefault(), catalog, "lodgings");
    ViewerSupport.bind(listViewer, 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], listViewer.getElementAt(i));
    }
    // Verify that the String being shown in the list 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, list.getItems());
    // Verify that the list has no selected item
    assertEquals(null, listViewer.getStructuredSelection().getFirstElement());
    // Now bind the selection of the combo to the "defaultLodging" property
    // of an adventure
    final Adventure adventure = SampleData.WINTER_HOLIDAY;
    IObservableValue selection = ViewersObservables.observeSingleSelection(listViewer);
    getDbc().bindValue(selection, BeansObservables.observeValue(adventure, "defaultLodging"));
    // Verify that the list selection is the default lodging
    assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
    // Change the model and verify that the list selection changes
    adventure.setDefaultLodging(SampleData.CAMP_GROUND);
    assertEquals(adventure.getDefaultLodging(), SampleData.CAMP_GROUND);
    assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
    // Change the list selection and verify that the model changes
    listViewer.getList().select(3);
    assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
    adventure.setDefaultLodging(SampleData.YOUTH_HOSTEL);
    spinEventLoop(0);
    assertEquals(listViewer.getStructuredSelection().getFirstElement(), adventure.getDefaultLodging());
}
Also used : Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Lodging(org.eclipse.jface.examples.databinding.model.Lodging) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) Test(org.junit.Test)

Example 8 with Adventure

use of org.eclipse.jface.examples.databinding.model.Adventure 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());
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) ComputedValue(org.eclipse.core.databinding.observable.value.ComputedValue) Category(org.eclipse.jface.examples.databinding.model.Category) Adventure(org.eclipse.jface.examples.databinding.model.Adventure) GridData(org.eclipse.swt.layout.GridData) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Text(org.eclipse.swt.widgets.Text) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) Catalog(org.eclipse.jface.examples.databinding.model.Catalog) IObservableList(org.eclipse.core.databinding.observable.list.IObservableList) Test(org.junit.Test)

Example 9 with Adventure

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

the class PropertyScenarios method testScenario08.

@Test
public void testScenario08() {
    // Binding the price property of an Adventure to a Text control but with
    // custom conversion � the double will be validated to only have two
    // decimal places and displayed with a leading currency symbol, and can
    // be entered with or without the currency symbol.
    Text text = new Text(getComposite(), SWT.BORDER);
    adventure.setPrice(5.0);
    final String cannotBeNegativeMessage = "Price cannot be negative.";
    final String mustBeCurrencyMessage = "Price must be a currency.";
    final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.CANADA);
    IConverter toCurrency = new Converter(double.class, String.class) {

        @Override
        public Object convert(Object toObject) {
            return currencyFormat.format(((Double) toObject).doubleValue());
        }
    };
    IConverter toDouble = new Converter(String.class, double.class) {

        @Override
        public Object convert(Object fromObject) {
            try {
                return Double.valueOf(currencyFormat.parse((String) fromObject).doubleValue());
            } catch (ParseException e) {
                // IllegalConversionException?
                return Double.valueOf(0);
            }
        }
    };
    IValidator validator = value -> {
        String stringValue = (String) value;
        try {
            double doubleValue = currencyFormat.parse(stringValue).doubleValue();
            if (doubleValue < 0.0) {
                return ValidationStatus.error(cannotBeNegativeMessage);
            }
            return Status.OK_STATUS;
        } catch (ParseException e) {
            return ValidationStatus.error(mustBeCurrencyMessage);
        }
    };
    getDbc().bindValue(SWTObservables.observeText(text, SWT.Modify), BeansObservables.observeValue(adventure, "price"), new UpdateValueStrategy().setConverter(toDouble).setAfterGetValidator(validator), new UpdateValueStrategy().setConverter(toCurrency));
    String expected = currencyFormat.format(5);
    assertEquals(expected, text.getText());
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
    String toEnter = currencyFormat.format(0.65);
    enterText(text, toEnter);
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
    assertEquals(0.65, adventure.getPrice(), 0.0001);
    adventure.setPrice(42.24);
    expected = currencyFormat.format(adventure.getPrice());
    assertEquals(expected, text.getText());
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
    enterText(text, "jygt");
    assertEquals(mustBeCurrencyMessage, AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).getMessage());
    toEnter = currencyFormat.format(-23.9);
    enterText(text, toEnter);
    assertEquals(cannotBeNegativeMessage, AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).getMessage());
    assertEquals(42.24, adventure.getPrice(), 0.0001);
    adventure.setPrice(0.0);
    assertTrue(AggregateValidationStatus.getStatusMaxSeverity(getDbc().getBindings()).isOK());
}
Also used : Converter(org.eclipse.core.databinding.conversion.Converter) Date(java.util.Date) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) Spinner(org.eclipse.swt.widgets.Spinner) FocusEvent(org.eclipse.swt.events.FocusEvent) IdentityConverter(org.eclipse.core.internal.databinding.conversion.IdentityConverter) NumberFormat(java.text.NumberFormat) FocusListener(org.eclipse.swt.events.FocusListener) Event(org.eclipse.swt.widgets.Event) NumberToStringConverter(org.eclipse.core.databinding.conversion.text.NumberToStringConverter) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) IConverter(org.eclipse.core.databinding.conversion.IConverter) Locale(java.util.Locale) After(org.junit.After) BeansObservables(org.eclipse.core.databinding.beans.BeansObservables) SampleData(org.eclipse.jface.examples.databinding.model.SampleData) AggregateValidationStatus(org.eclipse.core.databinding.AggregateValidationStatus) ParseException(java.text.ParseException) Before(org.junit.Before) SWTObservables(org.eclipse.jface.databinding.swt.SWTObservables) Text(org.eclipse.swt.widgets.Text) Button(org.eclipse.swt.widgets.Button) Assert.assertTrue(org.junit.Assert.assertTrue) Status(org.eclipse.core.runtime.Status) StringToNumberConverter(org.eclipse.core.databinding.conversion.text.StringToNumberConverter) Test(org.junit.Test) Account(org.eclipse.jface.examples.databinding.model.Account) Binding(org.eclipse.core.databinding.Binding) Adventure(org.eclipse.jface.examples.databinding.model.Adventure) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) Cart(org.eclipse.jface.examples.databinding.model.Cart) Assert.assertEquals(org.junit.Assert.assertEquals) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) IValidator(org.eclipse.core.databinding.validation.IValidator) Converter(org.eclipse.core.databinding.conversion.Converter) IdentityConverter(org.eclipse.core.internal.databinding.conversion.IdentityConverter) NumberToStringConverter(org.eclipse.core.databinding.conversion.text.NumberToStringConverter) IConverter(org.eclipse.core.databinding.conversion.IConverter) StringToNumberConverter(org.eclipse.core.databinding.conversion.text.StringToNumberConverter) Text(org.eclipse.swt.widgets.Text) ParseException(java.text.ParseException) IConverter(org.eclipse.core.databinding.conversion.IConverter) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Aggregations

Adventure (org.eclipse.jface.examples.databinding.model.Adventure)9 Test (org.junit.Test)9 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)6 Text (org.eclipse.swt.widgets.Text)5 Lodging (org.eclipse.jface.examples.databinding.model.Lodging)4 NumberFormat (java.text.NumberFormat)3 ParseException (java.text.ParseException)3 Date (java.util.Date)3 Locale (java.util.Locale)3 AggregateValidationStatus (org.eclipse.core.databinding.AggregateValidationStatus)3 Binding (org.eclipse.core.databinding.Binding)3 UpdateValueStrategy (org.eclipse.core.databinding.UpdateValueStrategy)3 BeansObservables (org.eclipse.core.databinding.beans.BeansObservables)3 Converter (org.eclipse.core.databinding.conversion.Converter)3 IConverter (org.eclipse.core.databinding.conversion.IConverter)3 NumberToStringConverter (org.eclipse.core.databinding.conversion.text.NumberToStringConverter)3 StringToNumberConverter (org.eclipse.core.databinding.conversion.text.StringToNumberConverter)3 IValidator (org.eclipse.core.databinding.validation.IValidator)3 ValidationStatus (org.eclipse.core.databinding.validation.ValidationStatus)3 IdentityConverter (org.eclipse.core.internal.databinding.conversion.IdentityConverter)3