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