KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchPart;
33 import org.eclipse.ui.IWorkbenchPartReference;
34 import org.eclipse.ui.IWorkbenchSite;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.part.ISetSelectionTarget;
37 import org.eclipse.ui.texteditor.IUpdate;
38
39 import org.eclipse.jdt.core.IJavaProject;
40 import org.eclipse.jdt.core.JavaModelException;
41
42 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
43 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifierOperation;
44 import org.eclipse.jdt.internal.corext.buildpath.EditOutputFolderOperation;
45 import org.eclipse.jdt.internal.corext.buildpath.IClasspathInformationProvider;
46 import org.eclipse.jdt.internal.corext.util.Messages;
47
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
51 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IAddArchivesQuery;
52 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IAddLibrariesQuery;
53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IFolderCreationQuery;
54 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IInclusionExclusionQuery;
55 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.ILinkToQuery;
56 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IOutputLocationQuery;
57 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IRemoveLinkedFolderQuery;
58 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderQuery;
59
60 /**
61  * Buildpath action class implementing the <code>IClasspathInformationProvider</code> interface to pass
62  * information to the <code>ClasspathModifierOperation</code> that is passed as parameter in
63  * the contructor.
64  */

65 public class BuildPathAction extends Action implements IClasspathInformationProvider, IUpdate {
66     private ClasspathModifierOperation fOperation;
67     private IJavaProject fJavaProject;
68     private final BuildActionSelectionContext fContext;
69     private final IWorkbenchSite fSite;
70     private IStructuredSelection fCurrentSelection;
71
72     /**
73      * Creates an <code>AbstractModifierAction</code>.
74      *
75      * @param site the site providing context information for this action
76      */

77     public BuildPathAction(IWorkbenchSite site, BuildActionSelectionContext context) {
78         super();
79         fSite= site;
80         fContext= context;
81         fCurrentSelection= StructuredSelection.EMPTY;
82     }
83     
84     private Shell getShell() {
85         return fSite.getShell();
86     }
87
88     /*
89      * The reason why the operation is not passed as parameter in the
90      * constructor is this: the operation needs an information provider
91      * as parameter in its contructor. In this case, the BuildPathAction
92      * implements the provider for the operation. Therefore, it is not
93      * possible to pass the operation to the action before the action itself
94      * was created.
95      */

96     /**
97      * Set the operation for this action. This method has to be called right
98      * after having initialized this class.
99      *
100      * @param operation the operation to be used in this action
101      * @param imageDescriptor the image descriptor for the icon
102      * @param disabledImageDescriptor the image descriptor for the disabled icon
103      * @param text the text to be set as label for the action
104      * @param tooltip the text to be set as tool tip
105      */

106     public void initialize(ClasspathModifierOperation operation, ImageDescriptor imageDescriptor, ImageDescriptor disabledImageDescriptor, String JavaDoc text, String JavaDoc tooltip) {
107         fOperation= operation;
108         setImageDescriptor(imageDescriptor);
109         setDisabledImageDescriptor(disabledImageDescriptor);
110         setText(text);
111         setToolTipText(tooltip);
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run()
116      */

117     public void run() {
118         try {
119             IRunnableWithProgress runnable= new IRunnableWithProgress() {
120                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
121                     fOperation.run(monitor);
122                 }
123             };
124             PlatformUI.getWorkbench().getProgressService().run(true, false, runnable);
125         } catch (InvocationTargetException JavaDoc e) {
126             ExceptionHandler.handle(e, getShell(), Messages.format(NewWizardMessages.HintTextGroup_Exception_Title,
127                     fOperation.getName()), e.getMessage());
128         } catch (InterruptedException JavaDoc e) {
129             // operation canceled
130
}
131     }
132
133     public void update() {
134         IStructuredSelection structSelection;
135         ISelection selection= fSite.getSelectionProvider().getSelection();
136         if (selection instanceof IStructuredSelection) {
137             structSelection= (IStructuredSelection) selection;
138         } else {
139             structSelection= StructuredSelection.EMPTY;
140         }
141         
142         fCurrentSelection= structSelection;
143         try {
144             fContext.init(structSelection);
145             
146             fJavaProject= fContext.getJavaProject();
147             setEnabled(fJavaProject != null && fOperation.isValid(fContext.getElements(), fContext.getTypes()));
148
149         } catch (JavaModelException e) {
150             JavaPlugin.log(e);
151             setEnabled(false);
152         }
153     }
154     
155     /* (non-Javadoc)
156      * @see org.eclipse.jdt.internal.corext.buildpath.IClasspathInformationProvider#getSelection()
157      */

158     public IStructuredSelection getSelection() {
159         return fCurrentSelection;
160     }
161
162     /**
163      * Default getter of the Java project.
164      *
165      * @return an Java project
166      */

167     public IJavaProject getJavaProject() {
168         return fJavaProject;
169     }
170
171     /**
172      * Return an <code>IOutputFolderQuery</code>.
173      *
174      * @see ClasspathModifierQueries#getDefaultFolderQuery(Shell, IPath)
175      * @see IClasspathInformationProvider#getOutputFolderQuery()
176      *
177      * @throws JavaModelException if the project output location can not be retrieved
178      */

179     public OutputFolderQuery getOutputFolderQuery() throws JavaModelException {
180         return ClasspathModifierQueries.getDefaultFolderQuery(getShell(), fJavaProject.getOutputLocation().makeRelative());
181     }
182
183     /**
184      * Return an <code>IInclusionExclusionQuery</code>.
185      *
186      * @see ClasspathModifierQueries#getDefaultInclusionExclusionQuery(Shell)
187      * @see IClasspathInformationProvider#getInclusionExclusionQuery()
188      */

189     public IInclusionExclusionQuery getInclusionExclusionQuery() {
190         return ClasspathModifierQueries.getDefaultInclusionExclusionQuery(getShell());
191     }
192
193     /**
194      * Return an <code>IOutputLocationQuery</code>.
195      * @throws JavaModelException
196      *
197      * @see ClasspathModifierQueries#getDefaultOutputLocationQuery(Shell, IPath, List)
198      * @see IClasspathInformationProvider#getOutputLocationQuery()
199      */

200     public IOutputLocationQuery getOutputLocationQuery() throws JavaModelException {
201         List JavaDoc classpathEntries= ClasspathModifier.getExistingEntries(fJavaProject);
202         return ClasspathModifierQueries.getDefaultOutputLocationQuery(getShell(), fJavaProject.getOutputLocation().makeRelative(), classpathEntries);
203     }
204
205     /**
206      * Return an <code>IFolderCreationQuery</code>.
207      *
208      * @see ClasspathModifierQueries#getDefaultFolderCreationQuery(Shell, Object)
209      * @see IClasspathInformationProvider#getFolderCreationQuery()
210      */

211     public IFolderCreationQuery getFolderCreationQuery() {
212         IStructuredSelection selection= getSelection();
213         return ClasspathModifierQueries.getDefaultFolderCreationQuery(getShell(), selection.getFirstElement());
214     }
215
216     /**
217      * Get a query to create a linked source folder.
218      *
219      * @see ILinkToQuery
220      * @see org.eclipse.jdt.internal.corext.buildpath.IClasspathInformationProvider
221      */

222     public ILinkToQuery getLinkFolderQuery() throws JavaModelException {
223         return ClasspathModifierQueries.getDefaultLinkQuery(getShell(), fJavaProject, fJavaProject.getOutputLocation().makeRelative());
224     }
225
226     /**
227      * Get a query to create a linked source folder.
228      *
229      * @see IRemoveLinkedFolderQuery
230      * @see org.eclipse.jdt.internal.corext.buildpath.IClasspathInformationProvider
231      */

232     public IRemoveLinkedFolderQuery getRemoveLinkedFolderQuery() throws JavaModelException {
233         return ClasspathModifierQueries.getDefaultRemoveLinkedFolderQuery(getShell());
234     }
235
236     /**
237      * Return an <code>IAddArchivesQuery</code>.
238      *
239      * @see ClasspathModifierQueries#getDefaultArchivesQuery(Shell)
240      * @see IClasspathInformationProvider#getExternalArchivesQuery()
241      */

242     public IAddArchivesQuery getExternalArchivesQuery() throws JavaModelException {
243         return ClasspathModifierQueries.getDefaultArchivesQuery(getShell());
244     }
245
246     /**
247      * Return an <code>IAddLibrariesQuery</code>.
248      *
249      * @see ClasspathModifierQueries#getDefaultLibrariesQuery(Shell)
250      * @see IClasspathInformationProvider#getLibrariesQuery()
251      */

252     public IAddLibrariesQuery getLibrariesQuery() throws JavaModelException {
253         return ClasspathModifierQueries.getDefaultLibrariesQuery(getShell());
254     }
255
256     /**
257      * Does not do anything. Needs to be implemented if
258      * resources should be deleted.
259      *
260      * @see IClasspathInformationProvider#deleteCreatedResources()
261      */

262     public void deleteCreatedResources() {
263         // nothing to do
264
}
265
266     /**
267      * Handle the result by selecting the result elements or handle the exception if
268      * the exception was not <code>null</code>.
269      */

270     public void handleResult(List JavaDoc resultElements, CoreException exception, int operationType) {
271         if (exception != null) {
272             ExceptionHandler.handle(exception, getShell(), Messages.format(NewWizardMessages.HintTextGroup_Exception_Title,
273                     fOperation.getName()), exception.getMessage());
274             return;
275         }
276         if (resultElements.size() == 0)
277             return; // nothing changed
278

279         IStructuredSelection selection;
280         if (fOperation instanceof EditOutputFolderOperation)
281             selection= (StructuredSelection) getSelection();
282         else
283             selection= new StructuredSelection(resultElements);
284         selectAndReveal(selection);
285     }
286
287     /**
288      * For a given selection, try to select and reveal this selection for
289      * all parts of this site.
290      *
291      * @param selection the elements to be selected
292      */

293     private void selectAndReveal(final ISelection selection) {
294         // validate the input
295
IWorkbenchPage page= fSite.getPage();
296         if (page == null)
297             return;
298
299         // get all the view and editor parts
300
List JavaDoc parts= new ArrayList JavaDoc();
301         IWorkbenchPartReference refs[]= page.getViewReferences();
302         for (int i= 0; i < refs.length; i++) {
303             IWorkbenchPart part= refs[i].getPart(false);
304             if (part != null)
305                 parts.add(part);
306         }
307         refs= page.getEditorReferences();
308         for (int i= 0; i < refs.length; i++) {
309             if (refs[i].getPart(false) != null)
310                 parts.add(refs[i].getPart(false));
311         }
312
313         Iterator JavaDoc itr= parts.iterator();
314         while (itr.hasNext()) {
315             IWorkbenchPart part= (IWorkbenchPart) itr.next();
316
317             // get the part's ISetSelectionTarget implementation
318
ISetSelectionTarget target= null;
319             if (part instanceof ISetSelectionTarget)
320                 target= (ISetSelectionTarget) part;
321             else
322                 target= (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class);
323
324             if (target != null) {
325                 // select and reveal resource
326
final ISetSelectionTarget finalTarget= target;
327                 page.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
328                     public void run() {
329                         finalTarget.selectReveal(selection);
330                     }
331                 });
332             }
333         }
334     }
335
336
337 }
338
Popular Tags