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;
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.resources.IWorkspaceRoot;
31 import org.eclipse.core.resources.ResourcesPlugin;
32 import org.eclipse.core.runtime.IAdaptable;
33 import org.eclipse.core.runtime.IPath;
34 import org.eclipse.core.runtime.IStatus;
35 import org.eclipse.core.runtime.Path;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.ui.IWorkbenchPart;
39 import org.eclipse.ui.views.contentoutline.ContentOutline;
42 * Wizard page that acts as a base class for wizard pages that create new Java
43 * elements. The class provides a input field for source folders (called
44 * container in this class) and API to validate the enter source folder name.
48 public abstract class NewContainerWizardPage extends NewElementWizardPage {
50 /** Id of the container field */
51 protected static final String CONTAINER = "NewContainerWizardPage.container"; //$NON-NLS-1$
53 /** The status of the last validation. */
54 protected IStatus fContainerStatus;
56 private StringButtonDialogField fContainerDialogField;
59 * package fragment root corresponding to the input type (can be null)
61 private IPackageFragmentRoot fCurrRoot;
63 private IWorkspaceRoot fWorkspaceRoot;
66 * Create a new <code>NewContainerWizardPage</code>
69 * the wizard page's name
71 public NewContainerWizardPage(String name) {
73 fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
74 ContainerFieldAdapter adapter = new ContainerFieldAdapter();
76 fContainerDialogField = new StringButtonDialogField(adapter);
77 fContainerDialogField.setDialogFieldListener(adapter);
78 fContainerDialogField.setLabelText(NewWizardMessages
79 .getString("NewContainerWizardPage.container.label")); //$NON-NLS-1$
80 fContainerDialogField.setButtonLabel(NewWizardMessages
81 .getString("NewContainerWizardPage.container.button")); //$NON-NLS-1$
83 fContainerStatus = new StatusInfo();
88 * Initializes the source folder field with a valid package fragement root.
89 * The package fragement root is computed from the given Java element.
92 * the Java element used to compute the initial package fragment
93 * root used as the source folder
95 protected void initContainerPage(IJavaElement elem) {
96 IPackageFragmentRoot initRoot = null;
97 // if (elem != null) {
98 // initRoot= JavaModelUtil.getPackageFragmentRoot(elem);
99 // if (initRoot == null || initRoot.isArchive()) {
100 // IJavaProject jproject= elem.getJavaProject();
101 // if (jproject != null) {
104 // if (jproject.exists()) {
105 // IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots();
106 // for (int i= 0; i < roots.length; i++) {
107 // if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
108 // initRoot= roots[i];
113 // } catch (JavaModelException e) {
114 // PHPeclipsePlugin.log(e);
116 // if (initRoot == null) {
117 // initRoot= jproject.getPackageFragmentRoot(jproject.getResource());
122 // setPackageFragmentRoot(initRoot, true);
126 * Utility method to inspect a selection to find a Java element.
129 * the selection to be inspected
130 * @return a Java element to be used as the initial selection, or
131 * <code>null</code>, if no Java element exists in the given
134 protected IJavaElement getInitialJavaElement(IStructuredSelection selection) {
135 IJavaElement jelem = null;
136 if (selection != null && !selection.isEmpty()) {
137 Object selectedElement = selection.getFirstElement();
138 if (selectedElement instanceof IAdaptable) {
139 IAdaptable adaptable = (IAdaptable) selectedElement;
141 jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
143 IResource resource = (IResource) adaptable
144 .getAdapter(IResource.class);
146 && resource.getType() != IResource.ROOT) {
148 && resource.getType() != IResource.PROJECT) {
149 resource = resource.getParent();
150 jelem = (IJavaElement) resource
151 .getAdapter(IJavaElement.class);
154 jelem = JavaCore.create(resource); // java project
161 IWorkbenchPart part = PHPeclipsePlugin.getActivePage()
163 if (part instanceof ContentOutline) {
164 part = PHPeclipsePlugin.getActivePage().getActiveEditor();
167 if (part instanceof IViewPartInputProvider) {
168 Object elem = ((IViewPartInputProvider) part)
170 if (elem instanceof IJavaElement) {
171 jelem = (IJavaElement) elem;
176 if (jelem == null || jelem.getElementType() == IJavaElement.JAVA_MODEL) {
178 IJavaProject[] projects = JavaCore.create(getWorkspaceRoot())
180 if (projects.length == 1) {
183 } catch (JavaModelException e) {
184 PHPeclipsePlugin.log(e);
191 * Returns the recommended maximum width for text fields (in pixels). This
192 * method requires that createContent has been called before this method is
193 * call. Subclasses may override to change the maximum width for text
196 * @return the recommended maximum width for text fields.
198 protected int getMaxFieldWidth() {
199 return convertWidthInCharsToPixels(40);
203 * Creates the necessary controls (label, text field and browse button) to
204 * edit the source folder location. The method expects that the parent
205 * composite uses a <code>GridLayout</code> as its layout manager and that
206 * the grid layout has at least 3 columns.
209 * the parent composite
211 * the number of columns to span. This number must be greater or
214 protected void createContainerControls(Composite parent, int nColumns) {
215 fContainerDialogField.doFillIntoGrid(parent, nColumns);
216 LayoutUtil.setWidthHint(fContainerDialogField.getTextControl(null),
221 * Sets the focus to the source folder's text field.
223 protected void setFocusOnContainer() {
224 fContainerDialogField.setFocus();
227 // -------- ContainerFieldAdapter --------
229 private class ContainerFieldAdapter implements IStringButtonAdapter,
230 IDialogFieldListener {
232 // -------- IStringButtonAdapter
233 public void changeControlPressed(DialogField field) {
234 containerChangeControlPressed(field);
237 // -------- IDialogFieldListener
238 public void dialogFieldChanged(DialogField field) {
239 containerDialogFieldChanged(field);
243 private void containerChangeControlPressed(DialogField field) {
244 // take the current jproject as init element of the dialog
245 // IPackageFragmentRoot root= getPackageFragmentRoot();
246 // root= chooseSourceContainer(root);
247 // if (root != null) {
248 // setPackageFragmentRoot(root, true);
252 private void containerDialogFieldChanged(DialogField field) {
253 if (field == fContainerDialogField) {
254 fContainerStatus = containerChanged();
257 handleFieldChanged(CONTAINER);
260 // ----------- validation ----------
263 * This method is a hook which gets called after the source folder's text
264 * input field has changed. This default implementation updates the model
265 * and returns an error status. The underlying model is only valid if the
266 * returned status is OK.
268 * @return the model's error status
270 protected IStatus containerChanged() {
271 StatusInfo status = new StatusInfo();
274 String str = getPackageFragmentRootText();
275 if (str.length() == 0) {
277 .setError(NewWizardMessages
278 .getString("NewContainerWizardPage.error.EnterContainerName")); //$NON-NLS-1$
281 IPath path = new Path(str);
282 IResource res = fWorkspaceRoot.findMember(path);
284 int resType = res.getType();
285 if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
286 IProject proj = res.getProject();
287 if (!proj.isOpen()) {
289 .setError(NewWizardMessages
291 "NewContainerWizardPage.error.ProjectClosed", proj.getFullPath().toString())); //$NON-NLS-1$
294 IJavaProject jproject = JavaCore.create(proj);
295 // fCurrRoot= jproject.getPackageFragmentRoot(res);
296 // if (res.exists()) {
298 // if (!proj.hasNature(JavaCore.NATURE_ID)) {
299 // if (resType == IResource.PROJECT) {
300 // status.setError(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
303 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotInAJavaProject"));
308 // } catch (CoreException e) {
309 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
312 // if (!jproject.isOnClasspath(fCurrRoot)) {
313 // status.setWarning(NewWizardMessages.getFormattedString("NewContainerWizardPage.warning.NotOnClassPath",
314 // str)); //$NON-NLS-1$
316 // if (fCurrRoot.isArchive()) {
317 // status.setError(NewWizardMessages.getFormattedString("NewContainerWizardPage.error.ContainerIsBinary",
318 // str)); //$NON-NLS-1$
324 status.setError(NewWizardMessages.getFormattedString(
325 "NewContainerWizardPage.error.NotAFolder", str)); //$NON-NLS-1$
329 status.setError(NewWizardMessages.getFormattedString(
330 "NewContainerWizardPage.error.ContainerDoesNotExist", str)); //$NON-NLS-1$
335 // -------- update message ----------------
338 * Hook method that gets called when a field on this page has changed. For
339 * this page the method gets called when the source folder field changes.
341 * Every sub type is responsible to call this method when a field on its
342 * page has changed. Subtypes override (extend) the method to add
343 * verification when a own field has a dependency to an other field. For
344 * example the class name input must be verified again when the package
345 * field changes (check for duplicated class names).
348 * The name of the field that has changed (field id). For the
349 * source folder the field id is <code>CONTAINER</code>
351 protected void handleFieldChanged(String fieldName) {
354 // ---- get ----------------
357 * Returns the workspace root.
359 * @return the workspace root
361 protected IWorkspaceRoot getWorkspaceRoot() {
362 return fWorkspaceRoot;
366 * Returns the <code>IPackageFragmentRoot</code> that corresponds to the
367 * current value of the source folder field.
369 * @return the IPackageFragmentRoot or <code>null</code> if the current
370 * source folder value is not a valid package fragment root
373 public IPackageFragmentRoot getPackageFragmentRoot() {
378 * Returns the current text of source folder text field.
380 * @return the text of the source folder text field
382 public String getPackageFragmentRootText() {
383 return fContainerDialogField.getText();
387 * Sets the current source folder (model and text field) to the given
388 * package fragment root.
390 * @param canBeModified
391 * if <code>false</code> the source folder field can not be
392 * changed by the user. If <code>true</code> the field is
395 // public void setPackageFragmentRoot(IPackageFragmentRoot root, boolean
398 // String str= (root == null) ? "" :
399 // root.getPath().makeRelative().toString(); //$NON-NLS-1$
400 // fContainerDialogField.setText(str);
401 // fContainerDialogField.setEnabled(canBeModified);
403 // ------------- choose source container dialog
404 // private IPackageFragmentRoot chooseSourceContainer(IJavaElement
406 // Class[] acceptedClasses= new Class[] { IPackageFragmentRoot.class,
407 // IJavaProject.class };
408 // TypedElementSelectionValidator validator= new
409 // TypedElementSelectionValidator(acceptedClasses, false) {
410 // public boolean isSelectedValid(Object element) {
412 // if (element instanceof IJavaProject) {
413 // IJavaProject jproject= (IJavaProject)element;
414 // IPath path= jproject.getProject().getFullPath();
415 // return (jproject.findPackageFragmentRoot(path) != null);
416 // } else if (element instanceof IPackageFragmentRoot) {
417 // return (((IPackageFragmentRoot)element).getKind() ==
418 // IPackageFragmentRoot.K_SOURCE);
421 // } catch (JavaModelException e) {
422 // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
428 // acceptedClasses= new Class[] { IJavaModel.class,
429 // IPackageFragmentRoot.class, IJavaProject.class };
430 // ViewerFilter filter= new TypedViewerFilter(acceptedClasses) {
431 // public boolean select(Viewer viewer, Object parent, Object element) {
432 // if (element instanceof IPackageFragmentRoot) {
434 // return (((IPackageFragmentRoot)element).getKind() ==
435 // IPackageFragmentRoot.K_SOURCE);
436 // } catch (JavaModelException e) {
437 // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
441 // return super.select(viewer, parent, element);
445 // StandardJavaElementContentProvider provider= new
446 // StandardJavaElementContentProvider();
447 // ILabelProvider labelProvider= new
448 // JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
449 // ElementTreeSelectionDialog dialog= new
450 // ElementTreeSelectionDialog(getShell(), labelProvider, provider);
451 // dialog.setValidator(validator);
452 // dialog.setSorter(new JavaElementSorter());
453 // dialog.setTitle(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.title"));
455 // dialog.setMessage(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.description"));
457 // dialog.addFilter(filter);
458 // dialog.setInput(JavaCore.create(fWorkspaceRoot));
459 // dialog.setInitialSelection(initElement);
461 // if (dialog.open() == ElementTreeSelectionDialog.OK) {
462 // Object element= dialog.getFirstResult();
463 // if (element instanceof IJavaProject) {
464 // IJavaProject jproject= (IJavaProject)element;
465 // return jproject.getPackageFragmentRoot(jproject.getProject());
466 // } else if (element instanceof IPackageFragmentRoot) {
467 // return (IPackageFragmentRoot)element;