private void createRelayCheck(Composite parent, Result notReferenced, FormToolkit toolkit) { Label l = toolkit.createLabel(parent, "Is Relay Port:", SWT.NONE); l.setLayoutData(new GridData(SWT.NONE)); relayCheck = toolkit.createButton(parent, "", SWT.CHECK); relayCheck.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); relayCheck.setSelection(relay); relayCheck.setEnabled(notReferenced.isOk()); RelayValidator validator = new RelayValidator(); UpdateValueStrategy t2m = new UpdateValueStrategy(); t2m.setAfterConvertValidator(validator); t2m.setBeforeSetValidator(validator); UpdateValueStrategy m2t = new UpdateValueStrategy(); m2t.setAfterConvertValidator(validator); m2t.setBeforeSetValidator(validator); getBindingContext() .bindValue( SWTObservables.observeSelection(relayCheck), PojoObservables.observeValue(this, "relay"), t2m, m2t); if (notReferenced.isOk()) createDecorator(relayCheck, ""); else createInfoDecorator(relayCheck, notReferenced.getMsg()); }
/** * Sets up data binding between the text fields and the connection details object. Also attaches a * "string required" validator to the "password" text field. This validator is configured to do * the following on validation failure<br> * <li>show an ERROR decorator * <li>disable the "Login" button */ private void setupDataBinding() { DataBindingContext dataBindingContext = new DataBindingContext(SWTObservables.getRealm(Display.getCurrent())); UpdateValueStrategy passwordBindingStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE); // The Validator shows error decoration and disables OK button on // validation failure passwordBindingStrategy.setBeforeSetValidator( new StringRequiredValidator( "Please enter password!", guiHelper.createErrorDecoration(passwordText), okButton)); dataBindingContext.bindValue( WidgetProperties.text(SWT.Modify).observe(passwordText), PojoProperties.value("password").observe(connectionDetails), passwordBindingStrategy, passwordBindingStrategy); UpdateValueStrategy userIdBindingStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE); dataBindingContext.bindValue( WidgetProperties.text(SWT.Modify).observe(userIdText), PojoProperties.value("userId").observe(connectionDetails), userIdBindingStrategy, userIdBindingStrategy); }
protected void bindValidator(Text field, Object obj, String property, IValidator validator) { DataBindingContext bindingContext = new DataBindingContext(); UpdateValueStrategy ttm = new UpdateValueStrategy(); ttm.setBeforeSetValidator(validator); // IObservableValue countryTxtObserveTextObserveWidget = SWTObservables.observeText(field, SWT.Modify); IObservableValue currentAddressCountryObserveValue = PojoObservables.observeValue(obj, property); Binding binding = bindingContext.bindValue( countryTxtObserveTextObserveWidget, currentAddressCountryObserveValue, ttm, null); ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT); }
@Override protected Binding createBinding(IObservableValue target, final IObservableValue model) { UpdateValueStrategy targetToModel = new UpdateValueStrategy(); targetToModel.setBeforeSetValidator( new IValidator() { @Override public IStatus validate(Object input) { if (input == null || input.toString().isEmpty()) { return ValidationStatus.error(Messages.inputIsEmpty); } return Status.OK_STATUS; } }); return context.bindValue(target, model, targetToModel, null); }
private void bindControls() { // Bind source file path widget to UI model IObservableValue widgetValue = WidgetProperties.text(SWT.Modify).observe(_javaClassText); IObservableValue modelValue = null; if (isSourcePage()) { modelValue = BeanProperties.value(Model.class, "sourceFilePath").observe(model); } else { modelValue = BeanProperties.value(Model.class, "targetFilePath").observe(model); } UpdateValueStrategy strategy = new UpdateValueStrategy(); strategy.setBeforeSetValidator( new IValidator() { @Override public IStatus validate(final Object value) { final String path = value == null ? null : value.toString().trim(); if (path == null || path.isEmpty()) { return ValidationStatus.error( "A source file path must be supplied for the transformation."); } NewTransformationWizard wizard = (NewTransformationWizard) getWizard(); try { Class<?> tempClass = wizard.getLoader().loadClass(path); if (tempClass == null) { return ValidationStatus.error( "Unable to find a source file with the supplied path"); } } catch (ClassNotFoundException e) { return ValidationStatus.error("Unable to find a source file with the supplied path"); } return ValidationStatus.ok(); } }); _binding = context.bindValue(widgetValue, modelValue, strategy, null); ControlDecorationSupport.create( _binding, decoratorPosition, _javaClassText.getParent(), new WizardControlDecorationUpdater()); listenForValidationChanges(); }
private void createNameAndDescription(Composite composite) { Label nameLabel = new Label(composite, SWT.NONE); nameLabel.setText(Messages.dataNameLabel); final Text labelText = new Text(composite, SWT.BORDER); labelText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); // Add an validator so that age can only be a number IValidator validator = new IValidator() { Set<String> existingDataNames = null; @Override public IStatus validate(Object arg0) { if (existingDataNames == null) { existingDataNames = new HashSet<String>(); if (element != null) { for (SimulationData simuData : element.getSimulationData()) { existingDataNames.add(simuData.getName()); } } } if (existingDataNames.contains(labelText.getText())) { return ValidationStatus.error("Data name already exists."); } return ValidationStatus.ok(); } }; UpdateValueStrategy strategy = new UpdateValueStrategy(); strategy.setBeforeSetValidator(validator); Binding bindingDataName = context.bindValue( SWTObservables.observeText(labelText, SWT.Modify), PojoObservables.observeValue(this, "dataName"), strategy, null); ControlDecorationSupport.create(bindingDataName, SWT.TOP | SWT.LEFT); // labelText.addModifyListener(updateButtonModifyListener) ; Label isExpressionLabel = new Label(composite, SWT.NONE); isExpressionLabel.setText(Messages.BasedOn); Composite radioBasedComposite = new Composite(composite, SWT.NONE); radioBasedComposite.setLayout(new GridLayout(2, true)); isExpressionBased = new Button(radioBasedComposite, SWT.RADIO); isExpressionBased.setText(Messages.Expression); isExpressionBased.setSelection(expressionBased); isOtherBased = new Button(radioBasedComposite, SWT.RADIO); isOtherBased.setSelection(!expressionBased); isOtherBased.setText(Messages.AddSimulationDataWizardPage_probability); context.bindValue( SWTObservables.observeSelection(isExpressionBased), PojoObservables.observeValue(this, "expressionBased")); context.bindValue( SWTObservables.observeSelection(isOtherBased), PojoObservables.observeValue(this, "expressionBased"), new UpdateValueStrategy() .setConverter( new Converter(Boolean.class, Boolean.class) { @Override public Object convert(Object fromObject) { return !(Boolean) fromObject; } }), new UpdateValueStrategy() .setConverter( new Converter(Boolean.class, Boolean.class) { @Override public Object convert(Object fromObject) { return !(Boolean) fromObject; } })); Label expressionLabel = new Label(composite, SWT.NONE); expressionLabel.setText(Messages.Expression); ExpressionViewer expressionViewer = new ExpressionViewer(composite, SWT.BORDER, null); // FIXME: Expressionviewer expressionViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); if (element != null) { expressionViewer.setInput(element); } else { expressionViewer.setInput(data.eContainer()); } expressionViewer.addFilter( new AvailableExpressionTypeFilter( new String[] { ExpressionConstants.CONSTANT_TYPE, ExpressionConstants.VARIABLE_TYPE, ExpressionConstants.SCRIPT_TYPE, ExpressionConstants.SIMULATION_VARIABLE_TYPE })); expressionViewer.setSelection(new StructuredSelection(dataExpression)); context.bindValue( SWTObservables.observeVisible(expressionLabel), SWTObservables.observeSelection(isExpressionBased)); context.bindValue( SWTObservables.observeVisible(expressionViewer.getControl()), SWTObservables.observeSelection(isExpressionBased)); isOtherBased.addSelectionListener(updateButtonSelectionListener); isExpressionBased.addSelectionListener(updateButtonSelectionListener); }
protected UpdateValueStrategy createRequiredUpdateStrategy(String validationMessage) { UpdateValueStrategy updateStrategy = new UpdateValueStrategy(); updateStrategy.setBeforeSetValidator(new RequiredValidator(validationMessage)); return updateStrategy; }