view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java @ 127:f64440e971ac

Validate thermostat versions in project wizard
author Omair Majid <omajid@redhat.com>
date Fri, 28 Mar 2014 12:52:27 -0400
parents f425cf9c32e8
children
line wrap: on
line source

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

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.swt.WidgetProperties;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.Text;

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

public class ThermostatProjectCreationWizardPage extends WizardPage {

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

    public ThermostatProjectCreationWizardPage() {
        super("wizardPage"); //$NON-NLS-1$
        setTitle(Messages.ThermostatProjectCreationWizardPage_wizardTitle);
        setDescription(Messages.ThermostatProjectCreationWizardPage_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.ThermostatProjectCreationWizardPage_groupIdLabel, "groupId", //$NON-NLS-1$
                new Validators.ValidIdRequired(Messages.ThermostatProjectCreationWizardPage_groupIdLabel));

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

        createLabelAndInputField(container, Messages.ThermostatProjectCreationWizardPage_versionLabel, "version", //$NON-NLS-1$
                new Validators.NonEmptyStringRequired(Messages.ThermostatProjectCreationWizardPage_versionErrorMessage));

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

        createLabelAndInputField(container, Messages.ThermostatProjectCreationWizardPage_thermostatVersionLabel, "thermostatVersion", //$NON-NLS-1$
                new Validators.VersionRequired(Messages.ThermostatProjectCreationWizardPage_thermostatVersionErrorMessage));

        setControl(container);

        WizardPageSupport.create(this, context);
    }

    private void createLabelAndInputField(Composite container, String labelText, 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));
        IObservableValue widgetValue = WidgetProperties.text(SWT.Modify).observe(text);
        IObservableValue modelValue = PojoObservables.observeValue(model, propertyName);
        UpdateValueStrategy targetToModelStrategy = new UpdateValueStrategy();
        targetToModelStrategy.setBeforeSetValidator(validator);
        context.bindValue(widgetValue, modelValue, targetToModelStrategy, null);
    }

    @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;
        }
    }
}