Repository navigation
Expand file tree
/
Copy pathGenerator.java
More file actions
184 lines (141 loc) · 4.99 KB
/
Copy pathGenerator.java
File metadata and controls
184 lines (141 loc) · 4.99 KB
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
package code2code.core.generator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
public class Generator {
final private IFolder generatorFolder;
final private DescriptionConfig descriptionConfig;
final private GlobalParamsConfig globalParamsConfig;
final private ParamsConfig paramsConfig;
final private TemplatesConfig templatesConfig;
final private List<Generator> nestedGenerators;
final private List<Template> templates;
final private List<Template> launchers;
final private UserParams userParams;
private TemplatesConfig launchersConfig;
private Map<String, String> eclipseParams;
private Generator(IFolder folder) throws Exception {
generatorFolder = folder;
globalParamsConfig = new GlobalParamsConfig(this);
descriptionConfig = new DescriptionConfig(this, globalParamsConfig);
paramsConfig = new ParamsConfig(this);
templatesConfig = new TemplatesConfig(this);
launchersConfig = new TemplatesConfig(this, "postactions");
nestedGenerators = templatesConfig.getNestedGenerators();
templates = calculateGeneratorTemplates();
launchers = calculateGeneratorLaunchers();
userParams = new UserParams(this, globalParamsConfig, paramsConfig,
nestedGenerators);
}
public static Generator fromFolder(IFolder folder) throws Exception {
return new Generator(folder);
}
private List<Template> calculateGeneratorTemplates() throws Exception {
List<Template> templates = templatesConfig.getTemplates();
for (Generator nestedGenerator : nestedGenerators) {
templates.addAll(nestedGenerator.calculateGeneratorTemplates());
}
return templates;
}
private List<Template> calculateGeneratorLaunchers() throws Exception {
List<Template> templates = launchersConfig.getTemplates();
for (Generator nestedGenerator : nestedGenerators) {
templates.addAll(nestedGenerator.calculateGeneratorLaunchers());
}
return templates;
}
public List<Template> calculateChoosenTemplatesToGenerate()
throws Exception {
List<Template> templatesToGenerate = new ArrayList<Template>();
for (Template template : templates) {
if (template.isSelectedToGenerate()) {
templatesToGenerate.add(template);
}
}
for (Template template : launchers) {
if (template.isSelectedToGenerate()) {
templatesToGenerate.add(template);
}
}
return templatesToGenerate;
}
public IFolder getGeneratorFolder() {
return generatorFolder;
}
public String getName() {
return generatorFolder.getFullPath().removeFirstSegments(2)
.removeFileExtension().toString();
}
public String getDescription() {
return descriptionConfig.getDescription();
}
public Map<String, String> calculateRequiredParams() {
return userParams.calculateGeneratorRequiredParams();
}
public Map<String, String> calculateContext() throws Exception {
//3Zen
Map<String,String> result = new HashMap<String,String>();
result.putAll(calculateEclipseParams());
result.putAll(userParams.translated());
return result; //userParams.translated();
}
public void setUserConfiguredParams(Map<String, String> userConfiguredParams) {
userParams.setUserConfiguredParams(userConfiguredParams);
}
public List<Template> getTemplates() {
return templates;
}
public List<Template> getPostActions() {
return launchers;
}
public Map<String, String> calculateEclipseParams() {
if (eclipseParams == null) {
eclipseParams = new TreeMap<String, String>();
IProject eclipse = getCurrentEclipseProject();
Bundle bundle = FrameworkUtil.getBundle(getClass());
final Map<String, String> params = (paramsConfig.getParams());
if (eclipse != null) {
setEclipseParam(params,"eclipse_project_name", eclipse.getName());
setEclipseParam(params,"eclipse_project_path", eclipse.getLocation().toFile().getAbsolutePath());
}
if (bundle!=null){
setEclipseParam(params,"eclipse_plugin_name", bundle.getSymbolicName());
setEclipseParam(params,"eclipse_plugin_version",bundle.getVersion().toString());
}
}
return eclipseParams;
}
private void setEclipseParam(
final Map<String, String> params,
String name,
String value) {
if (!params.containsKey(name)) {
eclipseParams
.put(name,value);
}
}
private IProject getCurrentEclipseProject() {
IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (window != null) {
IStructuredSelection selection = (IStructuredSelection) window
.getSelectionService().getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IAdaptable) {
return (IProject) ((IAdaptable) firstElement)
.getAdapter(IProject.class);
}
}
return null;
}
}