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
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;
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).
45 public class PHPFileWizardPage extends WizardPage {
46 private Text containerText;
48 private Text fileText;
50 private ISelection selection;
53 * Constructor for SampleNewWizardPage.
57 public PHPFileWizardPage(ISelection selection) {
59 setTitle(PHPWizardMessages.getString("WizardPage.title"));
60 setDescription(PHPWizardMessages.getString("WizardPage.description"));
61 this.selection = selection;
65 * @see IDialogPage#createControl(Composite)
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"));
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) {
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) {
92 label = new Label(container, SWT.NULL);
93 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
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) {
105 setControl(container);
109 * Tests if the current workbench selection is a suitable container to use.
112 private void initialize() {
113 if (selection != null && selection.isEmpty() == false
114 && selection instanceof IStructuredSelection) {
115 IStructuredSelection ssel = (IStructuredSelection) selection;
118 Object obj = ssel.getFirstElement();
119 if (obj instanceof IResource) {
120 IContainer container;
121 if (obj instanceof IContainer)
122 container = (IContainer) obj;
124 container = ((IResource) obj).getParent();
125 containerText.setText(container.getFullPath().toString());
128 fileText.setText("*.php");
132 * Uses the standard container selection dialog to choose the new value for
133 * the container field.
136 private void handleBrowse() {
137 ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
138 ResourcesPlugin.getWorkspace().getRoot(), false, PHPWizardMessages
139 .getString("WizardPage.selectNewFileContainer"));
140 if (dialog.open() == ContainerSelectionDialog.OK) {
141 Object[] results = dialog.getResult();
142 if (results.length == 1) {
143 Object result=results[0];
144 if (result instanceof IPath) {
145 IPath ipath = (IPath) result;
146 containerText.setText(ipath.toString());
153 * Ensures that both text fields are set.
155 private void dialogChanged() {
156 String container = getContainerName();
157 String fileName = getFileName();
159 if (container.length() == 0) {
160 updateStatus(PHPWizardMessages
161 .getString("WizardPage.containerMustBeSpecified"));
164 if (fileName.length() == 0) {
165 updateStatus("WizardPage.nameMustBeSpecified");
169 // if (!PHPFileUtil.isPHPFileName(fileName)) {
170 // updateStatus(PHPWizardMessages.getString("WizardPage.mustBePHP"));
176 private void updateStatus(String message) {
177 setErrorMessage(message);
178 setPageComplete(message == null);
181 public String getContainerName() {
182 return containerText.getText();
185 public String getFileName() {
186 return fileText.getText();
190 * @see WizardPage#isPageComplete()
192 public boolean isPageComplete() {
193 return !checkFolderForExistingFile() && super.isPageComplete();
197 * Finds the current directory where the file should be created
199 protected boolean checkFolderForExistingFile() {
200 boolean result = false;
202 if (containerText.getText() != null) {
203 IPath containerPath = new Path(containerText.getText().trim());
204 if (containerPath.segmentCount() > 1) {
205 IFolder container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
207 if (container != null && container.exists()) {
208 IResource file = container.getFile(fileText.getText().trim());
209 if (file != null && file.exists()) {
210 this.setErrorMessage(PHPWizardMessages
211 .getString("WizardPage.fileAlreadyExists"));
217 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
218 containerText.getText().trim());
219 if (project != null && project.exists()) {
220 IResource file = project.getFile(fileText.getText().trim());
221 if (file != null && file.exists()) {
222 this.setErrorMessage(PHPWizardMessages
223 .getString("WizardPage.fileAlreadyExists"));
231 ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());