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.JTree ; 20 import javax.swing.ToolTipManager ; 21 import javax.swing.tree.DefaultTreeCellRenderer ; 22 import javax.swing.tree.TreeModel ; 23 24 import org.objectweb.kilim.KilimException; 25 import org.objectweb.kilim.model.Component; 26 import org.objectweb.kilim.model.ComponentInterface; 27 import org.objectweb.kilim.model.ComponentSlot; 28 import org.objectweb.kilim.model.RtComponentElement; 29 import org.objectweb.kilim.model.RtCollectionPort; 30 31 32 35 public class KilimComponentViewer { 36 37 private static final String WINDOW_NAME = "Kilim Component Viewer"; 38 39 private static ActionListener action_listener = new ActionListener () { 40 public void actionPerformed(ActionEvent evt) { 41 synchronized (this) { 42 this.notifyAll(); 43 } 44 } 45 }; 46 47 private static JFrame view_frame = new JFrame (); 48 49 static { 50 view_frame.setTitle(WINDOW_NAME); 51 view_frame.addWindowListener(new WindowAdapter (){ 52 public void windowClosing(WindowEvent evt) { 53 synchronized (action_listener) { 54 action_listener.notifyAll(); 55 } 56 } 57 }); 58 } 59 60 private static JTree tree = new JTree (); 61 private static TreeModel treeModel; 62 63 67 public static void viewComponent(Component cmpnnt) { 68 tree.setCellRenderer(new KilimComponentTreeCellRenderer()); 69 70 Container cntnr = view_frame.getContentPane(); 71 cntnr.setLayout(new BorderLayout ()); 72 JScrollPane scrllpn = new JScrollPane (tree); 73 cntnr.add(BorderLayout.CENTER, scrllpn); 74 view_frame.setSize(500, 600); 75 76 ToolTipManager.sharedInstance().registerComponent(tree); 77 78 JButton button = new JButton ("continue"); 79 button.addActionListener(action_listener); 80 cntnr.add(BorderLayout.SOUTH, button); 81 82 TreeModel treeModel = new KilimComponentTreeModel(cmpnnt); 83 tree.setModel(treeModel); 84 tree.repaint(); 85 86 String tmplt_nm = "error"; 87 tmplt_nm = cmpnnt.getTemplate().getName(); 88 89 view_frame.setTitle(WINDOW_NAME + " - " + tmplt_nm); 90 91 view_frame.setVisible(true); 92 93 synchronized (action_listener) { 94 try { 95 action_listener.wait(); 96 } catch (InterruptedException exp) { 97 exp.printStackTrace(); 98 } 99 } 100 } 101 102 105 public static void updateView() { 106 view_frame.validate(); 109 view_frame.repaint(); 110 111 synchronized (action_listener) { 112 try { 113 action_listener.wait(); 114 } catch (InterruptedException exp) { 115 exp.printStackTrace(); 116 } 117 } 118 } 119 } 120 121 class KilimComponentTreeCellRenderer extends DefaultTreeCellRenderer { 122 123 private static final String [] ICON_NAMES = {"provider.gif", "transformer.gif", "template_instance.gif", 124 "binding.gif", "port.gif", "template.gif", "property.gif", 125 "multiPort.gif", "slot.gif"}; 126 127 private static final Icon [] ICONS = new Icon [ICON_NAMES.length]; 128 129 static { 130 for (int i = 0; i < ICON_NAMES.length; i++) { 131 ICONS[i] = getIcon(ICON_NAMES[i]); 132 } 133 } 134 135 138 public java.awt.Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 139 boolean leaf, int row, boolean hasFocus) { 140 141 super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); 142 143 if (value instanceof Component) { 144 if (ICONS[2] != null) { 145 setIcon(ICONS[2]); 146 } 147 setText(((RtComponentElement) value).getLocalName()); 148 149 } else if (value instanceof RtComponentElement) { 150 151 String addtnl_text = ""; 152 153 if (value instanceof ComponentInterface) { 154 155 ComponentInterface cmpnnt_intrf = (ComponentInterface) value; 156 157 if (cmpnnt_intrf.isProperty()) { 158 if (ICONS[6] != null) { 159 setIcon(ICONS[6]); 160 } 161 } else if (cmpnnt_intrf.isProvider()) { 162 if (ICONS[0] != null) { 163 setIcon(ICONS[0]); 164 } 165 } else if (cmpnnt_intrf.isSingleValuePort()) { 166 if (ICONS[4] != null) { 167 setIcon(ICONS[4]); 168 } 169 } else if (cmpnnt_intrf.isCollectionPort()) { 170 if (ICONS[7] != null) { 171 setIcon(ICONS[7]); 172 } 173 } 174 175 try { 176 if (!(value instanceof RtCollectionPort)) { 177 addtnl_text = ((ComponentInterface) value).getValue() + ""; 178 } 179 } catch (KilimException exp) { 180 exp.printStackTrace(); 181 } 182 } 183 184 if (value instanceof ComponentSlot) { 185 if (ICONS[8] != null) { 186 setIcon(ICONS[8]); 187 } 188 } 189 190 setToolTipText(addtnl_text); 191 192 if (addtnl_text.length() > 40) { 193 addtnl_text = addtnl_text.substring(0 , 40) + "..."; 194 } 195 setText(((RtComponentElement) value).getLocalName() + " [" + addtnl_text + "]"); 196 197 } 198 199 200 201 return this; 202 } 203 204 private static ImageIcon getIcon(String name) { 205 URL url = ClassLoader.getSystemClassLoader().getSystemResource(name); 206 if (url != null) { 207 Image img = Toolkit.getDefaultToolkit().getImage(url); 208 if (img != null) { 209 return new ImageIcon (img); 210 } else { 211 return null; 212 } 213 } else { 214 return null; 215 } 216 } 217 } 218 | Popular Tags |