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