view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/NewProjectInformationPage.java @ 147:ee8e92ed83ba default tip

Add hints for fields in new project dialog Add some hints to clarify/illustrate what valid contents of fields in the new project dailog might look like.
author Omair Majid <omajid@redhat.com>
date Tue, 12 Aug 2014 09:50:52 -0400
parents eef52fe80553
children
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin.wizards;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.redhat.thermostat.tools.eclipse.plugin.Messages;
import com.redhat.thermostat.tools.eclipse.plugin.Validators;

public class NewProjectInformationPage extends WizardPage {

    static final String PAGE_NAME = "newProjectInformation"; //$NON-NLS-1$

    private ProjectModel model = new ProjectModel();
    private DataBindingContext context;

    public NewProjectInformationPage() {
        super(PAGE_NAME);
        setTitle(Messages.NewProjectInformationPage_wizardTitle);
        setDescription(Messages.NewProjectInformationPage_wizardDescription);
    }

    @Override
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout(2, false);
        container.setLayout(layout);

        context = new DataBindingContext();

        createLabelAndInputField(container,
                Messages.NewProjectInformationPage_groupIdLabel,
                Messages.NewProjectInformationPage_groupIdHint,
                "groupId", //$NON-NLS-1$
                new Validators.ValidIdRequired(Messages.NewProjectInformationPage_groupIdLabel));

        createLabelAndInputField(container,
                Messages.NewProjectInformationPage_artifactIdLabel,
                Messages.NewProjectInformationPage_artifactIdHint,
                "artifactId", //$NON-NLS-1$
                new Validators.ValidIdRequired(Messages.NewProjectInformationPage_artifactIdLabel));

        createLabelAndInputField(container,
                Messages.NewProjectInformationPage_versionLabel,
                Messages.NewProjectInformationPage_versionHint,
                "version", //$NON-NLS-1$
                new Validators.NonEmptyStringRequired(Messages.NewProjectInformationPage_versionErrorMessage));

        createLabelAndInputField(container,
                Messages.NewProjectInformationPage_packagePrefixLabel,
                Messages.NewProjectInformationPage_packagePrefixHint,
                "packageName", //$NON-NLS-1$
                new Validators.PackageNameRequired());

        createLabelAndInputField(container,
                Messages.NewProjectInformationPage_thermostatVersionLabel,
                Messages.NewProjectInformationPage_thermostatVersionHint,
                "thermostatVersion", //$NON-NLS-1$
                new Validators.VersionRequired(Messages.NewProjectInformationPage_thermostatVersionErrorMessage));

        setControl(container);
    }

    private void createLabelAndInputField(Composite container, String labelText, String hint, String propertyName, IValidator validator) {
        Label label = new Label(container, SWT.NULL);
        label.setText(labelText);

        Text text = new Text(container, SWT.BORDER | SWT.SINGLE);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        hookHintToDialog(container.getShell(), text, hint);

        IObservableValue widgetValue = WidgetProperties.text(SWT.Modify).observe(text);
        IObservableValue modelValue = PojoObservables.observeValue(model, propertyName);
        UpdateValueStrategy targetToModelStrategy = new UpdateValueStrategy();
        targetToModelStrategy.setBeforeSetValidator(validator);
        Binding binding = context.bindValue(widgetValue, modelValue, targetToModelStrategy, null);
        ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT);
    }

    private void hookHintToDialog(Shell shell, Text text, final String hint) {
        text.addFocusListener(new FocusListener() {
            private String oldMessage;
            private int oldType;
            @Override
            public void focusGained(FocusEvent e) {
                oldMessage = NewProjectInformationPage.this.getMessage();
                oldType = NewProjectInformationPage.this.getMessageType();
                NewProjectInformationPage.this.setMessage(hint, DialogPage.INFORMATION);
            }
            @Override
            public void focusLost(FocusEvent e) {
                NewProjectInformationPage.this.setMessage(oldMessage, oldType);
            }
        });
    }

    @Override
    public void dispose() {
        context.dispose();

        super.dispose();
    }

    public ProjectModel getProjectInformation() {
        return model;
    }

    static class ProjectModel {
        private String groupId;
        private String artifactId;
        private String version;

        private String packageName;

        private String thermostatVersion;

        public String getGroupId() {
            return groupId;
        }

        public void setGroupId(String groupId) {
            this.groupId = groupId;
        }

        public String getArtifactId() {
            return artifactId;
        }

        public void setArtifactId(String artifactId) {
            this.artifactId = artifactId;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }

        public String getThermostatVersion() {
            return thermostatVersion;
        }

        public void setThermostatVersion(String thermostatVersion) {
            this.thermostatVersion = thermostatVersion;
        }
    }
}