-
Notifications
You must be signed in to change notification settings - Fork 4
/
PartManager.java
131 lines (114 loc) · 3 KB
/
PartManager.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
package com.xored.q7.quality.mockups.issues;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import com.xored.q7.quality.mockups.issues.internal.QualityUIPlugin;
public class PartManager {
private List<MockupPart> allParts = null;
private List<MockupPart> roots = null;
public PartManager() {
}
public static class MockupPart {
private String name;
private String issue;
private String category;
private IQ7MockupPart part;
private List<MockupPart> children;
public String getName() {
return name;
}
public String getIssue() {
return issue;
}
public IQ7MockupPart getPart() {
return part;
}
public String getCategory() {
return category;
}
void addChild(MockupPart part) {
if (children == null) {
children = new ArrayList<PartManager.MockupPart>();
}
children.add(part);
}
public List<MockupPart> getChildren() {
return children;
}
}
private synchronized void initialize() {
if (allParts != null) {
return;
}
allParts = new ArrayList<MockupPart>();
IConfigurationElement[] elements = Platform.getExtensionRegistry()
.getConfigurationElementsFor(
QualityUIPlugin.PLUGIN_ID + ".part");
for (IConfigurationElement cfg : elements) {
try {
MockupPart part = new MockupPart();
part.part = (IQ7MockupPart) cfg
.createExecutableExtension("class");
part.name = cfg.getAttribute("name");
part.category = cfg.getAttribute("category");
part.issue = cfg.getAttribute("issue");
allParts.add(part);
} catch (CoreException e) {
e.printStackTrace();
}
}
// Move to categories
roots = new ArrayList<PartManager.MockupPart>();
Map<String, MockupPart> partsMap = new HashMap<String, PartManager.MockupPart>();
MockupPart other = new MockupPart();
for (MockupPart part : allParts) {
String category = part.getCategory();
if (category == null) {
other.addChild(part);
continue;
}
String[] split = category.split("\\.");
MockupPart parentPart = getCategory(roots, split, partsMap);
parentPart.addChild(part);
}
if (other.getChildren() != null && other.getChildren().size() > 0) {
roots.add(other);
}
}
private MockupPart getCategory(List<MockupPart> roots, String[] split,
Map<String, MockupPart> categoryMap) {
if (roots == null) {
return null;
}
if (split.length == 0) {
return null;
}
String ss = "";
MockupPart prev = null;
for (String s : split) {
MockupPart part = categoryMap.get(ss + s);
if (part == null) {
part = new MockupPart();
part.name = s;
part.category = ss + s;
categoryMap.put(part.category, part);
if (prev != null) {
prev.addChild(part);
} else {
roots.add(part);
}
}
ss = ss + s + ".";
prev = part;
}
return prev;
}
public List<MockupPart> getParts() {
initialize();
return roots;
}
}