Search in sources :

Example 1 with MultiValidator

use of org.eclipse.core.databinding.validation.MultiValidator in project portfolio by buchen.

the class PasswordDialog method createFormElements.

@Override
protected void createFormElements(Composite editArea) {
    final IObservableValue password = bindings().bindStringInput(editArea, Messages.LabelPassword, "password", // $NON-NLS-1$
    SWT.PASSWORD);
    final IObservableValue repeat = bindings().bindStringInput(editArea, Messages.LabelPasswordRepeat, "repeat", // $NON-NLS-1$
    SWT.PASSWORD);
    // multi validator (passwords must be identical)
    MultiValidator validator = new MultiValidator() {

        @Override
        protected IStatus validate() {
            String pwd = (String) password.getValue();
            String rpt = (String) repeat.getValue();
            if (pwd.length() < 6)
                return ValidationStatus.error(Messages.MsgPasswordMinCharacters);
            return pwd.equals(rpt) ? ValidationStatus.ok() : ValidationStatus.error(Messages.MsgPasswordNotIdentical);
        }
    };
    bindings().getBindingContext().addValidationStatusProvider(validator);
}
Also used : IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator)

Example 2 with MultiValidator

use of org.eclipse.core.databinding.validation.MultiValidator in project portfolio by buchen.

the class SecurityTransferDialog method createFormElements.

@Override
protected void createFormElements(Composite editArea) {
    // 
    // input elements
    // 
    // security
    ComboInput securities = new ComboInput(editArea, Messages.ColumnSecurity);
    securities.value.setInput(including(client.getActiveSecurities(), model().getSecurity()));
    securities.bindValue(Properties.security.name(), Messages.MsgMissingSecurity);
    securities.bindCurrency(Properties.securityCurrencyCode.name());
    // source portfolio
    ComboInput source = new ComboInput(editArea, Messages.ColumnAccountFrom);
    source.value.setInput(including(client.getActivePortfolios(), model().getSourcePortfolio()));
    IObservableValue sourceObservable = source.bindValue(Properties.sourcePortfolio.name(), Messages.MsgPortfolioFromMissing);
    source.bindCurrency(Properties.sourcePortfolioLabel.name());
    // target portfolio
    ComboInput target = new ComboInput(editArea, Messages.ColumnAccountTo);
    target.value.setInput(including(client.getActivePortfolios(), model().getTargetPortfolio()));
    IObservableValue targetObservable = target.bindValue(Properties.targetPortfolio.name(), Messages.MsgPortfolioToMissing);
    target.bindCurrency(Properties.targetPortfolioLabel.name());
    MultiValidator validator = new PortfoliosMustBeDifferentValidator(sourceObservable, targetObservable);
    context.addValidationStatusProvider(validator);
    // date
    Label lblDate = new Label(editArea, SWT.RIGHT);
    lblDate.setText(Messages.ColumnDate);
    DatePicker valueDate = new DatePicker(editArea);
    context.bindValue(new SimpleDateTimeDateSelectionProperty().observe(valueDate.getControl()), BeanProperties.value(Properties.date.name()).observe(model));
    DateTime valueTime = new DateTime(editArea, SWT.TIME | SWT.SHORT | SWT.DROP_DOWN | SWT.BORDER);
    context.bindValue(new SimpleDateTimeTimeSelectionProperty().observe(valueTime), BeanProperties.value(Properties.time.name()).observe(model));
    // amount
    Input shares = new Input(editArea, Messages.ColumnShares);
    shares.bindValue(Properties.shares.name(), Messages.ColumnShares, Values.Share, true);
    // $NON-NLS-1$
    Input quote = new Input(editArea, "x " + Messages.ColumnQuote);
    quote.bindBigDecimal(Properties.quote.name(), Values.Quote.pattern());
    quote.bindCurrency(Properties.securityCurrencyCode.name());
    // $NON-NLS-1$
    Input amount = new Input(editArea, "=");
    amount.bindValue(Properties.amount.name(), Messages.ColumnAmount, Values.Amount, true);
    amount.bindCurrency(Properties.securityCurrencyCode.name());
    // note
    Label lblNote = new Label(editArea, SWT.LEFT);
    lblNote.setText(Messages.ColumnNote);
    Text valueNote = new Text(editArea, SWT.BORDER);
    context.bindValue(WidgetProperties.text(SWT.Modify).observe(valueNote), BeanProperties.value(Properties.note.name()).observe(model));
    // 
    // form layout
    // 
    int amountWidth = amountWidth(amount.value);
    int currencyWidth = currencyWidth(amount.currency);
    startingWith(securities.value.getControl(), securities.label).suffix(securities.currency).thenBelow(source.value.getControl()).label(source.label).suffix(source.currency).thenBelow(target.value.getControl()).label(target.label).suffix(target.currency).thenBelow(valueDate.getControl()).label(lblDate).thenRight(valueTime);
    // shares - quote - amount
    startingWith(valueDate.getControl()).thenBelow(shares.value).width(amountWidth).label(shares.label).thenRight(quote.label).thenRight(quote.value).width(amountWidth).thenRight(quote.currency).width(currencyWidth).thenRight(amount.label).thenRight(amount.value).width(amountWidth).thenRight(amount.currency).width(currencyWidth);
    startingWith(shares.value).thenBelow(valueNote).left(securities.value.getControl()).right(amount.value).label(lblNote);
    int widest = widest(securities.label, source.label, target.label, lblDate, shares.label, lblNote);
    startingWith(securities.label).width(widest);
    WarningMessages warnings = new WarningMessages(this);
    warnings.add(() -> LocalDateTime.of(model().getDate(), model().getTime()).isAfter(LocalDateTime.now()) ? Messages.MsgDateIsInTheFuture : null);
    warnings.add(() -> new StockSplitWarning().check(model().getSecurity(), model().getDate()));
    model.addPropertyChangeListener(Properties.security.name(), e -> warnings.check());
    model.addPropertyChangeListener(Properties.date.name(), e -> warnings.check());
}
Also used : SimpleDateTimeDateSelectionProperty(name.abuchen.portfolio.ui.util.SimpleDateTimeDateSelectionProperty) Label(org.eclipse.swt.widgets.Label) Text(org.eclipse.swt.widgets.Text) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) LocalDateTime(java.time.LocalDateTime) DateTime(org.eclipse.swt.widgets.DateTime) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) DatePicker(name.abuchen.portfolio.ui.util.DatePicker) SimpleDateTimeTimeSelectionProperty(name.abuchen.portfolio.ui.util.SimpleDateTimeTimeSelectionProperty)

Example 3 with MultiValidator

use of org.eclipse.core.databinding.validation.MultiValidator in project portfolio by buchen.

the class SelectSplitPage method createControl.

@Override
public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    setControl(container);
    container.setLayout(new FormLayout());
    Label labelSecurity = new Label(container, SWT.NONE);
    labelSecurity.setText(Messages.ColumnSecurity);
    List<Security> securities = model.getClient().getActiveSecurities();
    if (model.getSecurity() != null && !securities.contains(model.getSecurity()))
        securities.add(0, model.getSecurity());
    ComboViewer comboSecurity = new ComboViewer(container, SWT.READ_ONLY);
    comboSecurity.setContentProvider(ArrayContentProvider.getInstance());
    comboSecurity.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            return ((Security) element).getName();
        }
    });
    comboSecurity.setInput(securities);
    Label labelExDate = new Label(container, SWT.NONE);
    labelExDate.setText(Messages.ColumnExDate);
    DatePicker boxExDate = new DatePicker(container);
    Label labelSplit = new Label(container, SWT.NONE);
    labelSplit.setText(Messages.SplitWizardLabelSplit);
    Spinner spinnerNewShares = new Spinner(container, SWT.BORDER);
    spinnerNewShares.setMinimum(1);
    spinnerNewShares.setMaximum(1000000);
    spinnerNewShares.setSelection(1);
    spinnerNewShares.setIncrement(1);
    spinnerNewShares.setFocus();
    Label labelColon = new Label(container, SWT.NONE);
    labelColon.setText(Messages.SplitWizardLabelNewForOld);
    Spinner spinnerOldShares = new Spinner(container, SWT.BORDER);
    spinnerOldShares.setMinimum(1);
    spinnerOldShares.setMaximum(1000000);
    spinnerOldShares.setSelection(1);
    spinnerOldShares.setIncrement(1);
    // form layout data
    int labelWidth = widest(labelSecurity, labelExDate, labelSplit);
    // 
    startingWith(comboSecurity.getControl(), labelSecurity).thenBelow(boxExDate.getControl()).label(// 
    labelExDate).thenBelow(spinnerNewShares).label(labelSplit).thenRight(labelColon).thenRight(spinnerOldShares);
    startingWith(labelSecurity).width(labelWidth);
    // model binding
    DataBindingContext context = bindings.getBindingContext();
    context.bindValue(ViewersObservables.observeSingleSelection(comboSecurity), BeanProperties.value("security").observe(model), null, // $NON-NLS-1$
    null);
    // 
    context.bindValue(// 
    new SimpleDateTimeDateSelectionProperty().observe(boxExDate.getControl()), // $NON-NLS-1$
    BeanProperties.value("exDate").observe(model), // 
    new UpdateValueStrategy().setAfterConvertValidator(value -> {
        return value != null ? ValidationStatus.ok() : ValidationStatus.error(MessageFormat.format(Messages.MsgDialogInputRequired, Messages.ColumnExDate));
    }), null);
    final ISWTObservableValue observeNewShares = WidgetProperties.selection().observe(spinnerNewShares);
    // $NON-NLS-1$
    context.bindValue(observeNewShares, BeanProperties.value("newShares").observe(model));
    final ISWTObservableValue observeOldShares = WidgetProperties.selection().observe(spinnerOldShares);
    // $NON-NLS-1$
    context.bindValue(observeOldShares, BeanProperties.value("oldShares").observe(model));
    MultiValidator validator = new MultiValidator() {

        @Override
        protected IStatus validate() {
            Object newShares = observeNewShares.getValue();
            Object oldShares = observeOldShares.getValue();
            return newShares.equals(oldShares) ? ValidationStatus.error(Messages.SplitWizardErrorNewAndOldMustNotBeEqual) : ValidationStatus.ok();
        }
    };
    context.addValidationStatusProvider(validator);
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) FormDataFactory.startingWith(name.abuchen.portfolio.ui.util.FormDataFactory.startingWith) UpdateValueStrategy(org.eclipse.core.databinding.UpdateValueStrategy) DatePicker(name.abuchen.portfolio.ui.util.DatePicker) ISWTObservableValue(org.eclipse.jface.databinding.swt.ISWTObservableValue) Spinner(org.eclipse.swt.widgets.Spinner) DataBindingContext(org.eclipse.core.databinding.DataBindingContext) ComboViewer(org.eclipse.jface.viewers.ComboViewer) MessageFormat(java.text.MessageFormat) ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) WidgetProperties(org.eclipse.jface.databinding.swt.WidgetProperties) AbstractWizardPage(name.abuchen.portfolio.ui.wizards.AbstractWizardPage) IStatus(org.eclipse.core.runtime.IStatus) Composite(org.eclipse.swt.widgets.Composite) Messages(name.abuchen.portfolio.ui.Messages) SimpleDateTimeDateSelectionProperty(name.abuchen.portfolio.ui.util.SimpleDateTimeDateSelectionProperty) BindingHelper(name.abuchen.portfolio.ui.util.BindingHelper) SWTHelper.widest(name.abuchen.portfolio.ui.util.SWTHelper.widest) FormLayout(org.eclipse.swt.layout.FormLayout) Security(name.abuchen.portfolio.model.Security) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) List(java.util.List) ViewersObservables(org.eclipse.jface.databinding.viewers.ViewersObservables) BeanProperties(org.eclipse.core.databinding.beans.BeanProperties) SWT(org.eclipse.swt.SWT) Label(org.eclipse.swt.widgets.Label) LabelProvider(org.eclipse.jface.viewers.LabelProvider) Composite(org.eclipse.swt.widgets.Composite) SimpleDateTimeDateSelectionProperty(name.abuchen.portfolio.ui.util.SimpleDateTimeDateSelectionProperty) Spinner(org.eclipse.swt.widgets.Spinner) Label(org.eclipse.swt.widgets.Label) DataBindingContext(org.eclipse.core.databinding.DataBindingContext) Security(name.abuchen.portfolio.model.Security) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) ComboViewer(org.eclipse.jface.viewers.ComboViewer) ISWTObservableValue(org.eclipse.jface.databinding.swt.ISWTObservableValue) DatePicker(name.abuchen.portfolio.ui.util.DatePicker) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Example 4 with MultiValidator

use of org.eclipse.core.databinding.validation.MultiValidator in project jbosstools-openshift by jbosstools.

the class ConnectionValidatorFactory method createBasicAuthenticationValidator.

/**
 * Returns new validator that checks that there is no connection other than the selected one
 * with host and username provided by the observable values.
 * @param pageModel
 * @param usernameObservable
 * @param urlObservable
 * @return
 */
public static MultiValidator createBasicAuthenticationValidator(ConnectionWizardPageModel pageModel, IObservableValue usernameObservable, IObservableValue<?> urlObservable) {
    return new MultiValidator() {

        @Override
        protected IStatus validate() {
            String user1 = (String) usernameObservable.getValue();
            String mHost = (String) urlObservable.getValue();
            IConnection current = pageModel.getSelectedConnection();
            for (Connection c : ConnectionsRegistrySingleton.getInstance().getAll(Connection.class)) {
                if (c != current && IAuthorizationContext.AUTHSCHEME_BASIC.equals(c.getAuthScheme())) {
                    String host = c.getHost();
                    String user = c.getUsername();
                    if (host != null && host.equals(mHost) && user != null && user.equals(user1)) {
                        return ValidationStatus.error("Connection for the server with this username already exists.");
                    }
                }
            }
            return ValidationStatus.ok();
        }
    };
}
Also used : IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) Connection(org.jboss.tools.openshift.core.connection.Connection) IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator)

Example 5 with MultiValidator

use of org.eclipse.core.databinding.validation.MultiValidator in project jbosstools-openshift by jbosstools.

the class ConnectionValidatorFactory method createOAuthAuthenticationValidator.

/**
 * Returns new validator that checks that there is no connection other than the selected one
 * with host and token provided by the observable values.
 * @param pageModel
 * @param tokenObservable
 * @param urlObservable
 * @return
 */
public static MultiValidator createOAuthAuthenticationValidator(ConnectionWizardPageModel pageModel, IObservableValue tokenObservable, IObservableValue<?> urlObservable) {
    return new MultiValidator() {

        @Override
        protected IStatus validate() {
            String token1 = (String) tokenObservable.getValue();
            String mHost = (String) urlObservable.getValue();
            IConnection current = pageModel.getSelectedConnection();
            for (Connection c : ConnectionsRegistrySingleton.getInstance().getAll(Connection.class)) {
                if (c != current && IAuthorizationContext.AUTHSCHEME_OAUTH.equals(c.getAuthScheme())) {
                    String host = c.getHost();
                    String token = c.getToken();
                    if (host != null && host.equals(mHost) && token != null && token.equals(token1)) {
                        return ValidationStatus.error("Connection for the server with this token already exists.");
                    }
                }
            }
            return ValidationStatus.ok();
        }
    };
}
Also used : IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) Connection(org.jboss.tools.openshift.core.connection.Connection) IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator)

Aggregations

MultiValidator (org.eclipse.core.databinding.validation.MultiValidator)27 IStatus (org.eclipse.core.runtime.IStatus)12 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)11 Composite (org.eclipse.swt.widgets.Composite)11 Label (org.eclipse.swt.widgets.Label)11 Button (org.eclipse.swt.widgets.Button)6 Connection (org.jboss.tools.openshift.core.connection.Connection)6 DataBindingContext (org.eclipse.core.databinding.DataBindingContext)5 Text (org.eclipse.swt.widgets.Text)5 RequiredControlDecorationUpdater (org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater)5 WritableValue (org.eclipse.core.databinding.observable.value.WritableValue)4 ConnectionWizardPageModel (org.jboss.tools.openshift.internal.common.ui.connection.ConnectionWizardPageModel)4 Test (org.junit.Test)4 DatePicker (name.abuchen.portfolio.ui.util.DatePicker)3 SimpleDateTimeDateSelectionProperty (name.abuchen.portfolio.ui.util.SimpleDateTimeDateSelectionProperty)3 Binding (org.eclipse.core.databinding.Binding)3 UpdateValueStrategy (org.eclipse.core.databinding.UpdateValueStrategy)3 Converter (org.eclipse.core.databinding.conversion.Converter)3 IValueChangeListener (org.eclipse.core.databinding.observable.value.IValueChangeListener)3 ValueChangeEvent (org.eclipse.core.databinding.observable.value.ValueChangeEvent)3