-
Notifications
You must be signed in to change notification settings - Fork 86
/
EditOutputFolderAction.java
242 lines (197 loc) · 8.92 KB
/
EditOutputFolderAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
* Copyright (c) 2000, 2016 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ISetSelectionTarget;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject;
import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.util.Progress;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.OutputLocationDialog;
//SelectedElements iff enabled: (IPackageFragmentRoot || IJavaProject || CPListElementAttribute) && size == 1
public class EditOutputFolderAction extends BuildpathModifierAction {
private final IRunnableContext fContext;
private boolean fShowOutputFolders;
public EditOutputFolderAction(final IWorkbenchSite site) {
this(site, null, PlatformUI.getWorkbench().getProgressService());
fShowOutputFolders= true;
}
public EditOutputFolderAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
this(null, selectionTarget, context);
}
private EditOutputFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
super(site, selectionTarget, BuildpathModifierAction.EDIT_OUTPUT);
fContext= context;
fShowOutputFolders= false;
setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_EditOutput_label);
setImageDescriptor(JavaPluginImages.DESC_ELCL_CONFIGURE_OUTPUT_FOLDER);
setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_EditOutput_tooltip);
setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CONFIGURE_OUTPUT_FOLDER);
}
@Override
public String getDetailedDescription() {
return NewWizardMessages.PackageExplorerActionGroup_FormText_EditOutputFolder;
}
public void showOutputFolders(boolean showOutputFolders) {
fShowOutputFolders= showOutputFolders;
}
@Override
public void run() {
try {
final Shell shell= getShell();
final IJavaProject javaProject;
CPListElement cpElement= null;
Object firstElement= getSelectedElements().get(0);
if (firstElement instanceof IJavaProject) {
javaProject= (IJavaProject)firstElement;
final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(javaProject.getPath(), javaProject, IClasspathEntry.CPE_SOURCE);
cpElement= CPListElement.createFromExisting(entry, javaProject);
} else if (firstElement instanceof IPackageFragmentRoot) {
IPackageFragmentRoot root= (IPackageFragmentRoot)firstElement;
javaProject= root.getJavaProject();
final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(root.getPath(), javaProject, IClasspathEntry.CPE_SOURCE);
cpElement= CPListElement.createFromExisting(entry, javaProject);
} else if (firstElement instanceof CPListElementAttribute) {
CPListElementAttribute attribute= (CPListElementAttribute)firstElement;
cpElement= attribute.getParent();
javaProject= cpElement.getJavaProject();
} else {
return;
}
final List<CPListElement> classpathEntries= ClasspathModifier.getExistingEntries(javaProject);
final CPListElement element= ClasspathModifier.getClasspathEntry(classpathEntries, cpElement);
final OutputLocationDialog dialog= new OutputLocationDialog(shell, element, classpathEntries, javaProject.getOutputLocation(), false);
if (dialog.open() != Window.OK)
return;
final CPJavaProject cpProject= CPJavaProject.createFromExisting(javaProject);
final BuildpathDelta delta= ClasspathModifier.setOutputLocation(cpProject.getCPElement(element), dialog.getOutputLocation(), false, cpProject);
IFolder oldOutputFolder= getOldOutputFolder(delta);
final IFolder folderToDelete;
if (oldOutputFolder != null) {
String message= Messages.format(NewWizardMessages.EditOutputFolderAction_DeleteOldOutputFolderQuestion, BasicElementLabels.getPathLabel(oldOutputFolder.getFullPath(), false));
if (MessageDialog.openQuestion(getShell(), NewWizardMessages.OutputLocationDialog_title, message)) {
folderToDelete= oldOutputFolder;
} else {
folderToDelete= null;
}
} else {
folderToDelete= null;
}
try {
final IRunnableWithProgress runnable= monitor -> {
try {
monitor.beginTask(NewWizardMessages.EditOutputFolderAction_ProgressMonitorDescription, 50 + (folderToDelete == null?0:10));
ClasspathModifier.commitClassPath(cpProject, Progress.subMonitor(monitor, 50));
if (folderToDelete != null)
folderToDelete.delete(true, Progress.subMonitor(monitor, 10));
informListeners(delta);
selectAndReveal(new StructuredSelection(JavaCore.create(element.getResource())));
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
};
fContext.run(false, false, runnable);
} catch (final InvocationTargetException e) {
JavaPlugin.log(e);
} catch (final InterruptedException e) {
}
} catch (final CoreException e) {
showExceptionDialog(e, NewWizardMessages.EditOutputFolderAction_ErrorDescription);
}
}
private IFolder getOldOutputFolder(final BuildpathDelta delta) {
List<IResource> existingFolders= new ArrayList<>();
for (IResource deletedResource : delta.getDeletedResources()) {
if (deletedResource instanceof IFolder && deletedResource.exists()) {
existingFolders.add(deletedResource);
}
}
if (existingFolders.size() > 0) {
if (existingFolders.size() > 1) {
StringBuilder message= new StringBuilder("Found more then one existing folders:"); //$NON-NLS-1$
for (IResource iResource : existingFolders) {
IFolder folder= (IFolder)iResource;
message.append("\n").append(folder.toString()); //$NON-NLS-1$
}
Assert.isTrue(false, message.toString());
}
return (IFolder)existingFolders.get(0);
}
return null;
}
@Override
protected boolean canHandle(final IStructuredSelection elements) {
if (!fShowOutputFolders)
return false;
if (elements.size() != 1)
return false;
final Object element= elements.getFirstElement();
try {
if (element instanceof IPackageFragmentRoot) {
final IPackageFragmentRoot root= (IPackageFragmentRoot)element;
if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
return false;
IJavaProject javaProject= root.getJavaProject();
if (javaProject == null)
return false;
final IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(root.getPath(), javaProject, IClasspathEntry.CPE_SOURCE);
if (entry == null)
return false;
return true;
} else if (element instanceof IJavaProject) {
IJavaProject project= (IJavaProject)element;
if (!(ClasspathModifier.isSourceFolder(project)))
return false;
return true;
} else if (element instanceof CPListElementAttribute) {
CPListElementAttribute attribute= (CPListElementAttribute)element;
if (!CPListElement.OUTPUT.equals(attribute.getKey()))
return false;
return true;
}
} catch (final JavaModelException e) {
return false;
}
return false;
}
}