Example usage for org.eclipse.jface.databinding.dialog ValidationMessageProvider ValidationMessageProvider

List of usage examples for org.eclipse.jface.databinding.dialog ValidationMessageProvider ValidationMessageProvider

Introduction

In this page you can find the example usage for org.eclipse.jface.databinding.dialog ValidationMessageProvider ValidationMessageProvider.

Prototype

ValidationMessageProvider

Source Link

Usage

From source file:com.google.cloud.tools.eclipse.appengine.deploy.ui.DeployPreferencesDialog.java

License:Apache License

@Override
protected Control createDialogArea(final Composite parent) {
    Composite dialogArea = (Composite) super.createDialogArea(parent);

    Composite container = new Composite(dialogArea, SWT.NONE);
    content = new StandardDeployPreferencesPanel(container, project, loginService, getLayoutChangedHandler(),
            true /* requireValues */);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(content);

    // we pull in Dialog's content margins which are zeroed out by TitleAreaDialog
    GridDataFactory.fillDefaults().grab(true, true).applyTo(container);
    GridLayoutFactory.fillDefaults()//from  w  w w . ja v  a2s  .co m
            .margins(convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN),
                    convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN))
            .spacing(convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING),
                    convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING))
            .generateLayout(container);

    TitleAreaDialogSupport.create(this, content.getDataBindingContext())
            .setValidationMessageProvider(new ValidationMessageProvider() {
                @Override
                public int getMessageType(ValidationStatusProvider statusProvider) {
                    int type = super.getMessageType(statusProvider);
                    setValid(type != IMessageProvider.ERROR);
                    return type;
                }
            });
    return dialogArea;
}

From source file:mlm.eclipse.ide.jsworkingset.internal.JSWorkingSetPage.java

License:Open Source License

@Override
public void createControl(final Composite pParent) {

    mDataBindingContext = new DataBindingContext(SWTObservables.getRealm(pParent.getDisplay()));

    final Composite composite = new Composite(pParent, SWT.NONE);
    composite.setLayout(GridLayoutFactory.fillDefaults() //
            .numColumns(3) //
            .create());/*from w ww .  j a v a 2 s. co  m*/
    setControl(composite);

    // working set label
    {
        final String workingSetLabel = mWorkingSet != null ? mWorkingSet.getLabel() : ""; //$NON-NLS-1$
        mWorkingSetLabel = WritableValue.withValueType(String.class);
        mWorkingSetLabel.setValue(workingSetLabel);

        final Label label = new Label(composite, SWT.WRAP);
        label.setText("Label:");

        final Text text = new Text(composite, SWT.BORDER | SWT.SINGLE);
        text.setToolTipText("The label for the working set.");
        text.setLayoutData(GridDataFactory.swtDefaults() //
                .align(SWT.FILL, SWT.CENTER) //
                .grab(true, false) //
                .span(2, 1) //
                .create());

        final IValidator validator = (v) -> {

            final String value = (String) v;

            if (value == null || value.trim().isEmpty()) {

                return ValidationStatus.ok();

            }

            return ValidationStatus.info("This may likely be overridden by the script.");

        };

        final UpdateValueStrategy targetToModel = new UpdateValueStrategy() //
                .setAfterConvertValidator(validator) //
        ;

        final ISWTObservableValue target = WidgetProperties.text(SWT.Modify) //
                .observe(text) //
        ;

        final Binding binding = mDataBindingContext.bindValue(target, mWorkingSetLabel, targetToModel, null);
        binding.getValidationStatus().setValue(ValidationStatus.ok());

        ControlDecorationSupport.create(binding, SWT.LEFT | SWT.TOP);
    }

    // working set name
    {
        final String workingSetName = JSWorkingSetPrefs.getName(mWorkingSet);
        mWorkingSetName = WritableValue.withValueType(String.class);
        mWorkingSetName.setValue(workingSetName);

        final Label label = new Label(composite, SWT.WRAP);
        label.setText("Name:");

        final Text text = new Text(composite, SWT.BORDER | SWT.SINGLE);
        text.setToolTipText("The name for the working set.");
        text.setLayoutData(GridDataFactory.swtDefaults() //
                .align(SWT.FILL, SWT.CENTER) //
                .grab(true, false) //
                .span(2, 1) //
                .create());
        text.setFocus();

        final IValidator validator = (v) -> {

            final String value = (String) v;

            if (value == null || value.trim().isEmpty()) {

                return ValidationStatus.error("Please provide a name!");

            }

            return ValidationStatus.ok();

        };

        final UpdateValueStrategy targetToModel = new UpdateValueStrategy() //
                .setAfterConvertValidator(validator) //
        ;

        final ISWTObservableValue target = WidgetProperties.text(SWT.Modify) //
                .observe(text) //
        ;

        final Binding binding = mDataBindingContext.bindValue(target, mWorkingSetName, targetToModel, null);

        ControlDecorationSupport.create(binding, SWT.LEFT | SWT.TOP);
    }

    // script
    {
        final String workingSetScript = JSWorkingSetPrefs.getScript(mWorkingSet);
        mWorkingSetScript = WritableValue.withValueType(String.class);
        mWorkingSetScript.setValue(workingSetScript);

        final String linkText = String.format("<a href=\"native\">%s</a>", "Script:"); //$NON-NLS-1$
        final Link link = new Link(composite, SWT.NONE);
        link.setText(linkText);

        final Text text = new Text(composite, SWT.BORDER | SWT.SINGLE);
        text.setToolTipText("Enter a workspace file.");
        text.setLayoutData(GridDataFactory.swtDefaults() //
                .align(SWT.FILL, SWT.CENTER) //
                .grab(true, false) //
                .create());

        link.addListener(SWT.Selection, (e) -> {

            final String filename = text.getText();
            if (filename.isEmpty()) {

                return;

            }

            final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            final IResource resource = root.findMember(filename);
            if (resource == null || !resource.isAccessible() || resource.getType() != IResource.FILE) {

                return;

            }

            final IWorkbench workbench = PlatformUI.getWorkbench();
            final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
            if (window == null) {

                return;

            }

            final IWorkbenchPage page = window.getActivePage();
            if (page == null) {

                return;

            }

            try {

                final FileEditorInput input = new FileEditorInput((IFile) resource);
                final IEditorDescriptor editor = workbench.getEditorRegistry()
                        .getDefaultEditor(resource.getName());
                final String editorId = editor != null ? editor.getId() : EditorsUI.DEFAULT_TEXT_EDITOR_ID;
                page.openEditor(input, editorId);

            } catch (final PartInitException ex) {

                Activator.log(IStatus.ERROR, String.format("Failed to open editor for '%s'!", filename), ex); //$NON-NLS-1$

            }

        });

        // TODO history or path completion for script?

        final Button button = new Button(composite, SWT.PUSH);
        button.setText("Browse..."); //$NON-NLS-1$
        button.setLayoutData(GridDataFactory.swtDefaults() //
                .align(SWT.FILL, SWT.CENTER) //
                .grab(false, false) //
                .create());
        button.addListener(SWT.Selection, (e) -> {

            final Shell shell = button.getShell();
            final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            final SelectionDialog dialog = new ResourceListSelectionDialog(shell, root, IResource.FILE);
            dialog.setTitle("Select a JavaScript file");
            if (dialog.open() == Window.OK) {

                final Object[] objects = dialog.getResult();
                if (objects.length == 1) {

                    final IResource resource = (IResource) objects[0];
                    final IPath path = resource.getFullPath();
                    text.setText(path.toString());

                } else {

                    text.setText(""); //$NON-NLS-1$

                }

            }

        });

        final IValidator validator = (v) -> {

            final String value = (String) v;
            if (value == null || value.trim().isEmpty()) {

                return ValidationStatus.error("Please provide a script!");

            }

            final IResource member = ResourcesPlugin.getWorkspace().getRoot().findMember(value);
            if (member == null || !member.exists()) {

                return ValidationStatus.error("The provided script does not exist in the workspace!");

            }

            return ValidationStatus.ok();

        };

        final UpdateValueStrategy targetToModel = new UpdateValueStrategy() //
                .setAfterConvertValidator(validator) //
        ;

        final ISWTObservableValue target = WidgetProperties.text(SWT.Modify) //
                .observe(text) //
        ;

        final Binding binding = mDataBindingContext.bindValue(target, mWorkingSetScript, targetToModel, null);

        ControlDecorationSupport.create(binding, SWT.LEFT | SWT.TOP);
    }

    final WizardPageSupport wizardPageSupport = WizardPageSupport.create(this, mDataBindingContext);
    wizardPageSupport.setValidationMessageProvider(new ValidationMessageProvider() {

        @Override
        public String getMessage(final ValidationStatusProvider pStatusProvider) {

            final String message = super.getMessage(pStatusProvider);
            if (message != null) {

                return message;

            }

            return "Enter a working set label and name and select the script that makes up the content of the working set.";

        }

    });

    // provide a clean start
    setErrorMessage(null);
    setMessage(
            "Enter a working set label and name and select the script that makes up the content of the working set.");

}

From source file:org.eclipse.egit.gitflow.ui.internal.dialogs.InitDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {
    Composite area = (Composite) super.createDialogArea(parent);
    Composite container = new Composite(area, SWT.NONE);
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    GridLayoutFactory.swtDefaults().numColumns(4).applyTo(container);

    createInputs(container);/*from  w  w  w  . jav a 2s  .  c  om*/

    DataBindingContext bindingContext = initDataBinding();

    TitleAreaDialogSupport.create(this, bindingContext)
            .setValidationMessageProvider(new ValidationMessageProvider() {
                @Override
                public String getMessage(ValidationStatusProvider statusProvider) {
                    if (statusProvider == null) {
                        return InitDialog_chooseBranchNamesAndPrefixes;
                    }
                    return super.getMessage(statusProvider);
                }

                @Override
                public int getMessageType(ValidationStatusProvider statusProvider) {
                    int type = super.getMessageType(statusProvider);
                    Button okButton = getButton(OK_ID);
                    if (okButton != null) {
                        okButton.setEnabled(type != ERROR);
                    }

                    return type;
                }
            });

    return area;
}