1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui.wizards;
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaProject;
15 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
16 import net.sourceforge.phpdt.core.JavaCore;
17 import net.sourceforge.phpdt.core.JavaModelException;
18 import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
19 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
20 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
21 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
22 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
23 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
24 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
25 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 import net.sourceforge.phpeclipse.ui.WebUI;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.IWorkspaceRoot;
32 import org.eclipse.core.resources.ResourcesPlugin;
33 import org.eclipse.core.runtime.IAdaptable;
34 import org.eclipse.core.runtime.IPath;
35 import org.eclipse.core.runtime.IStatus;
36 import org.eclipse.core.runtime.Path;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.ui.IWorkbenchPart;
40 import org.eclipse.ui.views.contentoutline.ContentOutline;
43 * Wizard page that acts as a base class for wizard pages that create new Java
44 * elements. The class provides a input field for source folders (called
45 * container in this class) and API to validate the enter source folder name.
49 public abstract class NewContainerWizardPage extends NewElementWizardPage {
51 /** Id of the container field */
52 protected static final String CONTAINER = "NewContainerWizardPage.container"; //$NON-NLS-1$
54 /** The status of the last validation. */
55 protected IStatus fContainerStatus;
57 private StringButtonDialogField fContainerDialogField;
60 * package fragment root corresponding to the input type (can be null)
62 private IPackageFragmentRoot fCurrRoot;
64 private IWorkspaceRoot fWorkspaceRoot;
67 * Create a new <code>NewContainerWizardPage</code>
70 * the wizard page's name
72 public NewContainerWizardPage(String name) {
74 fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
75 ContainerFieldAdapter adapter = new ContainerFieldAdapter();
77 fContainerDialogField = new StringButtonDialogField(adapter);
78 fContainerDialogField.setDialogFieldListener(adapter);
79 fContainerDialogField.setLabelText(NewWizardMessages
80 .getString("NewContainerWizardPage.container.label")); //$NON-NLS-1$
81 fContainerDialogField.setButtonLabel(NewWizardMessages
82 .getString("NewContainerWizardPage.container.button")); //$NON-NLS-1$
84 fContainerStatus = new StatusInfo();
89 * Initializes the source folder field with a valid package fragement root.
90 * The package fragement root is computed from the given Java element.
93 * the Java element used to compute the initial package fragment
94 * root used as the source folder
96 protected void initContainerPage(IJavaElement elem) {
97 IPackageFragmentRoot initRoot = null;
98 // if (elem != null) {
99 // initRoot= JavaModelUtil.getPackageFragmentRoot(elem);
100 // if (initRoot == null || initRoot.isArchive()) {
101 // IJavaProject jproject= elem.getJavaProject();
102 // if (jproject != null) {
105 // if (jproject.exists()) {
106 // IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots();
107 // for (int i= 0; i < roots.length; i++) {
108 // if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
109 // initRoot= roots[i];
114 // } catch (JavaModelException e) {
115 // PHPeclipsePlugin.log(e);
117 // if (initRoot == null) {
118 // initRoot= jproject.getPackageFragmentRoot(jproject.getResource());
123 // setPackageFragmentRoot(initRoot, true);
127 * Utility method to inspect a selection to find a Java element.
130 * the selection to be inspected
131 * @return a Java element to be used as the initial selection, or
132 * <code>null</code>, if no Java element exists in the given
135 protected IJavaElement getInitialJavaElement(IStructuredSelection selection) {
136 IJavaElement jelem = null;
137 if (selection != null && !selection.isEmpty()) {
138 Object selectedElement = selection.getFirstElement();
139 if (selectedElement instanceof IAdaptable) {
140 IAdaptable adaptable = (IAdaptable) selectedElement;
142 jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
144 IResource resource = (IResource) adaptable
145 .getAdapter(IResource.class);
147 && resource.getType() != IResource.ROOT) {
149 && resource.getType() != IResource.PROJECT) {
150 resource = resource.getParent();
151 jelem = (IJavaElement) resource
152 .getAdapter(IJavaElement.class);
155 jelem = JavaCore.create(resource); // java project
162 IWorkbenchPart part = WebUI.getActivePage()
164 if (part instanceof ContentOutline) {
165 part = WebUI.getActivePage().getActiveEditor();
168 if (part instanceof IViewPartInputProvider) {
169 Object elem = ((IViewPartInputProvider) part)
171 if (elem instanceof IJavaElement) {
172 jelem = (IJavaElement) elem;
177 if (jelem == null || jelem.getElementType() == IJavaElement.JAVA_MODEL) {
179 IJavaProject[] projects = JavaCore.create(getWorkspaceRoot())
181 if (projects.length == 1) {
184 } catch (JavaModelException e) {
185 PHPeclipsePlugin.log(e);
192 * Returns the recommended maximum width for text fields (in pixels). This
193 * method requires that createContent has been called before this method is
194 * call. Subclasses may override to change the maximum width for text
197 * @return the recommended maximum width for text fields.
199 protected int getMaxFieldWidth() {
200 return convertWidthInCharsToPixels(40);
204 * Creates the necessary controls (label, text field and browse button) to
205 * edit the source folder location. The method expects that the parent
206 * composite uses a <code>GridLayout</code> as its layout manager and that
207 * the grid layout has at least 3 columns.
210 * the parent composite
212 * the number of columns to span. This number must be greater or
215 protected void createContainerControls(Composite parent, int nColumns) {
216 fContainerDialogField.doFillIntoGrid(parent, nColumns);
217 LayoutUtil.setWidthHint(fContainerDialogField.getTextControl(null),
222 * Sets the focus to the source folder's text field.
224 protected void setFocusOnContainer() {
225 fContainerDialogField.setFocus();
228 // -------- ContainerFieldAdapter --------
230 private class ContainerFieldAdapter implements IStringButtonAdapter,
231 IDialogFieldListener {
233 // -------- IStringButtonAdapter
234 public void changeControlPressed(DialogField field) {
235 containerChangeControlPressed(field);
238 // -------- IDialogFieldListener
239 public void dialogFieldChanged(DialogField field) {
240 containerDialogFieldChanged(field);
244 private void containerChangeControlPressed(DialogField field) {
245 // take the current jproject as init element of the dialog
246 // IPackageFragmentRoot root= getPackageFragmentRoot();
247 // root= chooseSourceContainer(root);
248 // if (root != null) {
249 // setPackageFragmentRoot(root, true);
253 private void containerDialogFieldChanged(DialogField field) {
254 if (field == fContainerDialogField) {
255 fContainerStatus = containerChanged();
258 handleFieldChanged(CONTAINER);
261 // ----------- validation ----------
264 * This method is a hook which gets called after the source folder's text
265 * input field has changed. This default implementation updates the model
266 * and returns an error status. The underlying model is only valid if the
267 * returned status is OK.
269 * @return the model's error status
271 protected IStatus containerChanged() {
272 StatusInfo status = new StatusInfo();
275 String str = getPackageFragmentRootText();
276 if (str.length() == 0) {
278 .setError(NewWizardMessages
279 .getString("NewContainerWizardPage.error.EnterContainerName")); //$NON-NLS-1$
282 IPath path = new Path(str);
283 IResource res = fWorkspaceRoot.findMember(path);
285 int resType = res.getType();
286 if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
287 IProject proj = res.getProject();
288 if (!proj.isOpen()) {
290 .setError(NewWizardMessages
292 "NewContainerWizardPage.error.ProjectClosed", proj.getFullPath().toString())); //$NON-NLS-1$
295 IJavaProject jproject = JavaCore.create(proj);
296 // fCurrRoot= jproject.getPackageFragmentRoot(res);
297 // if (res.exists()) {
299 // if (!proj.hasNature(JavaCore.NATURE_ID)) {
300 // if (resType == IResource.PROJECT) {
301 // status.setError(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
304 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotInAJavaProject"));
309 // } catch (CoreException e) {
310 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
313 // if (!jproject.isOnClasspath(fCurrRoot)) {
314 // status.setWarning(NewWizardMessages.getFormattedString("NewContainerWizardPage.warning.NotOnClassPath",
315 // str)); //$NON-NLS-1$
317 // if (fCurrRoot.isArchive()) {
318 // status.setError(NewWizardMessages.getFormattedString("NewContainerWizardPage.error.ContainerIsBinary",
319 // str)); //$NON-NLS-1$
325 status.setError(NewWizardMessages.getFormattedString(
326 "NewContainerWizardPage.error.NotAFolder", str)); //$NON-NLS-1$
330 status.setError(NewWizardMessages.getFormattedString(
331 "NewContainerWizardPage.error.ContainerDoesNotExist", str)); //$NON-NLS-1$
336 // -------- update message ----------------
339 * Hook method that gets called when a field on this page has changed. For
340 * this page the method gets called when the source folder field changes.
342 * Every sub type is responsible to call this method when a field on its
343 * page has changed. Subtypes override (extend) the method to add
344 * verification when a own field has a dependency to an other field. For
345 * example the class name input must be verified again when the package
346 * field changes (check for duplicated class names).
349 * The name of the field that has changed (field id). For the
350 * source folder the field id is <code>CONTAINER</code>
352 protected void handleFieldChanged(String fieldName) {
355 // ---- get ----------------
358 * Returns the workspace root.
360 * @return the workspace root
362 protected IWorkspaceRoot getWorkspaceRoot() {
363 return fWorkspaceRoot;
367 * Returns the <code>IPackageFragmentRoot</code> that corresponds to the
368 * current value of the source folder field.
370 * @return the IPackageFragmentRoot or <code>null</code> if the current
371 * source folder value is not a valid package fragment root
374 public IPackageFragmentRoot getPackageFragmentRoot() {
379 * Returns the current text of source folder text field.
381 * @return the text of the source folder text field
383 public String getPackageFragmentRootText() {
384 return fContainerDialogField.getText();
388 * Sets the current source folder (model and text field) to the given
389 * package fragment root.
391 * @param canBeModified
392 * if <code>false</code> the source folder field can not be
393 * changed by the user. If <code>true</code> the field is
396 // public void setPackageFragmentRoot(IPackageFragmentRoot root, boolean
399 // String str= (root == null) ? "" :
400 // root.getPath().makeRelative().toString(); //$NON-NLS-1$
401 // fContainerDialogField.setText(str);
402 // fContainerDialogField.setEnabled(canBeModified);
404 // ------------- choose source container dialog
405 // private IPackageFragmentRoot chooseSourceContainer(IJavaElement
407 // Class[] acceptedClasses= new Class[] { IPackageFragmentRoot.class,
408 // IJavaProject.class };
409 // TypedElementSelectionValidator validator= new
410 // TypedElementSelectionValidator(acceptedClasses, false) {
411 // public boolean isSelectedValid(Object element) {
413 // if (element instanceof IJavaProject) {
414 // IJavaProject jproject= (IJavaProject)element;
415 // IPath path= jproject.getProject().getFullPath();
416 // return (jproject.findPackageFragmentRoot(path) != null);
417 // } else if (element instanceof IPackageFragmentRoot) {
418 // return (((IPackageFragmentRoot)element).getKind() ==
419 // IPackageFragmentRoot.K_SOURCE);
422 // } catch (JavaModelException e) {
423 // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
429 // acceptedClasses= new Class[] { IJavaModel.class,
430 // IPackageFragmentRoot.class, IJavaProject.class };
431 // ViewerFilter filter= new TypedViewerFilter(acceptedClasses) {
432 // public boolean select(Viewer viewer, Object parent, Object element) {
433 // if (element instanceof IPackageFragmentRoot) {
435 // return (((IPackageFragmentRoot)element).getKind() ==
436 // IPackageFragmentRoot.K_SOURCE);
437 // } catch (JavaModelException e) {
438 // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
442 // return super.select(viewer, parent, element);
446 // StandardJavaElementContentProvider provider= new
447 // StandardJavaElementContentProvider();
448 // ILabelProvider labelProvider= new
449 // JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
450 // ElementTreeSelectionDialog dialog= new
451 // ElementTreeSelectionDialog(getShell(), labelProvider, provider);
452 // dialog.setValidator(validator);
453 // dialog.setSorter(new JavaElementSorter());
454 // dialog.setTitle(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.title"));
456 // dialog.setMessage(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.description"));
458 // dialog.addFilter(filter);
459 // dialog.setInput(JavaCore.create(fWorkspaceRoot));
460 // dialog.setInitialSelection(initElement);
462 // if (dialog.open() == ElementTreeSelectionDialog.OK) {
463 // Object element= dialog.getFirstResult();
464 // if (element instanceof IJavaProject) {
465 // IJavaProject jproject= (IJavaProject)element;
466 // return jproject.getPackageFragmentRoot(jproject.getProject());
467 // } else if (element instanceof IPackageFragmentRoot) {
468 // return (IPackageFragmentRoot)element;