1 package net.sourceforge.phpeclipse.wizards;
 
   3 /**********************************************************************
 
   4  Copyright (c) 2000, 2002 IBM Corp. and others.
 
   5  All rights reserved. This program and the accompanying materials
 
   6  are made available under the terms of the Common Public License v1.0
 
   7  which accompanies this distribution, and is available at
 
   8  http://www.eclipse.org/legal/cpl-v10.html
 
  11  IBM Corporation - Initial implementation
 
  12  Klaus Hartlage - www.eclipseproject.de
 
  13  **********************************************************************/
 
  15 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
 
  17 import org.eclipse.core.resources.IContainer;
 
  18 import org.eclipse.core.resources.IFolder;
 
  19 import org.eclipse.core.resources.IProject;
 
  20 import org.eclipse.core.resources.IResource;
 
  21 import org.eclipse.core.resources.ResourcesPlugin;
 
  22 import org.eclipse.core.runtime.IPath;
 
  23 import org.eclipse.core.runtime.Path;
 
  24 import org.eclipse.jface.dialogs.IDialogPage;
 
  25 import org.eclipse.jface.viewers.ISelection;
 
  26 import org.eclipse.jface.viewers.IStructuredSelection;
 
  27 import org.eclipse.jface.wizard.WizardPage;
 
  28 import org.eclipse.swt.SWT;
 
  29 import org.eclipse.swt.events.ModifyEvent;
 
  30 import org.eclipse.swt.events.ModifyListener;
 
  31 import org.eclipse.swt.events.SelectionAdapter;
 
  32 import org.eclipse.swt.events.SelectionEvent;
 
  33 import org.eclipse.swt.layout.GridData;
 
  34 import org.eclipse.swt.layout.GridLayout;
 
  35 import org.eclipse.swt.widgets.Button;
 
  36 import org.eclipse.swt.widgets.Composite;
 
  37 import org.eclipse.swt.widgets.Label;
 
  38 import org.eclipse.swt.widgets.Text;
 
  39 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
 
  42  * The "New" wizard page allows setting the container for the new file as well
 
  43  * as the file name. The page will only accept file name without the extension
 
  44  * OR with the extension that matches the expected one (cs).
 
  47 public class PHPFileWizardPage extends WizardPage {
 
  48   private Text containerText;
 
  50   private Text fileText;
 
  52   private ISelection selection;
 
  55    * Constructor for SampleNewWizardPage.
 
  59   public PHPFileWizardPage(ISelection selection) {
 
  61     setTitle(PHPWizardMessages.getString("WizardPage.title"));
 
  62     setDescription(PHPWizardMessages.getString("WizardPage.description"));
 
  63     this.selection = selection;
 
  67    * @see IDialogPage#createControl(Composite)
 
  69   public void createControl(Composite parent) {
 
  70     Composite container = new Composite(parent, SWT.NULL);
 
  71     GridLayout layout = new GridLayout();
 
  72     container.setLayout(layout);
 
  73     layout.numColumns = 3;
 
  74     layout.verticalSpacing = 9;
 
  75     Label label = new Label(container, SWT.NULL);
 
  76     label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
 
  78     containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
 
  79     GridData gd = new GridData(GridData.FILL_HORIZONTAL);
 
  80     containerText.setLayoutData(gd);
 
  81     containerText.addModifyListener(new ModifyListener() {
 
  82       public void modifyText(ModifyEvent e) {
 
  87     Button button = new Button(container, SWT.PUSH);
 
  88     button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
 
  89     button.addSelectionListener(new SelectionAdapter() {
 
  90       public void widgetSelected(SelectionEvent e) {
 
  94     label = new Label(container, SWT.NULL);
 
  95     label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
 
  97     fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
 
  98     gd = new GridData(GridData.FILL_HORIZONTAL);
 
  99     fileText.setLayoutData(gd);
 
 100     fileText.addModifyListener(new ModifyListener() {
 
 101       public void modifyText(ModifyEvent e) {
 
 107     setControl(container);
 
 111    * Tests if the current workbench selection is a suitable container to use.
 
 114   private void initialize() {
 
 115     if (selection != null && selection.isEmpty() == false
 
 116         && selection instanceof IStructuredSelection) {
 
 117       IStructuredSelection ssel = (IStructuredSelection) selection;
 
 120       Object obj = ssel.getFirstElement();
 
 121       if (obj instanceof IResource) {
 
 122         IContainer container;
 
 123         if (obj instanceof IContainer)
 
 124           container = (IContainer) obj;
 
 126           container = ((IResource) obj).getParent();
 
 127         containerText.setText(container.getFullPath().toString());
 
 130     fileText.setText("index.php");
 
 134    * Uses the standard container selection dialog to choose the new value for
 
 135    * the container field.
 
 138   private void handleBrowse() {
 
 139     ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
 
 140         ResourcesPlugin.getWorkspace().getRoot(), false, PHPWizardMessages
 
 141             .getString("WizardPage.selectNewFileContainer"));
 
 142     if (dialog.open() == ContainerSelectionDialog.OK) {
 
 143       Object[] result = dialog.getResult();
 
 144       if (result.length == 1) {
 
 145         IContainer container = (IContainer) result[0];
 
 146         containerText.setText(container.getFullPath().toString());
 
 152    * Ensures that both text fields are set.
 
 154   private void dialogChanged() {
 
 155     String container = getContainerName();
 
 156     String fileName = getFileName();
 
 158     if (container.length() == 0) {
 
 159       updateStatus(PHPWizardMessages
 
 160           .getString("WizardPage.containerMustBeSpecified"));
 
 163     if (fileName.length() == 0) {
 
 164       updateStatus("WizardPage.nameMustBeSpecified");
 
 168     if (!PHPFileUtil.isPHPFileName(fileName)) {
 
 169       updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
 
 175   private void updateStatus(String message) {
 
 176     setErrorMessage(message);
 
 177     setPageComplete(message == null);
 
 180   public String getContainerName() {
 
 181     return containerText.getText();
 
 184   public String getFileName() {
 
 185     return fileText.getText();
 
 189    * @see WizardPage#isPageComplete()
 
 191   public boolean isPageComplete() {
 
 192     return !checkFolderForExistingFile() && super.isPageComplete();
 
 196    * Finds the current directory where the file should be created
 
 198   protected boolean checkFolderForExistingFile() {
 
 199     boolean result = false;
 
 201     if (containerText.getText() != null) {
 
 202       IPath containerPath = new Path(containerText.getText().trim());
 
 203       if (containerPath.segmentCount() > 1) {
 
 204         IFolder container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
 
 206         if (container != null && container.exists()) {
 
 207           IResource file = container.getFile(fileText.getText().trim());
 
 208           if (file != null && file.exists()) {
 
 209             this.setErrorMessage(PHPWizardMessages
 
 210                 .getString("WizardPage.fileAlreadyExists"));
 
 216         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
 
 217             containerText.getText().trim());
 
 218         if (project != null && project.exists()) {
 
 219           IResource file = project.getFile(fileText.getText().trim());
 
 220           if (file != null && file.exists()) {
 
 221             this.setErrorMessage(PHPWizardMessages
 
 222                 .getString("WizardPage.fileAlreadyExists"));
 
 230       ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());