1 package net.sourceforge.phpeclipse.wizards;
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
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
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 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
41 * The "New" wizard page allows setting the container for the new file as well
42 * as the file name. The page will only accept file name without the extension
43 * OR with the extension that matches the expected one (cs).
46 public class HTMLFileWizardPage extends WizardPage {
47 private Text containerText;
49 private Text fileText;
51 private ISelection selection;
54 * Constructor for SampleNewWizardPage.
58 public HTMLFileWizardPage(ISelection selection) {
60 setTitle(PHPWizardMessages.getString("WizardPage.html.title"));
61 setDescription(PHPWizardMessages.getString("WizardPage.html.description"));
62 this.selection = selection;
66 * @see IDialogPage#createControl(Composite)
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"));
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) {
86 Button button = new Button(container, SWT.PUSH);
87 button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
88 button.addSelectionListener(new SelectionAdapter() {
89 public void widgetSelected(SelectionEvent e) {
93 label = new Label(container, SWT.NULL);
94 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
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) {
106 setControl(container);
110 * Tests if the current workbench selection is a suitable container to use.
113 private void initialize() {
114 if (selection != null && selection.isEmpty() == false
115 && selection instanceof IStructuredSelection) {
116 IStructuredSelection ssel = (IStructuredSelection) selection;
119 Object obj = ssel.getFirstElement();
120 if (obj instanceof IResource) {
121 IContainer container;
122 if (obj instanceof IContainer)
123 container = (IContainer) obj;
125 container = ((IResource) obj).getParent();
126 containerText.setText(container.getFullPath().toString());
129 fileText.setText("*.html");
133 * Uses the standard container selection dialog to choose the new value for
134 * the container field.
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());
151 * Ensures that both text fields are set.
153 private void dialogChanged() {
154 String container = getContainerName();
155 String fileName = getFileName();
157 if (container.length() == 0) {
158 updateStatus(PHPWizardMessages
159 .getString("WizardPage.containerMustBeSpecified"));
162 if (fileName.length() == 0) {
163 updateStatus("WizardPage.nameMustBeSpecified");
167 // if (!PHPFileUtil.isPHPFileName(fileName)) {
168 // updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
174 private void updateStatus(String message) {
175 setErrorMessage(message);
176 setPageComplete(message == null);
179 public String getContainerName() {
180 return containerText.getText();
183 public String getFileName() {
184 return fileText.getText();
188 * @see WizardPage#isPageComplete()
190 public boolean isPageComplete() {
191 return !checkFolderForExistingFile() && super.isPageComplete();
195 * Finds the current directory where the file should be created
197 protected boolean checkFolderForExistingFile() {
198 boolean result = false;
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(
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"));
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"));
229 ((HTMLFileWizard) this.getWizard()).setFileName(fileText.getText().trim());