1 package org.objectweb.kilim.tools; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Container ; 5 import java.awt.Image ; 6 import java.awt.Toolkit ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.ActionListener ; 9 import java.net.URL ; 10 11 import java.awt.event.WindowAdapter ; 12 import java.awt.event.WindowEvent ; 13 14 import javax.swing.Icon ; 15 import javax.swing.ImageIcon ; 16 import javax.swing.JButton ; 17 import javax.swing.JFrame ; 18 import javax.swing.JScrollPane ; 19 import javax.swing.JTextArea ; 20 import javax.swing.JTree ; 21 import javax.swing.ToolTipManager ; 22 import javax.swing.tree.DefaultTreeCellRenderer ; 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 40 public class KilimTraceTreeViewer { 41 42 private static final String WINDOW_NAME = "Kilim Trace Viewer"; 43 44 private static ActionListener action_listener = new ActionListener () { 45 public void actionPerformed(ActionEvent evt) { 46 synchronized (this) { 47 this.notifyAll(); 48 } 49 } 50 }; 51 52 private static JFrame tree_frame = new JFrame (); 53 private static JFrame text_frame = new JFrame (); 54 private static JTextArea textArea; 55 56 static { 57 tree_frame.setTitle(WINDOW_NAME); 58 tree_frame.addWindowListener(new WindowAdapter (){ 59 public void windowClosing(WindowEvent 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 (){ 68 public void windowClosing(WindowEvent evt) { 69 synchronized (action_listener) { 70 action_listener.notifyAll(); 71 } 72 } 73 }); 74 } 75 76 private static JTree tree = new JTree (); 77 private static KilimTraceTreeModel treeModel; 78 79 84 public static void viewTrace(KilimTraceTreeModel aModel, JTextArea aTextArea) { 85 textArea = aTextArea; 86 tree.setCellRenderer(new KilimTraceTreeCellRenderer()); 87 Container cntnr = tree_frame.getContentPane(); 88 cntnr.setLayout(new BorderLayout ()); 89 JScrollPane scrllpn = new JScrollPane (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 button = new JButton ("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 textCont = text_frame.getContentPane(); 103 textCont.setLayout(new BorderLayout ()); 104 JScrollPane textSPane = new JScrollPane (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 exp) { 118 exp.printStackTrace(); 119 } 120 } 121 } 122 123 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 exp) { 138 exp.printStackTrace(); 139 } 140 } 141 } 142 } 143 144 class KilimTraceTreeCellRenderer extends DefaultTreeCellRenderer { 145 146 private static final String TRACE_START_ICON_NAME = "start.gif"; 147 private static Icon TRACE_START_ICON = null; 148 149 private static final String [] ICON_NAMES = { "provider.gif" , "transformer.gif" , "property.gif" , "trigger.gif", "external.gif" }; 150 private static Icon [] ICONS = new Icon [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 ex) { 159 System.err.println("problem when getting the start icon "); 160 } 161 } 162 163 166 public java.awt.Component getTreeCellRendererComponent(JTree tree, Object 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 ) { 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 object = node.getValue(); 183 if (object instanceof String ) { 184 setText("+++++++++++++START TRACE+++++++++++++"); 185 return this; 186 } 187 188 int seqNumber = node.getSeqNumber(); 189 int mode = node.getMode(); 190 String 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 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 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 mName = mDesc.getMethodName(); 247 setText("<" + seqNumber + " > " + "method " + mName); 248 break; 249 case KILIM.CLASS : 250 ClassSource cDesc = (ClassSource) desc; 251 String 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 setIcon(ICONS[4]); 271 setText("<" + seqNumber + " > " + "undefined element of class " + object.getClass().getName()); 272 return this; 273 } 274 275 private static ImageIcon getIcon(String name) { 276 URL url = ClassLoader.getSystemClassLoader().getSystemResource(name); 277 if (url != null) { 278 Image img = Toolkit.getDefaultToolkit().getImage(url); 279 if (img != null) { 280 return new ImageIcon (img); 281 } else { 282 return null; 283 } 284 } else { 285 return null; 286 } 287 } 288 } 289 | Popular Tags |