]>
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.swt.widgets.Shell; | |
19 | ||
20 | import org.eclipse.core.runtime.Assert; | |
21 | import org.eclipse.core.runtime.CoreException; | |
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.SubProgressMonitor; | |
24 | ||
25 | import org.eclipse.core.resources.IFolder; | |
26 | import org.eclipse.core.resources.IResource; | |
27 | ||
28 | import org.eclipse.jface.dialogs.MessageDialog; | |
29 | import org.eclipse.jface.operation.IRunnableContext; | |
30 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
31 | import org.eclipse.jface.viewers.IStructuredSelection; | |
32 | import org.eclipse.jface.viewers.StructuredSelection; | |
33 | import org.eclipse.jface.window.Window; | |
34 | ||
35 | import org.eclipse.ui.IWorkbenchSite; | |
36 | import org.eclipse.ui.PlatformUI; | |
37 | import org.eclipse.ui.part.ISetSelectionTarget; | |
38 | ||
39 | import org.eclipse.jdt.core.IClasspathEntry; | |
40 | import org.eclipse.jdt.core.IJavaProject; | |
41 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
42 | import org.eclipse.jdt.core.JavaCore; | |
43 | import org.eclipse.jdt.core.JavaModelException; | |
44 | ||
45 | import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; | |
46 | import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject; | |
47 | import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; | |
48 | import org.eclipse.jdt.internal.corext.util.Messages; | |
49 | ||
50 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
51 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
52 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
53 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
54 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; | |
55 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute; | |
56 | import org.eclipse.jdt.internal.ui.wizards.buildpaths.OutputLocationDialog; | |
57 | ||
58 | //SelectedElements iff enabled: (IPackageFragmentRoot || IJavaProject || CPListElementAttribute) && size == 1 | |
59 | public class EditOutputFolderAction extends BuildpathModifierAction { | |
60 | ||
61 | private final IRunnableContext fContext; | |
62 | private boolean fShowOutputFolders; | |
63 | ||
64 | public EditOutputFolderAction(final IWorkbenchSite site) { | |
65 | this(site, null, PlatformUI.getWorkbench().getProgressService()); | |
66 | ||
67 | fShowOutputFolders= true; | |
68 | } | |
69 | ||
70 | public EditOutputFolderAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { | |
71 | this(null, selectionTarget, context); | |
72 | } | |
73 | ||
74 | private EditOutputFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { | |
75 | super(site, selectionTarget, BuildpathModifierAction.EDIT_OUTPUT); | |
76 | ||
77 | fContext= context; | |
78 | fShowOutputFolders= false; | |
79 | ||
80 | setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_EditOutput_label); | |
81 | setImageDescriptor(JavaPluginImages.DESC_ELCL_CONFIGURE_OUTPUT_FOLDER); | |
82 | setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_EditOutput_tooltip); | |
83 | setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CONFIGURE_OUTPUT_FOLDER); | |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritDoc} | |
88 | */ | |
89 | @Override | |
90 | public String getDetailedDescription() { | |
91 | return NewWizardMessages.PackageExplorerActionGroup_FormText_EditOutputFolder; | |
92 | } | |
93 | ||
94 | ||
95 | public void showOutputFolders(boolean showOutputFolders) { | |
96 | fShowOutputFolders= showOutputFolders; | |
97 | } | |
98 | ||
99 | /** | |
100 | * {@inheritDoc} | |
101 | */ | |
102 | @Override | |
103 | public void run() { | |
104 | try { | |
105 | ||
106 | final Shell shell= getShell(); | |
107 | ||
108 | final IJavaProject javaProject; | |
109 | CPListElement cpElement= null; | |
110 | Object firstElement= getSelectedElements().get(0); | |
111 | if (firstElement instanceof IJavaProject) { | |
112 | javaProject= (IJavaProject)firstElement; | |
113 | ||
114 | final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(javaProject.getPath(), javaProject, IClasspathEntry.CPE_SOURCE); | |
115 | cpElement= CPListElement.createFromExisting(entry, javaProject); | |
116 | } else if (firstElement instanceof IPackageFragmentRoot) { | |
117 | IPackageFragmentRoot root= (IPackageFragmentRoot)firstElement; | |
118 | ||
119 | javaProject= root.getJavaProject(); | |
120 | ||
121 | final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(root.getPath(), javaProject, IClasspathEntry.CPE_SOURCE); | |
122 | cpElement= CPListElement.createFromExisting(entry, javaProject); | |
123 | } else if (firstElement instanceof CPListElementAttribute) { | |
124 | CPListElementAttribute attribute= (CPListElementAttribute)firstElement; | |
125 | ||
126 | cpElement= attribute.getParent(); | |
127 | javaProject= cpElement.getJavaProject(); | |
128 | } else { | |
129 | return; | |
130 | } | |
131 | ||
132 | final List<CPListElement> classpathEntries= ClasspathModifier.getExistingEntries(javaProject); | |
133 | final CPListElement element= ClasspathModifier.getClasspathEntry(classpathEntries, cpElement); | |
134 | ||
135 | final OutputLocationDialog dialog= new OutputLocationDialog(shell, element, classpathEntries, javaProject.getOutputLocation(), false); | |
136 | if (dialog.open() != Window.OK) | |
137 | return; | |
138 | ||
139 | final CPJavaProject cpProject= CPJavaProject.createFromExisting(javaProject); | |
140 | final BuildpathDelta delta= cpProject.generated_2854853818809277972(element, dialog); | |
141 | ||
142 | IFolder oldOutputFolder= getOldOutputFolder(delta); | |
143 | final IFolder folderToDelete; | |
144 | if (oldOutputFolder != null) { | |
145 | String message= Messages.format(NewWizardMessages.EditOutputFolderAction_DeleteOldOutputFolderQuestion, BasicElementLabels.getPathLabel(oldOutputFolder.getFullPath(), false)); | |
146 | if (MessageDialog.openQuestion(getShell(), NewWizardMessages.OutputLocationDialog_title, message)) { | |
147 | folderToDelete= oldOutputFolder; | |
148 | } else { | |
149 | folderToDelete= null; | |
150 | } | |
151 | } else { | |
152 | folderToDelete= null; | |
153 | } | |
154 | ||
155 | try { | |
156 | final IRunnableWithProgress runnable= new IRunnableWithProgress() { | |
157 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
158 | try { | |
159 | monitor.beginTask(NewWizardMessages.EditOutputFolderAction_ProgressMonitorDescription, 50 + (folderToDelete == null?0:10)); | |
160 | ||
161 | ClasspathModifier.commitClassPath(cpProject, new SubProgressMonitor(monitor, 50)); | |
162 | if (folderToDelete != null) | |
163 | folderToDelete.delete(true, new SubProgressMonitor(monitor, 10)); | |
164 | ||
165 | informListeners(delta); | |
166 | selectAndReveal(new StructuredSelection(JavaCore.create(element.getResource()))); | |
167 | } catch (CoreException e) { | |
168 | throw new InvocationTargetException(e); | |
169 | } finally { | |
170 | monitor.done(); | |
171 | } | |
172 | } | |
173 | }; | |
174 | fContext.run(false, false, runnable); | |
175 | } catch (final InvocationTargetException e) { | |
176 | JavaPlugin.log(e); | |
177 | } catch (final InterruptedException e) { | |
178 | } | |
179 | ||
180 | } catch (final CoreException e) { | |
181 | showExceptionDialog(e, NewWizardMessages.EditOutputFolderAction_ErrorDescription); | |
182 | } | |
183 | } | |
184 | ||
185 | private IFolder getOldOutputFolder(final BuildpathDelta delta) { | |
186 | IResource[] deletedResources= delta.getDeletedResources(); | |
187 | List<IResource> existingFolders= new ArrayList<IResource>(); | |
188 | for (int i= 0; i < deletedResources.length; i++) { | |
189 | if (deletedResources[i] instanceof IFolder && deletedResources[i].exists()) { | |
190 | existingFolders.add(deletedResources[i]); | |
191 | } | |
192 | } | |
193 | if (existingFolders.size() > 0) { | |
194 | if (existingFolders.size() > 1) { | |
195 | String message= "Found more then one existing folders:"; //$NON-NLS-1$ | |
196 | for (Iterator<IResource> iterator= existingFolders.iterator(); iterator.hasNext();) { | |
197 | IFolder folder= (IFolder)iterator.next(); | |
198 | message+= "\n" + folder.toString(); //$NON-NLS-1$ | |
199 | } | |
200 | Assert.isTrue(false, message); | |
201 | } | |
202 | return (IFolder)existingFolders.get(0); | |
203 | } | |
204 | return null; | |
205 | } | |
206 | ||
207 | @Override | |
208 | protected boolean canHandle(final IStructuredSelection elements) { | |
209 | if (!fShowOutputFolders) | |
210 | return false; | |
211 | ||
212 | if (elements.size() != 1) | |
213 | return false; | |
214 | ||
215 | final Object element= elements.getFirstElement(); | |
216 | try { | |
217 | if (element instanceof IPackageFragmentRoot) { | |
218 | final IPackageFragmentRoot root= (IPackageFragmentRoot)element; | |
219 | if (root.getKind() != IPackageFragmentRoot.K_SOURCE) | |
220 | return false; | |
221 | ||
222 | IJavaProject javaProject= root.getJavaProject(); | |
223 | if (javaProject == null) | |
224 | return false; | |
225 | ||
226 | final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(root.getPath(), javaProject, IClasspathEntry.CPE_SOURCE); | |
227 | if (entry == null) | |
228 | return false; | |
229 | ||
230 | return true; | |
231 | } else if (element instanceof IJavaProject) { | |
232 | IJavaProject project= (IJavaProject)element; | |
233 | if (!(ClasspathModifier.isSourceFolder(project))) | |
234 | return false; | |
235 | ||
236 | return true; | |
237 | } else if (element instanceof CPListElementAttribute) { | |
238 | CPListElementAttribute attribute= (CPListElementAttribute)element; | |
239 | if (attribute.getKey() != CPListElement.OUTPUT) | |
240 | return false; | |
241 | ||
242 | return true; | |
243 | } | |
244 | ||
245 | } catch (final JavaModelException e) { | |
246 | return false; | |
247 | } | |
248 | return false; | |
249 | } | |
250 | } |