View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: NodeIconProvider.java
13   * Created: 2008-11-11
14   * Author: tmilos
15   * $Id: NodeIconProvider.java 3054 2009-07-27 10:23:07Z kpietak $
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   * Provides uniform and centralized way of accessing <code>NodeIcon</code>s.
38   *
39   * Manages icons provided by all extenders of <code>pl.edu.agh.cast.nodeIcons</code> extension point.
40   *
41   * @author AGH CAST Team
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  	 * Returns single, shared instance of {@link NodeIconProvider}.
57  	 *
58  	 * @return single, shared instance of {@link NodeIconProvider}
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  	 * Hidden constructor.
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  	 * Initializes the instance. Loads descriptors of all icons provided by factories plugged into
87  	 * <code>pl.edu.agh.cast.nodeIcons</code> extension point. Registers all icon images in <code>Images</code>'s
88  	 * <code>ImageRegistry</code>.
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: " //$NON-NLS-1$
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: " //$NON-NLS-1$
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", //$NON-NLS-1$ //$NON-NLS-2$
118 			        Messages.CoreNodeIcon_Group_0);
119 
120 			addNodeIcon(defaultIcon);
121 
122 		} catch (Exception e) {
123 			// ignore: this may happen when RCP Platform is not running
124 			// - ex. during tests
125 		}
126 	}
127 
128 	/**
129 	 * Returns {@link NodeIcon} for given node type.
130 	 *
131 	 * @param nodeType
132 	 *            type of the node
133 	 * @return node icon for requested type
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 	 * Returns Id of the icon image for given node type.
145 	 *
146 	 * @param nodeType
147 	 *            type of the node
148 	 * @return Id of the icon image for requested type
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 	 * Returns Id of the small icon image for given node type.
161 	 *
162 	 * @param nodeType
163 	 *            type of the node
164 	 * @return Id of the small icon image for requested type
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 	 * Returns localized display name for given icon image.
177 	 *
178 	 * @param imageId
179 	 *            id of the icon image
180 	 * @return localized icon description
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 	 * Returns a map of <code>NodeIcon</code>s groups, with localized group names as keys.
200 	 *
201 	 * @return map of <code>NodeIcon</code>s groups
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 	 * Returns the default {@link NodeIcon}.
221 	 *
222 	 * @return the default {@link NodeIcon}
223 	 */
224 	public NodeIcon getDefaultIcon() {
225 		return defaultIcon;
226 	}
227 
228 }