1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.backward.figure.icons;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.log4j.Logger;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IConfigurationElement;
30
31 import pl.edu.agh.cast.Activator;
32 import pl.edu.agh.cast.CastApplication;
33 import pl.edu.agh.cast.util.Images;
34 import pl.edu.agh.cast.util.Messages;
35
36
37
38
39
40
41
42
43 public final class NodeIconProvider {
44
45 private static Logger log = Activator.getLogger();
46
47 private static NodeIconProvider instance;
48
49 private HashMap<String, NodeIcon> nodeIcons;
50
51 private HashMap<String, NodeIcon> nodeIconsByID;
52
53 private NodeIcon defaultIcon;
54
55
56
57
58
59
60 public static NodeIconProvider getInstance() {
61 if (instance == null) {
62 instance = new NodeIconProvider();
63 instance.init();
64 }
65 return instance;
66 }
67
68
69
70
71 private NodeIconProvider() {
72 nodeIcons = new HashMap<String, NodeIcon>();
73 nodeIconsByID = new HashMap<String, NodeIcon>();
74 }
75
76 private void addNodeIcon(NodeIcon nodeIcon) {
77 nodeIcons.put(nodeIcon.getNodeType(), nodeIcon);
78 nodeIconsByID.put(nodeIcon.getIconId(), nodeIcon);
79 nodeIconsByID.put(nodeIcon.getSmallIconId(), nodeIcon);
80
81 Images.getInstance().putDescriptor(nodeIcon.getIconId(), nodeIcon.getIconDescriptor());
82 Images.getInstance().putDescriptor(nodeIcon.getSmallIconId(), nodeIcon.getSmallIconDescriptor());
83 }
84
85
86
87
88
89
90 private void init() {
91 try {
92 for (IConfigurationElement nodeIconCE : Activator.getNodeIconsConfigurations()) {
93
94 INodeIconFactory factory = null;
95 try {
96 factory = (INodeIconFactory)nodeIconCE.createExecutableExtension(Activator.NODE_ICON_FACTORY);
97 } catch (CoreException e) {
98 log.warn("Failed to instantiate INodeIconFactory provided by extension: "
99 + nodeIconCE.getDeclaringExtension().getUniqueIdentifier(), e);
100 continue;
101 }
102
103 for (NodeIcon nodeIcon : factory.getNodeIcons()) {
104 if (nodeIcon == null) {
105 log.warn("Invalid (null) NodeIcon provided by extension: "
106 + nodeIconCE.getDeclaringExtension().getUniqueIdentifier());
107 continue;
108 }
109
110 addNodeIcon(nodeIcon);
111
112 }
113 }
114
115 defaultIcon = AbstractNodeIconFactory.createNodeIcon(CastApplication.class, null,
116 Messages.CoreNodeIcon_DisplayName_Default,
117 "icons/people/entity.png", "icons/people/entity_small.png",
118 Messages.CoreNodeIcon_Group_0);
119
120 addNodeIcon(defaultIcon);
121
122 } catch (Exception e) {
123
124
125 }
126 }
127
128
129
130
131
132
133
134
135 public NodeIcon getNodeIcon(String nodeType) {
136 NodeIcon nodeIcon = nodeIcons.get(nodeType);
137 if (nodeIcon == null) {
138 nodeIcon = defaultIcon;
139 }
140 return nodeIcon;
141 }
142
143
144
145
146
147
148
149
150 public String getIconId(String nodeType) {
151 NodeIcon nodeIcon = getNodeIcon(nodeType);
152 if (nodeIcon != null) {
153 return nodeIcon.getIconId();
154 } else {
155 return null;
156 }
157 }
158
159
160
161
162
163
164
165
166 public String getSmallIconId(String nodeType) {
167 NodeIcon nodeIcon = getNodeIcon(nodeType);
168 if (nodeIcon != null) {
169 return nodeIcon.getSmallIconId();
170 } else {
171 return null;
172 }
173 }
174
175
176
177
178
179
180
181
182 public String getFriendlyName(String imageId) {
183 NodeIcon nodeIcon = nodeIconsByID.get(imageId);
184 if (nodeIcon == null) {
185 return null;
186 }
187 if (nodeIcon.getName() != null) {
188 return nodeIcon.getName();
189 } else {
190 return nodeIcon.getNodeType();
191 }
192 }
193
194 public Collection<NodeIcon> getAllNodeIcons() {
195 return Collections.unmodifiableCollection(nodeIcons.values());
196 }
197
198
199
200
201
202
203 public Map<String, List<NodeIcon>> getGroupedNodeIcons() {
204 Map<String, List<NodeIcon>> groupMap = new HashMap<String, List<NodeIcon>>();
205 for (NodeIcon nodeIcon : getAllNodeIcons()) {
206 if (nodeIcon == null) {
207 continue;
208 }
209 List<NodeIcon> group = groupMap.get(nodeIcon.getGroup());
210 if (group == null) {
211 group = new LinkedList<NodeIcon>();
212 groupMap.put(nodeIcon.getGroup(), group);
213 }
214 group.add(nodeIcon);
215 }
216 return groupMap;
217 }
218
219
220
221
222
223
224 public NodeIcon getDefaultIcon() {
225 return defaultIcon;
226 }
227
228 }