KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > ClasspathModifierQueries


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IResource;
20
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.window.Window;
26
27 import org.eclipse.ui.dialogs.NewFolderDialog;
28
29 import org.eclipse.jdt.core.IClasspathEntry;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.JavaModelException;
32
33 import org.eclipse.jdt.internal.corext.util.Messages;
34
35 import org.eclipse.jdt.ui.PreferenceConstants;
36 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
37
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
40 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
41 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ExclusionInclusionDialog;
42
43 /**
44  * Helper class for queries used by the <code>ClasspathModifier</code>.
45  * Clients can either decide to implement their own queries or just taking
46  * the predefined queries.
47  */

48 public class ClasspathModifierQueries {
49
50     /**
51      * A validator for the output location that can be
52      * used to find out whether the entred location can be
53      * used for an output folder or not.
54      */

55     public static abstract class OutputFolderValidator {
56         protected IClasspathEntry[] fEntries;
57         protected List JavaDoc fElements;
58         
59         /**
60          * Create a output folder validator.
61          *
62          * @param newElements a list of elements that will be added
63          * to the buildpath. The list's items can be of type:
64          * <li><code>IJavaProject</code></li>
65          * <li><code>IPackageFragment</code></li>
66          * <li><code>IFolder</code></li>
67          * @param project the Java project
68          * @throws JavaModelException
69          */

70         public OutputFolderValidator(List JavaDoc newElements, IJavaProject project) throws JavaModelException {
71             fEntries= project.getRawClasspath();
72             fElements= newElements;
73         }
74         
75         /**
76          * The path of the output location to be validated. The path
77          * should contain the full path within the project, for example:
78          * /ProjectXY/folderA/outputLocation.
79          *
80          * @param outputLocation the output location for the project
81          * @return <code>true</code> if the output location is valid,
82          * <code>false</code> otherwise.
83          */

84         public abstract boolean validate(IPath outputLocation);
85     }
86     
87     /**
88      * Query that processes the request of
89      * creating a link to an existing source
90      * folder.
91      */

92     public static interface ILinkToQuery {
93         /**
94          * Query that processes the request of
95          * creating a link to an existing source
96          * folder.
97          *
98          * @return <code>true</code> if the query was
99          * executed successfully (that is the result of
100          * this query can be used), <code>false</code>
101          * otherwise
102          */

103         public boolean doQuery();
104         
105         /**
106          * Get the newly created folder.
107          * This method is only valid after having
108          * called <code>doQuery</code>.
109          *
110          * @return the created folder of type
111          * <code>IFolder</code>
112          */

113         public IFolder getCreatedFolder();
114         
115         /**
116          * Getter for an output folder query.
117          *
118          * @return an output folder query which will be needed
119          * when adding the folder to the build path
120          *
121          */

122         public OutputFolderQuery getOutputFolderQuery();
123     }
124     /**
125      * Query to get information about whether the project should be removed as
126      * source folder and update build folder to <code>outputLocation</code>
127      */

128     public static abstract class OutputFolderQuery {
129         protected IPath fDesiredOutputLocation;
130         
131         /**
132          * Constructor gets the desired output location
133          * of the project
134          *
135          * @param outputLocation desired output location for the
136          * project. It is possible that the desired output location
137          * equals the current project's output location (for example if
138          * it is not intended to change the output location at this time).
139          */

140         public OutputFolderQuery(IPath outputLocation) {
141             if (outputLocation != null)
142                 fDesiredOutputLocation= outputLocation.makeAbsolute();
143         }
144         
145         /**
146          * Getter for the desired output location.
147          *
148          * @return the project's desired output location
149          */

150         public IPath getDesiredOutputLocation() {
151             return fDesiredOutputLocation;
152         }
153         
154         /**
155          * Get the output location that was determined by the
156          * query for the project. Note that this output location
157          * does not have to be the same as the desired output location
158          * that is passed to the constructor.
159          *
160          * This method is only intended to be called if <code>doQuery</code>
161          * has been executed successfully and had return <code>true</code> to
162          * indicate that changes were accepted.
163          *
164          *@return the effective output location
165          */

166         public abstract IPath getOutputLocation();
167         
168         /**
169          * Find out wheter the project should be removed from the classpath
170          * or not.
171          *
172          * This method is only intended to be called if <code>doQuery</code>
173          * has been executed successfully and had return <code>true</code> to
174          * indicate that changes were accepted.
175          *
176          * @return <code>true</code> if the project should be removed from
177          * the classpath, <code>false</code> otherwise.
178          */

179         public abstract boolean removeProjectFromClasspath();
180         
181         /**
182          * Query to get information about whether the project should be removed as
183          * source folder and update build folder to <code>outputLocation</code>.
184          *
185          * There are several situations for setting up a project where it is not possible
186          * to have the project folder itself as output folder. Therefore, the query asks in the
187          * first place for changing the output folder. Additionally, it also can be usefull to
188          * remove the project from the classpath. This information can be retrieved by calling
189          * <code>removeProjectFromClasspath()</code>.
190          *
191          * Note: if <code>doQuery</code> returns false, the started computation will stop immediately.
192          * There is no additional dialog that informs the user about this abort. Therefore it is important
193          * that the query informs the users about the consequences of not allowing to change the output
194          * folder.
195          *
196          * @param editingOutputFolder <code>true</code> if currently an output folder is changed,
197          * <code>false</code> otherwise. This information can be usefull to generate an appropriate
198          * message to ask the user for an action.
199          * @param validator a validator to find out whether the chosen output location is valid or not
200          * @param project the Java project
201          * @return <code>true</code> if the execution was successfull (e.g. not aborted) and
202          * the caller should execute additional steps as setting the output location for the project or (optionally)
203          * removing the project from the classpath, <code>false</code> otherwise.
204          * @throws JavaModelException if the output location of the project could not be retrieved
205          */

206         public abstract boolean doQuery(final boolean editingOutputFolder, final OutputFolderValidator validator, final IJavaProject project) throws JavaModelException;
207         
208     }
209     
210     /**
211      * Query to get information about the inclusion and exclusion filters of
212      * an element.
213      */

214     public static interface IInclusionExclusionQuery {
215         /**
216          * Query to get information about the
217          * inclusion and exclusion filters of
218          * an element.
219          *
220          * While executing <code>doQuery</code>,
221          * these filter might change.
222          *
223          * On calling <code>getInclusionPattern()</code>
224          * or <code>getExclusionPattern()</code> it
225          * is expected to get the new and updated
226          * filters back.
227          *
228          * @param element the element to get the
229          * information from
230          * @param focusOnExcluded
231          * @return <code>true</code> if changes
232          * have been accepted and <code>getInclusionPatter</code>
233          * or <code>getExclusionPattern</code> can
234          * be called.
235          */

236         public boolean doQuery(CPListElement element, boolean focusOnExcluded);
237         
238         /**
239          * Can only be called after <code>
240          * doQuery</code> has been executed and
241          * has returned <code>true</code>
242          *
243          * @return the new inclusion filters
244          */

245         public IPath[] getInclusionPattern();
246         
247         /**
248          * Can only be called after <code>
249          * doQuery</code> has been executed and
250          * has returned <code>true</code>
251          *
252          * @return the new exclusion filters
253          */

254         public IPath[] getExclusionPattern();
255     }
256
257     /**
258      * Query to get information about the output location that should be used for a
259      * given element.
260      */

261     public static interface IOutputLocationQuery {
262         /**
263          * Query to get information about the output
264          * location that should be used for a
265          * given element.
266          *
267          * @param element the element to get
268          * an output location for
269          * @return <code>true</code> if the output
270          * location has changed, <code>false</code>
271          * otherwise.
272          */

273         public boolean doQuery(CPListElement element);
274         
275         /**
276          * Gets the new output location.
277          *
278          * May only be called after having
279          * executed <code>doQuery</code> which
280          * must have returned <code>true</code>
281          *
282          * @return the new output location, can be <code>null</code>
283          */

284         public IPath getOutputLocation();
285         
286         /**
287          * Get a query for information about whether the project should be removed as
288          * source folder and update build folder
289          *
290          * @param outputLocation desired output location for the
291          * project
292          * @return query giving information about output and source folders
293          * @throws JavaModelException
294          *
295          */

296         public OutputFolderQuery getOutputFolderQuery(IPath outputLocation) throws JavaModelException;
297     }
298
299     /**
300      * Query to determine whether a linked folder should be removed.
301      */

302     public static interface IRemoveLinkedFolderQuery {
303
304         /** Remove status indicating that the removal should be cancelled */
305         public static final int REMOVE_CANCEL= 0;
306
307         /** Remove status indicating that the folder should be removed from the build path only */
308         public static final int REMOVE_BUILD_PATH= 1;
309
310         /** Remove status indicating that the folder should be removed from the build path and deleted */
311         public static final int REMOVE_BUILD_PATH_AND_FOLDER= 2;
312
313         /**
314          * Query to determined whether the linked folder should be removed as well.
315          *
316          * @param folder the linked folder to remove
317          * @return a status code corresponding to one of the IRemoveLinkedFolderQuery#REMOVE_XXX constants
318          */

319         public int doQuery(IFolder folder);
320     }
321
322     /**
323      * Query to create a folder.
324      */

325     public static interface ICreateFolderQuery {
326         /**
327          * Query to create a folder.
328          *
329          * @return <code>true</code> if the operation
330          * was successful (e.g. no cancelled), <code>
331          * false</code> otherwise
332          */

333         public boolean doQuery();
334         
335         /**
336          * Find out whether a source folder is about
337          * to be created or a normal folder which
338          * is not on the classpath (and therefore
339          * might have to be excluded).
340          *
341          * Should only be called after having executed
342          * <code>doQuery</code>, because otherwise
343          * it might not be sure if a result exists or
344          * not.
345          *
346          * @return <code>true</code> if a source
347          * folder should be created, <code>false
348          * </code> otherwise
349          */

350         public boolean isSourceFolder();
351         
352         /**
353          * Get the newly created folder.
354          * This method is only valid after having
355          * called <code>doQuery</code>.
356          *
357          * @return the created folder of type
358          * <code>IFolder</code>
359          */

360         public IFolder getCreatedFolder();
361     }
362
363     /**
364      * Query to add archives (.jar or .zip files) to the buildpath.
365      */

366     public static interface IAddArchivesQuery {
367         /**
368          * Get the paths to the new archive entries that should be added to the buildpath.
369          *
370          * @return Returns the new classpath container entry paths or an empty array if the query has
371          * been cancelled by the user.
372          */

373         public IPath[] doQuery();
374     }
375     
376     /**
377      * Query to add libraries to the buildpath.
378      */

379     public static interface IAddLibrariesQuery {
380         /**
381          * Get the new classpath entries for libraries to be added to the buildpath.
382          *
383          * @param project the Java project
384          * @param entries an array of classpath entries for the project
385          * @return Returns the selected classpath container entries or an empty if the query has
386          * been cancelled by the user.
387          */

388         public IClasspathEntry[] doQuery(final IJavaProject project, final IClasspathEntry[] entries);
389     }
390     
391     /**
392      * The query is used to get information about whether the project should be removed as
393      * source folder and update build folder to <code>outputLocation</code>
394      *
395      * @param shell shell if there is any or <code>null</code>
396      * @param outputLocation the desired project's output location
397      * @return an <code>IOutputFolderQuery</code> that can be executed
398      *
399      * @see OutputFolderQuery
400      */

401     public static OutputFolderQuery getDefaultFolderQuery(final Shell shell, IPath outputLocation) {
402         
403         return new OutputFolderQuery(outputLocation) {
404             protected IPath fOutputLocation;
405             protected boolean fRemoveProject;
406             
407             public boolean doQuery(final boolean editingOutputFolder, final OutputFolderValidator validator, final IJavaProject project) throws JavaModelException {
408                 final boolean[] result= { false };
409                 fRemoveProject= false;
410                 fOutputLocation= project.getOutputLocation();
411                 Display.getDefault().syncExec(new Runnable JavaDoc() {
412                     public void run() {
413                         Shell sh= shell != null ? shell : JavaPlugin.getActiveWorkbenchShell();
414                         
415                         String JavaDoc title= NewWizardMessages.ClasspathModifier_ChangeOutputLocationDialog_title;
416                         
417                         if (fDesiredOutputLocation.segmentCount() == 1) {
418                             String JavaDoc outputFolderName= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME);
419                             IPath newOutputFolder= fDesiredOutputLocation.append(outputFolderName);
420                             newOutputFolder= getValidPath(newOutputFolder, validator);
421                             String JavaDoc message= Messages.format(NewWizardMessages.ClasspathModifier_ChangeOutputLocationDialog_project_outputLocation, newOutputFolder);
422                             fRemoveProject= true;
423                             if (MessageDialog.openConfirm(sh, title, message)) {
424                                 fOutputLocation= newOutputFolder;
425                                 result[0]= true;
426                             }
427                         } else {
428                             IPath newOutputFolder= fDesiredOutputLocation;
429                             newOutputFolder= getValidPath(newOutputFolder, validator);
430                             if (editingOutputFolder) {
431                                 fOutputLocation= newOutputFolder;
432                                 result[0]= true;
433                                 return; // show no dialog
434
}
435                             String JavaDoc message= NewWizardMessages.ClasspathModifier_ChangeOutputLocationDialog_project_message;
436                             fRemoveProject= true;
437                             if (MessageDialog.openQuestion(sh, title, message)) {
438                                 fOutputLocation= newOutputFolder;
439                                 result[0]= true;
440                             }
441                         }
442                     }
443                 });
444                 return result[0];
445             }
446             
447             public IPath getOutputLocation() {
448                 return fOutputLocation;
449             }
450             
451             public boolean removeProjectFromClasspath() {
452                 return fRemoveProject;
453             }
454             
455             private IPath getValidPath(IPath newOutputFolder, OutputFolderValidator validator) {
456                 int i= 1;
457                 IPath path= newOutputFolder;
458                 while (!validator.validate(path)) {
459                     path= new Path(newOutputFolder.toString() + i);
460                     i++;
461                 }
462                 return path;
463             }
464         };
465     }
466
467     /**
468      * A default query for inclusion and exclusion filters.
469      * The query is used to get information about the
470      * inclusion and exclusion filters of an element.
471      *
472      * @param shell shell if there is any or <code>null</code>
473      * @return an <code>IInclusionExclusionQuery</code> that can be executed
474      *
475      * @see ClasspathModifierQueries.IInclusionExclusionQuery
476      */

477     public static IInclusionExclusionQuery getDefaultInclusionExclusionQuery(final Shell shell) {
478         return new IInclusionExclusionQuery() {
479             
480             protected IPath[] fInclusionPattern;
481             protected IPath[] fExclusionPattern;
482             
483             public boolean doQuery(final CPListElement element, final boolean focusOnExcluded) {
484                 final boolean[] result= { false };
485                 Display.getDefault().syncExec(new Runnable JavaDoc() {
486                     public void run() {
487                         Shell sh= shell != null ? shell : JavaPlugin.getActiveWorkbenchShell();
488                         ExclusionInclusionDialog dialog= new ExclusionInclusionDialog(sh, element, focusOnExcluded);
489                         result[0]= dialog.open() == Window.OK;
490                         fInclusionPattern= dialog.getInclusionPattern();
491                         fExclusionPattern= dialog.getExclusionPattern();
492                     }
493                 });
494                 return result[0];
495             }
496             
497             public IPath[] getInclusionPattern() {
498                 return fInclusionPattern;
499             }
500             
501             public IPath[] getExclusionPattern() {
502                 return fExclusionPattern;
503             }
504         };
505     }
506
507     /**
508      * Query to create a linked source folder.
509      *
510      * The default query shows a dialog which allows
511      * the user to specify the new folder that should
512      * be created.
513      *
514      * @param shell shell if there is any or <code>null</code>
515      * @param project the Java project to create the linked source folder for
516      * @return an <code>ILinkToQuery</code> showing a dialog
517      * to create a linked source folder.
518      *
519      * @see ClasspathModifierQueries.ICreateFolderQuery
520      * @see LinkFolderDialog
521      */

522     public static ILinkToQuery getDefaultLinkQuery(final Shell shell, final IJavaProject project, final IPath desiredOutputLocation) {
523         return new ILinkToQuery() {
524             protected IFolder fFolder;
525             
526             public boolean doQuery() {
527                 final boolean[] isOK= {false};
528                 Display.getDefault().syncExec(new Runnable JavaDoc() {
529                     public void run() {
530                         Shell sh= shell != null ? shell : JavaPlugin.getActiveWorkbenchShell();
531
532                         LinkFolderDialog dialog= new LinkFolderDialog(sh, project.getProject());
533                         isOK[0]= dialog.open() == Window.OK;
534                         if (isOK[0])
535                             fFolder= dialog.getCreatedFolder();
536                     }
537                 });
538                 return isOK[0];
539             }
540
541             public IFolder getCreatedFolder() {
542                 return fFolder;
543             }
544
545             public OutputFolderQuery getOutputFolderQuery() {
546                 return getDefaultFolderQuery(shell, desiredOutputLocation);
547             }
548             
549         };
550     }
551     
552     /**
553      * Shows the UI to select new external JAR or ZIP archive entries. If the query
554      * was aborted, the result is an empty array.
555      *
556      * @param shell The parent shell for the dialog, can be <code>null</code>
557      * @return an <code>IAddArchivesQuery</code> showing a dialog to selected archive files
558      * to be added to the buildpath
559      *
560      * @see IAddArchivesQuery
561      */

562     public static IAddArchivesQuery getDefaultArchivesQuery(final Shell shell) {
563         return new IAddArchivesQuery() {
564
565             public IPath[] doQuery() {
566                 final IPath[][] selected= {null};
567                 Display.getDefault().syncExec(new Runnable JavaDoc() {
568                     public void run() {
569                         Shell sh= shell != null ? shell : JavaPlugin.getActiveWorkbenchShell();
570                         selected[0]= BuildPathDialogAccess.chooseExternalJAREntries(sh);
571                     }
572                 });
573                 if(selected[0] == null)
574                     return new IPath[0];
575                 return selected[0];
576             }
577         };
578     }
579
580     /**
581      * Shows the UI to prompt whether a linked folder which has been removed from the build path should be deleted as well.
582      *
583      * @param shell The parent shell for the dialog, can be <code>null</code>
584      * @return an <code>IRemoveLinkedFolderQuery</code> showing a dialog to prompt whether the linked folder should be deleted as well
585      *
586      * @see IRemoveLinkedFolderQuery
587      */

588     public static IRemoveLinkedFolderQuery getDefaultRemoveLinkedFolderQuery(final Shell shell) {
589         return new IRemoveLinkedFolderQuery() {
590
591             public final int doQuery(final IFolder folder) {
592                 final int[] result= { IRemoveLinkedFolderQuery.REMOVE_BUILD_PATH};
593                 Display.getDefault().syncExec(new Runnable JavaDoc() {
594
595                     public final void run() {
596                         final RemoveLinkedFolderDialog dialog= new RemoveLinkedFolderDialog((shell != null ? shell : JavaPlugin.getActiveWorkbenchShell()), folder);
597                         final int status= dialog.open();
598                         if (status == 0)
599                             result[0]= dialog.getRemoveStatus();
600                         else
601                             result[0]= IRemoveLinkedFolderQuery.REMOVE_CANCEL;
602                     }
603                 });
604                 return result[0];
605             }
606         };
607     }
608
609     /**
610      * Shows the UI to choose new classpath container classpath entries. See {@link IClasspathEntry#CPE_CONTAINER} for
611      * details about container classpath entries.
612      * The query returns the selected classpath entries or an empty array if the query has
613      * been cancelled.
614      *
615      * @param shell The parent shell for the dialog, can be <code>null</code>
616      * @return Returns the selected classpath container entries or an empty array if the query has
617      * been cancelled by the user.
618      */

619     public static IAddLibrariesQuery getDefaultLibrariesQuery(final Shell shell) {
620         return new IAddLibrariesQuery() {
621
622             public IClasspathEntry[] doQuery(final IJavaProject project, final IClasspathEntry[] entries) {
623                 final IClasspathEntry[][] selected= {null};
624                 Display.getDefault().syncExec(new Runnable JavaDoc() {
625                     public void run() {
626                         Shell sh= shell != null ? shell : JavaPlugin.getActiveWorkbenchShell();
627                         selected[0]= BuildPathDialogAccess.chooseContainerEntries(sh, project, entries);
628                     }
629                 });
630                 if(selected[0] == null)
631                     return new IClasspathEntry[0];
632                 return selected[0];
633             }
634         };
635     }
636
637     /**
638      * Shows the UI to create a new source folder.
639      *
640      * @param shell The parent shell for the dialog, can be <code>null</code>
641      * @param project the Java project to create the source folder for
642      * @return returns the query
643      */

644     public static ICreateFolderQuery getDefaultCreateFolderQuery(final Shell shell, final IJavaProject project) {
645         return new ICreateFolderQuery() {
646
647             private IFolder fNewFolder;
648
649             public boolean doQuery() {
650                 final boolean[] isOK= {false};
651                 Display.getDefault().syncExec(new Runnable JavaDoc() {
652                     public void run() {
653                         Shell sh= shell != null ? shell : JavaPlugin.getActiveWorkbenchShell();
654                         
655                         NewFolderDialog dialog= new NewFolderDialog(sh, project.getProject());
656                         isOK[0]= dialog.open() == Window.OK;
657                         if (isOK[0]) {
658                             IResource sourceContainer= (IResource) dialog.getResult()[0];
659                             if (sourceContainer instanceof IFolder) {
660                                 fNewFolder= (IFolder)sourceContainer;
661                             } else {
662                                 fNewFolder= null;
663                             }
664                         }
665                     }
666                 });
667                 return isOK[0];
668             }
669
670
671             public boolean isSourceFolder() {
672                 return true;
673             }
674
675             public IFolder getCreatedFolder() {
676                 return fNewFolder;
677             }
678             
679         };
680     }
681 }
682
Popular Tags