Documentation update from hudson build on Thu 28 Aug 2008 09:25:08 PM CDT
[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
87                                 .getString("WizardPage.browseButtonText"));
88                 button.addSelectionListener(new SelectionAdapter() {
89                         public void widgetSelected(SelectionEvent e) {
90                                 handleBrowse();
91                         }
92                 });
93                 label = new Label(container, SWT.NULL);
94                 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
95
96                 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
97                 gd = new GridData(GridData.FILL_HORIZONTAL);
98                 fileText.setLayoutData(gd);
99                 fileText.addModifyListener(new ModifyListener() {
100                         public void modifyText(ModifyEvent e) {
101                                 dialogChanged();
102                         }
103                 });
104                 initialize();
105                 dialogChanged();
106                 setControl(container);
107         }
108
109         /**
110          * Tests if the current workbench selection is a suitable container to use.
111          */
112
113         private void initialize() {
114                 if (selection != null && selection.isEmpty() == false
115                                 && selection instanceof IStructuredSelection) {
116                         IStructuredSelection ssel = (IStructuredSelection) selection;
117                         if (ssel.size() > 1)
118                                 return;
119                         Object obj = ssel.getFirstElement();
120                         if (obj instanceof IResource) {
121                                 IContainer container;
122                                 if (obj instanceof IContainer)
123                                         container = (IContainer) obj;
124                                 else
125                                         container = ((IResource) obj).getParent();
126                                 containerText.setText(container.getFullPath().toString());
127                                 fileText.setFocus();
128                         }
129                 }
130                 fileText.setText(INITIAL_FILENAME);
131         }
132
133         /**
134          * Uses the standard container selection dialog to choose the new value for
135          * the container field.
136          */
137
138         private void handleBrowse() {
139                 ContainerSelectionDialog dialog = new ContainerSelectionDialog(
140                                 getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
141                                 PHPWizardMessages
142                                                 .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
164                                         .getString("WizardPage.containerMustBeSpecified"));
165                         return;
166                 }
167                 if (fileName.length() == 0) {
168                         updateStatus("WizardPage.nameMustBeSpecified");
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
184         public String getFileName() {
185                 return fileText.getText();
186         }
187
188         /**
189          * @see WizardPage#isPageComplete()
190          */
191         public boolean isPageComplete() {
192                 return !checkFolderForExistingFile() && super.isPageComplete();
193         }
194
195         /**
196          * Finds the current directory where the file should be created
197          */
198         protected boolean checkFolderForExistingFile() {
199                 IContainer container = getFileContainer();
200                 if (container != null) {
201                         IResource file = container.getFile(new Path(fileText.getText()
202                                         .trim()));
203                         if (file != null && file.exists()) {
204                                 this.setErrorMessage(PHPWizardMessages
205                                                 .getString("WizardPage.fileAlreadyExists"));
206                                 return true;
207                         }
208                 }
209                 return false;
210         }
211
212         private IContainer getFileContainer() {
213                 if (containerText.getText() != null) {
214                         IPath containerPath = new Path(containerText.getText().trim());
215                         IContainer container = null;
216                         if (containerPath.segmentCount() > 1) {
217                                 container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
218                                                 containerPath);
219                         } else {
220                                 if (containerPath.segmentCount() == 1) {
221                                         // this is a project
222                                         container = ResourcesPlugin.getWorkspace().getRoot()
223                                                         .getProject(containerText.getText().trim());
224                                 }
225                         }
226                         if (container != null && container.exists()) {
227                                 return container;
228                         }
229                 }
230                 return null;
231         }
232
233         public void setVisible(boolean visible) {
234                 super.setVisible(visible);
235                 if (visible) {
236                         String fileName = fileText.getText().trim();
237                         if (getFileContainer() != null
238                                         && fileName.equalsIgnoreCase(INITIAL_FILENAME)) {
239                                 fileText.setFocus();
240                                 fileText.setText(fileName);
241                                 fileText.setSelection(0, fileName.length()
242                                                 - (new Path(INITIAL_FILENAME)).getFileExtension()
243                                                                 .length() - 1);
244                         }
245                 }
246         }
247
248 }