initially focus file name field, if container field contains useful value
[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.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.dialogs.IDialogPage;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
38
39 /**
40  * The "New" wizard page allows setting the container for the new file as well
41  * as the file name. The page will only accept file name without the extension
42  * OR with the extension that matches the expected one (cs).
43  */
44
45 public class HTMLFileWizardPage extends WizardPage {
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.getString("WizardPage.html.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
114         && selection instanceof IStructuredSelection) {
115       IStructuredSelection ssel = (IStructuredSelection) selection;
116       if (ssel.size() > 1)
117         return;
118       Object obj = ssel.getFirstElement();
119       if (obj instanceof IResource) {
120         IContainer container;
121         if (obj instanceof IContainer)
122           container = (IContainer) obj;
123         else
124           container = ((IResource) obj).getParent();
125         containerText.setText(container.getFullPath().toString());
126         fileText.setFocus();
127       }
128     }
129     fileText.setText("*.html");
130   }
131
132   /**
133    * Uses the standard container selection dialog to choose the new value for
134    * the container field.
135    */
136
137   private void handleBrowse() {
138     ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
139         ResourcesPlugin.getWorkspace().getRoot(), false, PHPWizardMessages
140             .getString("WizardPage.selectNewFileContainer"));
141     if (dialog.open() == ContainerSelectionDialog.OK) {
142       Object[] result = dialog.getResult();
143       if (result.length == 1) {
144         IContainer container = (IContainer) result[0];
145         containerText.setText(container.getFullPath().toString());
146       }
147     }
148   }
149
150   /**
151    * Ensures that both text fields are set.
152    */
153   private void dialogChanged() {
154     String container = getContainerName();
155     String fileName = getFileName();
156
157     if (container.length() == 0) {
158       updateStatus(PHPWizardMessages
159           .getString("WizardPage.containerMustBeSpecified"));
160       return;
161     }
162     if (fileName.length() == 0) {
163       updateStatus("WizardPage.nameMustBeSpecified");
164       return;
165     }
166
167 //    if (!PHPFileUtil.isPHPFileName(fileName)) {
168 //      updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
169 //      return;
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     boolean result = false;
199
200     if (containerText.getText() != null) {
201       IPath containerPath = new Path(containerText.getText().trim());
202       if (containerPath.segmentCount() > 1) {
203         IFolder container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
204             containerPath);
205         if (container != null && container.exists()) {
206           IResource file = container.getFile(fileText.getText().trim());
207           if (file != null && file.exists()) {
208             this.setErrorMessage(PHPWizardMessages
209                 .getString("WizardPage.fileAlreadyExists"));
210             result = true;
211           }
212         }
213       } else {
214         // this is a project
215         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
216             containerText.getText().trim());
217         if (project != null && project.exists()) {
218           IResource file = project.getFile(fileText.getText().trim());
219           if (file != null && file.exists()) {
220             this.setErrorMessage(PHPWizardMessages
221                 .getString("WizardPage.fileAlreadyExists"));
222             result = true;
223           }
224         }
225       }
226     }
227
228     if (!result)
229       ((HTMLFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
230
231     return result;
232   }
233
234 }