changed .classpath to relative PATH ECLIPSE_HOME
[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         private void dialogChanged() {
152                 String container = getContainerName();
153                 String fileName = getFileName();
154
155                 if (container.length() == 0) {
156                         updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
157                         return;
158                 }
159                 if (fileName.length() == 0) {
160                         updateStatus("WizardPage.nameMustBeSpecified");
161                         return;
162                 }
163                 int dotLoc = fileName.lastIndexOf('.');
164                 if (dotLoc != -1) {
165                         String ext = fileName.substring(dotLoc + 1);
166                         if (ext.equalsIgnoreCase("php") == false) {
167                                 updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
168                                 return;
169                         }
170                 }
171                 updateStatus(null);
172         }
173
174         private void updateStatus(String message) {
175                 setErrorMessage(message);
176                 setPageComplete(message == null);
177         }
178
179         public String getContainerName() {
180                 return containerText.getText();
181         }
182         public String getFileName() {
183                 return fileText.getText();
184         }
185         
186         /**
187          * @see WizardPage#isPageComplete()
188          */
189         public boolean isPageComplete() {
190                 return !checkFolderForExistingFile() && super.isPageComplete();
191         }
192
193         /**
194          * Finds the current directory where the file should be created
195          */
196         protected boolean checkFolderForExistingFile() {
197                 boolean result = false;
198                 
199                 if (containerText.getText() != null) {
200                         IPath containerPath = new Path(containerText.getText().trim());
201                         if (containerPath.segmentCount() > 1) {
202                                 IFolder container = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
203                                 if (container != null && container.exists()) {
204                                         IResource file = container.getFile(fileText.getText().trim());
205                                         if (file != null && file.exists()) {
206                                                 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
207                                                 result = true;
208                                         }
209                                 }
210                         } else {
211                                 // this is a project
212                                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
213                                 if (project != null && project.exists()) {
214                                         IResource file = project.getFile(fileText.getText().trim());
215                                         if (file != null && file.exists()) {
216                                                 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
217                                                 result = true;
218                                         }
219                                 }
220                         }
221                 }
222                 
223                 if (!result)
224                         ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
225                 
226                 return result;
227         }
228
229 }