]>
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.Iterator; | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.core.runtime.CoreException; | |
18 | import org.eclipse.core.runtime.IPath; | |
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | ||
21 | import org.eclipse.core.resources.IResource; | |
22 | ||
23 | import org.eclipse.jface.operation.IRunnableContext; | |
24 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
25 | import org.eclipse.jface.viewers.IStructuredSelection; | |
26 | import org.eclipse.jface.viewers.StructuredSelection; | |
27 | ||
28 | import org.eclipse.ui.part.ISetSelectionTarget; | |
29 | ||
30 | import org.eclipse.jdt.core.ElementChangedEvent; | |
31 | import org.eclipse.jdt.core.IClasspathEntry; | |
32 | import org.eclipse.jdt.core.IElementChangedListener; | |
33 | import org.eclipse.jdt.core.IJavaProject; | |
34 | import org.eclipse.jdt.core.JavaCore; | |
35 | import org.eclipse.jdt.core.JavaModelException; | |
36 | ||
37 | import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; | |
38 | import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; | |
39 | ||
40 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
41 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
42 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
43 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; | |
44 | ||
45 | //TODO: Use global history | |
46 | public class ResetAllAction extends BuildpathModifierAction { | |
47 | ||
48 | private final HintTextGroup fProvider; | |
49 | private final IRunnableContext fContext; | |
50 | private IJavaProject fJavaProject; | |
51 | private List<CPListElement> fEntries; | |
52 | private IPath fOutputLocation; | |
53 | ||
54 | public ResetAllAction(HintTextGroup provider, IRunnableContext context, ISetSelectionTarget selectionTarget) { | |
55 | super(null, selectionTarget, BuildpathModifierAction.RESET_ALL); | |
56 | ||
57 | fProvider= provider; | |
58 | fContext= context; | |
59 | ||
60 | setImageDescriptor(JavaPluginImages.DESC_ELCL_CLEAR); | |
61 | setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CLEAR); | |
62 | setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_label); | |
63 | setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); | |
64 | setEnabled(false); | |
65 | } | |
66 | ||
67 | /** | |
68 | * {@inheritDoc} | |
69 | */ | |
70 | @Override | |
71 | public String getDetailedDescription() { | |
72 | return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_ResetAll; | |
73 | } | |
74 | ||
75 | public void setBreakPoint(IJavaProject javaProject) { | |
76 | fJavaProject= javaProject; | |
77 | if (fJavaProject.exists()) { | |
78 | try { | |
79 | fEntries= ClasspathModifier.getExistingEntries(fJavaProject); | |
80 | fOutputLocation= fJavaProject.getOutputLocation(); | |
81 | } catch (JavaModelException e) { | |
82 | JavaPlugin.log(e); | |
83 | return; | |
84 | } | |
85 | setEnabled(true); | |
86 | } else { | |
87 | JavaCore.addElementChangedListener(new IElementChangedListener() { | |
88 | ||
89 | public void elementChanged(ElementChangedEvent event) { | |
90 | if (fJavaProject.exists()) { | |
91 | try { | |
92 | fEntries= ClasspathModifier.getExistingEntries(fJavaProject); | |
93 | fOutputLocation= fJavaProject.getOutputLocation(); | |
94 | } catch (JavaModelException e) { | |
95 | JavaPlugin.log(e); | |
96 | return; | |
97 | } finally { | |
98 | JavaCore.removeElementChangedListener(this); | |
99 | } | |
100 | setEnabled(true); | |
101 | } | |
102 | } | |
103 | ||
104 | }, ElementChangedEvent.POST_CHANGE); | |
105 | } | |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritDoc} | |
110 | */ | |
111 | @Override | |
112 | public void run() { | |
113 | ||
114 | try { | |
115 | final IRunnableWithProgress runnable= new IRunnableWithProgress() { | |
116 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
117 | ||
118 | monitor.beginTask("", 3); //$NON-NLS-1$ | |
119 | try { | |
120 | if (!hasChange(fJavaProject)) | |
121 | return; | |
122 | ||
123 | BuildpathDelta delta= new BuildpathDelta(getToolTipText()); | |
124 | ||
125 | ClasspathModifier.commitClassPath(fEntries, fJavaProject, monitor); | |
126 | delta.setNewEntries(fEntries.toArray(new CPListElement[fEntries.size()])); | |
127 | ||
128 | fJavaProject.setOutputLocation(fOutputLocation, monitor); | |
129 | delta.setDefaultOutputLocation(fOutputLocation); | |
130 | ||
131 | for (Iterator<IResource> iterator= fProvider.getCreatedResources().iterator(); iterator.hasNext();) { | |
132 | IResource resource= iterator.next(); | |
133 | resource.delete(false, null); | |
134 | delta.addDeletedResource(resource); | |
135 | } | |
136 | ||
137 | fProvider.resetCreatedResources(); | |
138 | ||
139 | informListeners(delta); | |
140 | ||
141 | selectAndReveal(new StructuredSelection(fJavaProject)); | |
142 | } catch (JavaModelException e) { | |
143 | showExceptionDialog(e, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); | |
144 | } catch (CoreException e) { | |
145 | showExceptionDialog(e, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); | |
146 | } finally { | |
147 | monitor.done(); | |
148 | } | |
149 | } | |
150 | }; | |
151 | fContext.run(false, false, runnable); | |
152 | } catch (InvocationTargetException e) { | |
153 | if (e.getCause() instanceof CoreException) { | |
154 | showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ClearAll_tooltip); | |
155 | } else { | |
156 | JavaPlugin.log(e); | |
157 | } | |
158 | } catch (InterruptedException e) { | |
159 | } | |
160 | } | |
161 | ||
162 | ||
163 | /** | |
164 | * {@inheritDoc} | |
165 | */ | |
166 | @Override | |
167 | protected boolean canHandle(IStructuredSelection elements) { | |
168 | if (fJavaProject == null) | |
169 | return false; | |
170 | ||
171 | return true; | |
172 | } | |
173 | ||
174 | ||
175 | //TODO: Remove, action should be disabled if not hasChange | |
176 | private boolean hasChange(IJavaProject project) throws JavaModelException { | |
177 | if (!project.getOutputLocation().equals(fOutputLocation)) | |
178 | return true; | |
179 | ||
180 | IClasspathEntry[] currentEntries= project.getRawClasspath(); | |
181 | if (currentEntries.length != fEntries.size()) | |
182 | return true; | |
183 | ||
184 | int i= 0; | |
185 | for (Iterator<CPListElement> iterator= fEntries.iterator(); iterator.hasNext();) { | |
186 | CPListElement oldEntrie= iterator.next(); | |
187 | if (!oldEntrie.getClasspathEntry().equals(currentEntries[i])) | |
188 | return true; | |
189 | i++; | |
190 | } | |
191 | return false; | |
192 | } | |
193 | } |