Search in sources :

Example 1 with TrimTrailingSlashConverter

use of org.jboss.tools.openshift.internal.common.ui.databinding.TrimTrailingSlashConverter in project jbosstools-openshift by jbosstools.

the class ConnectionWizardPage method doCreateControls.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void doCreateControls(final Composite parent, DataBindingContext dbc) {
    GridLayoutFactory.fillDefaults().numColumns(3).margins(10, 10).applyTo(parent);
    // userdoc link (JBIDE-20401)
    // text set in #showHideUserdocLink
    this.userdocLink = new StyledText(parent, SWT.WRAP);
    GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).span(3, 1).applyTo(userdocLink);
    showHideUserdocLink();
    IObservableValue userdocUrlObservable = BeanProperties.value(ConnectionWizardPageModel.PROPERTY_USERDOCURL).observe(pageModel);
    StyledTextUtils.emulateLinkAction(userdocLink, r -> onUserdocLinkClicked(userdocUrlObservable));
    userdocUrlObservable.addValueChangeListener(new IValueChangeListener() {

        @Override
        public void handleValueChange(ValueChangeEvent event) {
            showHideUserdocLink();
        }
    });
    IObservableValue connectionFactoryObservable = BeanProperties.value(ConnectionWizardPageModel.PROPERTY_CONNECTION_FACTORY).observe(pageModel);
    // filler
    Label fillerLabel = new Label(parent, SWT.NONE);
    GridDataFactory.fillDefaults().span(3, 3).hint(SWT.DEFAULT, 6).applyTo(fillerLabel);
    // existing connections combo
    Label connectionLabel = new Label(parent, SWT.NONE);
    connectionLabel.setText("Connection:");
    GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(connectionLabel);
    Combo connectionCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
    // disable the connection combo if we're editing a connection or creating a new one from scratch
    connectionCombo.setEnabled(pageModel.isAllowConnectionChange());
    GridDataFactory.fillDefaults().span(2, 1).align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(connectionCombo);
    ComboViewer connectionComboViewer = new ComboViewer(connectionCombo);
    connectionComboViewer.setContentProvider(ArrayContentProvider.getInstance());
    connectionComboViewer.setLabelProvider(new ConnectionColumLabelProvider());
    connectionComboViewer.setInput(pageModel.getAllConnections());
    Binding selectedConnectionBinding = ValueBindingBuilder.bind(ViewerProperties.singleSelection().observe(connectionComboViewer)).validatingAfterGet(new IsNotNullValidator(ValidationStatus.cancel("You have to select or create a new connection."))).to(BeanProperties.value(ConnectionWizardPageModel.PROPERTY_SELECTED_CONNECTION, IConnection.class).observe(pageModel)).in(dbc);
    ControlDecorationSupport.create(selectedConnectionBinding, SWT.LEFT | SWT.TOP, null, new RequiredControlDecorationUpdater());
    // server type
    Label connectionFactoryLabel = new Label(parent, SWT.NONE);
    connectionFactoryLabel.setText("Server type:");
    GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(connectionFactoryLabel);
    Combo connectionFactoryCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
    GridDataFactory.fillDefaults().span(2, 1).align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(connectionFactoryCombo);
    ComboViewer connectionFactoriesViewer = new ComboViewer(connectionFactoryCombo);
    connectionFactoriesViewer.setContentProvider(ArrayContentProvider.getInstance());
    connectionFactoriesViewer.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            if (!(element instanceof IConnectionFactory)) {
                return element.toString();
            } else {
                return ((IConnectionFactory) element).getName();
            }
        }
    });
    connectionFactoriesViewer.setInput(pageModel.getAllConnectionFactories());
    // Disable the server type when editing a connection
    if (!pageModel.isAllowConnectionChange() && pageModel.getSelectedConnection() != null) {
        IConnection c = pageModel.getSelectedConnection();
        if (!(c instanceof NewConnectionMarker)) {
            connectionFactoryCombo.setEnabled(false);
        }
    }
    final IViewerObservableValue selectedServerType = ViewerProperties.singleSelection().observe(connectionFactoriesViewer);
    ValueBindingBuilder.bind(selectedServerType).to(connectionFactoryObservable).in(dbc);
    // server
    Button useDefaultServerCheckbox = new Button(parent, SWT.CHECK);
    useDefaultServerCheckbox.setText("Use default server");
    GridDataFactory.fillDefaults().span(3, 1).align(SWT.FILL, SWT.FILL).applyTo(useDefaultServerCheckbox);
    ValueBindingBuilder.bind(WidgetProperties.selection().observe(useDefaultServerCheckbox)).to(BeanProperties.value(ConnectionWizardPageModel.PROPERTY_USE_DEFAULT_HOST, IConnection.class).observe(pageModel)).in(dbc);
    IObservableValue hasDefaultHostObservable = BeanProperties.value(ConnectionWizardPageModel.PROPERTY_HAS_DEFAULT_HOST).observe(pageModel);
    ValueBindingBuilder.bind(WidgetProperties.enabled().observe(useDefaultServerCheckbox)).notUpdating(hasDefaultHostObservable).in(dbc);
    Label serverLabel = new Label(parent, SWT.NONE);
    serverLabel.setText("Server:");
    GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).applyTo(serverLabel);
    Combo serversCombo = new Combo(parent, SWT.BORDER);
    ComboViewer serversViewer = new ComboViewer(serversCombo);
    serversViewer.setContentProvider(new ObservableListContentProvider());
    serversViewer.setInput(BeanProperties.list(ConnectionWizardPageModel.PROPERTY_ALL_HOSTS).observe(pageModel));
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, false).applyTo(serversCombo);
    final IObservableValue serverUrlObservable = WidgetProperties.text().observe(serversCombo);
    serversCombo.addFocusListener(onServerFocusLost(serverUrlObservable));
    ValueBindingBuilder.bind(serverUrlObservable).converting(new TrimTrailingSlashConverter()).to(BeanProperties.value(ConnectionWizardPageModel.PROPERTY_HOST).observe(pageModel)).in(dbc);
    MultiValidator serverUrlValidator = new MultiValidator() {

        @Override
        protected IStatus validate() {
            Object value = serverUrlObservable.getValue();
            if (!(value instanceof String) || StringUtils.isEmpty((String) value)) {
                return ValidationStatus.cancel("Please provide an OpenShift server url.");
            } else if (!UrlUtils.isValid((String) value)) {
                return ValidationStatus.error("Please provide a valid OpenShift server url.");
            }
            return ValidationStatus.ok();
        }
    };
    ControlDecorationSupport.create(serverUrlValidator, SWT.LEFT | SWT.TOP, null, new RequiredControlDecorationUpdater());
    dbc.addValidationStatusProvider(serverUrlValidator);
    ValueBindingBuilder.bind(WidgetProperties.enabled().observe(serversCombo)).notUpdatingParticipant().to(BeanProperties.value(ConnectionWizardPageModel.PROPERTY_USE_DEFAULT_HOST).observe(pageModel)).converting(new InvertingBooleanConverter()).in(dbc);
    // connect error
    dbc.addValidationStatusProvider(new MultiValidator() {

        IObservableValue observable = BeanProperties.value(ConnectionWizardPageModel.PROPERTY_CONNECTED_STATUS, IStatus.class).observe(pageModel);

        @Override
        protected IStatus validate() {
            return (IStatus) observable.getValue();
        }
    });
    // connection editors
    Group authenticationDetailsGroup = new Group(parent, SWT.NONE);
    authenticationDetailsGroup.setText("Authentication");
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).span(3, 1).applyTo(authenticationDetailsGroup);
    GridLayoutFactory.fillDefaults().margins(0, 0).applyTo(authenticationDetailsGroup);
    // additional nesting required because of https://bugs.eclipse.org/bugs/show_bug.cgi?id=478618
    Composite authenticationDetailsContainer = new Composite(authenticationDetailsGroup, SWT.None);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(authenticationDetailsContainer);
    this.connectionEditors = new ConnectionEditorsStackedView(connectionFactoryObservable, this, authenticationDetailsContainer, dbc);
    connectionEditors.createControls();
    // adv editors
    Composite advEditorContainer = new Composite(parent, SWT.NONE);
    GridLayoutFactory.fillDefaults().margins(0, 0).applyTo(authenticationDetailsGroup);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).span(3, 1).grab(true, true).applyTo(advEditorContainer);
    this.advConnectionEditors = new AdvancedConnectionEditorsStackedView(connectionFactoryObservable, pageModel, advEditorContainer, dbc);
    advConnectionEditors.createControls();
}
Also used : IValueChangeListener(org.eclipse.core.databinding.observable.value.IValueChangeListener) Group(org.eclipse.swt.widgets.Group) ObservableListContentProvider(org.eclipse.jface.databinding.viewers.ObservableListContentProvider) IStatus(org.eclipse.core.runtime.IStatus) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) IConnection(org.jboss.tools.openshift.common.core.connection.IConnection) RequiredControlDecorationUpdater(org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) Button(org.eclipse.swt.widgets.Button) IConnectionFactory(org.jboss.tools.openshift.common.core.connection.IConnectionFactory) TrimTrailingSlashConverter(org.jboss.tools.openshift.internal.common.ui.databinding.TrimTrailingSlashConverter) IViewerObservableValue(org.eclipse.jface.databinding.viewers.IViewerObservableValue) Binding(org.eclipse.core.databinding.Binding) NewConnectionMarker(org.jboss.tools.openshift.common.core.connection.NewConnectionMarker) StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) InvertingBooleanConverter(org.jboss.tools.common.ui.databinding.InvertingBooleanConverter) MultiValidator(org.eclipse.core.databinding.validation.MultiValidator) ValueChangeEvent(org.eclipse.core.databinding.observable.value.ValueChangeEvent) ComboViewer(org.eclipse.jface.viewers.ComboViewer) IObservableValue(org.eclipse.core.databinding.observable.value.IObservableValue) IsNotNullValidator(org.jboss.tools.openshift.internal.common.ui.databinding.IsNotNullValidator)

Example 2 with TrimTrailingSlashConverter

use of org.jboss.tools.openshift.internal.common.ui.databinding.TrimTrailingSlashConverter 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

Binding (org.eclipse.core.databinding.Binding)2 MultiValidator (org.eclipse.core.databinding.validation.MultiValidator)2 IStatus (org.eclipse.core.runtime.IStatus)2 Button (org.eclipse.swt.widgets.Button)2 Composite (org.eclipse.swt.widgets.Composite)2 Label (org.eclipse.swt.widgets.Label)2 RequiredControlDecorationUpdater (org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater)2 TrimTrailingSlashConverter (org.jboss.tools.openshift.internal.common.ui.databinding.TrimTrailingSlashConverter)2 ValidationStatusProvider (org.eclipse.core.databinding.ValidationStatusProvider)1 WritableList (org.eclipse.core.databinding.observable.list.WritableList)1 IObservableValue (org.eclipse.core.databinding.observable.value.IObservableValue)1 IValueChangeListener (org.eclipse.core.databinding.observable.value.IValueChangeListener)1 ValueChangeEvent (org.eclipse.core.databinding.observable.value.ValueChangeEvent)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 IViewerObservableValue (org.eclipse.jface.databinding.viewers.IViewerObservableValue)1 ObservableListContentProvider (org.eclipse.jface.databinding.viewers.ObservableListContentProvider)1 GridLayoutFactory (org.eclipse.jface.layout.GridLayoutFactory)1 PreferenceDialog (org.eclipse.jface.preference.PreferenceDialog)1 ColumnLabelProvider (org.eclipse.jface.viewers.ColumnLabelProvider)1