Commiting more changes to fix RSE issues with PHP projects.
[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  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 HTMLFileWizardPage extends WizardPage {
44         private static final String INITIAL_FILENAME = "file.html";
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 HTMLFileWizardPage(ISelection selection) {
58                 super("wizardPage");
59                 setTitle(PHPWizardMessages.getString("WizardPage.html.title"));
60                 setDescription(PHPWizardMessages
61                                 .getString("WizardPage.html.description"));
62                 this.selection = selection;
63         }
64
65         /**
66          * @see IDialogPage#createControl(Composite)
67          */
68         public void createControl(Composite parent) {
69                 Composite container = new Composite(parent, SWT.NULL);
70                 GridLayout layout = new GridLayout();
71                 container.setLayout(layout);
72                 layout.numColumns = 3;
73                 layout.verticalSpacing = 9;
74                 Label label = new Label(container, SWT.NULL);
75                 label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
76
77                 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
78                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
79                 containerText.setLayoutData(gd);
80                 containerText.addModifyListener(new ModifyListener() {
81                         public void modifyText(ModifyEvent e) {
82                                 dialogChanged();
83                         }
84                 });
85
86                 Button button = new Button(container, SWT.PUSH);
87                 button.setText(PHPWizardMessages
88                                 .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                                 fileText.setFocus();
129                         }
130                 }
131                 fileText.setText(INITIAL_FILENAME);
132         }
133
134         /**
135          * Uses the standard container selection dialog to choose the new value for
136          * the container field.
137          */
138
139         private void handleBrowse() {
140                 ContainerSelectionDialog dialog = new ContainerSelectionDialog(
141                                 getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
142                                 PHPWizardMessages
143                                                 .getString("WizardPage.selectNewFileContainer"));
144                 if (dialog.open() == ContainerSelectionDialog.OK) {
145                         Object[] result = dialog.getResult();
146                         if (result.length == 1) {
147                                 IContainer container = (IContainer) result[0];
148                                 containerText.setText(container.getFullPath().toString());
149                         }
150                 }
151         }
152
153         /**
154          * Ensures that both text fields are set.
155          */
156         private void dialogChanged() {
157                 String container = getContainerName();
158                 String fileName = getFileName();
159
160                 if (container.length() == 0) {
161                         updateStatus(PHPWizardMessages
162                                         .getString("WizardPage.containerMustBeSpecified"));
163                         return;
164                 }
165                 if (fileName.length() == 0) {
166                         updateStatus("WizardPage.nameMustBeSpecified");
167                         return;
168                 }
169
170                 updateStatus(null);
171         }
172
173         private void updateStatus(String message) {
174                 setErrorMessage(message);
175                 setPageComplete(message == null);
176         }
177
178         public String getContainerName() {
179                 return containerText.getText();
180         }
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                 IContainer container = getFileContainer();
198                 if (container != null) {
199                         IResource file = container.getFile(new Path(fileText.getText()
200                                         .trim()));
201                         if (file != null && file.exists()) {
202                                 this.setErrorMessage(PHPWizardMessages
203                                                 .getString("WizardPage.fileAlreadyExists"));
204                                 return true;
205                         }
206                 }
207                 return false;
208         }
209
210         private IContainer getFileContainer() {
211                 if (containerText.getText() != null) {
212                         IPath containerPath = new Path(containerText.getText().trim());
213                         IContainer container;
214                         if (containerPath.segmentCount() > 1) {
215                                 container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
216                                                 containerPath);
217                         } else {
218                                 // this is a project
219                                 container = ResourcesPlugin.getWorkspace().getRoot()
220                                                 .getProject(containerText.getText().trim());
221                         }
222                         if (container != null && container.exists()) {
223                                 return container;
224                         }
225                 }
226                 return null;
227         }
228
229         public void setVisible(boolean visible) {
230                 super.setVisible(visible);
231                 if (visible) {
232                         String fileName = fileText.getText().trim();
233                         if (getFileContainer() != null
234                                         && fileName.equalsIgnoreCase(INITIAL_FILENAME)) {
235                                 fileText.setFocus();
236                                 fileText.setText(fileName);
237                                 fileText.setSelection(0, fileName.length()
238                                                 - (new Path(INITIAL_FILENAME)).getFileExtension()
239                                                                 .length() - 1);
240                         }
241                 }
242         }
243
244 }