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