-
Notifications
You must be signed in to change notification settings - Fork 82
/
CreateLinkedSourceFolderAction.java
140 lines (107 loc) · 5.09 KB
/
CreateLinkedSourceFolderAction.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
/*******************************************************************************
* Copyright (c) 2000, 2011 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.util.ArrayList;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
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.internal.corext.buildpath.BuildpathDelta;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.AddSourceFolderWizard;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
//SelectedElements iff enabled: IJavaProject && size == 1
public class CreateLinkedSourceFolderAction extends BuildpathModifierAction {
public CreateLinkedSourceFolderAction(IWorkbenchSite site) {
this(site, null);
}
public CreateLinkedSourceFolderAction(ISetSelectionTarget selectionTarget) {
this(null, selectionTarget);
}
private CreateLinkedSourceFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget) {
super(site, selectionTarget, BuildpathModifierAction.CREATE_LINK);
setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Link_label);
setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Link_tooltip);
setImageDescriptor(JavaPluginImages.DESC_ELCL_ADD_LINKED_SOURCE_TO_BUILDPATH);
setDescription(NewWizardMessages.PackageExplorerActionGroup_FormText_createLinkedFolder);
}
@Override
public String getDetailedDescription() {
return NewWizardMessages.PackageExplorerActionGroup_FormText_createLinkedFolder;
}
@Override
public void run() {
Shell shell= getShell();
try {
IJavaProject javaProject= (IJavaProject)getSelectedElements().get(0);
CPListElement newEntrie= new CPListElement(javaProject, IClasspathEntry.CPE_SOURCE);
CPListElement[] existing= CPListElement.createFromExisting(javaProject);
boolean isProjectSrcFolder= CPListElement.isProjectSourceFolder(existing, javaProject);
AddSourceFolderWizard wizard= new AddSourceFolderWizard(existing, newEntrie, getOutputLocation(javaProject), true, false, false, isProjectSrcFolder, isProjectSrcFolder);
wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(javaProject));
WizardDialog dialog= new WizardDialog(shell, wizard);
PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
dialog.create();
int res= dialog.open();
if (res == Window.OK) {
BuildpathDelta delta= new BuildpathDelta(getToolTipText());
ArrayList<CPListElement> newEntries= wizard.getExistingEntries();
delta.setNewEntries(newEntries.toArray(new CPListElement[newEntries.size()]));
IResource resource= wizard.getCreatedElement().getCorrespondingResource();
delta.addCreatedResource(resource);
delta.setDefaultOutputLocation(wizard.getOutputLocation());
informListeners(delta);
selectAndReveal(new StructuredSelection(wizard.getCreatedElement()));
}
notifyResult(res == Window.OK);
} catch (CoreException e) {
String title= NewWizardMessages.AbstractOpenWizardAction_createerror_title;
String message= NewWizardMessages.AbstractOpenWizardAction_createerror_message;
ExceptionHandler.handle(e, shell, title, message);
}
}
private IPath getOutputLocation(IJavaProject javaProject) {
try {
return javaProject.getOutputLocation();
} catch (CoreException e) {
IProject project= javaProject.getProject();
IPath projPath= project.getFullPath();
return projPath.append(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
}
}
@Override
protected boolean canHandle(IStructuredSelection selection) {
if (selection.size() != 1)
return false;
if (!(selection.getFirstElement() instanceof IJavaProject))
return false;
return true;
}
}