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());
}
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());
}
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());
}
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());
}
Aggregations