fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / wizards / NewContainerWizardPage.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui.wizards;
12
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
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;
40
41 /**
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.
45  * 
46  * @since 2.0
47  */
48 public abstract class NewContainerWizardPage extends NewElementWizardPage {
49
50         /** Id of the container field */
51         protected static final String CONTAINER = "NewContainerWizardPage.container"; //$NON-NLS-1$
52
53         /** The status of the last validation. */
54         protected IStatus fContainerStatus;
55
56         private StringButtonDialogField fContainerDialogField;
57
58         /*
59          * package fragment root corresponding to the input type (can be null)
60          */
61         private IPackageFragmentRoot fCurrRoot;
62
63         private IWorkspaceRoot fWorkspaceRoot;
64
65         /**
66          * Create a new <code>NewContainerWizardPage</code>
67          * 
68          * @param name
69          *            the wizard page's name
70          */
71         public NewContainerWizardPage(String name) {
72                 super(name);
73                 fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
74                 ContainerFieldAdapter adapter = new ContainerFieldAdapter();
75
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$
82
83                 fContainerStatus = new StatusInfo();
84                 fCurrRoot = null;
85         }
86
87         /**
88          * Initializes the source folder field with a valid package fragement root.
89          * The package fragement root is computed from the given Java element.
90          * 
91          * @param elem
92          *            the Java element used to compute the initial package fragment
93          *            root used as the source folder
94          */
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) {
102                 // try {
103                 // initRoot= 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];
109                 // break;
110                 // }
111                 // }
112                 // }
113                 // } catch (JavaModelException e) {
114                 // PHPeclipsePlugin.log(e);
115                 // }
116                 // if (initRoot == null) {
117                 // initRoot= jproject.getPackageFragmentRoot(jproject.getResource());
118                 // }
119                 // }
120                 // }
121                 // }
122                 // setPackageFragmentRoot(initRoot, true);
123         }
124
125         /**
126          * Utility method to inspect a selection to find a Java element.
127          * 
128          * @param selection
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
132          *         selection
133          */
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;
140
141                                 jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
142                                 if (jelem == null) {
143                                         IResource resource = (IResource) adaptable
144                                                         .getAdapter(IResource.class);
145                                         if (resource != null
146                                                         && resource.getType() != IResource.ROOT) {
147                                                 while (jelem == null
148                                                                 && resource.getType() != IResource.PROJECT) {
149                                                         resource = resource.getParent();
150                                                         jelem = (IJavaElement) resource
151                                                                         .getAdapter(IJavaElement.class);
152                                                 }
153                                                 if (jelem == null) {
154                                                         jelem = JavaCore.create(resource); // java project
155                                                 }
156                                         }
157                                 }
158                         }
159                 }
160                 if (jelem == null) {
161                         IWorkbenchPart part = PHPeclipsePlugin.getActivePage()
162                                         .getActivePart();
163                         if (part instanceof ContentOutline) {
164                                 part = PHPeclipsePlugin.getActivePage().getActiveEditor();
165                         }
166
167                         if (part instanceof IViewPartInputProvider) {
168                                 Object elem = ((IViewPartInputProvider) part)
169                                                 .getViewPartInput();
170                                 if (elem instanceof IJavaElement) {
171                                         jelem = (IJavaElement) elem;
172                                 }
173                         }
174                 }
175
176                 if (jelem == null || jelem.getElementType() == IJavaElement.JAVA_MODEL) {
177                         try {
178                                 IJavaProject[] projects = JavaCore.create(getWorkspaceRoot())
179                                                 .getJavaProjects();
180                                 if (projects.length == 1) {
181                                         jelem = projects[0];
182                                 }
183                         } catch (JavaModelException e) {
184                                 PHPeclipsePlugin.log(e);
185                         }
186                 }
187                 return jelem;
188         }
189
190         /**
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
194          * fields.
195          * 
196          * @return the recommended maximum width for text fields.
197          */
198         protected int getMaxFieldWidth() {
199                 return convertWidthInCharsToPixels(40);
200         }
201
202         /**
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.
207          * 
208          * @param parent
209          *            the parent composite
210          * @param nColumns
211          *            the number of columns to span. This number must be greater or
212          *            equal three
213          */
214         protected void createContainerControls(Composite parent, int nColumns) {
215                 fContainerDialogField.doFillIntoGrid(parent, nColumns);
216                 LayoutUtil.setWidthHint(fContainerDialogField.getTextControl(null),
217                                 getMaxFieldWidth());
218         }
219
220         /**
221          * Sets the focus to the source folder's text field.
222          */
223         protected void setFocusOnContainer() {
224                 fContainerDialogField.setFocus();
225         }
226
227         // -------- ContainerFieldAdapter --------
228
229         private class ContainerFieldAdapter implements IStringButtonAdapter,
230                         IDialogFieldListener {
231
232                 // -------- IStringButtonAdapter
233                 public void changeControlPressed(DialogField field) {
234                         containerChangeControlPressed(field);
235                 }
236
237                 // -------- IDialogFieldListener
238                 public void dialogFieldChanged(DialogField field) {
239                         containerDialogFieldChanged(field);
240                 }
241         }
242
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);
249                 // }
250         }
251
252         private void containerDialogFieldChanged(DialogField field) {
253                 if (field == fContainerDialogField) {
254                         fContainerStatus = containerChanged();
255                 }
256                 // tell all others
257                 handleFieldChanged(CONTAINER);
258         }
259
260         // ----------- validation ----------
261
262         /**
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.
267          * 
268          * @return the model's error status
269          */
270         protected IStatus containerChanged() {
271                 StatusInfo status = new StatusInfo();
272
273                 fCurrRoot = null;
274                 String str = getPackageFragmentRootText();
275                 if (str.length() == 0) {
276                         status
277                                         .setError(NewWizardMessages
278                                                         .getString("NewContainerWizardPage.error.EnterContainerName")); //$NON-NLS-1$
279                         return status;
280                 }
281                 IPath path = new Path(str);
282                 IResource res = fWorkspaceRoot.findMember(path);
283                 if (res != null) {
284                         int resType = res.getType();
285                         if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
286                                 IProject proj = res.getProject();
287                                 if (!proj.isOpen()) {
288                                         status
289                                                         .setError(NewWizardMessages
290                                                                         .getFormattedString(
291                                                                                         "NewContainerWizardPage.error.ProjectClosed", proj.getFullPath().toString())); //$NON-NLS-1$
292                                         return status;
293                                 }
294                                 IJavaProject jproject = JavaCore.create(proj);
295                                 // fCurrRoot= jproject.getPackageFragmentRoot(res);
296                                 // if (res.exists()) {
297                                 // try {
298                                 // if (!proj.hasNature(JavaCore.NATURE_ID)) {
299                                 // if (resType == IResource.PROJECT) {
300                                 // status.setError(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
301                                 // //$NON-NLS-1$
302                                 // } else {
303                                 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotInAJavaProject"));
304                                 // //$NON-NLS-1$
305                                 // }
306                                 // return status;
307                                 // }
308                                 // } catch (CoreException e) {
309                                 // status.setWarning(NewWizardMessages.getString("NewContainerWizardPage.warning.NotAJavaProject"));
310                                 // //$NON-NLS-1$
311                                 // }
312                                 // if (!jproject.isOnClasspath(fCurrRoot)) {
313                                 // status.setWarning(NewWizardMessages.getFormattedString("NewContainerWizardPage.warning.NotOnClassPath",
314                                 // str)); //$NON-NLS-1$
315                                 // }
316                                 // if (fCurrRoot.isArchive()) {
317                                 // status.setError(NewWizardMessages.getFormattedString("NewContainerWizardPage.error.ContainerIsBinary",
318                                 // str)); //$NON-NLS-1$
319                                 // return status;
320                                 // }
321                                 // }
322                                 return status;
323                         } else {
324                                 status.setError(NewWizardMessages.getFormattedString(
325                                                 "NewContainerWizardPage.error.NotAFolder", str)); //$NON-NLS-1$
326                                 return status;
327                         }
328                 } else {
329                         status.setError(NewWizardMessages.getFormattedString(
330                                         "NewContainerWizardPage.error.ContainerDoesNotExist", str)); //$NON-NLS-1$
331                         return status;
332                 }
333         }
334
335         // -------- update message ----------------
336
337         /**
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.
340          * <p>
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).
346          * 
347          * @param fieldName
348          *            The name of the field that has changed (field id). For the
349          *            source folder the field id is <code>CONTAINER</code>
350          */
351         protected void handleFieldChanged(String fieldName) {
352         }
353
354         // ---- get ----------------
355
356         /**
357          * Returns the workspace root.
358          * 
359          * @return the workspace root
360          */
361         protected IWorkspaceRoot getWorkspaceRoot() {
362                 return fWorkspaceRoot;
363         }
364
365         /**
366          * Returns the <code>IPackageFragmentRoot</code> that corresponds to the
367          * current value of the source folder field.
368          * 
369          * @return the IPackageFragmentRoot or <code>null</code> if the current
370          *         source folder value is not a valid package fragment root
371          * 
372          */
373         public IPackageFragmentRoot getPackageFragmentRoot() {
374                 return fCurrRoot;
375         }
376
377         /**
378          * Returns the current text of source folder text field.
379          * 
380          * @return the text of the source folder text field
381          */
382         public String getPackageFragmentRootText() {
383                 return fContainerDialogField.getText();
384         }
385
386         /**
387          * Sets the current source folder (model and text field) to the given
388          * package fragment root.
389          * 
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
393          *            editable
394          */
395         // public void setPackageFragmentRoot(IPackageFragmentRoot root, boolean
396         // canBeModified) {
397         // fCurrRoot= root;
398         // String str= (root == null) ? "" :
399         // root.getPath().makeRelative().toString(); //$NON-NLS-1$
400         // fContainerDialogField.setText(str);
401         // fContainerDialogField.setEnabled(canBeModified);
402         // }
403         // ------------- choose source container dialog
404         // private IPackageFragmentRoot chooseSourceContainer(IJavaElement
405         // initElement) {
406         // Class[] acceptedClasses= new Class[] { IPackageFragmentRoot.class,
407         // IJavaProject.class };
408         // TypedElementSelectionValidator validator= new
409         // TypedElementSelectionValidator(acceptedClasses, false) {
410         // public boolean isSelectedValid(Object element) {
411         // try {
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);
419         // }
420         // return true;
421         // } catch (JavaModelException e) {
422         // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
423         // }
424         // return false;
425         // }
426         // };
427         //              
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) {
433         // try {
434         // return (((IPackageFragmentRoot)element).getKind() ==
435         // IPackageFragmentRoot.K_SOURCE);
436         // } catch (JavaModelException e) {
437         // PHPeclipsePlugin.log(e.getStatus()); // just log, no ui in validation
438         // return false;
439         // }
440         // }
441         // return super.select(viewer, parent, element);
442         // }
443         // };
444         //
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"));
454         // //$NON-NLS-1$
455         // dialog.setMessage(NewWizardMessages.getString("NewContainerWizardPage.ChooseSourceContainerDialog.description"));
456         // //$NON-NLS-1$
457         // dialog.addFilter(filter);
458         // dialog.setInput(JavaCore.create(fWorkspaceRoot));
459         // dialog.setInitialSelection(initElement);
460         //              
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;
468         // }
469         // return null;
470         // }
471         // return null;
472         // }
473 }