KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.kilim.tools;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Container JavaDoc;
5 import java.awt.Image JavaDoc;
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.net.URL JavaDoc;
10
11 import java.awt.event.WindowAdapter JavaDoc;
12 import java.awt.event.WindowEvent JavaDoc;
13
14 import javax.swing.Icon JavaDoc;
15 import javax.swing.ImageIcon JavaDoc;
16 import javax.swing.JButton JavaDoc;
17 import javax.swing.JFrame JavaDoc;
18 import javax.swing.JScrollPane JavaDoc;
19 import javax.swing.JTextArea JavaDoc;
20 import javax.swing.JTree JavaDoc;
21 import javax.swing.ToolTipManager JavaDoc;
22 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
23
24 import org.objectweb.kilim.KilimException;
25 import org.objectweb.kilim.description.BasicElement;
26 import org.objectweb.kilim.description.ClassSource;
27 import org.objectweb.kilim.description.KILIM;
28 import org.objectweb.kilim.description.TpConstructor;
29 import org.objectweb.kilim.description.TpGetter;
30 import org.objectweb.kilim.description.TpMethod;
31 import org.objectweb.kilim.model.RtCollectionPort;
32 import org.objectweb.kilim.model.RtComponentProperty;
33 import org.objectweb.kilim.model.RtComponentProvider;
34 import org.objectweb.kilim.model.RtSingleValuePort;
35 import org.objectweb.kilim.model.mapping.TreeModelMapper;
36
37 /**
38  * @author delpiano, horn
39  */

40 public class KilimTraceTreeViewer {
41     
42     private static final String JavaDoc WINDOW_NAME = "Kilim Trace Viewer";
43     
44     private static ActionListener JavaDoc action_listener = new ActionListener JavaDoc() {
45         public void actionPerformed(ActionEvent JavaDoc evt) {
46             synchronized (this) {
47                 this.notifyAll();
48             }
49         }
50     };
51     
52     private static JFrame JavaDoc tree_frame = new JFrame JavaDoc();
53     private static JFrame JavaDoc text_frame = new JFrame JavaDoc();
54     private static JTextArea JavaDoc textArea;
55     
56     static {
57         tree_frame.setTitle(WINDOW_NAME);
58         tree_frame.addWindowListener(new WindowAdapter JavaDoc(){
59             public void windowClosing(WindowEvent JavaDoc evt) {
60                 synchronized (action_listener) {
61                     action_listener.notifyAll();
62                 }
63             }
64         });
65
66         text_frame.setTitle(WINDOW_NAME);
67         text_frame.addWindowListener(new WindowAdapter JavaDoc(){
68             public void windowClosing(WindowEvent JavaDoc evt) {
69                 synchronized (action_listener) {
70                     action_listener.notifyAll();
71                 }
72             }
73         });
74     }
75     
76     private static JTree JavaDoc tree = new JTree JavaDoc();
77     private static KilimTraceTreeModel treeModel;
78         
79     /**
80      * Method viewComponent.
81      * @param aModel :
82      * @param aTextArea :
83      */

84     public static void viewTrace(KilimTraceTreeModel aModel, JTextArea JavaDoc aTextArea) {
85         textArea = aTextArea;
86         tree.setCellRenderer(new KilimTraceTreeCellRenderer());
87         Container JavaDoc cntnr = tree_frame.getContentPane();
88         cntnr.setLayout(new BorderLayout JavaDoc());
89         JScrollPane JavaDoc scrllpn = new JScrollPane JavaDoc(tree);
90         cntnr.add(BorderLayout.CENTER, scrllpn);
91         tree_frame.setSize(650, 800);
92         tree_frame.setLocation(50, 50);
93         ToolTipManager.sharedInstance().registerComponent(tree);
94         JButton JavaDoc button = new JButton JavaDoc("continue");
95         button.addActionListener(action_listener);
96         cntnr.add(BorderLayout.SOUTH, button);
97
98         treeModel = aModel;
99         tree.setModel(treeModel);
100         tree.repaint();
101
102         Container JavaDoc textCont = text_frame.getContentPane();
103         textCont.setLayout(new BorderLayout JavaDoc());
104         JScrollPane JavaDoc textSPane = new JScrollPane JavaDoc(aTextArea);
105         textCont.add(BorderLayout.CENTER, textSPane);
106         text_frame.setSize(600, 800);
107         text_frame.setLocation(700, 50);
108         textArea.append("\n// -----------------------END VIEW-----------------------");
109         textArea.repaint();
110                 
111         tree_frame.setVisible(true);
112         text_frame.setVisible(true);
113
114         synchronized (action_listener) {
115             try {
116                 action_listener.wait();
117             } catch (InterruptedException JavaDoc exp) {
118                 exp.printStackTrace();
119             }
120         }
121     }
122     
123     /**
124      * Method updateView.
125      */

126     public static void updateView() {
127         tree.setModel(null);
128         tree.setModel(treeModel);
129         tree_frame.validate();
130         tree_frame.repaint();
131         textArea.append("\n// -----------------------END VIEW-----------------------");
132         textArea.repaint();
133
134         synchronized (action_listener) {
135             try {
136                 action_listener.wait();
137             } catch (InterruptedException JavaDoc exp) {
138                 exp.printStackTrace();
139             }
140         }
141     }
142 }
143
144 class KilimTraceTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
145     
146     private static final String JavaDoc TRACE_START_ICON_NAME = "start.gif";
147     private static Icon JavaDoc TRACE_START_ICON = null;
148
149     private static final String JavaDoc[] ICON_NAMES = { "provider.gif" , "transformer.gif" , "property.gif" , "trigger.gif", "external.gif" };
150     private static Icon JavaDoc[] ICONS = new Icon JavaDoc[ICON_NAMES.length];
151     
152     static {
153         try {
154             TRACE_START_ICON = getIcon(TRACE_START_ICON_NAME);
155             for (int i = 0; i < ICON_NAMES.length; i++) {
156                 ICONS[i] = getIcon(ICON_NAMES[i]);
157             }
158         } catch (Exception JavaDoc ex) {
159             System.err.println("problem when getting the start icon ");
160         }
161     }
162     
163     /**
164      * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean)
165      */

166     public java.awt.Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
167         super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
168
169         if (value instanceof String JavaDoc) {
170             if (TRACE_START_ICON != null) {
171                 setIcon(TRACE_START_ICON);
172             }
173             setText("Start");
174             return this;
175         }
176         
177         if (!(value instanceof KilimTraceTreeNode)) {
178             return this;
179         }
180         
181         KilimTraceTreeNode node = (KilimTraceTreeNode) value;
182         Object JavaDoc object = node.getValue();
183         if (object instanceof String JavaDoc) {
184             setText("+++++++++++++START TRACE+++++++++++++");
185             return this;
186         }
187         
188         int seqNumber = node.getSeqNumber();
189         int mode = node.getMode();
190         String JavaDoc addtnl_text = "undefined";
191         
192         if (object instanceof RtComponentProperty) {
193             RtComponentProperty prop = (RtComponentProperty) object;
194             if (ICONS[2] != null) {
195                     setIcon(ICONS[2]);
196             }
197             addtnl_text = "property : " + prop.getQualifiedName();
198             setToolTipText(addtnl_text);
199             String JavaDoc pValue ;
200             try {
201                  pValue = "" + prop.getValue();
202             } catch (KilimException ex) {
203                 pValue = "undefined property";
204             }
205             setText("<" + seqNumber + " > " + prop.getQualifiedName() + " [" + pValue + "]");
206             return this;
207         }
208         
209         if (object instanceof RtComponentProvider) {
210             RtComponentProvider elem = (RtComponentProvider) node.getValue();
211             int kind = elem.getKind();
212             
213             BasicElement desc = (BasicElement) elem.getElementDescription();
214             if (mode == TreeModelMapper.SOURCE) {
215                 if (ICONS[0] != null) {
216                     setIcon(ICONS[0]);
217                     if (object instanceof RtSingleValuePort) {
218                         addtnl_text = "1-ary port belonging to " + ((RtSingleValuePort) object).getContainingElement().getQualifiedName();
219                     } else if (object instanceof RtCollectionPort) {
220                         addtnl_text = "n-ary port belonging to " + ((RtCollectionPort) object).getContainingElement().getQualifiedName();
221                     } else {
222                         addtnl_text = "provider belonging to " + ((RtComponentProvider) object).getContainingElement().getQualifiedName();
223                     }
224                     setToolTipText(addtnl_text);
225                 }
226             } else {
227                 if (ICONS[1] != null) {
228                     setIcon(ICONS[1]);
229                     addtnl_text = "transformer belonging to " + ((RtComponentProvider) object).getContainingElement().getQualifiedName();
230                     setToolTipText(addtnl_text);
231                 }
232             }
233                         
234             switch(kind) {
235                 case KILIM.GETTER :
236                     TpGetter gDesc = (TpGetter) desc;
237                     String JavaDoc fName = gDesc.getFieldName();
238                     setText("<" + seqNumber + " > " + "getter of field " + fName);
239                     break;
240                 case KILIM.CONSTRUCTOR :
241                     TpConstructor nDesc = (TpConstructor) desc;
242                     setText("<" + seqNumber + " > " + "new ");
243                     break;
244                 case KILIM.METHOD :
245                     TpMethod mDesc = (TpMethod) desc;
246                     String JavaDoc mName = mDesc.getMethodName();
247                     setText("<" + seqNumber + " > " + "method " + mName);
248                     break;
249                 case KILIM.CLASS :
250                     ClassSource cDesc = (ClassSource) desc;
251                     String JavaDoc cName = cDesc.getClassName();
252                     setText("<" + seqNumber + " > " + "class " + cName);
253                     break;
254                 case KILIM.NULL_ELEMENT :
255                     setText("<" + seqNumber + " > " + "null element");
256                     break;
257                 case KILIM.REFERENCE :
258                     setText("<" + seqNumber + " > " + "reference ");
259                     break;
260                 default :
261                     break;
262             }
263             
264             return this;
265         }
266         
267         //TpSetter sDesc = (TpSetter) desc;
268
//String fName = sDesc.getFieldName();
269
//setText("getter of field " + fName);
270
setIcon(ICONS[4]);
271         setText("<" + seqNumber + " > " + "undefined element of class " + object.getClass().getName());
272         return this;
273     }
274
275     private static ImageIcon JavaDoc getIcon(String JavaDoc name) {
276         URL JavaDoc url = ClassLoader.getSystemClassLoader().getSystemResource(name);
277         if (url != null) {
278             Image JavaDoc img = Toolkit.getDefaultToolkit().getImage(url);
279             if (img != null) {
280                 return new ImageIcon JavaDoc(img);
281             } else {
282                 return null;
283             }
284         } else {
285             return null;
286         }
287     }
288 }
289
Popular Tags