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 net.sourceforge.phpdt.core.ICompilationUnit;
16 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.jface.dialogs.IDialogPage;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.wizard.WizardPage;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
42 * The "New" wizard page allows setting the container for the new file as well
43 * as the file name. The page will only accept file name without the extension
44 * OR with the extension that matches the expected one (cs).
47 public class PHPFileWizardPage extends WizardPage {
48 private static final String INITIAL_FILENAME = "file.php";
50 private Text containerText;
52 private Text fileText;
54 private ISelection selection;
57 * Constructor for SampleNewWizardPage.
61 public PHPFileWizardPage(ISelection selection) {
63 setTitle(PHPWizardMessages.getString("WizardPage.title"));
64 setDescription(PHPWizardMessages.getString("WizardPage.description"));
65 this.selection = selection;
69 * @see IDialogPage#createControl(Composite)
71 public void createControl(Composite parent) {
72 Composite container = new Composite(parent, SWT.NULL);
73 GridLayout layout = new GridLayout();
74 container.setLayout(layout);
75 layout.numColumns = 3;
76 layout.verticalSpacing = 9;
77 Label label = new Label(container, SWT.NULL);
78 label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
80 containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
81 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
82 containerText.setLayoutData(gd);
83 containerText.addModifyListener(new ModifyListener() {
84 public void modifyText(ModifyEvent e) {
89 Button button = new Button(container, SWT.PUSH);
90 button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
91 button.addSelectionListener(new SelectionAdapter() {
92 public void widgetSelected(SelectionEvent e) {
96 label = new Label(container, SWT.NULL);
97 label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
99 fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
100 gd = new GridData(GridData.FILL_HORIZONTAL);
101 fileText.setLayoutData(gd);
102 fileText.addModifyListener(new ModifyListener() {
103 public void modifyText(ModifyEvent e) {
109 setControl(container);
113 * Tests if the current workbench selection is a suitable container to use.
116 private void initialize() {
117 if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
118 IStructuredSelection ssel = (IStructuredSelection) selection;
121 Object obj = ssel.getFirstElement();
122 if (obj instanceof IResource) {
123 IContainer container;
124 if (obj instanceof IContainer)
125 container = (IContainer) obj;
127 container = ((IResource) obj).getParent();
128 containerText.setText(container.getFullPath().toString());
132 fileText.setText(INITIAL_FILENAME);
136 * Uses the standard container selection dialog to choose the new value for
137 * the container field.
140 private void handleBrowse() {
141 ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
142 PHPWizardMessages.getString("WizardPage.selectNewFileContainer"));
143 if (dialog.open() == ContainerSelectionDialog.OK) {
144 Object[] results = dialog.getResult();
145 if (results.length == 1) {
146 Object result = results[0];
147 if (result instanceof IPath) {
148 IPath ipath = (IPath) result;
149 containerText.setText(ipath.toString());
156 * Ensures that both text fields are set.
158 private void dialogChanged() {
159 String container = getContainerName();
160 String fileName = getFileName();
162 if (container.length() == 0) {
163 updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
166 if (fileName.length() == 0) {
167 updateStatus("WizardPage.nameMustBeSpecified");
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 IContainer container = getFileContainer();
199 if (container != null) {
200 IResource file = container.getFile(new Path(fileText.getText().trim()));
201 if (file != null && file.exists()) {
202 this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
206 ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
211 private IContainer getFileContainer() {
212 if (containerText.getText() != null) {
213 IPath containerPath = new Path(containerText.getText().trim());
214 IContainer container = null;
215 if (containerPath.segmentCount() > 1) {
216 container = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
218 if (containerPath.segmentCount() == 1) {
220 container = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
223 if (container != null && container.exists()) {
230 public void setVisible(boolean visible) {
231 super.setVisible(visible);
233 String fileName = fileText.getText().trim();
234 if (getFileContainer() != null && fileName.equalsIgnoreCase(INITIAL_FILENAME)) {
236 fileText.setText(fileName);
237 fileText.setSelection(0, fileName.length() - (new Path(INITIAL_FILENAME)).getFileExtension().length() - 1);