intial version
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / PHPFileWizardPage.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 org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
37
38 /**
39  * The "New" wizard page allows setting the container for
40  * the new file as well as the file name. The page
41  * will only accept file name without the extension OR
42  * with the extension that matches the expected one (cs).
43  */
44
45 public class PHPFileWizardPage extends WizardPage {
46         private Text containerText;
47         private Text fileText;
48         private ISelection selection;
49
50         /**
51          * Constructor for SampleNewWizardPage.
52          * @param pageName
53          */
54         public PHPFileWizardPage(ISelection selection) {
55                 super("wizardPage");
56                 setTitle(PHPWizardMessages.getString("WizardPage.title"));
57                 setDescription(PHPWizardMessages.getString("WizardPage.description"));
58                 this.selection = selection;
59         }
60
61         /**
62          * @see IDialogPage#createControl(Composite)
63          */
64         public void createControl(Composite parent) {
65                 Composite container = new Composite(parent, SWT.NULL);
66                 GridLayout layout = new GridLayout();
67                 container.setLayout(layout);
68                 layout.numColumns = 3;
69                 layout.verticalSpacing = 9;
70                 Label label = new Label(container, SWT.NULL);
71                 label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
72
73                 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
74                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
75                 containerText.setLayoutData(gd);
76                 containerText.addModifyListener(new ModifyListener() {
77                         public void modifyText(ModifyEvent e) {
78                                 dialogChanged();
79                         }
80                 });
81
82                 Button button = new Button(container, SWT.PUSH);
83                 button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
84                 button.addSelectionListener(new SelectionAdapter() {
85                         public void widgetSelected(SelectionEvent e) {
86                                 handleBrowse();
87                         }
88                 });
89                 label = new Label(container, SWT.NULL);
90                 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
91
92                 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
93                 gd = new GridData(GridData.FILL_HORIZONTAL);
94                 fileText.setLayoutData(gd);
95                 fileText.addModifyListener(new ModifyListener() {
96                         public void modifyText(ModifyEvent e) {
97                                 dialogChanged();
98                         }
99                 });
100                 initialize();
101                 dialogChanged();
102                 setControl(container);
103         }
104         
105         /**
106          * Tests if the current workbench selection is a suitable
107          * container to use.
108          */
109         
110         private void initialize() {
111                 if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) {
112                         IStructuredSelection ssel = (IStructuredSelection)selection;
113                         if (ssel.size()>1) return;
114                         Object obj = ssel.getFirstElement();
115                         if (obj instanceof IResource) {
116                                 IContainer container;
117                                 if (obj instanceof IContainer)
118                                         container = (IContainer)obj;
119                                 else
120                                         container = ((IResource)obj).getParent();
121                                 containerText.setText(container.getFullPath().toString());
122                         }
123                 }
124                 fileText.setText("index.php");
125         }
126         
127         /**
128          * Uses the standard container selection dialog to
129          * choose the new value for the container field.
130          */
131
132         private void handleBrowse() {
133                 ContainerSelectionDialog dialog =
134                         new ContainerSelectionDialog(
135                                 getShell(),
136                                 ResourcesPlugin.getWorkspace().getRoot(),
137                                 false,
138                                 PHPWizardMessages.getString("WizardPage.selectNewFileContainer"));
139                 if (dialog.open() == ContainerSelectionDialog.OK) {
140                         Object[] result = dialog.getResult();
141                         if (result.length == 1) {
142                                 IContainer container = (IContainer) result[0];
143                                 containerText.setText(container.getFullPath().toString());
144                         }
145                 }
146         }
147         
148         /**
149          * Ensures that both text fields are set.
150          */
151
152         private void dialogChanged() {
153                 String container = getContainerName();
154                 String fileName = getFileName();
155
156                 if (container.length() == 0) {
157                         updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
158                         return;
159                 }
160                 if (fileName.length() == 0) {
161                         updateStatus("WizardPage.nameMustBeSpecified");
162                         return;
163                 }
164                 int dotLoc = fileName.indexOf('.');
165                 if (dotLoc != -1) {
166                         String ext = fileName.substring(dotLoc + 1);
167                         if (ext.equalsIgnoreCase("php") == false) {
168                                 updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
169                                 return;
170                         }
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         public String getFileName() {
184                 return fileText.getText();
185         }
186         
187         /**
188          * @see WizardPage#isPageComplete()
189          */
190         public boolean isPageComplete() {
191                 return !checkFolderForExistingFile() && super.isPageComplete();
192         }
193
194         /**
195          * Finds the current directory where the file should be created
196          */
197         protected boolean checkFolderForExistingFile() {
198                 boolean result = false;
199                 
200                 if (containerText.getText() != null) {
201                         IPath containerPath = new Path(containerText.getText().trim());
202                         if (containerPath.segmentCount() > 1) {
203                                 IFolder container = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
204                                 if (container != null && container.exists()) {
205                                         IResource file = container.getFile(fileText.getText().trim());
206                                         if (file != null && file.exists()) {
207                                                 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
208                                                 result = true;
209                                         }
210                                 }
211                         } else {
212                                 // this is a project
213                                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
214                                 if (project != null && project.exists()) {
215                                         IResource file = project.getFile(fileText.getText().trim());
216                                         if (file != null && file.exists()) {
217                                                 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
218                                                 result = true;
219                                         }
220                                 }
221                         }
222                 }
223                 
224                 if (!result)
225                         ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
226                 
227                 return result;
228         }
229
230 }