Package de.innovationgate.eclipse.wgadesigner.wizards

Source Code of de.innovationgate.eclipse.wgadesigner.wizards.NewWGAPluginPage

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.wizards;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.TypedEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.wga.model.ValidationError;
import de.innovationgate.wga.model.WGADesignConfigurationModel;

public class NewWGAPluginPage extends WizardPage {

  private Combo _comboRuntimeList;
  private Text _txtPluginUniqueName;

  private ISelection _selection;
  private WGARuntime _selectedRuntime;
  private Map<String, WGARuntime> _wgaRuntimeProjects = new HashMap<String, WGARuntime>();
  private static String WGARUNTIME_NONE = "none";

  public NewWGAPluginPage(ISelection selection) {
    super("wizardPage");
    _selection = selection;
    setTitle("WGA Plugin");
    setDescription("This wizard creates a new WGA Plugin.");
    setPageComplete(false);
  }

  public void createControl(Composite parent) {

    initialize();

    Composite container = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.verticalSpacing = 9;
    container.setLayout(layout);
   
    new Label(container, SWT.NULL).setText("Plugin unique name:");
    _txtPluginUniqueName = new Text(container, SWT.BORDER);
    GridData g = new GridData(GridData.FILL_HORIZONTAL);
    g.horizontalSpan = 1;
    _txtPluginUniqueName.setLayoutData(g);

    _txtPluginUniqueName.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        dialogChanged(e);
      }
    });
    new Label(container, SWT.NULL).setText("");
    new Label(container, SWT.NULL).setText("Should be in java package format for e.g. 'de.innovationgate.myplugin'");

    new Label(container, SWT.NULL).setText("Runtime: ");
    _comboRuntimeList = new Combo(container, SWT.READ_ONLY);
    ArrayList<String> projectnames = new ArrayList<String>(_wgaRuntimeProjects.keySet());
    Collections.sort(projectnames);
    projectnames.add(0, WGARUNTIME_NONE);

    _comboRuntimeList.setItems(projectnames.toArray(new String[0]));

    if (_selectedRuntime != null) {
      _comboRuntimeList.select(_comboRuntimeList.indexOf(_selectedRuntime.getName()));
    } else {
      _comboRuntimeList.select(0);
    }

    _comboRuntimeList.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        _selectedRuntime = _wgaRuntimeProjects.get(_comboRuntimeList.getText());
        dialogChanged(e);
      }
    });
   

    setControl(container);
  }


  private void initialize() {
    _wgaRuntimeProjects = WGADesignerPlugin.getAllRuntimes();
    if (_selection instanceof StructuredSelection) {
      StructuredSelection treeSel = (StructuredSelection) _selection;
      Object selectedElement = treeSel.getFirstElement();

      if (selectedElement instanceof IResource) {
        IResource resource = (IResource) selectedElement;
        IProject selectedRuntimeProject = resource.getProject();
        try {
          _selectedRuntime = (WGARuntime) selectedRuntimeProject.getNature(WGADesignerPlugin.NATURE_WGA_RUNTIME);
        } catch (CoreException e) {
          WGADesignerPlugin.getDefault().logError("This project does not exist. This project is not open. The project nature extension could not be found.", e);
        }
      }     
    }
  }

  public void dialogChanged(TypedEvent event) {
    applyStatus("", IStatus.OK);
    setPageComplete(true);

    if (!isEmpty(_txtPluginUniqueName.getText())) {
      String pluginUName = _txtPluginUniqueName.getText();
     
      List<ValidationError> errors = WGADesignConfigurationModel.validatePluginUniqueName(pluginUName);
      if (!errors.isEmpty()) {
        applyStatus(errors.get(0).getMessage(), IStatus.ERROR);
        setPageComplete(false);
        return;
      }
    } else {
      applyStatus("Please define a unique name for the plugin.", IStatus.ERROR);
      setPageComplete(false);
      return;
   
   
   
    String pluginName = getPluginName();
    // validate pluginname
    if (!WorkbenchUtils.isValidResourceName(pluginName)) {
      applyStatus("Plugin name '" + pluginName + "' contains illegal characters. Only alphanumeric ascii characters and '_', '-' are allowed.", IStatus.ERROR);
      setPageComplete(false);
      return;
    }

    if (getRuntime() != null && getRuntime().hasPlugin(pluginName)) {
      applyStatus("A plugin with name '" + pluginName + "' already exists in runtime '" + getRuntime().getName() + "'.", IStatus.ERROR);
      setPageComplete(false);
      return;
    }
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(pluginName);
    if (project != null && project.exists()) {
      applyStatus("A project with name '" + pluginName + "' already exists in the workspace.", IStatus.ERROR);
      setPageComplete(false);
      return;
    }
   
    if (_comboRuntimeList.getText().equals(WGARUNTIME_NONE)) {
      //applyStatus("Please select a runtime for the new plugin.", IStatus.ERROR);
      //setPageComplete(false);
      //return;
    }
   
    if (getPluginName().equals(getPluginUniqueName())) {
      applyStatus("Using an unqualified plugin unique name without '.' is discouraged", IStatus.WARNING);
    }
     
  }

  private boolean isEmpty(String value) {
    if (value != null) {
      return value.length() == 0;
    } else {
      return true;
    }
  }

  private void applyToStatusLine(IStatus status) {
    String message = status.getMessage();

    if (message.length() == 0)
      message = null;
    switch (status.getSeverity()) {
    case IStatus.OK:
      setErrorMessage(null);
      setMessage(message);
      break;
    case IStatus.WARNING:
      setErrorMessage(null);
      setMessage(message, WizardPage.WARNING);
      break;
    case IStatus.INFO:
      setErrorMessage(null);
      setMessage(message, WizardPage.INFORMATION);
      break;
    default:
      setErrorMessage(message);
      setMessage(null);
      break;
    }
  }

  private void applyStatus(String message, int severity) {
    Status status = new Status(severity, WGADesignerPlugin.PLUGIN_ID, message);
    applyToStatusLine(status);
  }

  public String getPluginName() {
    String uname = getPluginUniqueName();
    if (uname != null) {
      List<String> tokens =  WGUtils.deserializeCollection(uname, ".");
      return tokens.get(tokens.size()-1);
    } else {
      return null;
    }
  }

  public WGARuntime getRuntime() {
    _selectedRuntime = _wgaRuntimeProjects.get(_comboRuntimeList.getText());
    return _selectedRuntime;
  }

  public String getPluginUniqueName() {
    return _txtPluginUniqueName.getText();
  }

}
TOP

Related Classes of de.innovationgate.eclipse.wgadesigner.wizards.NewWGAPluginPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.