Search in sources :

Example 21 with MultiValidator

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

the class ServerSettingsWizardPage method createOCWarningControls.

@SuppressWarnings("unchecked")
private void createOCWarningControls(Composite container, ServerSettingsWizardPageModel model, DataBindingContext dbc) {
    Composite composite = new Composite(container, SWT.NONE);
    GridDataFactory.fillDefaults().applyTo(composite);
    GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
    ValueBindingBuilder.bind(WidgetProperties.visible().observe(composite)).to(BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_OC_BINARY_STATUS).observe(model)).converting(new Converter(IStatus.class, Boolean.class) {

        @Override
        public Object convert(Object fromObject) {
            return !((IStatus) fromObject).isOK();
        }
    }).in(dbc);
    Label label = new Label(composite, SWT.NONE);
    ValueBindingBuilder.bind(WidgetProperties.image().observe(label)).to(BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_OC_BINARY_STATUS).observe(model)).converting(new Converter(IStatus.class, Image.class) {

        @Override
        public Object convert(Object fromObject) {
            switch(((IStatus) fromObject).getSeverity()) {
                case IStatus.WARNING:
                    return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
                case IStatus.ERROR:
                    return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
                default:
                    return null;
            }
        }
    }).in(dbc);
    Link link = new Link(composite, SWT.WRAP);
    ValueBindingBuilder.bind(WidgetProperties.text().observe(link)).to(BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_OC_BINARY_STATUS).observe(model)).converting(new Converter(IStatus.class, String.class) {

        @Override
        public Object convert(Object fromObject) {
            return ((IStatus) fromObject).getMessage();
        }
    }).in(dbc);
    link.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (DOWNLOAD_LINK_TEXT.equals(e.text)) {
                new BrowserUtility().checkedCreateExternalBrowser(DOWNLOAD_INSTRUCTIONS_URL, OpenShiftUIActivator.PLUGIN_ID, OpenShiftUIActivator.getDefault().getLog());
            } else {
                int rc = PreferencesUtil.createPreferenceDialogOn(getShell(), OPEN_SHIFT_PREFERENCE_PAGE_ID, new String[] { OPEN_SHIFT_PREFERENCE_PAGE_ID }, null).open();
                if (rc == Dialog.OK) {
                    new Job("Checking oc binary") {

                        @Override
                        protected IStatus run(IProgressMonitor monitor) {
                            IStatus stat = OCBinary.getInstance().getStatus(model.getConnection(), monitor);
                            model.setOCBinaryStatus(stat);
                            return stat;
                        }
                    }.schedule();
                }
            }
        }
    });
    GridDataFactory.fillDefaults().hint(LINK_DEFAULT_WIDTH, SWT.DEFAULT).applyTo(link);
    MultiValidator validator = new MultiValidator() {

        @Override
        protected IStatus validate() {
            IObservableValue<IStatus> observable = BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_OC_BINARY_STATUS).observe(model);
            Status status = (Status) observable.getValue();
            switch(status.getSeverity()) {
                case IStatus.ERROR:
                    return OpenShiftUIActivator.statusFactory().errorStatus(OpenShiftUIMessages.OCBinaryErrorMessage);
                case IStatus.WARNING:
                    return OpenShiftUIActivator.statusFactory().warningStatus(OpenShiftUIMessages.OCBinaryWarningMessage);
            }
            return status;
        }
    };
    dbc.addValidationStatusProvider(validator);
    BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_OC_BINARY_STATUS).observe(model).addValueChangeListener(event -> getShell().pack());
}
Also used : ValidationStatus(org.eclipse.core.databinding.validation.ValidationStatus) Status(org.eclipse.core.runtime.Status) RsyncStatus(org.jboss.tools.openshift.internal.core.util.RSyncValidator.RsyncStatus) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ExpandableComposite(org.eclipse.ui.forms.widgets.ExpandableComposite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Converter(org.eclipse.core.databinding.conversion.Converter) Model2ObservableTreeItemConverter(org.jboss.tools.openshift.internal.ui.treeitem.Model2ObservableTreeItemConverter) ObservableTreeItem2ModelConverter(org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem2ModelConverter) InvertingBooleanConverter(org.jboss.tools.common.ui.databinding.InvertingBooleanConverter) BrowserUtility(org.jboss.tools.foundation.ui.util.BrowserUtility) Job(org.eclipse.core.runtime.jobs.Job) Link(org.eclipse.swt.widgets.Link)

Example 22 with MultiValidator

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

the class ServerSettingsWizardPage method createRSyncControls.

private void createRSyncControls(Composite container, ServerSettingsWizardPageModel model, DataBindingContext dbc) {
    final RSyncValidator rsyncValidator = RSyncValidator.get();
    Composite composite = new Composite(container, SWT.NONE);
    GridDataFactory.fillDefaults().applyTo(composite);
    GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
    ValueBindingBuilder.bind(WidgetProperties.visible().observe(composite)).to(BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_RSYNC_STATUS).observe(model)).converting(new Converter(RsyncStatus.class, Boolean.class) {

        @Override
        public Object convert(Object fromObject) {
            return fromObject != RsyncStatus.OK;
        }
    }).in(dbc);
    Label label = new Label(composite, SWT.NONE);
    ValueBindingBuilder.bind(WidgetProperties.image().observe(label)).to(BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_RSYNC_STATUS).observe(model)).converting(new Converter(RsyncStatus.class, Image.class) {

        @Override
        public Object convert(Object fromObject) {
            switch((RsyncStatus) fromObject) {
                case OK:
                    return null;
                default:
                    return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
            }
        }
    }).in(dbc);
    Link link = new Link(composite, SWT.WRAP);
    ValueBindingBuilder.bind(WidgetProperties.text().observe(link)).to(BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_RSYNC_STATUS).observe(model)).converting(new Converter(RsyncStatus.class, String.class) {

        @Override
        public Object convert(Object fromObject) {
            switch((RsyncStatus) fromObject) {
                case OK:
                    return null;
                default:
                    return ((RsyncStatus) fromObject).getDetailedMessage(rsyncValidator.getBasePath());
            }
        }
    }).in(dbc);
    GridDataFactory.fillDefaults().hint(LINK_DEFAULT_WIDTH, SWT.DEFAULT).applyTo(link);
    link.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (REFRESH_LINK_TEXT.equals(e.text)) {
                new Job("Checking rsync environment") {

                    @Override
                    protected IStatus run(IProgressMonitor monitor) {
                        rsyncValidator.refresh();
                        model.setRsyncStatus(rsyncValidator.getStatus());
                        return Status.OK_STATUS;
                    }
                }.schedule();
            } else {
                new BrowserUtility().checkedCreateExternalBrowser(e.text, OpenShiftUIActivator.PLUGIN_ID, OpenShiftUIActivator.getDefault().getLog());
            }
        }
    });
    MultiValidator validator = new MultiValidator() {

        @Override
        protected IStatus validate() {
            RsyncStatus status = (RsyncStatus) BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_RSYNC_STATUS).observe(model).getValue();
            switch(status) {
                case OK:
                    return ValidationStatus.ok();
                default:
                    return OpenShiftUIActivator.statusFactory().warningStatus(status.getMessage());
            }
        }
    };
    dbc.addValidationStatusProvider(validator);
    BeanProperties.value(ServerSettingsWizardPageModel.PROPERTY_RSYNC_STATUS).observe(model).addValueChangeListener(event -> getShell().pack());
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ExpandableComposite(org.eclipse.ui.forms.widgets.ExpandableComposite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) RsyncStatus(org.jboss.tools.openshift.internal.core.util.RSyncValidator.RsyncStatus) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) RSyncValidator(org.jboss.tools.openshift.internal.core.util.RSyncValidator) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Converter(org.eclipse.core.databinding.conversion.Converter) Model2ObservableTreeItemConverter(org.jboss.tools.openshift.internal.ui.treeitem.Model2ObservableTreeItemConverter) ObservableTreeItem2ModelConverter(org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem2ModelConverter) InvertingBooleanConverter(org.jboss.tools.common.ui.databinding.InvertingBooleanConverter) BrowserUtility(org.jboss.tools.foundation.ui.util.BrowserUtility) Job(org.eclipse.core.runtime.jobs.Job) Link(org.eclipse.swt.widgets.Link)

Example 23 with MultiValidator

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

the class TemplateParametersPage method createTable.

public TableViewer createTable(final Composite tableContainer, IObservableList<IParameter> parametersObservable, DataBindingContext dbc) {
    final Table table = new Table(tableContainer, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    Image decorationImage = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).getImage();
    final ObservableMap<String, IStatus> cellsValidationStatusObservable = new WritableMap<>(String.class, IStatus.class);
    final TableViewerCellDecorationManager decorations = new TableViewerCellDecorationManager(decorationImage, table);
    DataBindingUtils.addDisposableListChangeListener(onParametersChanged(decorations, cellsValidationStatusObservable), parametersObservable, table);
    TableViewer tableViewer = buildTableViewer(table, tableContainer, cellsValidationStatusObservable, decorations);
    // cells validity
    dbc.addValidationStatusProvider(new MultiValidator() {

        @Override
        protected IStatus validate() {
            for (Iterator<IStatus> iterator = (Iterator<IStatus>) cellsValidationStatusObservable.values().iterator(); iterator.hasNext(); ) {
                IStatus cellValidationStatus = iterator.next();
                if (cellValidationStatus != null && !cellValidationStatus.isOK()) {
                    return cellValidationStatus;
                }
            }
            return ValidationStatus.ok();
        }
    });
    return tableViewer;
}
Also used : TableViewerCellDecorationManager(org.jboss.tools.openshift.internal.common.ui.utils.TableViewerCellDecorationManager) IStatus(org.eclipse.core.runtime.IStatus) Table(org.eclipse.swt.widgets.Table) WritableMap(org.eclipse.core.databinding.observable.map.WritableMap) Iterator(java.util.Iterator) Image(org.eclipse.swt.graphics.Image) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 24 with MultiValidator

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

the class AbstractTransactionDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite area = (Composite) super.createDialogArea(parent);
    Composite editArea = new Composite(area, SWT.NONE);
    FormLayout layout = new FormLayout();
    layout.marginWidth = 5;
    layout.marginHeight = 5;
    editArea.setLayout(layout);
    createFormElements(editArea);
    // $NON-NLS-1$
    IObservableValue<IStatus> calculationStatus = BeanProperties.value("calculationStatus").observe(model);
    this.context.addValidationStatusProvider(new MultiValidator() {

        @Override
        protected IStatus validate() {
            return calculationStatus.getValue();
        }
    });
    // $NON-NLS-1$
    context.bindValue(// $NON-NLS-1$
    PojoProperties.value("status").observe(status), new AggregateValidationStatus(context, AggregateValidationStatus.MAX_SEVERITY));
    return editArea;
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) IStatus(org.eclipse.core.runtime.IStatus) Composite(org.eclipse.swt.widgets.Composite) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) AggregateValidationStatus(org.eclipse.core.databinding.AggregateValidationStatus)

Example 25 with MultiValidator

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

the class AccountTransferDialog method createFormElements.

@Override
protected void createFormElements(Composite editArea) {
    // 
    // input elements
    // 
    // source account
    ComboInput source = new ComboInput(editArea, Messages.ColumnAccountFrom);
    source.value.setInput(including(client.getActiveAccounts(), model().getSourceAccount()));
    IObservableValue sourceObservable = source.bindValue(Properties.sourceAccount.name(), Messages.MsgAccountFromMissing);
    source.bindCurrency(Properties.sourceAccountCurrency.name());
    // target account
    ComboInput target = new ComboInput(editArea, Messages.ColumnAccountTo);
    target.value.setInput(including(client.getActiveAccounts(), model().getTargetAccount()));
    IObservableValue targetObservable = target.bindValue(Properties.targetAccount.name(), Messages.MsgAccountToMissing);
    target.bindCurrency(Properties.targetAccountCurrency.name());
    MultiValidator validator = new AccountsMustBeDifferentValidator(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));
    // other input fields
    Input fxAmount = new Input(editArea, Messages.ColumnAmount);
    fxAmount.bindValue(Properties.fxAmount.name(), Messages.ColumnAmount, Values.Amount, true);
    fxAmount.bindCurrency(Properties.sourceAccountCurrency.name());
    // $NON-NLS-1$ //$NON-NLS-2$
    Input exchangeRate = new Input(editArea, useIndirectQuotation ? "/ " : "x ");
    exchangeRate.bindBigDecimal(useIndirectQuotation ? Properties.inverseExchangeRate.name() : Properties.exchangeRate.name(), Values.ExchangeRate.pattern());
    exchangeRate.bindCurrency(useIndirectQuotation ? Properties.inverseExchangeRateCurrencies.name() : Properties.exchangeRateCurrencies.name());
    model().addPropertyChangeListener(Properties.exchangeRate.name(), e -> exchangeRate.value.setToolTipText(AbstractModel.createCurrencyToolTip(model().getExchangeRate(), model().getTargetAccountCurrency(), model().getSourceAccountCurrency())));
    // $NON-NLS-1$
    Input amount = new Input(editArea, "=");
    amount.bindValue(Properties.amount.name(), Messages.ColumnAmount, Values.Amount, true);
    amount.bindCurrency(Properties.targetAccountCurrency.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(fxAmount.currency);
    FormDataFactory forms = startingWith(source.value.getControl(), source.label).suffix(source.currency).thenBelow(target.value.getControl()).label(target.label).suffix(target.currency).thenBelow(valueDate.getControl()).label(lblDate);
    // fxAmount - exchange rate - amount
    // 
    forms.thenBelow(fxAmount.value).width(amountWidth).label(fxAmount.label).thenRight(fxAmount.currency).width(// 
    currencyWidth).thenRight(// 
    exchangeRate.label).thenRight(exchangeRate.value).width(// 
    amountWidth).thenRight(exchangeRate.currency).width(// 
    amountWidth).thenRight(// 
    amount.label).thenRight(amount.value).width(// 
    amountWidth).suffix(amount.currency, // 
    currencyWidth).thenBelow(valueNote).left(target.value.getControl()).right(amount.value).label(lblNote);
    int widest = widest(source.label, target.label, lblDate, fxAmount.label, lblNote);
    startingWith(source.label).width(widest);
    // 
    // hide / show exchange rate if necessary
    // 
    model.addPropertyChangeListener(Properties.exchangeRateCurrencies.name(), event -> {
        String sourceCurrency = model().getSourceAccountCurrency();
        String targetCurrency = model().getTargetAccountCurrency();
        // make exchange rate visible if both are set but different
        boolean visible = sourceCurrency.length() > 0 && targetCurrency.length() > 0 && !sourceCurrency.equals(targetCurrency);
        exchangeRate.setVisible(visible);
        amount.setVisible(visible);
    });
    WarningMessages warnings = new WarningMessages(this);
    warnings.add(() -> model().getDate().isAfter(LocalDate.now()) ? Messages.MsgDateIsInTheFuture : null);
    model.addPropertyChangeListener(Properties.date.name(), e -> warnings.check());
    // $NON-NLS-1$
    model.firePropertyChange(Properties.exchangeRateCurrencies.name(), "", model().getExchangeRateCurrencies());
}
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) FormDataFactory(name.abuchen.portfolio.ui.util.FormDataFactory) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) DatePicker(name.abuchen.portfolio.ui.util.DatePicker)

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