KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > tools > KilimTemplateViewer


1 package org.objectweb.kilim.tools;
2
3 import java.awt.event.WindowAdapter JavaDoc;
4 import java.awt.event.WindowEvent JavaDoc;
5
6 import java.awt.BorderLayout JavaDoc;
7 import java.awt.Container JavaDoc;
8 import java.awt.Image JavaDoc;
9 import java.awt.Toolkit JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import javax.swing.Icon JavaDoc;
16 import javax.swing.ImageIcon JavaDoc;
17 import javax.swing.JFrame JavaDoc;
18 import javax.swing.JScrollPane JavaDoc;
19 import javax.swing.JTree JavaDoc;
20 import javax.swing.ToolTipManager JavaDoc;
21 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
22 import javax.swing.tree.TreeModel JavaDoc;
23
24 import org.objectweb.kilim.description.Arity;
25 import org.objectweb.kilim.description.Binding;
26 import org.objectweb.kilim.description.Instance;
27 import org.objectweb.kilim.description.NamedElement;
28 import org.objectweb.kilim.description.Plug;
29 import org.objectweb.kilim.description.Port;
30 import org.objectweb.kilim.description.Property;
31 import org.objectweb.kilim.description.Provider;
32 import org.objectweb.kilim.description.Slot;
33 import org.objectweb.kilim.description.TemplateDescription;
34 import org.objectweb.kilim.description.TemplateElementImpl;
35 import org.objectweb.kilim.description.TpMethod;
36 import org.objectweb.kilim.description.Transformer;
37 import org.objectweb.kilim.description.Trigger;
38
39 /**
40  * @author delpiano
41  *
42  * To change this generated comment edit the template variable "typecomment":
43  * Window>Preferences>Java>Templates.
44  * To enable and disable the creation of type comments go to
45  * Window>Preferences>Java>Code Generation.
46  */

47 public class KilimTemplateViewer {
48     
49     private static final String JavaDoc WINDOW_NAME = "Kilim Template Viewer";
50     
51     private static ActionListener JavaDoc action_listener = new ActionListener JavaDoc(){
52         public void actionPerformed(ActionEvent JavaDoc evt) {
53             synchronized (this) {
54                 this.notifyAll();
55             }
56         }
57     };
58     
59     private static JFrame JavaDoc view_frame = new JFrame JavaDoc();
60     
61     static {
62         view_frame.setTitle(WINDOW_NAME);
63         view_frame.addWindowListener(new WindowAdapter JavaDoc() {
64             public void windowClosing(WindowEvent JavaDoc evt) {
65                 synchronized (action_listener) {
66                     action_listener.notifyAll();
67                 }
68             }
69         });
70     }
71     
72     private static JTree JavaDoc tree = new JTree JavaDoc();
73     private static TreeModel JavaDoc treeModel;
74     
75     static {
76     }
77     
78     /**
79      * Method viewTemplate.
80      * @param tmplt :
81      */

82     public static void viewTemplate(TemplateDescription tmplt) {
83         tree.setCellRenderer(new KilimTemplateTreeCellRenderer());
84         Container JavaDoc cntnr = view_frame.getContentPane();
85         cntnr.setLayout(new BorderLayout JavaDoc());
86         JScrollPane JavaDoc scrllpn = new JScrollPane JavaDoc(tree);
87         cntnr.add(BorderLayout.CENTER, scrllpn);
88         view_frame.setSize(400, 400);
89         
90         ToolTipManager.sharedInstance().registerComponent(tree);
91         
92         String JavaDoc tmplt_nm = "error";
93         tmplt_nm = tmplt.getName();
94         
95         view_frame.setTitle(WINDOW_NAME + " - " + tmplt_nm);
96         
97         treeModel = new KilimTemplateTreeModel(tmplt);
98         tree.setModel(treeModel);
99         tree.repaint();
100         view_frame.setVisible(true);
101
102         synchronized (action_listener) {
103             try {
104                 action_listener.wait();
105             } catch (InterruptedException JavaDoc exp) {
106                 exp.printStackTrace();
107             }
108         }
109     }
110
111     /**
112      * Method updateView.
113      */

114     public static void updateView() {
115         tree.setModel(null);
116         tree.setModel(treeModel);
117         view_frame.validate();
118         view_frame.repaint();
119
120         synchronized (action_listener) {
121             try {
122                 action_listener.wait();
123             } catch (InterruptedException JavaDoc exp) {
124                 exp.printStackTrace();
125             }
126         }
127     }
128 }
129
130 class KilimTemplateTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
131
132     private static final String JavaDoc[] ICON_NAMES = { "provider.gif" , "transformer.gif" , "template_instance.gif" ,
133                                                  "binding.gif" , "port.gif" , "template.gif" , "property.gif" ,
134                                                  "trigger.gif" , "multiPort.gif" , "optionalPort.gif" , "slot.gif" ,
135                                                  "plug.gif"};
136                                                  
137     private static final Icon JavaDoc[] ICONS = new Icon JavaDoc[ICON_NAMES.length];
138     
139     static {
140         for (int i = 0; i < ICON_NAMES.length; i++) {
141             ICONS[i] = getIcon(ICON_NAMES[i]);
142         }
143     }
144
145     /**
146      * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean)
147      */

148     public java.awt.Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded,
149                                                             boolean leaf, int row, boolean hasFocus) {
150                                                                 
151         super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
152         
153         if (value instanceof NamedElement) {
154             String JavaDoc nm = ((NamedElement) value).getLocalName();
155             setText(nm);
156             
157         } else if (value instanceof Binding) {
158             if (ICONS[3] != null) {
159                 setIcon(ICONS[3]);
160             }
161             
162             Binding bndng = (Binding) value;
163             Iterator JavaDoc prvdrs = bndng.getBoundProviders();
164             StringBuffer JavaDoc prvdrs_txt_bffr = new StringBuffer JavaDoc();
165             boolean frst = true;
166             for (; prvdrs.hasNext();) {
167                 Object JavaDoc nxt = prvdrs.next();
168                 if (frst) {
169                     frst = false;
170                 } else {
171                     prvdrs_txt_bffr.append(",");
172                 }
173                 if (nxt instanceof NamedElement) {
174                     prvdrs_txt_bffr.append(((NamedElement) nxt).getLocalName());
175                 } else if (nxt instanceof TpMethod) {
176                     TpMethod mthd = (TpMethod) nxt;
177                     prvdrs_txt_bffr.append(mthd.getSupport());
178                     prvdrs_txt_bffr.append(".");
179                     prvdrs_txt_bffr.append(mthd.getMethodName());
180                     prvdrs_txt_bffr.append("()");
181                 } else {
182                     prvdrs_txt_bffr.append(nxt);
183                 }
184             }
185             setText("Binding: {" + bndng.getPortName() + "} <-- " + prvdrs_txt_bffr.toString());
186             
187         } else if (value instanceof Trigger) {
188             
189             if (ICONS[7] != null) {
190                 setIcon(ICONS[7]);
191             }
192             Trigger trggr = (Trigger) value;
193             int evt_knd = trggr.getEventKind();
194             String JavaDoc src_nm = trggr.getSourceName();
195             Iterator JavaDoc trnsfrmrs = trggr.getTransformers();
196             
197             StringBuffer JavaDoc prvdrs_txt_bffr = new StringBuffer JavaDoc();
198             boolean frst = true;
199             for (; trnsfrmrs.hasNext();) {
200                 Object JavaDoc nxt = trnsfrmrs.next();
201                 if (frst) {
202                     frst = false;
203                 } else {
204                     prvdrs_txt_bffr.append(",");
205                 }
206                 if (nxt instanceof NamedElement) {
207                     prvdrs_txt_bffr.append(((NamedElement) nxt).getLocalName());
208                 } else if (nxt instanceof TpMethod) {
209                     TpMethod mthd = (TpMethod) nxt;
210                     prvdrs_txt_bffr.append(mthd.getSupport());
211                     prvdrs_txt_bffr.append(".");
212                     prvdrs_txt_bffr.append(mthd.getMethodName());
213                     prvdrs_txt_bffr.append("()");
214                 } else {
215                     prvdrs_txt_bffr.append(nxt);
216                 }
217             }
218             setText("Trigger on " + Trigger.EVENT_TYPE_NAME[evt_knd] + ": {" + src_nm + "} ~~> " + prvdrs_txt_bffr.toString());
219         }
220         
221         if (value instanceof Provider) {
222             
223             if (ICONS[0] != null) {
224                 setIcon(ICONS[0]);
225             }
226             
227         } else if (value instanceof Transformer) {
228             
229             if (ICONS[1] != null) {
230                 setIcon(ICONS[1]);
231             }
232             
233         } else if (value instanceof Instance) {
234             
235             if (ICONS[2] != null) {
236                 setIcon(ICONS[2]);
237             }
238             Instance instnc = (Instance) value;
239             TemplateDescription tmplt = instnc.getTemplate();
240             String JavaDoc tmplt_nm = getTemplateSuperAndSuperSuperTemplateName(tmplt);
241             setText(instnc.getLocalName() + " [" + tmplt_nm + "]");
242                 
243         } else if (value instanceof Port) {
244             
245             Port prt = (Port) value;
246             Arity art = prt.getArity();
247             
248             if (art == Arity.COLLECTION) {
249                 if (ICONS[8] != null) {
250                     setIcon(ICONS[8]);
251                 }
252             } else if (art == Arity.REQUIRED) {
253                 if (ICONS[4] != null) {
254                     setIcon(ICONS[4]);
255                 }
256             } else {
257                 if (ICONS[9] != null) {
258                     setIcon(ICONS[9]);
259                 }
260             }
261             
262             setText(prt.getLocalName() + " [" + (prt.isOffered() ? "offered" : "required") + "]");
263             
264         } else if (value instanceof TemplateDescription) {
265             
266             if (ICONS[5] != null) {
267                 setIcon(ICONS[5]);
268             }
269             TemplateDescription tmplt = (TemplateDescription) value;
270             String JavaDoc tmplt_nm = getTemplateSuperAndSuperSuperTemplateName(tmplt);
271             setText(tmplt_nm);
272                 
273         } else if (value instanceof Property) {
274             
275             Property prprt = (Property) value;
276             setText(prprt.getLocalName() + " [" + prprt.getType() + "]: " + prprt.getValue());
277             if (ICONS[6] != null) {
278                 setIcon(ICONS[6]);
279             }
280             
281         } else if (value instanceof Slot) {
282             
283             Slot slt = (Slot) value;
284             if (ICONS[10] != null) {
285                 setIcon(ICONS[10]);
286             }
287             
288         } else if (value instanceof Plug) {
289             
290             Plug slt = (Plug) value;
291             setText("Plugging [" + slt.getInstanceName() + "] in slot [" + slt.getSlotName() + "]");
292             if (ICONS[11] != null) {
293                 setIcon(ICONS[11]);
294             }
295             
296         }
297         
298         if (value instanceof TemplateElementImpl) {
299             TemplateElementImpl elmnt = (TemplateElementImpl) value;
300             Iterator JavaDoc dfntns = elmnt.getTemplateDefHierarchy();
301             StringBuffer JavaDoc bffr = new StringBuffer JavaDoc("<html><body>Defined in ");
302             boolean first = true;
303             for (; dfntns.hasNext() ;) {
304                 if (first) {
305                     first = false;
306                 } else {
307                  bffr.append(" -> ");
308                 }
309                 TemplateDescription tmplt_dsc = (TemplateDescription) dfntns.next();
310                 bffr.append("<b>");
311                 bffr.append(tmplt_dsc.getName());
312                 bffr.append("</b>");
313             }
314             bffr.append("</body></html>");
315             setToolTipText(bffr.toString());
316         }
317         
318         return this;
319     }
320
321     private static ImageIcon JavaDoc getIcon(String JavaDoc name) {
322         URL JavaDoc url = ClassLoader.getSystemClassLoader().getSystemResource(name);
323         if (url != null) {
324             Image JavaDoc img = Toolkit.getDefaultToolkit().getImage(url);
325             if (img != null) {
326                 return new ImageIcon JavaDoc(img);
327             } else {
328                 return null;
329             }
330         } else {
331             return null;
332         }
333     }
334     
335     private static final String JavaDoc getTemplateSuperAndSuperSuperTemplateName(TemplateDescription tmplt) {
336         if (tmplt == null) {
337             return "none";
338         }
339         
340         StringBuffer JavaDoc bffr = new StringBuffer JavaDoc();
341         bffr.append(tmplt.getName());
342             
343         TemplateDescription spr_spr_tmplt = null;
344         TemplateDescription spr_tmpl = null;
345         
346         if (tmplt != null) {
347             spr_tmpl = tmplt.getSuperTemplate();
348         }
349         if (spr_tmpl != null) {
350             bffr.append(" -> ");
351             bffr.append(spr_tmpl.getName());
352             spr_spr_tmplt = spr_tmpl.getSuperTemplate();
353             if (spr_spr_tmplt != null) {
354                 bffr.append(" -> ");
355                 bffr.append(spr_spr_tmplt.getName());
356             }
357         }
358         return bffr.toString();
359     }
360 }
361
Popular Tags