KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > viewer > Diagram


1 package org.coach.tracing.server.viewer;
2
3 import java.awt.print.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import org.coach.tracing.server.EventRecord;
9 import org.coach.tracing.server.EventDataBase;
10 import org.coach.tracing.api.*; // from tracing.idl
11

12 public class Diagram extends JPanel implements Printable {
13     private TraceViewerFrame frame;
14     private static Set trailLabels = new HashSet();
15     
16     //the space between the first message and the top of the diagram
17
public final static int lead_top = 10;
18
19     //the space between the last message and the bottom of the diagram
20
public final static int lead_bottom = 100;
21
22     //the space between two neighbour messages
23
public final static int space_between = 20;
24     private static ScaleManager sm;
25     private static LinkedList eventList = new LinkedList();
26     private static HashMap eventMap = new HashMap();
27     private static HashMap eventGuiMap = new HashMap();
28     private static HashMap messageGuiMap = new HashMap();
29     private static HashMap identityGuiMap = new HashMap();
30     private static EventDataBase eventDB;
31     private HashMap colorMap = new HashMap();
32     private String JavaDoc causalityTrailId;
33     private long currentIndex = 0;
34     private long range = 0;
35     private EventGui eventGuiPool;
36     private TrailLabelData[] trailLabelData;
37     private long eventCount = 0;
38     private boolean threadMode = false;
39
40     public Diagram(TraceViewerFrame f) {
41         sm = ScaleManager.getScaleManager();
42         frame = f;
43         setDoubleBuffered(true);
44         setPreferredSize(new Dimension(0, 0));
45         setMinimumSize(new Dimension(0, 0));
46         setLayout(null);
47         eventGuiPool = new EventGui(this);
48     }
49     
50     void setEventDB(EventDataBase db) {
51         clear();
52         eventDB = db;
53         updateStatus(0);
54         repaint();
55     }
56
57     public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
58         return 0;
59     }
60         
61     public void setRange(long range) {
62         this.range = range;
63         paintEvents(currentIndex);
64     }
65     
66     public void setThreadViewMode(boolean mode) {
67         threadMode = mode;
68         repaintEvents();
69     }
70             
71     public static ScaleManager getScaleManager() {
72         return sm;
73     }
74     
75     private void addMessage(EventRecord mm) {
76         EventGui e1 = (EventGui)eventGuiMap.get(mm.key());
77         EventGui e2 = (EventGui)eventGuiMap.get(mm.getLinkKey());
78
79         if (threadMode) {
80             
81             ThreadGui threadGui = (ThreadGui)identityGuiMap.get(ThreadNode.get(IdentityNode.getProcessId(mm.getIdentityKey()), mm.getThreadId()).getVisible());
82             if(e1 != null && e2 != null) {
83                 MessageGui mmg = new MessageGui(this, e1, e2, threadGui.getWidth());
84                 messageGuiMap.put(mm.key(), mmg);
85                 add(mmg, null, 0);
86             }
87         } else {
88             IdentityGui identityGui = (IdentityGui)identityGuiMap.get(IdentityNode.get(mm.getIdentityKey()).getVisible());
89             if(e1 != null && e2 != null) {
90                 MessageGui mmg = new MessageGui(this, e1, e2, identityGui.getWidth());
91                 messageGuiMap.put(mm.key(), mmg);
92                 add(mmg, null, 0);
93             }
94         }
95     }
96
97     private void addEvent(EventRecord r, int position) {
98         if (threadMode) {
99             String JavaDoc processId = IdentityNode.getProcessId(r.getIdentityKey());
100             ThreadNode v = ThreadNode.get(processId, r.getThreadId()).getVisible();
101             if (v != null) {
102                 // This event is on a visible thread.
103
ThreadGui threadGui = (ThreadGui)identityGuiMap.get(v);
104                 if (threadGui == null) {
105                     throw new RuntimeException JavaDoc("ThreadGui is missing!");
106                 }
107                         
108                 EventGui eg = eventGuiPool.getEventGui(r);
109                 eventGuiMap.put(r.key(), eg);
110                 eg.setPosition(position);
111                 int x = threadGui.getXoffset() - (eg.getWidth() / 2);
112                 int y = (int)((lead_top + (space_between * position)) * sm.getScale());
113                 eg.setLocation(x, y);
114                 add(eg, null, 0);
115             }
116         } else {
117             IdentityNode v = IdentityNode.get(r.getIdentityKey()).getVisible();
118             if (v != null) {
119                 // This event is on a visible identity.
120
IdentityGui identityGui = (IdentityGui)identityGuiMap.get(v);
121                 if (identityGui == null) {
122                     throw new RuntimeException JavaDoc("IdentityGui is missing!");
123                 }
124                         
125                 EventGui eg = eventGuiPool.getEventGui(r);
126                 eventGuiMap.put(r.key(), eg);
127                 eg.setPosition(position);
128                 int x = identityGui.getXoffset() - (eg.getWidth() / 2);
129                 int y = (int)((lead_top + (space_between * position)) * sm.getScale());
130                 eg.setLocation(x, y);
131                 add(eg, null, 0);
132             }
133         }
134         if (!r.getTrailLabel().equals("")) {
135             trailLabels.add(r.getTrailLabel());
136         }
137     }
138     
139     public synchronized void clear() {
140         removeAll();
141         eventGuiMap.clear();
142         messageGuiMap.clear();
143         colorMap.clear();
144         identityGuiMap.clear();
145         currentIndex = 0;
146         range = 0;
147         eventCount = 0;
148     }
149
150     public void print(Graphics g) {
151         Component clist[] = getComponents();
152         int ncomponents = clist.length;
153         Rectangle clip = g.getClipBounds();
154         for (int i = ncomponents - 1; i >= 0; i--) {
155             Component comp = clist[i];
156             if (comp != null) {
157                 Rectangle cr = comp.getBounds();
158                 if ((clip == null) || cr.intersects(clip)) {
159                     Graphics cg = g.create(cr.x, cr.y, cr.width, cr.height);
160                     cg.setFont(comp.getFont());
161                     try {
162                         comp.paint(cg);
163                     } finally {
164                         cg.dispose();
165                     }
166                 }
167             }
168         }
169     }
170     
171     public void createEventBrowser(Long JavaDoc key, Point p) {
172         try {
173             frame.createEventBrowser((EventRecord)eventMap.get(key), p);
174         } catch (Exception JavaDoc e) {
175             e.printStackTrace();
176         }
177     }
178
179     public EventGui getEventGui(Long JavaDoc key) {
180         return (EventGui)eventGuiMap.get(key);
181     }
182
183     public MessageGui getMessageGui(Long JavaDoc key) {
184         return (MessageGui)messageGuiMap.get(key);
185     }
186
187     public synchronized void colour_causality_chain(Long JavaDoc key) {
188         try {
189             EventRecord r = (EventRecord)eventMap.get(key);
190             causalityTrailId = r.getTrailId();
191             
192             Iterator it = eventList.iterator();
193             while(it.hasNext()) {
194                 r = (EventRecord)it.next();
195                 if (r.getTrailId().equals(causalityTrailId)) {
196                     //colour the event
197
colorMap.put(r.key(), java.awt.Color.magenta);
198                 }
199             }
200             repaintEvents();
201         } catch (Exception JavaDoc e) {
202             e.printStackTrace();
203         }
204     }
205     
206     public void uncolour() {
207         colorMap.clear();
208         causalityTrailId = null;
209         if (trailLabelData != null) {
210             for (int i = 0; i < trailLabelData.length; i++) {
211                 trailLabelData[i].active = false;
212             }
213         }
214         repaint();
215     }
216     
217     public java.awt.Color JavaDoc getColor(Object JavaDoc obj) {
218         return (java.awt.Color JavaDoc)colorMap.get(obj);
219     }
220
221     public void setTrailLabelData(TrailLabelData[] trailLabelData) {
222         this.trailLabelData = trailLabelData;
223     }
224     
225     public TrailLabelData[] getTrailLabelData() {
226         return trailLabelData;
227     }
228
229     public String JavaDoc[] getTrailLabels() {
230         String JavaDoc[] labels = new String JavaDoc[trailLabels.size()];
231         trailLabels.toArray(labels);
232         return labels;
233     }
234                 
235     public void colorTrail() {
236         try {
237             if (trailLabelData != null) {
238                 Iterator it = eventGuiMap.keySet().iterator();
239                 while(it.hasNext()) {
240                     Long JavaDoc key = (Long JavaDoc)it.next();
241                     EventRecord r = (EventRecord)eventMap.get(key);
242                     for (int i = 0; i < trailLabelData.length; i++) {
243                         if (r.getTrailLabel().equals(trailLabelData[i].name) && trailLabelData[i].active) {
244                             colorMap.put(r.key(), trailLabelData[i].color);
245                         }
246                     }
247                 }
248             }
249             if (causalityTrailId != null) {
250                 Iterator it = eventList.iterator();
251                 while(it.hasNext()) {
252                     EventRecord r = (EventRecord)it.next();
253                     if (r.getTrailId().equals(causalityTrailId)) {
254                         //colour the event
255
colorMap.put(r.key(), java.awt.Color.magenta);
256                     }
257                 }
258             }
259         } catch (Exception JavaDoc e) {
260             e.printStackTrace();
261         }
262     }
263     
264     public void updateStatus(long events) {
265         eventCount = events;
266         if (frame != null) {
267             long messages = eventDB.getMessageCount();
268             long identities = eventDB.getIdentityCount();
269             frame.updateStatusBar(eventCount, identities, messages);
270         }
271     }
272
273     /**
274      * Clears the display and repaints the events starting with the given index
275      */

276     public void repaintEvents() {
277         paintEvents(currentIndex);
278     }
279     
280     public void paintEvents(long startIndex) {
281         try {
282             if (eventCount == 0) {
283                 // No event has been received yet.
284
return;
285             }
286             currentIndex = startIndex;
287             long endIndex = Math.min(startIndex + range, eventCount);
288     
289             Iterator it = eventGuiMap.values().iterator();
290             while(it.hasNext()) {
291                 EventGui eg = (EventGui)it.next();
292                 eventGuiPool.releaseEventGui(eg);
293             }
294             eventList.clear();
295             eventMap.clear();
296             eventGuiMap.clear();
297             identityGuiMap.clear();
298             messageGuiMap.clear();
299             IdentityGui.reset();
300             ThreadGui.reset();
301             removeAll();
302
303             EventRecord[] events = eventDB.getEvents(startIndex, endIndex);
304             for (int i = 0; i < events.length; i++)
305             {
306                 IdentityNode.get(events[i].getIdentityKey());
307                 String JavaDoc processId = IdentityNode.getProcessId(events[i].getIdentityKey());
308                 ThreadNode.get(processId, events[i].getThreadId());
309                 eventMap.put(events[i].key(), events[i]);
310                 eventList.add(events[i]);
311             }
312             
313
314             // Now create identitylines for all visible identities.
315
int width = 0;
316             if (threadMode) {
317                 ThreadNode[] v = ThreadNode.getVisibles();
318                 for (int i = 0; i < v.length; i++) {
319                     ThreadGui threadGui = (ThreadGui)identityGuiMap.get(v[i]);
320                     if (threadGui == null) {
321                         threadGui = new ThreadGui(this, v[i].getName(), v[i]);
322                         identityGuiMap.put(v[i], threadGui);
323                         add(threadGui);
324                         width += threadGui.getWidth();
325                     }
326                 }
327             } else {
328                 IdentityNode[] v = IdentityNode.getVisibles();
329                 for (int i = 0; i < v.length; i++) {
330                     IdentityGui identityGui = (IdentityGui)identityGuiMap.get(v[i]);
331                     if (identityGui == null) {
332                         identityGui = new IdentityGui(this, v[i].getName(), v[i].getType(), v[i]);
333                         identityGuiMap.put(v[i], identityGui);
334                         add(identityGui);
335                         width += identityGui.getWidth();
336                     }
337                 }
338             }
339             
340             // set the new preferred size
341
setSize(new Dimension(getHeight(), width));
342             setPreferredSize(new Dimension(getHeight(), width));
343                             
344             // Add the event to the display
345
for(int index = 0; index < eventList.size(); index++) {
346                 EventRecord r = (EventRecord)eventList.get(index);
347                 
348                 // Could add a filter here to decide if the event is visible and must be
349
// added. The event position can then be compressed to allow additional events to
350
// be displayed.
351

352                 addEvent(r, (int)(r.getCurrentIndex() - startIndex));
353             }
354
355             // Add the messages to the display
356
for(int index = 0; index < eventList.size(); index++) {
357                 EventRecord r = (EventRecord)eventList.get(index);
358                 
359                 if (r.isLinked() && eventGuiMap.get(r.getLinkKey()) != null && getMessageGui(r.key()) == null && getMessageGui(r.getLinkKey()) == null) {
360                     addMessage(r);
361                 }
362             }
363                                                 
364             colorTrail();
365             
366             repaint();
367         } catch (Exception JavaDoc e) {
368             e.printStackTrace();
369         }
370     }
371 }
372
Popular Tags