554b6b40aa488aae33d7fcf4ce83ef0a1eb333d9
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / HTMLFileWizardPage.java
1 package net.sourceforge.phpeclipse.wizards;
2
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
9
10  Contributors:
11  IBM Corporation - Initial implementation
12  Klaus Hartlage - www.eclipseproject.de
13  **********************************************************************/
14
15 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
16
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;
40
41 /**
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).
45  */
46
47 public class HTMLFileWizardPage extends WizardPage {
48   private Text containerText;
49
50   private Text fileText;
51
52   private ISelection selection;
53
54   /**
55    * Constructor for SampleNewWizardPage.
56    * 
57    * @param pageName
58    */
59   public HTMLFileWizardPage(ISelection selection) {
60     super("wizardPage");
61     setTitle(PHPWizardMessages.getString("WizardPage.html.title"));
62     setDescription(PHPWizardMessages.getString("WizardPage.html.description"));
63     this.selection = selection;
64   }
65
66   /**
67    * @see IDialogPage#createControl(Composite)
68    */
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"));
77
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) {
83         dialogChanged();
84       }
85     });
86
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) {
91         handleBrowse();
92       }
93     });
94     label = new Label(container, SWT.NULL);
95     label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
96
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) {
102         dialogChanged();
103       }
104     });
105     initialize();
106     dialogChanged();
107     setControl(container);
108   }
109
110   /**
111    * Tests if the current workbench selection is a suitable container to use.
112    */
113
114   private void initialize() {
115     if (selection != null && selection.isEmpty() == false
116         && selection instanceof IStructuredSelection) {
117       IStructuredSelection ssel = (IStructuredSelection) selection;
118       if (ssel.size() > 1)
119         return;
120       Object obj = ssel.getFirstElement();
121       if (obj instanceof IResource) {
122         IContainer container;
123         if (obj instanceof IContainer)
124           container = (IContainer) obj;
125         else
126           container = ((IResource) obj).getParent();
127         containerText.setText(container.getFullPath().toString());
128       }
129     }
130     fileText.setText("*.html");
131   }
132
133   /**
134    * Uses the standard container selection dialog to choose the new value for
135    * the container field.
136    */
137
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());
147       }
148     }
149   }
150
151   /**
152    * Ensures that both text fields are set.
153    */
154   private void dialogChanged() {
155     String container = getContainerName();
156     String fileName = getFileName();
157
158     if (container.length() == 0) {
159       updateStatus(PHPWizardMessages
160           .getString("WizardPage.containerMustBeSpecified"));
161       return;
162     }
163     if (fileName.length() == 0) {
164       updateStatus("WizardPage.nameMustBeSpecified");
165       return;
166     }
167
168 //    if (!PHPFileUtil.isPHPFileName(fileName)) {
169 //      updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
170 //      return;
171 //    }
172     updateStatus(null);
173   }
174
175   private void updateStatus(String message) {
176     setErrorMessage(message);
177     setPageComplete(message == null);
178   }
179
180   public String getContainerName() {
181     return containerText.getText();
182   }
183
184   public String getFileName() {
185     return fileText.getText();
186   }
187
188   /**
189    * @see WizardPage#isPageComplete()
190    */
191   public boolean isPageComplete() {
192     return !checkFolderForExistingFile() && super.isPageComplete();
193   }
194
195   /**
196    * Finds the current directory where the file should be created
197    */
198   protected boolean checkFolderForExistingFile() {
199     boolean result = false;
200
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(
205             containerPath);
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"));
211             result = true;
212           }
213         }
214       } else {
215         // this is a project
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"));
223             result = true;
224           }
225         }
226       }
227     }
228
229     if (!result)
230       ((HTMLFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
231
232     return result;
233   }
234
235 }