-
Notifications
You must be signed in to change notification settings - Fork 86
/
AddFolderToBuildpathAction.java
303 lines (259 loc) · 11.6 KB
/
AddFolderToBuildpathAction.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************************
* 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.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.jface.dialogs.IDialogConstants;
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.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ISetSelectionTarget;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.JavaElementLabels;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.jdt.internal.ui.util.Progress;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderQuery;
//SelectedElements iff enabled: IJavaProject || IPackageFrament || IFolder
public class AddFolderToBuildpathAction extends BuildpathModifierAction {
private final IRunnableContext fContext;
public AddFolderToBuildpathAction(IWorkbenchSite site) {
this(site, null, PlatformUI.getWorkbench().getProgressService());
}
public AddFolderToBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
this(null, selectionTarget, context);
}
private AddFolderToBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
super(site, selectionTarget, BuildpathModifierAction.ADD_SEL_SF_TO_BP);
fContext= context;
setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelSFToCP_label);
setImageDescriptor(JavaPluginImages.DESC_ELCL_ADD_AS_SOURCE_FOLDER);
setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelSFToCP_tooltip);
}
@Override
public String getDetailedDescription() {
if (!isEnabled())
return null;
if (getSelectedElements().size() != 1)
return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath;
Object obj= getSelectedElements().get(0);
String elementLabel= JavaElementLabels.getTextLabel(obj, JavaElementLabels.ALL_DEFAULT);
if (obj instanceof IJavaProject) {
return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ProjectToBuildpath, elementLabel);
} else if (obj instanceof IPackageFragment) {
return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_PackageToBuildpath, elementLabel);
} else if (obj instanceof IResource) {
return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_FolderToBuildpath, elementLabel);
}
return null;
}
@Override
public void run() {
try {
final IJavaProject project;
Object object= getSelectedElements().get(0);
if (object instanceof IJavaProject) {
project= (IJavaProject)object;
} else if (object instanceof IPackageFragment) {
project= ((IPackageFragment)object).getJavaProject();
} else {
IFolder folder= (IFolder)object;
project= JavaCore.create(folder.getProject());
if (project == null)
return;
}
final Shell shell= getShell();
final boolean removeProjectFromClasspath;
IPath outputLocation= project.getOutputLocation();
final IPath defaultOutputLocation= outputLocation.makeRelative();
final IPath newDefaultOutputLocation;
final boolean removeOldClassFiles;
IPath projPath= project.getProject().getFullPath();
if (((getSelectedElements().size() != 1) || !(getSelectedElements().get(0) instanceof IJavaProject)) && //if only the project should be added, then the query does not need to be executed
(outputLocation.equals(projPath) || defaultOutputLocation.segmentCount() == 1)) {
final OutputFolderQuery outputFolderQuery= ClasspathModifierQueries.getDefaultFolderQuery(shell, defaultOutputLocation);
if (outputFolderQuery.doQuery(true, ClasspathModifier.getValidator(getSelectedElements(), project), project)) {
newDefaultOutputLocation= outputFolderQuery.getOutputLocation();
removeProjectFromClasspath= outputFolderQuery.removeProjectFromClasspath();
if (BuildPathsBlock.hasClassfiles(project.getProject()) && outputLocation.equals(projPath)) {
String title= NewWizardMessages.BuildPathsBlock_RemoveBinariesDialog_title;
String message= Messages.format(NewWizardMessages.BuildPathsBlock_RemoveBinariesDialog_description, BasicElementLabels.getPathLabel(projPath, false));
MessageDialog dialog= new MessageDialog(shell, title, null, message, MessageDialog.QUESTION, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 0);
int answer= dialog.open();
switch (answer) {
case 0:
removeOldClassFiles= true;
break;
case 1:
removeOldClassFiles= false;
break;
default:
return;
}
} else {
removeOldClassFiles= false;
}
} else {
return;
}
} else {
removeProjectFromClasspath= false;
removeOldClassFiles= false;
newDefaultOutputLocation= defaultOutputLocation;
}
try {
final IRunnableWithProgress runnable= monitor -> {
try {
List<IJavaElement> result= addToClasspath(getSelectedElements(), project, newDefaultOutputLocation.makeAbsolute(), removeProjectFromClasspath, removeOldClassFiles, monitor);
selectAndReveal(new StructuredSelection(result));
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
};
fContext.run(false, false, runnable);
} catch (final InvocationTargetException e) {
if (e.getCause() instanceof CoreException) {
showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.AddSourceFolderToBuildpathAction_ErrorTitle);
} else {
JavaPlugin.log(e);
}
} catch (final InterruptedException e) {
}
} catch (CoreException e) {
showExceptionDialog(e, NewWizardMessages.AddSourceFolderToBuildpathAction_ErrorTitle);
}
}
private List<IJavaElement> addToClasspath(List<?> elements, IJavaProject project, IPath outputLocation, boolean removeProjectFromClasspath, boolean removeOldClassFiles, IProgressMonitor monitor) throws OperationCanceledException, CoreException {
if (!project.getProject().hasNature(JavaCore.NATURE_ID)) {
StatusInfo rootStatus= new StatusInfo();
rootStatus.setError(NewWizardMessages.ClasspathModifier_Error_NoNatures);
throw new CoreException(rootStatus);
}
try {
monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_AddToBuildpath, elements.size() + 4);
IWorkspaceRoot workspaceRoot= JavaPlugin.getWorkspace().getRoot();
if (removeOldClassFiles) {
IResource res= workspaceRoot.findMember(project.getOutputLocation());
if (res instanceof IContainer && BuildPathsBlock.hasClassfiles(res)) {
BuildPathsBlock.removeOldClassfiles(res);
}
}
BuildpathDelta delta= new BuildpathDelta(getToolTipText());
if (!project.getOutputLocation().equals(outputLocation)) {
project.setOutputLocation(outputLocation, Progress.subMonitor(monitor, 1));
delta.setDefaultOutputLocation(outputLocation);
} else {
monitor.worked(1);
}
List<CPListElement> existingEntries= ClasspathModifier.getExistingEntries(project);
if (removeProjectFromClasspath) {
ClasspathModifier.removeFromClasspath(project, existingEntries, Progress.subMonitor(monitor, 1));
} else {
monitor.worked(1);
}
List<CPListElement> newEntries= new ArrayList<>();
for (Object element : elements) {
CPListElement entry;
if (element instanceof IResource)
entry= ClasspathModifier.addToClasspath((IResource) element, existingEntries, newEntries, project, Progress.subMonitor(monitor, 1));
else
entry= ClasspathModifier.addToClasspath((IJavaElement) element, existingEntries, newEntries, project, Progress.subMonitor(monitor, 1));
newEntries.add(entry);
}
Set<CPListElement> modifiedSourceEntries= new HashSet<>();
BuildPathBasePage.fixNestingConflicts(newEntries.toArray(new CPListElement[newEntries.size()]), existingEntries.toArray(new CPListElement[existingEntries.size()]), modifiedSourceEntries);
ClasspathModifier.setNewEntry(existingEntries, newEntries, project, Progress.subMonitor(monitor, 1));
ClasspathModifier.commitClassPath(existingEntries, project, Progress.subMonitor(monitor, 1));
delta.setNewEntries(existingEntries.toArray(new CPListElement[existingEntries.size()]));
informListeners(delta);
List<IJavaElement> result= new ArrayList<>();
for (CPListElement newEntrie : newEntries) {
IClasspathEntry entry= newEntrie.getClasspathEntry();
IJavaElement root;
if (entry.getPath().equals(project.getPath()))
root= project;
else
root= project.findPackageFragmentRoot(entry.getPath());
if (root != null) {
result.add(root);
}
}
return result;
} finally {
monitor.done();
}
}
@Override
protected boolean canHandle(IStructuredSelection elements) {
if (elements.size() == 0)
return false;
try {
for (Object element : elements) {
if (element instanceof IJavaProject) {
if (ClasspathModifier.isSourceFolder((IJavaProject)element))
return false;
} else if (element instanceof IPackageFragment) {
IPackageFragment fragment= (IPackageFragment)element;
if (ClasspathModifier.isDefaultFragment(fragment))
return false;
if (ClasspathModifier.isInExternalOrArchive(fragment))
return false;
IResource res;
if ((res= fragment.getResource()) != null && res.isVirtual())
return false;
} else if (element instanceof IFolder) {
IProject project= ((IFolder)element).getProject();
IJavaProject javaProject= JavaCore.create(project);
if (javaProject == null || !javaProject.exists() || ((IFolder)element).isVirtual())
return false;
} else {
return false;
}
}
return true;
} catch (CoreException e) {
}
return false;
}
}