KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.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 /**
33  * @author delpiano
34  */

35 public class KilimComponentViewer {
36     
37     private static final String JavaDoc WINDOW_NAME = "Kilim Component Viewer";
38     
39     private static ActionListener JavaDoc action_listener = new ActionListener JavaDoc() {
40         public void actionPerformed(ActionEvent JavaDoc evt) {
41             synchronized (this) {
42                 this.notifyAll();
43             }
44         }
45     };
46     
47     private static JFrame JavaDoc view_frame = new JFrame JavaDoc();
48     
49     static {
50         view_frame.setTitle(WINDOW_NAME);
51         view_frame.addWindowListener(new WindowAdapter JavaDoc(){
52             public void windowClosing(WindowEvent JavaDoc evt) {
53                 synchronized (action_listener) {
54                     action_listener.notifyAll();
55                 }
56             }
57         });
58     }
59     
60     private static JTree JavaDoc tree = new JTree JavaDoc();
61     private static TreeModel JavaDoc treeModel;
62     
63     /**
64      * Method viewComponent.
65      * @param cmpnnt : the component to be displayed.
66      */

67     public static void viewComponent(Component cmpnnt) {
68         tree.setCellRenderer(new KilimComponentTreeCellRenderer());
69         
70         Container JavaDoc cntnr = view_frame.getContentPane();
71         cntnr.setLayout(new BorderLayout JavaDoc());
72         JScrollPane JavaDoc scrllpn = new JScrollPane JavaDoc(tree);
73         cntnr.add(BorderLayout.CENTER, scrllpn);
74         view_frame.setSize(500, 600);
75         
76         ToolTipManager.sharedInstance().registerComponent(tree);
77         
78         JButton JavaDoc button = new JButton JavaDoc("continue");
79         button.addActionListener(action_listener);
80         cntnr.add(BorderLayout.SOUTH, button);
81         
82         TreeModel JavaDoc treeModel = new KilimComponentTreeModel(cmpnnt);
83         tree.setModel(treeModel);
84         tree.repaint();
85         
86         String JavaDoc 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 JavaDoc exp) {
97                 exp.printStackTrace();
98             }
99         }
100     }
101     
102     /**
103      * Method updateView.
104      */

105     public static void updateView() {
106         //tree.setModel(null);
107
//tree.setModel(treeModel);
108
view_frame.validate();
109         view_frame.repaint();
110
111         synchronized (action_listener) {
112             try {
113                 action_listener.wait();
114             } catch (InterruptedException JavaDoc exp) {
115                 exp.printStackTrace();
116             }
117         }
118     }
119 }
120
121 class KilimComponentTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
122
123     private static final String JavaDoc[] 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 JavaDoc[] ICONS = new Icon JavaDoc[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     /**
136      * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean)
137      */

138     public java.awt.Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc 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 JavaDoc 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 JavaDoc getIcon(String JavaDoc name) {
205         URL JavaDoc url = ClassLoader.getSystemClassLoader().getSystemResource(name);
206         if (url != null) {
207             Image JavaDoc img = Toolkit.getDefaultToolkit().getImage(url);
208             if (img != null) {
209                 return new ImageIcon JavaDoc(img);
210             } else {
211                 return null;
212             }
213         } else {
214             return null;
215         }
216     }
217 }
218
Popular Tags