KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > ActiveObjectCommunicationRecorder


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.gui;
32
33 import org.objectweb.proactive.ic2d.gui.data.ActiveObjectPanel;
34 import org.objectweb.proactive.ic2d.data.ActiveObject;
35
36 public class ActiveObjectCommunicationRecorder {
37
38   public static final int PROPORTIONAL_DRAWING_STYLE = 1;
39   public static final int RATIO_DRAWING_STYLE = 2;
40   public static final int FILAIRE_DRAWING_STYLE = 3;
41
42   private static final java.awt.Color JavaDoc STROKE_COLOR = new java.awt.Color JavaDoc(0, 0, 0, 125);
43   private static final int MAX_STROKE_WIDTH_RATIO = 12;
44   private static final int MAX_STROKE_WIDTH_PROPORTIONAL = 80;
45
46   private java.util.HashMap JavaDoc panelToPanelsMap;
47   private java.util.HashMap JavaDoc activeObjectToPanelMap;
48   private int maxCommunicationCounter = 1;
49   private int drawingStyle;
50   private boolean enabled;
51
52   //
53
// -- CONSTRUCTORS -----------------------------------------------
54
//
55

56   public ActiveObjectCommunicationRecorder() {
57     panelToPanelsMap = new java.util.HashMap JavaDoc();
58     activeObjectToPanelMap = new java.util.HashMap JavaDoc();
59     enabled = true;
60     drawingStyle = FILAIRE_DRAWING_STYLE;
61   }
62   
63
64
65   //
66
// -- PUBLICS METHODS -----------------------------------------------
67
//
68

69   public void recordCommunication(ActiveObjectPanel source, ActiveObjectPanel dest) {
70     //System.out.println("ActiveObjectCommunicationRecorder.recordCommunication e="+enabled+" s="+source.isDestroyed()+" d="+dest.isDestroyed());
71
if (! enabled) return;
72     if (source.isDestroyed() || dest.isDestroyed()) return;
73     // try to find a mapping source <-> dest
74
java.util.HashMap JavaDoc destMap = (java.util.HashMap JavaDoc) panelToPanelsMap.get(source);
75 /* if (destMap == null) { // leads to weird results???
76       // try to find a mapping dest <-> source
77       destMap = (java.util.HashMap) panelToPanelsMap.get(dest);
78       if (destMap != null) {
79         // if we did find a mapping dest <-> source we swap source and dest
80         ActiveObjectPanel temp = source;
81         source = dest;
82         dest = temp;
83       }
84     } */

85     if (destMap == null) {
86       // new association source <-> dest
87
destMap = new java.util.HashMap JavaDoc();
88       destMap.put(dest, new int[] {1});
89       synchronized(panelToPanelsMap) {
90         panelToPanelsMap.put(source, destMap);
91         activeObjectToPanelMap.put(source.getActiveObject(),source);
92       }
93     } else {
94       // existing source
95
int[] existingCounter = (int[]) destMap.get(dest);
96       if (existingCounter == null) {
97         // new destination
98
synchronized(destMap) {
99           destMap.put(dest, new int[] {1});
100         }
101         synchronized(panelToPanelsMap) {
102           activeObjectToPanelMap.put(dest.getActiveObject(),dest);
103         }
104       } else {
105         // existing destination
106
existingCounter[0]++;
107         if (existingCounter[0] > maxCommunicationCounter) {
108           maxCommunicationCounter = existingCounter[0];
109         }
110       }
111     }
112   }
113   
114   
115   public void removeActiveObjectPanel(ActiveObjectPanel object) {
116     //System.out.println("ActiveObjectCommunicationRecorder.removeActiveObjectPanel object="+object);
117
synchronized(panelToPanelsMap) {
118       // remove the target as a source
119
panelToPanelsMap.remove(object);
120       // remove the mapping ActiveObject - Panel
121
activeObjectToPanelMap.remove(object.getActiveObject());
122       // remove the target as a destination
123
java.util.Iterator JavaDoc iterator = panelToPanelsMap.values().iterator();
124       while (iterator.hasNext()) {
125         java.util.HashMap JavaDoc destMap = (java.util.HashMap JavaDoc) iterator.next();
126         synchronized(destMap) {
127           destMap.remove(object);
128         }
129       }
130     }
131   }
132   
133   
134   public void removeActiveObject(ActiveObject object) {
135     //System.out.println("ActiveObjectCommunicationRecorder.removeActiveObject object="+object);
136
synchronized(panelToPanelsMap) {
137       ActiveObjectPanel panel = (ActiveObjectPanel) activeObjectToPanelMap.get(object);
138       if (panel == null) {
139         // cannot find a panel mapping this ActiveObject
140
} else {
141         removeActiveObjectPanel(panel);
142       }
143     }
144   }
145   
146   
147   public void setDrawingStyle(int drawingStyle) {
148     switch (drawingStyle) {
149       case PROPORTIONAL_DRAWING_STYLE:
150         this.drawingStyle = drawingStyle;
151         break;
152       case RATIO_DRAWING_STYLE:
153         this.drawingStyle = drawingStyle;
154         break;
155       case FILAIRE_DRAWING_STYLE:
156         this.drawingStyle = drawingStyle;
157         break;
158       default:
159         throw new IllegalArgumentException JavaDoc("The number passed is not a known drawing style");
160     }
161   }
162   
163   
164   public int getDrawingStyle() {
165     return drawingStyle;
166   }
167   
168   
169   public void clear() {
170     //System.out.println("ActiveObjectCommunicationRecorder.clear");
171
synchronized(panelToPanelsMap) {
172       maxCommunicationCounter = 1;
173       panelToPanelsMap.clear();
174       activeObjectToPanelMap.clear();
175     }
176   }
177   
178   public void setEnabled(boolean b) {
179     if (b == enabled) return;
180     enabled = b;
181     if (! enabled) {
182       clear();
183     }
184   }
185   
186   public boolean isEnabled() {
187     return enabled;
188   }
189   
190   public int getMaxCommunicationCounter() {
191     return maxCommunicationCounter;
192   }
193   
194   
195   public java.util.Iterator JavaDoc iterator() {
196     return new SourceIterator();
197   }
198
199
200   public void drawAllLinks(java.awt.Graphics JavaDoc g, java.awt.Point JavaDoc topLeftCornerScreenCoordinate) {
201     //System.out.println("ActiveObjectCommunicationRecorder.drawAllLinks");
202
java.awt.Graphics2D JavaDoc g2 = (java.awt.Graphics2D JavaDoc) g;
203     java.awt.Stroke JavaDoc oldStroke = g2.getStroke();
204     float ratio = 0;
205     switch (drawingStyle) {
206       case PROPORTIONAL_DRAWING_STYLE:
207         // proportional link : we draw has tick as the number of communication registered
208
// up the the max tickness
209
if (maxCommunicationCounter > MAX_STROKE_WIDTH_PROPORTIONAL) {
210           ratio = ((float)MAX_STROKE_WIDTH_PROPORTIONAL) / maxCommunicationCounter;
211         } else {
212           ratio = 1;
213         }
214         break;
215       case RATIO_DRAWING_STYLE:
216         // the more numerous communication is the max tick. The others are smaller
217
ratio = ((float)MAX_STROKE_WIDTH_RATIO) / maxCommunicationCounter;
218         break;
219       case FILAIRE_DRAWING_STYLE:
220       default:
221         // just draw 1 pixel tick line
222
ratio = -1;
223         break;
224     }
225     //System.out.println("ratio = "+ratio+" proportionalLinks="+proportionalLinks+" maxCommunicationCounter="+maxCommunicationCounter);
226
java.util.Iterator JavaDoc sourceEntryIterator = panelToPanelsMap.entrySet().iterator();
227     
228     // iterating on sources
229
synchronized (panelToPanelsMap) {
230       while (sourceEntryIterator.hasNext()) {
231         java.util.Map.Entry sourceEntry = (java.util.Map.Entry) sourceEntryIterator.next();
232         // source
233
ActiveObjectPanel sourcePanel = (ActiveObjectPanel) sourceEntry.getKey();
234         if (sourcePanel.isDestroyed() || ! sourcePanel.isVisible()) continue;
235         java.util.HashMap JavaDoc destMap = (java.util.HashMap JavaDoc) sourceEntry.getValue();
236         java.util.Iterator JavaDoc destEntryIterator = destMap.entrySet().iterator();
237         synchronized(destMap) {
238           drawOneSourceLinks(destEntryIterator, g2, topLeftCornerScreenCoordinate, oldStroke, ratio, sourcePanel);
239         }
240       } // end while
241
}
242   } // end method
243

244   
245
246
247   //
248
// -- PRIVATE METHODS -----------------------------------------------
249
//
250

251   private void drawOneSourceLinks(java.util.Iterator JavaDoc destEntryIterator, java.awt.Graphics2D JavaDoc g2, java.awt.Point JavaDoc topLeftCornerScreenCoordinate, java.awt.Stroke JavaDoc oldStroke,
252                                   float ratio, ActiveObjectPanel sourcePanel) {
253     // iterating on destinations
254
java.awt.Point JavaDoc pSource = sourcePanel.getLocationOnScreen();
255     int xSource = pSource.x - topLeftCornerScreenCoordinate.x;
256     int ySource = pSource.y - topLeftCornerScreenCoordinate.y;
257     int sourceWidth = sourcePanel.getWidth();
258     while (destEntryIterator.hasNext()) {
259       java.util.Map.Entry destEntry = (java.util.Map.Entry) destEntryIterator.next();
260       // destination
261
ActiveObjectPanel destPanel =(ActiveObjectPanel) destEntry.getKey();
262       if (destPanel.isDestroyed() || ! destPanel.isVisible()) continue;
263       int communicationCount = ((int[]) destEntry.getValue())[0];
264       java.awt.Point JavaDoc pDest = destPanel.getLocationOnScreen();
265       int xDest = pDest.x - topLeftCornerScreenCoordinate.x;
266       int yDest = pDest.y - topLeftCornerScreenCoordinate.y;
267       int destWidth = destPanel.getWidth();
268       // drawing line
269
g2.setPaint(STROKE_COLOR);
270       float strokeWidth;
271       if (ratio == -1) strokeWidth = 1.5f;
272       else strokeWidth = communicationCount * ratio + 1;
273       g2.setStroke(new java.awt.BasicStroke JavaDoc(strokeWidth));
274       boolean sameNode = sourcePanel.getActiveObject().isInsideSameNode(destPanel.getActiveObject());
275       if (sameNode) {
276         drawOneArcSameNode(xSource, ySource, sourceWidth, xDest, yDest, destWidth, g2);
277       } else {
278         drawOneArcDifferentNode(xSource, ySource, sourceWidth, xDest, yDest, destWidth, g2);
279       }
280       // drawing dot
281
if (sameNode) {
282         drawCommunicationPointSameNode(xSource, ySource, sourceWidth, xDest, yDest, destWidth, g2);
283       } else {
284         drawCommunicationPointDifferentNode(xSource, ySource, sourceWidth, xDest, yDest, destWidth, g2);
285       }
286       g2.setStroke(oldStroke);
287       g2.setPaint(java.awt.Color.black);
288     } // end while
289
}
290   
291   private void drawArrowHead(int xSource, int ySource, int xDest, int yDest, java.awt.Graphics2D JavaDoc g2) {
292     double angle;
293     if (xSource == xDest) {
294       angle = Math.PI / 2;
295     } else {
296       angle = Math.atan((yDest-ySource)/((double)xDest-xSource));
297     }
298     if (xDest < xSource) angle += Math.PI;
299     g2.drawLine(xDest, yDest, xDest-(int)(Math.cos(angle-Math.PI/4) * 6),
300                               yDest-(int)(Math.sin(angle-Math.PI/4) * 6));
301     g2.drawLine(xDest, yDest, xDest-(int)(Math.cos(angle+Math.PI/4) * 6),
302                               yDest-(int)(Math.sin(angle+Math.PI/4) * 6));
303     g2.drawLine(xDest-(int)(Math.cos(angle+Math.PI/4) * 6),
304                 yDest-(int)(Math.sin(angle+Math.PI/4) * 6),
305                 xDest-(int)(Math.cos(angle-Math.PI/4) * 6),
306                 yDest-(int)(Math.sin(angle-Math.PI/4) * 6));
307   }
308   
309   
310   private void drawCommunicationPointDifferentNode(int xSource, int ySource, int sourceWidth, int xDest, int yDest, int destWidth, java.awt.Graphics2D JavaDoc g2) {
311     // Look for the good corner to join...
312
if (Math.abs(xSource - xDest) > Math.abs(xSource + sourceWidth - xDest)) {
313       xSource += sourceWidth;
314     }
315     if (Math.abs(xDest - xSource) > Math.abs(xDest + destWidth - xSource)) {
316       xDest += destWidth;
317     }
318 // g2.fillOval(xDest - 4, yDest + 9, 8, 8); // xySource before, but that was obviously wrong
319
drawArrowHead(xSource, ySource+13, xDest, yDest+13, g2);
320   }
321
322   private void drawCommunicationPointSameNode(int xSource, int ySource, int sourceWidth, int xDest, int yDest, int destWidth, java.awt.Graphics2D JavaDoc g2) {
323     // draw a little black circle meaning : com point
324
if (ySource > yDest) {
325 // g2.fillOval(xDest - 4 + destWidth, yDest + 9, 8, 8);
326
drawArrowHead(xDest + destWidth + 100, yDest+13, xDest + destWidth, yDest+13, g2);
327     } else {
328 // g2.fillOval(xDest - 4, yDest + 9, 8, 8);
329
drawArrowHead(xDest - 100, yDest+13, xDest, yDest+13, g2);
330     }
331   }
332
333   private void drawOneArcDifferentNode(int xSource, int ySource, int sourceWidth, int xDest, int yDest, int destWidth, java.awt.Graphics2D JavaDoc g2) {
334     // Look for the good corner to join...
335
if (Math.abs(xSource - xDest) > Math.abs(xSource + sourceWidth - xDest)) {
336       xSource += sourceWidth;
337     }
338     if (Math.abs(xDest - xSource) > Math.abs(xDest + destWidth - xSource)) {
339       xDest += destWidth;
340     }
341     g2.drawLine(xSource, ySource + 13, xDest, yDest + 13);
342   }
343
344   private void drawOneArcSameNode(int xSource, int ySource, int sourceWidth, int xDest, int yDest, int destWidth, java.awt.Graphics2D JavaDoc g2) {
345     //Shape changing...
346
int shape = Math.abs(ySource - yDest) / 3;
347     if (ySource > yDest) {
348       g2.drawArc(xSource - shape + sourceWidth, yDest + 13, shape * 2, Math.abs(ySource - yDest), 90, -180);
349     } else {
350       g2.drawArc(xSource - shape, ySource + 13, shape * 2, Math.abs(ySource - yDest), 90, 180);
351     }
352   }
353
354   //
355
// -- INNER CLASSES -----------------------------------------------
356
//
357

358   private class SourceIterator implements java.util.Iterator JavaDoc {
359   
360     private java.util.Iterator JavaDoc myIterator;
361     
362     public SourceIterator() {
363       myIterator = panelToPanelsMap.values().iterator();
364     }
365     
366     public boolean hasNext() {
367       return myIterator.hasNext();
368     }
369     
370     public Object JavaDoc next() {
371       return ((java.util.HashMap JavaDoc) myIterator.next()).values().iterator();
372     }
373     
374     public void remove() {
375       throw new UnsupportedOperationException JavaDoc();
376     }
377   } // end inner class SourceIterator
378

379 }
Popular Tags