]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.ArrayList; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.core.runtime.CoreException; | |
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.core.runtime.NullProgressMonitor; | |
21 | import org.eclipse.core.runtime.SubProgressMonitor; | |
22 | ||
23 | import org.eclipse.core.resources.IResource; | |
24 | ||
25 | import org.eclipse.jface.operation.IRunnableContext; | |
26 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
27 | import org.eclipse.jface.viewers.IStructuredSelection; | |
28 | import org.eclipse.jface.viewers.StructuredSelection; | |
29 | ||
30 | import org.eclipse.ui.IWorkbenchSite; | |
31 | import org.eclipse.ui.PlatformUI; | |
32 | import org.eclipse.ui.part.ISetSelectionTarget; | |
33 | ||
34 | import org.eclipse.jdt.core.ICompilationUnit; | |
35 | import org.eclipse.jdt.core.IJavaElement; | |
36 | import org.eclipse.jdt.core.IJavaProject; | |
37 | import org.eclipse.jdt.core.IPackageFragment; | |
38 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
39 | import org.eclipse.jdt.core.JavaModelException; | |
40 | ||
41 | import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; | |
42 | import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; | |
43 | import org.eclipse.jdt.internal.corext.util.Messages; | |
44 | ||
45 | import org.eclipse.jdt.ui.JavaElementLabels; | |
46 | ||
47 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
48 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
49 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
50 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; | |
51 | ||
52 | //SelectedElements iff enabled: IPackageFragment || ICompilationUnit | |
53 | public class ExcludeFromBuildpathAction extends BuildpathModifierAction { | |
54 | ||
55 | private final IRunnableContext fContext; | |
56 | ||
57 | public ExcludeFromBuildpathAction(IWorkbenchSite site) { | |
58 | this(site, null, PlatformUI.getWorkbench().getProgressService()); | |
59 | } | |
60 | ||
61 | public ExcludeFromBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { | |
62 | this(null, selectionTarget, context); | |
63 | } | |
64 | ||
65 | private ExcludeFromBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { | |
66 | super(site, selectionTarget, BuildpathModifierAction.EXCLUDE); | |
67 | ||
68 | fContext= context; | |
69 | ||
70 | setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Exclude_label); | |
71 | setImageDescriptor(JavaPluginImages.DESC_ELCL_EXCLUDE_FROM_BUILDPATH); | |
72 | setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Exclude_tooltip); | |
73 | setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_EXCLUDE_FROM_BUILDPATH); | |
74 | } | |
75 | ||
76 | /** | |
77 | * {@inheritDoc} | |
78 | */ | |
79 | @Override | |
80 | public String getDetailedDescription() { | |
81 | if (!isEnabled()) | |
82 | return null; | |
83 | ||
84 | if (getSelectedElements().size() != 1) | |
85 | return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_Exclude; | |
86 | ||
87 | IJavaElement elem= (IJavaElement) getSelectedElements().get(0); | |
88 | String name= ClasspathModifier.escapeSpecialChars(JavaElementLabels.getElementLabel(elem, JavaElementLabels.ALL_DEFAULT)); | |
89 | if (elem instanceof IPackageFragment) { | |
90 | return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ExcludePackage, name); | |
91 | } else if (elem instanceof ICompilationUnit) { | |
92 | return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ExcludeFile, name); | |
93 | } | |
94 | ||
95 | return null; | |
96 | } | |
97 | ||
98 | /** | |
99 | * {@inheritDoc} | |
100 | */ | |
101 | @Override | |
102 | public void run() { | |
103 | final IJavaProject project; | |
104 | Object object= getSelectedElements().get(0); | |
105 | if (object instanceof ICompilationUnit) { | |
106 | project= ((ICompilationUnit)object).getJavaProject(); | |
107 | } else { | |
108 | project= ((IPackageFragment)object).getJavaProject(); | |
109 | } | |
110 | ||
111 | try { | |
112 | final IRunnableWithProgress runnable= new IRunnableWithProgress() { | |
113 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
114 | try { | |
115 | List<IResource> result= exclude(getSelectedElements(), project, monitor); | |
116 | selectAndReveal(new StructuredSelection(result)); | |
117 | } catch (CoreException e) { | |
118 | throw new InvocationTargetException(e); | |
119 | } | |
120 | } | |
121 | }; | |
122 | fContext.run(false, false, runnable); | |
123 | } catch (final InvocationTargetException e) { | |
124 | if (e.getCause() instanceof CoreException) { | |
125 | showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.ExcludeFromBuildathAction_ErrorTitle); | |
126 | } else { | |
127 | JavaPlugin.log(e); | |
128 | } | |
129 | } catch (final InterruptedException e) { | |
130 | } | |
131 | } | |
132 | ||
133 | private List<IResource> exclude(List<?> javaElements, IJavaProject project, IProgressMonitor monitor) throws JavaModelException { | |
134 | if (monitor == null) | |
135 | monitor= new NullProgressMonitor(); | |
136 | try { | |
137 | monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_Excluding, javaElements.size() + 4); | |
138 | ||
139 | List<CPListElement> existingEntries= ClasspathModifier.getExistingEntries(project); | |
140 | List<IResource> resources= new ArrayList<IResource>(); | |
141 | for (int i= 0; i < javaElements.size(); i++) { | |
142 | IJavaElement javaElement= (IJavaElement) javaElements.get(i); | |
143 | IPackageFragmentRoot root= (IPackageFragmentRoot) javaElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); | |
144 | CPListElement entry= ClasspathModifier.getClasspathEntry(existingEntries, root); | |
145 | ||
146 | IResource resource= ClasspathModifier.exclude(javaElement, entry, project, new SubProgressMonitor(monitor, 1)); | |
147 | if (resource != null) { | |
148 | resources.add(resource); | |
149 | } | |
150 | } | |
151 | ||
152 | ClasspathModifier.commitClassPath(existingEntries, project, new SubProgressMonitor(monitor, 4)); | |
153 | ||
154 | BuildpathDelta delta= new BuildpathDelta(getToolTipText()); | |
155 | delta.setNewEntries(existingEntries.toArray(new CPListElement[existingEntries.size()])); | |
156 | informListeners(delta); | |
157 | ||
158 | return resources; | |
159 | } finally { | |
160 | monitor.done(); | |
161 | } | |
162 | } | |
163 | ||
164 | @Override | |
165 | protected boolean canHandle(IStructuredSelection elements) { | |
166 | if (elements.size() == 0) | |
167 | return false; | |
168 | ||
169 | for (Iterator<?> iter= elements.iterator(); iter.hasNext();) { | |
170 | Object element= iter.next(); | |
171 | if (element instanceof IPackageFragment) { | |
172 | IPackageFragment fragment= (IPackageFragment)element; | |
173 | if (ClasspathModifier.isDefaultFragment(fragment)) | |
174 | return false; | |
175 | if (ClasspathModifier.isInExternalOrArchive(fragment)) | |
176 | return false; | |
177 | } else if (element instanceof ICompilationUnit) { | |
178 | } else { | |
179 | return false; | |
180 | } | |
181 | } | |
182 | return true; | |
183 | } | |
184 | ||
185 | } |