Search in sources :

Example 16 with MultiValidator

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

the class ConnectionValidatorsTest method testOAuthAuthenticationValidatorInUI.

@Test
public void testOAuthAuthenticationValidatorInUI() {
    Connection connection1 = mockConnection(HOST1, null, TOKEN1);
    mockConnection(HOST2, null, TOKEN2);
    ConnectionWizardPageModel pageModel = mockConnectionWizardPageModel(connection1);
    Mockito.when(pageModel.getHost()).thenReturn(HOST2);
    IValueChangeListener<Object> l = new IValueChangeListener<Object>() {

        @Override
        public void handleValueChange(ValueChangeEvent<? extends Object> event) {
        }
    };
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    Composite composite = new Composite(shell, SWT.NONE);
    try {
        OAuthDetailView view = new OAuthDetailView(null, pageModel, l, null, null);
        DataBindingContext dbc = new DataBindingContext();
        view.createControls(shell, null, dbc);
        view.onVisible(null, dbc);
        view.getTokenTextControl().setText(TOKEN2);
        MultiValidator v = findValidator(dbc);
        Assert.assertEquals(IStatus.ERROR, getStatusSeverity(v));
        view.getTokenTextControl().setText(TOKEN3);
        Assert.assertEquals(IStatus.OK, getStatusSeverity(v));
    } finally {
        composite.dispose();
    }
}
Also used : IValueChangeListener(org.eclipse.core.databinding.observable.value.IValueChangeListener) OAuthDetailView(org.jboss.tools.openshift.internal.ui.wizard.connection.OAuthDetailView) ValueChangeEvent(org.eclipse.core.databinding.observable.value.ValueChangeEvent) Shell(org.eclipse.swt.widgets.Shell) Composite(org.eclipse.swt.widgets.Composite) ConnectionWizardPageModel(org.jboss.tools.openshift.internal.common.ui.connection.ConnectionWizardPageModel) Connection(org.jboss.tools.openshift.core.connection.Connection) DataBindingContext(org.eclipse.core.databinding.DataBindingContext) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) Test(org.junit.Test)

Example 17 with MultiValidator

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

the class ConnectionValidatorsTest method testOAuthAuthenticationValidator.

@Test
public void testOAuthAuthenticationValidator() {
    Connection connection1 = mockConnection(HOST1, null, TOKEN1);
    mockConnection(HOST2, null, TOKEN2);
    ConnectionWizardPageModel pageModel = mockConnectionWizardPageModel(connection1);
    WritableValue<String> tokenObservable = new WritableValue<String>();
    WritableValue<String> urlObservable = new WritableValue<String>();
    MultiValidator v = ConnectionValidatorFactory.createOAuthAuthenticationValidator(pageModel, tokenObservable, urlObservable);
    v.observeValidatedValue(urlObservable);
    // New connection
    urlObservable.setValue(HOST3);
    tokenObservable.setValue(TOKEN3);
    Assert.assertEquals(IStatus.OK, getStatusSeverity(v));
    urlObservable.setValue(HOST2);
    // Host exists, but token is different
    Assert.assertEquals(IStatus.OK, getStatusSeverity(v));
    tokenObservable.setValue(TOKEN2);
    // Existing not selected connection
    Assert.assertEquals(IStatus.ERROR, getStatusSeverity(v));
    // Selected connection
    urlObservable.setValue(HOST1);
    tokenObservable.setValue(TOKEN1);
    Assert.assertEquals(IStatus.OK, getStatusSeverity(v));
}
Also used : ConnectionWizardPageModel(org.jboss.tools.openshift.internal.common.ui.connection.ConnectionWizardPageModel) Connection(org.jboss.tools.openshift.core.connection.Connection) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) WritableValue(org.eclipse.core.databinding.observable.value.WritableValue) Test(org.junit.Test)

Example 18 with MultiValidator

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

the class ConnectionValidatorsTest method findValidator.

private MultiValidator findValidator(DataBindingContext dbc) {
    MultiValidator result = null;
    for (Object o : dbc.getValidationStatusProviders()) {
        if (o instanceof MultiValidator) {
            Assert.assertNull("Multiple validators: " + result + ", " + o + ". Please improve the test to pick the right validator.", result);
            result = (MultiValidator) o;
        }
    }
    Assert.assertNotNull("Validator not found", result);
    return result;
}
Also used : MultiValidator(org.eclipse.core.databinding.validation.MultiValidator)

Example 19 with MultiValidator

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

the class OpenShiftServerEditorSection method setupWarningToRestartAdapter.

private void setupWarningToRestartAdapter(IEditorPart editor) {
    if (editor == null) {
        return;
    }
    IObservableValue<Boolean> dirtyStatusObservable = DataBindingUtils.createDirtyStatusObservable(editor);
    dbc.addValidationStatusProvider(new MultiValidator() {

        @Override
        protected IStatus validate() {
            if (Boolean.TRUE.equals(dirtyStatusObservable.getValue())) {
                return ValidationStatus.warning("Changes will only get active once the editor is saved and the adapter restarted.");
            }
            return ValidationStatus.ok();
        }
    });
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator)

Example 20 with MultiValidator

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

the class AdvancedConnectionEditor method createControls.

@SuppressWarnings("unchecked")
@Override
public Composite createControls(Composite parent, Object context, DataBindingContext dbc) {
    this.pageModel = (ConnectionWizardPageModel) context;
    this.selectedConnection = BeanProperties.value(ConnectionWizardPageModel.PROPERTY_SELECTED_CONNECTION).observe(pageModel);
    this.model = new AdvancedConnectionEditorModel();
    Composite composite = setControl(new Composite(parent, SWT.None));
    GridLayoutFactory.fillDefaults().applyTo(composite);
    DialogAdvancedPart part = new DialogAdvancedPart() {

        @Override
        protected void createAdvancedContent(Composite advancedComposite) {
            Label lblRegistry = new Label(advancedComposite, SWT.NONE);
            lblRegistry.setText("Image Registry URL:");
            GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(lblRegistry);
            Text txtRegistry = new Text(advancedComposite, SWT.BORDER);
            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).span(1, 1).applyTo(txtRegistry);
            Button registryDiscover = new Button(advancedComposite, SWT.PUSH);
            registryDiscover.setText("Discover...");
            registryDiscover.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    discoverRegistryPressed(registryDiscover.getShell());
                }
            });
            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(registryDiscover);
            UIUtils.setDefaultButtonWidth(registryDiscover);
            registryURLObservable = WidgetProperties.text(SWT.Modify).observeDelayed(DELAY, txtRegistry);
            Binding registryURLBinding = ValueBindingBuilder.bind(registryURLObservable).validatingAfterConvert(new URLValidator(VALIDATOR_URL_TYPE, true)).converting(new TrimTrailingSlashConverter()).to(BeanProperties.value(AdvancedConnectionEditorModel.PROP_REGISTRY_URL).observe(model)).in(dbc);
            ControlDecorationSupport.create(registryURLBinding, SWT.LEFT | SWT.TOP);
            Label lblNamespace = new Label(advancedComposite, SWT.NONE);
            lblNamespace.setText("Cluster namespace:");
            GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(lblNamespace);
            Text txtClusterNamespace = new Text(advancedComposite, SWT.BORDER);
            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).span(2, 1).applyTo(txtClusterNamespace);
            clusterNamespaceObservable = WidgetProperties.text(SWT.Modify).observeDelayed(DELAY, txtClusterNamespace);
            ValueBindingBuilder.bind(clusterNamespaceObservable).converting(new TrimmingStringConverter()).to(BeanProperties.value(AdvancedConnectionEditorModel.PROP_CLUSTER_NAMESPACE).observe(model)).in(dbc);
            Link ocWorkspace = new Link(advancedComposite, SWT.PUSH);
            ocWorkspace.setText("<a>Workspace OC Settings</a>");
            GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).span(3, 1).applyTo(ocWorkspace);
            // Override OC location widgets
            Button overrideOC = new Button(advancedComposite, SWT.CHECK);
            overrideOC.setText("Override 'oc' location: ");
            GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(lblRegistry);
            IObservableValue<Boolean> overrideOCObservable = WidgetProperties.selection().observe(overrideOC);
            ValueBindingBuilder.bind(overrideOCObservable).to(BeanProperties.value(AdvancedConnectionEditorModel.PROP_OC_OVERRIDE).observe(model)).in(dbc);
            final Text ocText = new Text(advancedComposite, SWT.SINGLE | SWT.BORDER);
            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(ocText);
            IObservableValue<String> ocLocationObservable = WidgetProperties.text(SWT.Modify).observe(ocText);
            Binding ocLocationBinding = ValueBindingBuilder.bind(ocLocationObservable).converting(new TrimmingStringConverter()).to(BeanProperties.value(AdvancedConnectionEditorModel.PROP_OC_OVERRIDE_LOCATION).observe(model)).in(dbc);
            overrideOCObservable.addValueChangeListener(new IValueChangeListener<Boolean>() {

                @Override
                public void handleValueChange(ValueChangeEvent<? extends Boolean> event) {
                    updateOcObservables();
                }
            });
            ocLocationBinding.getValidationStatus().addValueChangeListener(new IValueChangeListener<IStatus>() {

                @Override
                public void handleValueChange(ValueChangeEvent<? extends IStatus> event) {
                    updateOcObservables();
                }
            });
            ValueBindingBuilder.bind(overrideOCObservable).to(WidgetProperties.enabled().observe(ocText)).notUpdatingParticipant().in(dbc);
            Button ocBrowse = new Button(advancedComposite, SWT.PUSH);
            ocBrowse.setText("Browse...");
            GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(ocBrowse);
            UIUtils.setDefaultButtonWidth(ocBrowse);
            ocWorkspace.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    PreferenceDialog pd = PreferencesUtil.createPreferenceDialogOn(null, IOpenShiftCoreConstants.OPEN_SHIFT_PREFERENCE_PAGE_ID, new String[] {}, null);
                    pd.open();
                    if (!overrideOC.getSelection()) {
                        String ocLoc = OpenShiftCorePreferences.INSTANCE.getOCBinaryLocation();
                        String nullsafe = ocLoc == null ? "" : ocLoc;
                        ocText.setText(nullsafe);
                    }
                    updateOcObservables();
                }
            });
            // Validation here is done via a listener rather than dbc validators
            // because dbc validators will validate in the UI thread, but validation
            // of this field requires a background job.
            ocBrowse.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    FileDialog fd = new FileDialog(ocBrowse.getShell());
                    fd.setText(ocText.getText());
                    IPath p = new Path(ocText.getText());
                    if (p.segmentCount() > 1) {
                        fd.setFilterPath(p.removeLastSegments(1).toOSString());
                    }
                    String result = fd.open();
                    if (result != null) {
                        ocLocationObservable.setValue(result);
                    }
                }
            });
            ValueBindingBuilder.bind(overrideOCObservable).to(WidgetProperties.enabled().observe(ocBrowse)).notUpdatingParticipant().in(dbc);
            ValidationStatusProvider ocValidator = new MultiValidator() {

                public IObservableList getTargets() {
                    WritableList targets = new WritableList();
                    targets.add(ocLocationObservable);
                    targets.add(ocLocationValidity);
                    targets.add(ocVersionValidity);
                    return targets;
                }

                @Override
                protected IStatus validate() {
                    // access all observables that the framework should listen to
                    IStatus ocLocationStatus = ocLocationValidity.getValue();
                    IStatus ocVersionStatus = ocVersionValidity.getValue();
                    IStatus status;
                    if (!ocLocationStatus.isOK()) {
                        status = ocLocationStatus;
                    } else {
                        status = ocVersionStatus;
                    }
                    return status;
                }
            };
            dbc.addValidationStatusProvider(ocValidator);
            ControlDecorationSupport.create(ocValidator, SWT.LEFT | SWT.TOP, null, new RequiredControlDecorationUpdater());
        }

        protected GridLayoutFactory adjustAdvancedCompositeLayout(GridLayoutFactory gridLayoutFactory) {
            return gridLayoutFactory.numColumns(3);
        }
    };
    part.createAdvancedGroup(composite, 1);
    this.connectionAdvancedPropertiesProvider = new ConnectionAdvancedPropertiesProvider();
    updateOcObservables();
    return composite;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Label(org.eclipse.swt.widgets.Label) RequiredControlDecorationUpdater(org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater) PreferenceDialog(org.eclipse.jface.preference.PreferenceDialog) Button(org.eclipse.swt.widgets.Button) URLValidator(org.jboss.tools.openshift.internal.ui.validator.URLValidator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DialogAdvancedPart(org.jboss.tools.openshift.internal.common.ui.utils.DialogAdvancedPart) TrimTrailingSlashConverter(org.jboss.tools.openshift.internal.common.ui.databinding.TrimTrailingSlashConverter) Binding(org.eclipse.core.databinding.Binding) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Composite(org.eclipse.swt.widgets.Composite) IPath(org.eclipse.core.runtime.IPath) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) ValidationStatusProvider(org.eclipse.core.databinding.ValidationStatusProvider) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) TrimmingStringConverter(org.jboss.tools.openshift.internal.common.ui.databinding.TrimmingStringConverter) WritableList(org.eclipse.core.databinding.observable.list.WritableList) IConnectionAdvancedPropertiesProvider(org.jboss.tools.openshift.internal.common.ui.connection.ConnectionWizardPageModel.IConnectionAdvancedPropertiesProvider) FileDialog(org.eclipse.swt.widgets.FileDialog) Link(org.eclipse.swt.widgets.Link)

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