Package org.playframework.playclipse.wizards

Source Code of org.playframework.playclipse.wizards.PlayWizardPage

package org.playframework.playclipse.wizards;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
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.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import org.eclipse.jdt.core.IJavaElement;

/**
* The "New" wizard page allows setting the container for the new file as well
* as the file name. The page will only accept file name without the extension
* OR with the extension that matches the expected one (java).
*/

public abstract class PlayWizardPage extends WizardPage {
  protected Text containerText;
  protected Text name;
  protected ISelection selection;
  protected IProject project;
 
  protected abstract String description();
  protected abstract String defaultName();

  /**
   * Constructor for SampleNewWizardPage.
   *
   * @param pageName
   */
  public PlayWizardPage(ISelection selection) {
    super("wizardPage");
    setDescription(description());
    this.selection = selection;
  }

  /**
   * @see IDialogPage#createControl(Composite)
   */
  @Override
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    container.setLayout(layout);
    layout.numColumns = 3;
    layout.verticalSpacing = 9;
    Label label = new Label(container, SWT.NULL);
    label.setText("&Source folder:");

    containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    containerText.setLayoutData(gd);
    containerText.addModifyListener(new ModifyListener() {
      @Override
      public void modifyText(ModifyEvent e) {
        dialogChanged();
      }
    });

    Button button = new Button(container, SWT.PUSH);
    button.setText("Browse...");
    button.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        handleBrowse();
      }
    });
    label = new Label(container, SWT.NULL);
    label.setText("Controller &Name:");

    name = new Text(container, SWT.BORDER | SWT.SINGLE);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    name.setLayoutData(gd);
    name.addModifyListener(new ModifyListener() {
      @Override
      public void modifyText(ModifyEvent e) {
        dialogChanged();
      }
    });
   
    initialize();
    dialogChanged();
    setControl(container);
  }

  /**
   * Tests if the current workbench selection is a suitable container to use.
   */

  protected void initialize() {
    if (selection != null && selection.isEmpty() == false
        && selection instanceof IStructuredSelection) {
      IStructuredSelection ssel = (IStructuredSelection) selection;
      if (ssel.size() > 1)
        return;
      Object obj = ssel.getFirstElement();
      if (obj instanceof IJavaElement) {
        obj = ((IJavaElement)obj).getResource();
      }
      if (obj instanceof IResource) {
        IContainer container;
        if (obj instanceof IContainer) {
          container = (IContainer) obj;
        } else {
          container = ((IResource) obj).getParent();
        }
        containerText.setText(container.getFullPath().toString());
        while (container != null) {
          if (container instanceof IProject) {
            project = (IProject)container;
          }
          container = container.getParent();
        }
      }
    }
    name.setText(defaultName());
  }

  /**
   * Uses the standard container selection dialog to choose the new value for
   * the container field.
   */

  protected void handleBrowse() {
    ContainerSelectionDialog dialog = new ContainerSelectionDialog(
        getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
        "Select new file container");
    if (dialog.open() == ContainerSelectionDialog.OK) {
      Object[] result = dialog.getResult();
      if (result.length == 1) {
        containerText.setText(((Path) result[0]).toString());
      }
    }
  }

  /**
   * Ensures that both text fields are set.
   */

  protected void dialogChanged() {
    IResource container = ResourcesPlugin.getWorkspace().getRoot()
        .findMember(new Path(getContainerName()));
    String fileName = getControllerName();

    if (getContainerName().length() == 0) {
      updateStatus("File container must be specified");
      return;
    }
    if (container == null
        || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
      updateStatus("File container must exist");
      return;
    }
    if (!container.isAccessible()) {
      updateStatus("Project must be writable");
      return;
    }
    if (fileName.length() == 0) {
      updateStatus("File name must be specified");
      return;
    }
    if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
      updateStatus("File name must be valid");
      return;
    }
    updateStatus(null);
  }

  private void updateStatus(String message) {
    setErrorMessage(message);
    setPageComplete(message == null);
  }

  public Map<String, String> getParameters() {
    Map<String, String> result = new HashMap<String, String>();
    result.put("name", name.getText());
    result.put("container", containerText.getText());
    return result;
  }

  private String getContainerName() {
    return containerText.getText();
  }

  private String getControllerName() {
    return name.getText();
  }
}
TOP

Related Classes of org.playframework.playclipse.wizards.PlayWizardPage

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.