com.google.cloud.tools.eclipse.appengine.newproject.AppEngineStandardWizardPage.java Source code

Java tutorial

Introduction

Here is the source code for com.google.cloud.tools.eclipse.appengine.newproject.AppEngineStandardWizardPage.java

Source

/*
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.tools.eclipse.appengine.newproject;

import java.io.File;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import com.google.cloud.tools.eclipse.appengine.libraries.model.Library;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineLibrariesSelectorGroup;
import com.google.cloud.tools.eclipse.appengine.ui.AppEngineImages;
import com.google.cloud.tools.eclipse.usagetracker.AnalyticsEvents;
import com.google.cloud.tools.eclipse.usagetracker.AnalyticsPingManager;

/**
 * UI to collect all information necessary to create a new App Engine Standard Java Eclipse project.
 */
public class AppEngineStandardWizardPage extends WizardNewProjectCreationPage {

    private Text javaPackageField;
    private AppEngineLibrariesSelectorGroup appEngineLibrariesSelectorGroup;

    public AppEngineStandardWizardPage() {
        super("basicNewProjectPage"); //$NON-NLS-1$
        // todo instead of hard coding strings, read the wizard.name and wizard.description properties
        // from plugins/com.google.cloud.tools.eclipse.appengine.newproject/plugin.properties
        this.setTitle("App Engine Standard Project");
        this.setDescription("Create a new App Engine Standard Project in the workspace.");

        this.setImageDescriptor(AppEngineImages.googleCloudPlatform(32));
    }

    // todo is there a way to call this for a test?
    @Override
    public void createControl(Composite parent) {
        super.createControl(parent);
        AnalyticsPingManager.getInstance().sendPing(AnalyticsEvents.APP_ENGINE_NEW_PROJECT_WIZARD,
                AnalyticsEvents.APP_ENGINE_NEW_PROJECT_WIZARD_TYPE,
                AnalyticsEvents.APP_ENGINE_NEW_PROJECT_WIZARD_TYPE_NATIVE, parent.getShell());

        Composite container = (Composite) getControl();

        ModifyListener pageValidator = new PageValidator();

        // Java package name
        Label packageNameLabel = new Label(container, SWT.NONE);
        packageNameLabel.setText("Java package:");
        javaPackageField = new Text(container, SWT.BORDER);
        GridData javaPackagePosition = new GridData(GridData.FILL_HORIZONTAL);
        javaPackagePosition.horizontalSpan = 2;
        javaPackageField.setLayoutData(javaPackagePosition);
        javaPackageField.addModifyListener(pageValidator);

        // Manage APIs
        appEngineLibrariesSelectorGroup = new AppEngineLibrariesSelectorGroup(container);

        Dialog.applyDialogFont(container);
    }

    @Override
    public boolean validatePage() {
        if (!super.validatePage()) {
            return false;
        }

        return validateLocalFields();
    }

    private boolean validateLocalFields() {
        String packageName = javaPackageField.getText();
        IStatus packageStatus = JavaPackageValidator.validate(packageName);
        if (!packageStatus.isOK()) {
            setErrorMessage("Illegal package name: " + packageStatus.getMessage());
            return false;
        }

        File parent = getLocationPath().toFile();
        File projectDirectory = new File(parent, getProjectName());
        if (projectDirectory.exists()) {
            setErrorMessage("Project location already exists: " + projectDirectory);
            return false;
        }

        return true;
    }

    private final class PageValidator implements ModifyListener {
        @Override
        public void modifyText(ModifyEvent event) {
            setPageComplete(validatePage());
        }
    }

    public String getPackageName() {
        return this.javaPackageField.getText();
    }

    public List<Library> getSelectedLibraries() {
        return appEngineLibrariesSelectorGroup.getSelectedLibraries();
    }

    @Override
    public void dispose() {
        appEngineLibrariesSelectorGroup.dispose();
        super.dispose();
    }
}