KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21
22 import org.eclipse.core.resources.IContainer;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.jface.operation.IRunnableContext;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30
31 import org.eclipse.ui.IWorkbenchSite;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.part.ISetSelectionTarget;
34
35 import org.eclipse.jdt.core.IJavaProject;
36 import org.eclipse.jdt.core.IPackageFragmentRoot;
37 import org.eclipse.jdt.core.JavaCore;
38 import org.eclipse.jdt.core.JavaModelException;
39
40 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
41 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
42 import org.eclipse.jdt.internal.corext.util.Messages;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.JavaPluginImages;
46 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
47 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
48
49 //SelectedElements iff enabled: IResource
50
public class IncludeToBuildpathAction extends BuildpathModifierAction {
51
52     private final IRunnableContext fContext;
53
54     public IncludeToBuildpathAction(IWorkbenchSite site) {
55         this(site, null, PlatformUI.getWorkbench().getProgressService());
56     }
57     
58     public IncludeToBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
59         this(null, selectionTarget, context);
60     }
61     
62     private IncludeToBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
63         super(site, selectionTarget, BuildpathModifierAction.INCLUDE);
64         
65         fContext= context;
66         
67         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Unexclude_label);
68         setImageDescriptor(JavaPluginImages.DESC_ELCL_INCLUDE_ON_BUILDPATH);
69         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Unexclude_tooltip);
70         setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_INCLUDE_ON_BUILDPATH);
71     }
72
73     /**
74      * {@inheritDoc}
75      */

76     public String JavaDoc getDetailedDescription() {
77         if (!isEnabled())
78             return null;
79         
80         if (getSelectedElements().size() != 1)
81             return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Unexclude;
82         
83         IResource resource= (IResource) getSelectedElements().get(0);
84         String JavaDoc name= ClasspathModifier.escapeSpecialChars(resource.getName());
85         
86         if (resource instanceof IContainer) {
87             return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_UnexcludeFolder, name);
88         } else if (resource instanceof IFile) {
89             return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_UnexcludeFile, name);
90         }
91         
92         return null;
93     }
94     
95     /**
96      * {@inheritDoc}
97      */

98     public void run() {
99         IResource resource= (IResource)getSelectedElements().get(0);
100         final IJavaProject project= JavaCore.create(resource.getProject());
101
102         try {
103             final IRunnableWithProgress runnable= new IRunnableWithProgress() {
104                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
105                     try {
106                         List JavaDoc result= unExclude(getSelectedElements(), project, monitor);
107                         selectAndReveal(new StructuredSelection(result));
108                     } catch (CoreException e) {
109                         throw new InvocationTargetException JavaDoc(e);
110                     }
111                 }
112             };
113             fContext.run(false, false, runnable);
114         } catch (final InvocationTargetException JavaDoc e) {
115             if (e.getCause() instanceof CoreException) {
116                 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.IncludeToBuildpathAction_ErrorTitle);
117             } else {
118                 JavaPlugin.log(e);
119             }
120         } catch (final InterruptedException JavaDoc e) {
121         }
122     }
123
124     protected List JavaDoc unExclude(List JavaDoc elements, IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
125         if (monitor == null)
126             monitor= new NullProgressMonitor();
127         try {
128             monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Including, 2 * elements.size());
129
130             List JavaDoc entries= ClasspathModifier.getExistingEntries(project);
131             for (int i= 0; i < elements.size(); i++) {
132                 IResource resource= (IResource) elements.get(i);
133                 IPackageFragmentRoot root= ClasspathModifier.getFragmentRoot(resource, project, new SubProgressMonitor(monitor, 1));
134                 if (root != null) {
135                     CPListElement entry= ClasspathModifier.getClasspathEntry(entries, root);
136                     ClasspathModifier.unExclude(resource, entry, project, new SubProgressMonitor(monitor, 1));
137                 }
138             }
139
140             ClasspathModifier.commitClassPath(entries, project, new SubProgressMonitor(monitor, 4));
141             
142             BuildpathDelta delta= new BuildpathDelta(getToolTipText());
143             delta.setNewEntries((CPListElement[])entries.toArray(new CPListElement[entries.size()]));
144             informListeners(delta);
145             
146             List JavaDoc resultElements= ClasspathModifier.getCorrespondingElements(elements, project);
147             return resultElements;
148         } finally {
149             monitor.done();
150         }
151     }
152
153     protected boolean canHandle(IStructuredSelection elements) {
154         if (elements.size() == 0)
155             return false;
156
157         try {
158             for (Iterator JavaDoc iter= elements.iterator(); iter.hasNext();) {
159                 Object JavaDoc element= iter.next();
160                 if (element instanceof IResource) {
161                     IResource resource= (IResource)element;
162                     IJavaProject project= JavaCore.create(resource.getProject());
163                     if (project == null || !project.exists())
164                         return false;
165
166                     if (!ClasspathModifier.isExcluded(resource, project))
167                         return false;
168                 } else {
169                     return false;
170                 }
171             }
172             return true;
173         } catch (CoreException e) {
174         }
175         return false;
176     }
177 }
178
Popular Tags