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.
151 private void dialogChanged() {
152 String container = getContainerName();
153 String fileName = getFileName();
155 if (container.length() == 0) {
156 updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
159 if (fileName.length() == 0) {
160 updateStatus("WizardPage.nameMustBeSpecified");
163 int dotLoc = fileName.lastIndexOf('.');
165 String ext = fileName.substring(dotLoc + 1);
166 if (ext.equalsIgnoreCase("php") == false) {
167 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();
182 public String getFileName() {
183 return fileText.getText();
187 * @see WizardPage#isPageComplete()
189 public boolean isPageComplete() {
190 return !checkFolderForExistingFile() && super.isPageComplete();
194 * Finds the current directory where the file should be created
196 protected boolean checkFolderForExistingFile() {
197 boolean result = false;
199 if (containerText.getText() != null) {
200 IPath containerPath = new Path(containerText.getText().trim());
201 if (containerPath.segmentCount() > 1) {
202 IFolder container = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
203 if (container != null && container.exists()) {
204 IResource file = container.getFile(fileText.getText().trim());
205 if (file != null && file.exists()) {
206 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
212 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
213 if (project != null && project.exists()) {
214 IResource file = project.getFile(fileText.getText().trim());
215 if (file != null && file.exists()) {
216 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
224 ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());