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.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.dialogs.IDialogPage;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.wizard.WizardPage;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
38 * The "New" wizard page allows setting the container for the new file as well
39 * as the file name. The page will only accept file name without the extension
40 * OR with the extension that matches the expected one (cs).
43 public class PHPFileWizardPage extends WizardPage {
44 private static final String INITIAL_FILENAME = "file.php";
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());
129 fileText.setText(INITIAL_FILENAME);
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[] results = dialog.getResult();
143 if (results.length == 1) {
144 Object result=results[0];
145 if (result instanceof IPath) {
146 IPath ipath = (IPath) result;
147 containerText.setText(ipath.toString());
154 * Ensures that both text fields are set.
156 private void dialogChanged() {
157 String container = getContainerName();
158 String fileName = getFileName();
160 if (container.length() == 0) {
161 updateStatus(PHPWizardMessages
162 .getString("WizardPage.containerMustBeSpecified"));
165 if (fileName.length() == 0) {
166 updateStatus("WizardPage.nameMustBeSpecified");
173 private void updateStatus(String message) {
174 setErrorMessage(message);
175 setPageComplete(message == null);
178 public String getContainerName() {
179 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 IContainer container = getFileContainer();
198 if (container != null) {
199 IResource file = container.getFile(new Path(fileText.getText()
201 if (file != null && file.exists()) {
202 this.setErrorMessage(PHPWizardMessages
203 .getString("WizardPage.fileAlreadyExists"));
207 ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText()
213 private IContainer getFileContainer() {
214 if (containerText.getText() != null) {
215 IPath containerPath = new Path(containerText.getText().trim());
216 IContainer container;
217 if (containerPath.segmentCount() > 1) {
218 container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
222 container = ResourcesPlugin.getWorkspace().getRoot()
223 .getProject(containerText.getText().trim());
225 if (container != null && container.exists()) {
232 public void setVisible(boolean visible) {
233 super.setVisible(visible);
235 String fileName=fileText.getText().trim();
236 if (getFileContainer() != null && fileName.equalsIgnoreCase(INITIAL_FILENAME)) {
238 fileText.setText(fileName);
239 fileText.setSelection(0, fileName.length()-(new Path(INITIAL_FILENAME)).getFileExtension().length()-1);