view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/NewProjectInformationPage.java @ 135:eef52fe80553

Make wizard page names describe functionality Make it easier to share wizard pages between wizards by making names reflect purpose rather than which wizard is using them.
author Omair Majid <omajid@redhat.com>
date Tue, 03 Jun 2014 17:32:08 -0400
parents com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ThermostatProjectCreationWizardPage.java@f64440e971ac
children ee8e92ed83ba
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 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, "groupId", //$NON-NLS-1$
                new Validators.ValidIdRequired(Messages.NewProjectInformationPage_groupIdLabel));

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

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

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

        createLabelAndInputField(container, Messages.NewProjectInformationPage_thermostatVersionLabel, "thermostatVersion", //$NON-NLS-1$
                new Validators.VersionRequired(Messages.NewProjectInformationPage_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;
        }
    }
}