View Javadoc

1   //UML Model Transformation Tool (UMT)
2   //Copyright (C) 2003, 2004, 2005 SINTEF 
3   //Authors:  jon.oldevik at sintef.no | roy.gronmo at sintef.no | tor.neple at sintef.no | fredrik.vraalsen at sintef.no
4   //Webpage: http://umt.sourceforge.net 
5   //Deloped in the projects:  ACEGIS (EU project - IST-2002-37724), 
6   // CAFE (EUREKA/ITEA - ip00004), FAMILIES (ITEA project ip02009)
7   //
8   //This program is free software; you can redistribute it and/or
9   //modify it under the terms of the GNU Lesser General Public License
10  //as published by the Free Software Foundation; either version 2.1
11  //of the License, or (at your option) any later version.
12  //
13  //This program is distributed in the hope that it will be useful,
14  //but WITHOUT ANY WARRANTY; without even the implied warranty of
15  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  //Lesser General Public License for more details.
17  //
18  //You should have received a copy of the GNU Lesser General Public
19  //License along with this program; if not, write to the Free
20  //Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  //02111-1307 USA
22  package org.sintef.umt.hutntree;
23  
24  
25  import java.awt.Color;
26  import java.awt.Component;
27  import java.awt.Font;
28  import java.util.ResourceBundle;
29  
30  import javax.swing.ImageIcon;
31  import javax.swing.JLabel;
32  import javax.swing.JTree;
33  import javax.swing.ToolTipManager;
34  import javax.swing.tree.DefaultMutableTreeNode;
35  
36  import org.w3c.dom.Element;
37  
38  
39  
40  public class HutnTreeCellRenderer implements javax.swing.tree.TreeCellRenderer 
41  {
42  	
43      protected HutnTreeView _owner = null;
44      
45  	/***
46  	 *  General icons
47  	 * 
48  	 */
49  
50  	private String img_dir = "org/sintef/umt/hutntree/img/";
51  	private ClassLoader cl = getClass().getClassLoader(); 
52  	protected static ImageIcon packageimg = null; 	
53  	protected static ImageIcon typepackageimg = null;
54  	protected static ImageIcon classimg = null;
55  	protected static ImageIcon typeimg = null;	
56  	protected static ImageIcon associationimg = null;
57  	protected static ImageIcon attributeimg = null;
58  	protected static ImageIcon operationimg = null;
59  	protected static ImageIcon parameterimg = null;
60  	protected static ImageIcon interfaceimg = null;
61  	protected static ImageIcon dependencyimg = null;
62  	protected static ImageIcon actionstateimg = null;
63  	protected static ImageIcon outtransitionimg = null;
64  	protected static ImageIcon intransitionimg = null;
65  	protected static ImageIcon objectflowimg = null;
66  	protected static ImageIcon activitygraphimg = null;
67  	protected static ImageIcon choiceimg = null;	
68  	protected static ImageIcon forkimg = null;
69  	protected static ImageIcon joinimg = null;
70  	protected static ImageIcon roleimg = null;
71  	protected static ImageIcon startstateimg = null;
72  	protected static ImageIcon endstateimg = null;
73  	protected static ImageIcon usecaseimg = null;
74  	protected static ImageIcon threatscenarioimg = null;
75  	protected static ImageIcon incidentscenarioimg = null;
76  	protected static ImageIcon treatmentimg = null;
77  	protected static ImageIcon actorimg = null;
78  	protected static ImageIcon threatimg = null;
79  	protected static ImageIcon assetimg = null;
80  	
81  	/***
82  	 *  Icons for technology marks
83  	 * 
84  	 */
85  	
86  	final protected ImageIcon marked_packageimg = new ImageIcon(cl.getResource(img_dir + "marked_package.gif"));	
87  	final protected ImageIcon defaultimg = new ImageIcon (cl.getResource(img_dir + "default.gif"));
88  	
89  	final protected Font small_font = new Font("SansSerif", Font.PLAIN, 9);
90  	final protected Font normal_font = new Font("SansSerif", Font.PLAIN, 10);
91  	final protected Font bold_normal = new Font("SansSerif", Font.BOLD, 10);
92  	private ResourceBundle resourceBundle = null;
93  
94  	
95  	public HutnTreeCellRenderer (HutnTreeView owner) {
96  	    super();	    
97  	    init ();
98  	    _owner = owner;
99  
100 	}
101 	
102 	private void init () {
103 	    try {
104 	        resourceBundle = ResourceBundle.getBundle("org.sintef.umt.hutntree.HutnTreeResources");
105 	    } catch (Exception ex) {
106 	        ex.printStackTrace();
107 	    }
108 	    try {
109 			packageimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.package")));	
110 			typepackageimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.types_package")));
111 			classimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.class")));
112 			typeimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.type")));	
113 			associationimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.association")));
114 			attributeimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.attribute")));
115 			operationimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.operation")));
116 			parameterimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.parameter")));
117 			interfaceimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.interface")));
118 			dependencyimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.dependency")));
119 			actionstateimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.actionstate")));
120 			outtransitionimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.transition.out")));
121 			intransitionimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.transition.in")));
122 			objectflowimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.objectflow")));
123 			activitygraphimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activitygraph")));
124 			choiceimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activity.choice")));	
125 			forkimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activity.fork")));
126 			joinimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activity.join")));
127 			roleimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activity.role")));
128 			startstateimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activity.initstate")));
129 			endstateimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.activity.endstate")));	
130 			usecaseimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.usecase")));	
131 			threatscenarioimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.usecase.threat")));	
132 			incidentscenarioimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.usecase.incident")));	
133 			treatmentimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.usecase.treatment")));	
134 			actorimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.actor")));	
135 			threatimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.actor.threat")));	
136 			assetimg = new ImageIcon(cl.getResource(img_dir + resourceBundle.getString("icon.actor.asset")));	
137 	    } catch (Exception ex) {
138 	        System.out.println ("HutnTreeCellRenderer::init() - " + ex);
139 	    }
140 	}
141 	
142 	        public Component getTreeCellRendererComponent(JTree tree,
143                 Object value,
144                 boolean selected,
145                 boolean expanded,
146                 boolean leaf,
147                 int row,
148                 boolean hasFocus)
149 		{
150 			try{				
151 				JLabel l = new JLabel ();
152 				if (value == _owner.getModel().getRoot()) {
153 					String project_context = null;
154 					if (project_context != null && !project_context.equals("")){
155 						l.setText("Project: " + project_context);
156 					} else					
157 						l.setText("Model");										
158 					return l;
159 				}
160 				Object userobj = ((DefaultMutableTreeNode)value).getUserObject ();
161 				l.setText(userobj.toString());				
162 				ToolTipManager ttmanager = ToolTipManager.sharedInstance ();
163 				ttmanager.registerComponent(l);
164 				if (userobj instanceof Element) {
165 					Element e = (Element) userobj;
166 					String nodename = e.getNodeName ();
167 					String name = e.getAttribute ("name");
168 					if (name == null) name = "";
169 					String id = e.getAttribute ("id");
170 					String stereotype = e.getAttribute ("stereoType");
171 					if (nodename.equalsIgnoreCase("usecaseinclude")
172 						|| nodename.equalsIgnoreCase("usecaseextend")) {
173 						stereotype = nodename.substring("usecase".length());
174 					}
175 					if (stereotype == null) stereotype = "";
176 					
177 					l.setToolTipText("<<" + stereotype + ">>" + name);
178 					l.setFont(normal_font);
179 					if (stereotype != null && !stereotype.equals(""))
180 						l.setText (" <<"+ stereotype+">> " + name);
181 					else
182 						l.setText (name);
183 					
184 					l.setToolTipText(l.getText());
185 					
186 					if (nodename.equalsIgnoreCase ("model")){
187 						l.setIcon (packageimg);
188 						if (name.equals("")) {
189 							l.setText("Model");
190 						}
191 					}
192 					else if (nodename.equalsIgnoreCase ("package")){
193 //						String tech = e.getAttribute("technology");
194 						String transform = e.getAttribute ("transform");						
195 						if (transform.equalsIgnoreCase ("true")) {
196 /*							if (tech.equalsIgnoreCase("ejb")) {
197 								l.setIcon (marked_ejbimg);
198 							} else if (tech.equalsIgnoreCase("web service")){
199 							    l.setIcon (marked_greenpackageimg);
200 							} else if (tech.equalsIgnoreCase("servlet")) {
201 							} else
202 */
203 							l.setIcon (marked_packageimg);							
204 						}
205 						else {
206 							/*
207    							if (tech.equalsIgnoreCase("ejb")) {
208 								l.setIcon (ejbimg);
209 							} else if (tech.equalsIgnoreCase("web service")){
210 							    l.setIcon (greenpackageimg);
211 							} else if (tech.equalsIgnoreCase("servlet")) {
212 							} else
213 						*/
214 							    l.setIcon (packageimg);						
215 					    }
216 						if (name.equals("")) {
217 							l.setText("Package");
218 						}
219 					}
220 					else if (nodename.equalsIgnoreCase ("class")){
221 						l.setToolTipText(l.getToolTipText() + " (" + id + ")");
222 					    if (stereotype.equalsIgnoreCase("interface"))
223 							l.setIcon(interfaceimg);
224 						else if (stereotype.equalsIgnoreCase("datatype"))
225 							l.setIcon(typeimg);
226 					    else {
227 							String superclass = e.getAttribute("superClass");
228 							l.setIcon (classimg);
229 							if (superclass != null && !superclass.equals(""))
230 								l.setToolTipText(l.getToolTipText() + " extends " + _owner.getNameForID(superclass) + "(" + superclass +")");
231 					    }
232 					}
233 					else if (nodename.equalsIgnoreCase("datatype")) {
234 						l.setIcon(typeimg);
235 					}
236 					else if (nodename.equalsIgnoreCase ("attribute")){
237 						l.setIcon (attributeimg);
238 						String type = _owner.getNameForID (e.getAttribute ("type"));
239 						if (type != null)
240 							l.setText (l.getText() + " : " + type);	
241 						else
242 							type = e.getAttribute("type");
243 						l.setToolTipText("<<attribute>>" + name + ":" + type);
244 					}
245 					
246 					else if (nodename.equalsIgnoreCase("association") 
247 							|| nodename.equalsIgnoreCase("usecaseassociation")
248 							|| nodename.equalsIgnoreCase("usecaseparticipant")
249 							|| nodename.equalsIgnoreCase("usecaseinclude")
250 							|| nodename.equalsIgnoreCase("usecaseextend")) {
251 						String target = _owner.getNameForID(e.getAttribute ("targetClass"));
252 						if (target == null || target.equals(""))
253 							target = _owner.getNameForID(e.getAttribute ("target"));
254 						String cardinality = e.getAttribute ("cardinality");
255 						if (!cardinality.equals("")) 
256 							cardinality = "[" + cardinality + "]";
257 						l.setIcon (associationimg);						
258 						l.setText (l.getText () + " : " + target + " " + cardinality); 
259 						l.setToolTipText("<<association>>" + name + ":" + target + cardinality);
260 					}
261 					else if (nodename.equalsIgnoreCase ("operation")){
262 						String returntype = _owner.getNameForID (e.getAttribute ("returnType"));
263 						if (returntype == null || returntype.equals(""))
264 							returntype = "void";
265 						l.setText (l.getText() + " (): " + returntype);
266 						l.setIcon (operationimg);
267 						l.setToolTipText("<<operation>>" + name + "(...):" + returntype);
268 					}
269 					else if (nodename.equalsIgnoreCase ("parameter")){
270 						l.setIcon (parameterimg);
271 						String type = _owner.getNameForID(e.getAttribute ("type"));
272 						if (type != null)
273 							l.setText (l.getText () + ": " + type);
274 						else 
275 							type = e.getAttribute("type");
276 						l.setToolTipText(name + ":" + type);
277 					} else if (nodename.equalsIgnoreCase ("uses")) {
278 						// l.setIcon (dependencyimg);
279 						String target = _owner.getNameForID(e.getAttribute ("target"));
280 						l.setFont(small_font);
281 						l.setIcon(intransitionimg);
282 						if (target != null)
283 							l.setText ("<<uses>>" + target);
284 						else 
285 							l.setText("<<uses>>" + e.getAttribute("target"));
286 					} else if (nodename.equalsIgnoreCase ("taggedvalue")) {
287 						String tag = e.getAttribute("tag");
288 						String tagvalue = e.getAttribute("value");
289 						if (tag != null && value != null) {
290 							l.setText("<<tag>>" + tag + "=" + tagvalue);
291 							l.setToolTipText("<<TaggedValue>>" + tag + " = " + tagvalue);
292 						}
293 					} else if (nodename.equalsIgnoreCase("implements")) {
294 						l.setFont(small_font);
295 						String implname = _owner.getNameForID(id);
296 						l.setText("<<implements>>" + implname);
297 						l.setToolTipText("<<implements>>" + implname + " (" + id + ")");
298 					}
299 					/*
300 					 * Activity Presentation
301 					 */ 
302 					else if (nodename.equalsIgnoreCase("activity")) {
303 						l.setIcon(actionstateimg);
304 						String stype = e.getAttribute("stereoType"); if (stype == null) stype = "";
305 						stype = stype.equals("")?"":"<<"+stype+">>"; 
306 						l.setText(name);
307 						l.setToolTipText(stype +"activity " + e.getAttribute("name") + " id = " + e.getAttribute("id"));
308 					} else if (nodename.equalsIgnoreCase("incoming")) {
309 						l.setIcon(intransitionimg);
310 						l.setText("<<" + nodename + ">>" + e.getAttribute("source"));
311 						l.setToolTipText("incoming from source " + e.getAttribute("source") + " sourceId = " + e.getAttribute("sourceId"));
312 					} else if (nodename.equalsIgnoreCase("outgoing")) {
313 						l.setIcon(outtransitionimg);
314 						l.setText("<<" + nodename + ">>" + e.getAttribute("target") + " [guard:" + e.getAttribute("guard") + "]");
315 						l.setToolTipText("outgoing to target " + e.getAttribute("target") + " targetId = " + e.getAttribute("targetId"));						
316 					} else if (nodename.equalsIgnoreCase("objectflow")) {
317 						l.setIcon(objectflowimg);
318 						l.setText(name);
319 						l.setToolTipText("objectflow " + name + " id = " + e.getAttribute("id"));						
320 					} else if (nodename.equalsIgnoreCase("activitygraph") || nodename.equalsIgnoreCase("activitymodel")) {
321 						l.setIcon(activitygraphimg);
322 					} else if (nodename.equalsIgnoreCase("choice")) {
323 						l.setIcon(choiceimg);
324 						if (name.equals(""))
325 							l.setText("choice");
326 						else l.setText(e.getAttribute("name"));
327 					} else if (nodename.equalsIgnoreCase ("PseudoState")) {
328 						l.setText (name);
329 						String kind = e.getAttribute("kind");
330 						if (kind.equalsIgnoreCase("fork")) {
331 							l.setIcon(forkimg);
332 							l.setText(l.getText() + " (fork)");
333 						} else if (kind.equalsIgnoreCase("join")) {
334 							l.setIcon(joinimg);
335 							l.setText(l.getText() + " (join)");
336 						} else if (kind.equalsIgnoreCase("branch")) {
337 							l.setIcon(choiceimg);
338 							l.setText(l.getText() + " (branch)");
339 						} else if (kind.equalsIgnoreCase("initial")) {
340 							l.setIcon(startstateimg);
341 							l.setText(l.getText() + " (Start state)");
342 							l.setToolTipText("Start state " + e.getAttribute("id"));							
343 						} else if (kind.equalsIgnoreCase("final")) {
344 							l.setIcon(endstateimg);
345 							l.setText(l.getText () + " (End state)");
346 							l.setToolTipText("End state " + e.getAttribute("id"));														
347 						}							
348 					} else if (nodename.equalsIgnoreCase("role") || nodename.equalsIgnoreCase("roles")) {
349 						l.setIcon(roleimg);
350 						if (nodename.equalsIgnoreCase("roles"))
351 							l.setText("Roles");
352 						else {
353 							l.setText(name);
354 							l.setToolTipText(name + " (" + e.getAttribute("id") + ")");
355 						}
356 					} else if (nodename.equalsIgnoreCase("usecase")) {
357 						if (stereotype.equalsIgnoreCase("threatscenario")) {
358 							l.setIcon(threatscenarioimg);
359 						} else if (stereotype.equalsIgnoreCase("unwantedincident") || stereotype.equalsIgnoreCase("incidentscenario")) {
360 							l.setIcon(incidentscenarioimg);
361 						} else if (stereotype.equalsIgnoreCase("treatment")) {
362 							l.setIcon(treatmentimg);
363 						} else {
364 							l.setIcon(usecaseimg);
365 						}
366 					} else if (nodename.equalsIgnoreCase("actor")) {
367 						if (stereotype.equalsIgnoreCase("threatagent") || stereotype.equalsIgnoreCase("threat")) {
368 							l.setIcon(threatimg);
369 						} else if (stereotype.equalsIgnoreCase("asset")) {
370 							l.setIcon(assetimg);
371 						} else {
372 							l.setIcon(actorimg);
373 						}
374 					}
375 				}
376 				else if (userobj instanceof String && ((String)userobj).equalsIgnoreCase("datatypes"))
377 					l.setIcon(typepackageimg);
378 				else if (userobj instanceof String && ((String)userobj).equalsIgnoreCase("uses")) {
379 					l.setIcon(intransitionimg);
380 					l.setFont(small_font);
381 				}
382 				else if (userobj instanceof String)
383 					l.setIcon (defaultimg);
384 				else
385 					l.setIcon (defaultimg);
386 					
387 				if (selected){ 
388 					// l.setFont (new Font("SansSerif", Font.BOLD, 10));
389 					l.setForeground (Color.blue);
390 				}
391 				else{
392 					// l.setFont (new Font("SansSerif", Font.PLAIN, 10));
393 					l.setForeground(Color.black);
394 				}					
395 				return l;
396 			} catch (Exception ex){
397 			    System.out.println (ex);
398 			    ex.printStackTrace();
399 				return new JLabel ("....");
400 			}
401         }
402 }