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;
19 //import net.sourceforge.phpdt.externaltools.internal.ui.StatusInfo;
20 import net.sourceforge.phpeclipse.ui.StatusInfo;
21 import net.sourceforge.phpdt.internal.ui.viewsupport.IViewPartInputProvider;
22 import net.sourceforge.phpdt.internal.ui.wizards.NewWizardMessages;
23 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
24 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
25 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
26 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
27 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
28 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
30 import org.eclipse.core.resources.IProject;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.resources.IWorkspaceRoot;
33 import org.eclipse.core.resources.ResourcesPlugin;
34 import org.eclipse.core.runtime.IAdaptable;
35 import org.eclipse.core.runtime.IPath;
36 import org.eclipse.core.runtime.IStatus;
37 import org.eclipse.core.runtime.Path;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.ui.IWorkbenchPart;
41 import org.eclipse.ui.views.contentoutline.ContentOutline;
44 * Wizard page that acts as a base class for wizard pages that create new Java
45 * elements. The class provides a input field for source folders (called
46 * container in this class) and API to validate the enter source folder name.
50 public abstract class NewContainerWizardPage extends NewElementWizardPage {
52 /** Id of the container field */
53 protected static final String CONTAINER = "NewContainerWizardPage.container"; //$NON-NLS-1$
55 /** The status of the last validation. */
56 protected IStatus fContainerStatus;
58 private StringButtonDialogField fContainerDialogField;
61 * package fragment root corresponding to the input type (can be null)
63 private IPackageFragmentRoot fCurrRoot;
65 private IWorkspaceRoot fWorkspaceRoot;
68 * Create a new <code>NewContainerWizardPage</code>
71 * the wizard page's name
73 public NewContainerWizardPage(String name) {
75 fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
76 ContainerFieldAdapter adapter = new ContainerFieldAdapter();
78 fContainerDialogField = new StringButtonDialogField(adapter);
79 fContainerDialogField.setDialogFieldListener(adapter);
80 fContainerDialogField.setLabelText(NewWizardMessages
81 .getString("NewContainerWizardPage.container.label")); //$NON-NLS-1$
82 fContainerDialogField.setButtonLabel(NewWizardMessages
83 .getString("NewContainerWizardPage.container.button")); //$NON-NLS-1$
85 fContainerStatus = new StatusInfo();
90 * Initializes the source folder field with a valid package fragement root.
91 * The package fragement root is computed from the given Java element.
94 * the Java element used to compute the initial package fragment
95 * root used as the source folder
97 // protected void initContainerPage(IJavaElement elem) {
98 // //IPackageFragmentRoot initRoot = null;
99 // // if (elem != null) {
100 // // initRoot= JavaModelUtil.getPackageFragmentRoot(elem);
101 // // if (initRoot == null || initRoot.isArchive()) {
102 // // IJavaProject jproject= elem.getJavaProject();
103 // // if (jproject != null) {
105 // // initRoot= null;
106 // // if (jproject.exists()) {
107 // // IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots();
108 // // for (int i= 0; i < roots.length; i++) {
109 // // if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
110 // // initRoot= roots[i];
115 // // } catch (JavaModelException e) {
116 // // PHPeclipsePlugin.log(e);
118 // // if (initRoot == null) {
119 // // initRoot= jproject.getPackageFragmentRoot(jproject.getResource());
124 // // setPackageFragmentRoot(initRoot, true);
128 * Utility method to inspect a selection to find a Java element.
131 * the selection to be inspected
132 * @return a Java element to be used as the initial selection, or
133 * <code>null</code>, if no Java element exists in the given
136 protected IJavaElement getInitialJavaElement(IStructuredSelection selection) {
137 IJavaElement jelem = null;
138 if (selection != null && !selection.isEmpty()) {
139 Object selectedElement = selection.getFirstElement();
140 if (selectedElement instanceof IAdaptable) {
141 IAdaptable adaptable = (IAdaptable) selectedElement;
143 jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
145 IResource resource = (IResource) adaptable
146 .getAdapter(IResource.class);
148 && resource.getType() != IResource.ROOT) {
150 && resource.getType() != IResource.PROJECT) {
151 resource = resource.getParent();
152 jelem = (IJavaElement) resource
153 .getAdapter(IJavaElement.class);
156 jelem = JavaCore.create(resource); // java project
163 IWorkbenchPart part = PHPeclipsePlugin.getActivePage()
165 if (part instanceof ContentOutline) {
166 part = PHPeclipsePlugin.getActivePage().getActiveEditor();
169 if (part instanceof IViewPartInputProvider) {
170 Object elem = ((IViewPartInputProvider) part)
172 if (elem instanceof IJavaElement) {
173 jelem = (IJavaElement) elem;
178 if (jelem == null || jelem.getElementType() == IJavaElement.JAVA_MODEL) {
180 IJavaProject[] projects = JavaCore.create(getWorkspaceRoot())
182 if (projects.length == 1) {
185 } catch (JavaModelException e) {
186 PHPeclipsePlugin.log(e);
193 * Returns the recommended maximum width for text fields (in pixels). This
194 * method requires that createContent has been called before this method is
195 * call. Subclasses may override to change the maximum width for text
198 * @return the recommended maximum width for text fields.
200 protected int getMaxFieldWidth() {
201 return convertWidthInCharsToPixels(40);
205 * Creates the necessary controls (label, text field and browse button) to
206 * edit the source folder location. The method expects that the parent
207 * composite uses a <code>GridLayout</code> as its layout manager and that
208 * the grid layout has at least 3 columns.
211 * the parent composite
213 * the number of columns to span. This number must be greater or
216 protected void createContainerControls(Composite parent, int nColumns) {
217 fContainerDialogField.doFillIntoGrid(parent, nColumns);
218 LayoutUtil.setWidthHint(fContainerDialogField.getTextControl(null),
223 * Sets the focus to the source folder's text field.
225 // protected void setFocusOnContainer() {
226 // fContainerDialogField.setFocus();
229 // -------- ContainerFieldAdapter --------
231 private class ContainerFieldAdapter implements IStringButtonAdapter,
232 IDialogFieldListener {
234 // -------- IStringButtonAdapter
235 public void changeControlPressed(DialogField field) {
236 //containerChangeControlPressed(field);
239 // -------- IDialogFieldListener
240 public void dialogFieldChanged(DialogField field) {
241 containerDialogFieldChanged(field);
245 // private void containerChangeControlPressed(DialogField field) {
246 // // take the current jproject as init element of the dialog
247 // // IPackageFragmentRoot root= getPackageFragmentRoot();
248 // // root= chooseSourceContainer(root);
249 // // if (root != null) {
250 // // setPackageFragmentRoot(root, true);
254 private void containerDialogFieldChanged(DialogField field) {
255 if (field == fContainerDialogField) {
256 fContainerStatus = containerChanged();
259 //handleFieldChanged(CONTAINER);
262 // ----------- validation ----------
265 * This method is a hook which gets called after the source folder's text
266 * input field has changed. This default implementation updates the model
267 * and returns an error status. The underlying model is only valid if the
268 * returned status is OK.
270 * @return the model's error status
272 protected IStatus containerChanged() {
273 StatusInfo status = new StatusInfo();
276 String str = getPackageFragmentRootText();
277 if (str.length() == 0) {
279 .setError(NewWizardMessages
280 .getString("NewContainerWizardPage.error.EnterContainerName")); //$NON-NLS-1$
283 IPath path = new Path(str);
284 IResource res = fWorkspaceRoot.findMember(path);
286 int resType = res.getType();
287 if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
288 IProject proj = res.getProject();
289 if (!proj.isOpen()) {
291 .setError(NewWizardMessages
293 "NewContainerWizardPage.error.ProjectClosed", proj.getFullPath().toString())); //$NON-NLS-1$
296 //IJavaProject jproject = JavaCore.create(proj);
297 // fCurrRoot= jproject.getPackageFragmentRoot(res);
298 // if (res.exists()) {
300 // if (!proj.hasNature(JavaCore.NATURE_ID)) {
301 // if (resType == IResource.PROJECT) {
302 // status.setError(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
305 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotInAJavaProject"));
310 // } catch (CoreException e) {
311 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
314 // if (!jproject.isOnClasspath(fCurrRoot)) {
315 // status.setWarning(NewWizardMessages.getFormattedString("NewContainerWizardPage.warning.NotOnClassPath",
316 // str)); //$NON-NLS-1$
318 // if (fCurrRoot.isArchive()) {
319 // status.setError(NewWizardMessages.getFormattedString("NewContainerWizardPage.error.ContainerIsBinary",
320 // str)); //$NON-NLS-1$
326 status.setError(NewWizardMessages.getFormattedString(
327 "NewContainerWizardPage.error.NotAFolder", str)); //$NON-NLS-1$
331 status.setError(NewWizardMessages.getFormattedString(
332 "NewContainerWizardPage.error.ContainerDoesNotExist", str)); //$NON-NLS-1$
337 // -------- update message ----------------
340 * Hook method that gets called when a field on this page has changed. For
341 * this page the method gets called when the source folder field changes.
343 * Every sub type is responsible to call this method when a field on its
344 * page has changed. Subtypes override (extend) the method to add
345 * verification when a own field has a dependency to an other field. For
346 * example the class name input must be verified again when the package
347 * field changes (check for duplicated class names).
350 * The name of the field that has changed (field id). For the
351 * source folder the field id is <code>CONTAINER</code>
353 // protected void handleFieldChanged(String fieldName) {
356 // ---- get ----------------
359 * Returns the workspace root.
361 * @return the workspace root
363 protected IWorkspaceRoot getWorkspaceRoot() {
364 return fWorkspaceRoot;
368 * Returns the <code>IPackageFragmentRoot</code> that corresponds to the
369 * current value of the source folder field.
371 * @return the IPackageFragmentRoot or <code>null</code> if the current
372 * source folder value is not a valid package fragment root
375 public IPackageFragmentRoot getPackageFragmentRoot() {
380 * Returns the current text of source folder text field.
382 * @return the text of the source folder text field
384 public String getPackageFragmentRootText() {
385 return fContainerDialogField.getText();
389 * Sets the current source folder (model and text field) to the given
390 * package fragment root.
392 * @param canBeModified
393 * if <code>false</code> the source folder field can not be
394 * changed by the user. If <code>true</code> the field is
397 // public void setPackageFragmentRoot(IPackageFragmentRoot root, boolean
400 // String str= (root == null) ? "" :
401 // root.getPath().makeRelative().toString(); //$NON-NLS-1$
402 // fContainerDialogField.setText(str);
403 // fContainerDialogField.setEnabled(canBeModified);
405 // ------------- choose source container dialog
406 // private IPackageFragmentRoot chooseSourceContainer(IJavaElement
408 // Class[] acceptedClasses= new Class[] { IPackageFragmentRoot.class,
409 // IJavaProject.class };
410 // TypedElementSelectionValidator validator= new
411 // TypedElementSelectionValidator(acceptedClasses, false) {
412 // public boolean isSelectedValid(Object element) {
414 // if (element instanceof IJavaProject) {
415 // IJavaProject jproject= (IJavaProject)element;
416 // IPath path= jproject.getProject().getFullPath();
417 // return (jproject.findPackageFragmentRoot(path) != null);
418 // } else if (element instanceof IPackageFragmentRoot) {
419 // return (((IPackageFragmentRoot)element).getKind() ==
420 // IPackageFragmentRoot.K_SOURCE);
423 // } catch (JavaModelException e) {
424 // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
430 // acceptedClasses= new Class[] { IJavaModel.class,
431 // IPackageFragmentRoot.class, IJavaProject.class };
432 // ViewerFilter filter= new TypedViewerFilter(acceptedClasses) {
433 // public boolean select(Viewer viewer, Object parent, Object element) {
434 // if (element instanceof IPackageFragmentRoot) {
436 // return (((IPackageFragmentRoot)element).getKind() ==
437 // IPackageFragmentRoot.K_SOURCE);
438 // } catch (JavaModelException e) {
439 // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
443 // return super.select(viewer, parent, element);
447 // StandardJavaElementContentProvider provider= new
448 // StandardJavaElementContentProvider();
449 // ILabelProvider labelProvider= new
450 // JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
451 // ElementTreeSelectionDialog dialog= new
452 // ElementTreeSelectionDialog(getShell(), labelProvider, provider);
453 // dialog.setValidator(validator);
454 // dialog.setSorter(new JavaElementSorter());
455 // dialog.setTitle(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.title"));
457 // dialog.setMessage(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.description"));
459 // dialog.addFilter(filter);
460 // dialog.setInput(JavaCore.create(fWorkspaceRoot));
461 // dialog.setInitialSelection(initElement);
463 // if (dialog.open() == ElementTreeSelectionDialog.OK) {
464 // Object element= dialog.getFirstResult();
465 // if (element instanceof IJavaProject) {
466 // IJavaProject jproject= (IJavaProject)element;
467 // return jproject.getPackageFragmentRoot(jproject.getProject());
468 // } else if (element instanceof IPackageFragmentRoot) {
469 // return (IPackageFragmentRoot)element;