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.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
39 * The "New" wizard page allows setting the container for
40 * the new file as well as the file name. The page
41 * will only accept file name without the extension OR
42 * with the extension that matches the expected one (cs).
45 public class PHPFileWizardPage extends WizardPage {
46 private Text containerText;
47 private Text fileText;
48 private ISelection selection;
51 * Constructor for SampleNewWizardPage.
54 public PHPFileWizardPage(ISelection selection) {
56 setTitle(PHPWizardMessages.getString("WizardPage.title"));
57 setDescription(PHPWizardMessages.getString("WizardPage.description"));
58 this.selection = selection;
62 * @see IDialogPage#createControl(Composite)
64 public void createControl(Composite parent) {
65 Composite container = new Composite(parent, SWT.NULL);
66 GridLayout layout = new GridLayout();
67 container.setLayout(layout);
68 layout.numColumns = 3;
69 layout.verticalSpacing = 9;
70 Label label = new Label(container, SWT.NULL);
71 label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
73 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
74 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
75 containerText.setLayoutData(gd);
76 containerText.addModifyListener(new ModifyListener() {
77 public void modifyText(ModifyEvent e) {
82 Button button = new Button(container, SWT.PUSH);
83 button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
84 button.addSelectionListener(new SelectionAdapter() {
85 public void widgetSelected(SelectionEvent e) {
89 label = new Label(container, SWT.NULL);
90 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
92 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
93 gd = new GridData(GridData.FILL_HORIZONTAL);
94 fileText.setLayoutData(gd);
95 fileText.addModifyListener(new ModifyListener() {
96 public void modifyText(ModifyEvent e) {
102 setControl(container);
106 * Tests if the current workbench selection is a suitable
110 private void initialize() {
111 if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) {
112 IStructuredSelection ssel = (IStructuredSelection)selection;
113 if (ssel.size()>1) return;
114 Object obj = ssel.getFirstElement();
115 if (obj instanceof IResource) {
116 IContainer container;
117 if (obj instanceof IContainer)
118 container = (IContainer)obj;
120 container = ((IResource)obj).getParent();
121 containerText.setText(container.getFullPath().toString());
124 fileText.setText("index.php");
128 * Uses the standard container selection dialog to
129 * choose the new value for the container field.
132 private void handleBrowse() {
133 ContainerSelectionDialog dialog =
134 new ContainerSelectionDialog(
136 ResourcesPlugin.getWorkspace().getRoot(),
138 PHPWizardMessages.getString("WizardPage.selectNewFileContainer"));
139 if (dialog.open() == ContainerSelectionDialog.OK) {
140 Object[] result = dialog.getResult();
141 if (result.length == 1) {
142 IContainer container = (IContainer) result[0];
143 containerText.setText(container.getFullPath().toString());
149 * Ensures that both text fields are set.
152 private void dialogChanged() {
153 String container = getContainerName();
154 String fileName = getFileName();
156 if (container.length() == 0) {
157 updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
160 if (fileName.length() == 0) {
161 updateStatus("WizardPage.nameMustBeSpecified");
164 int dotLoc = fileName.indexOf('.');
166 String ext = fileName.substring(dotLoc + 1);
167 if (ext.equalsIgnoreCase("php") == false) {
168 updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
175 private void updateStatus(String message) {
176 setErrorMessage(message);
177 setPageComplete(message == null);
180 public String getContainerName() {
181 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(containerPath);
204 if (container != null && container.exists()) {
205 IResource file = container.getFile(fileText.getText().trim());
206 if (file != null && file.exists()) {
207 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
213 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
214 if (project != null && project.exists()) {
215 IResource file = project.getFile(fileText.getText().trim());
216 if (file != null && file.exists()) {
217 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
225 ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());