bug 1037094, to much indentation for first comment in a function parameters and defau...
[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 net.sourceforge.phpdt.core.ICompilationUnit;
16 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
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 PHPFileWizardPage extends WizardPage {
48         private static final String INITIAL_FILENAME = "file.php";
49
50         private Text containerText;
51
52         private Text fileText;
53
54         private ISelection selection;
55
56         /**
57          * Constructor for SampleNewWizardPage.
58          *
59          * @param pageName
60          */
61         public PHPFileWizardPage(ISelection selection) {
62                 super("wizardPage");
63                 setTitle(PHPWizardMessages.getString("WizardPage.title"));
64                 setDescription(PHPWizardMessages.getString("WizardPage.description"));
65                 this.selection = selection;
66         }
67
68         /**
69          * @see IDialogPage#createControl(Composite)
70          */
71         public void createControl(Composite parent) {
72                 Composite container = new Composite(parent, SWT.NULL);
73                 GridLayout layout = new GridLayout();
74                 container.setLayout(layout);
75                 layout.numColumns = 3;
76                 layout.verticalSpacing = 9;
77                 Label label = new Label(container, SWT.NULL);
78                 label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
79
80                 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
81                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
82                 containerText.setLayoutData(gd);
83                 containerText.addModifyListener(new ModifyListener() {
84                         public void modifyText(ModifyEvent e) {
85                                 dialogChanged();
86                         }
87                 });
88
89                 Button button = new Button(container, SWT.PUSH);
90                 button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
91                 button.addSelectionListener(new SelectionAdapter() {
92                         public void widgetSelected(SelectionEvent e) {
93                                 handleBrowse();
94                         }
95                 });
96                 label = new Label(container, SWT.NULL);
97                 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
98
99                 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
100                 gd = new GridData(GridData.FILL_HORIZONTAL);
101                 fileText.setLayoutData(gd);
102                 fileText.addModifyListener(new ModifyListener() {
103                         public void modifyText(ModifyEvent e) {
104                                 dialogChanged();
105                         }
106                 });
107                 initialize();
108                 dialogChanged();
109                 setControl(container);
110         }
111
112         /**
113          * Tests if the current workbench selection is a suitable container to use.
114          */
115
116         private void initialize() {
117                 if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
118                         IStructuredSelection ssel = (IStructuredSelection) selection;
119                         if (ssel.size() > 1)
120                                 return;
121                         Object obj = ssel.getFirstElement();
122                         if (obj instanceof IResource) {
123                                 IContainer container;
124                                 if (obj instanceof IContainer)
125                                         container = (IContainer) obj;
126                                 else
127                                         container = ((IResource) obj).getParent();
128                                 containerText.setText(container.getFullPath().toString());
129                                 fileText.setFocus();
130                         }
131                 }
132                 fileText.setText(INITIAL_FILENAME);
133         }
134
135         /**
136          * Uses the standard container selection dialog to choose the new value for
137          * the container field.
138          */
139
140         private void handleBrowse() {
141                 ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
142                                 PHPWizardMessages.getString("WizardPage.selectNewFileContainer"));
143                 if (dialog.open() == ContainerSelectionDialog.OK) {
144                         Object[] results = dialog.getResult();
145                         if (results.length == 1) {
146                                 Object result = results[0];
147                                 if (result instanceof IPath) {
148                                         IPath ipath = (IPath) result;
149                                         containerText.setText(ipath.toString());
150                                 }
151                         }
152                 }
153         }
154
155         /**
156          * Ensures that both text fields are set.
157          */
158         private void dialogChanged() {
159                 String container = getContainerName();
160                 String fileName = getFileName();
161
162                 if (container.length() == 0) {
163                         updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
164                         return;
165                 }
166                 if (fileName.length() == 0) {
167                         updateStatus("WizardPage.nameMustBeSpecified");
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
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                 IContainer container = getFileContainer();
199                 if (container != null) {
200                         IResource file = container.getFile(new Path(fileText.getText().trim()));
201                         if (file != null && file.exists()) {
202                                 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
203                                 return true;
204                         }
205
206                         ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
207                 }
208                 return false;
209         }
210
211         private IContainer getFileContainer() {
212                 if (containerText.getText() != null) {
213                         IPath containerPath = new Path(containerText.getText().trim());
214                         IContainer container = null;
215                         if (containerPath.segmentCount() > 1) {
216                                 container = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
217                         } else {
218                                 if (containerPath.segmentCount() == 1) {
219                                         // this is a project
220                                         container = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
221                                 }
222                         }
223                         if (container != null && container.exists()) {
224                                 return container;
225                         }
226                 }
227                 return null;
228         }
229
230         public void setVisible(boolean visible) {
231                 super.setVisible(visible);
232                 if (visible) {
233                         String fileName = fileText.getText().trim();
234                         if (getFileContainer() != null && fileName.equalsIgnoreCase(INITIAL_FILENAME)) {
235                                 fileText.setFocus();
236                                 fileText.setText(fileName);
237                                 fileText.setSelection(0, fileName.length() - (new Path(INITIAL_FILENAME)).getFileExtension().length() - 1);
238                         }
239                 }
240         }
241
242 }