1 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 STROKE_COLOR = new java.awt.Color (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 panelToPanelsMap; 47 private java.util.HashMap activeObjectToPanelMap; 48 private int maxCommunicationCounter = 1; 49 private int drawingStyle; 50 private boolean enabled; 51 52 56 public ActiveObjectCommunicationRecorder() { 57 panelToPanelsMap = new java.util.HashMap (); 58 activeObjectToPanelMap = new java.util.HashMap (); 59 enabled = true; 60 drawingStyle = FILAIRE_DRAWING_STYLE; 61 } 62 63 64 65 69 public void recordCommunication(ActiveObjectPanel source, ActiveObjectPanel dest) { 70 if (! enabled) return; 72 if (source.isDestroyed() || dest.isDestroyed()) return; 73 java.util.HashMap destMap = (java.util.HashMap ) panelToPanelsMap.get(source); 75 85 if (destMap == null) { 86 destMap = new java.util.HashMap (); 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 int[] existingCounter = (int[]) destMap.get(dest); 96 if (existingCounter == null) { 97 synchronized(destMap) { 99 destMap.put(dest, new int[] {1}); 100 } 101 synchronized(panelToPanelsMap) { 102 activeObjectToPanelMap.put(dest.getActiveObject(),dest); 103 } 104 } else { 105 existingCounter[0]++; 107 if (existingCounter[0] > maxCommunicationCounter) { 108 maxCommunicationCounter = existingCounter[0]; 109 } 110 } 111 } 112 } 113 114 115 public void removeActiveObjectPanel(ActiveObjectPanel object) { 116 synchronized(panelToPanelsMap) { 118 panelToPanelsMap.remove(object); 120 activeObjectToPanelMap.remove(object.getActiveObject()); 122 java.util.Iterator iterator = panelToPanelsMap.values().iterator(); 124 while (iterator.hasNext()) { 125 java.util.HashMap destMap = (java.util.HashMap ) iterator.next(); 126 synchronized(destMap) { 127 destMap.remove(object); 128 } 129 } 130 } 131 } 132 133 134 public void removeActiveObject(ActiveObject object) { 135 synchronized(panelToPanelsMap) { 137 ActiveObjectPanel panel = (ActiveObjectPanel) activeObjectToPanelMap.get(object); 138 if (panel == null) { 139 } 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 ("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 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 iterator() { 196 return new SourceIterator(); 197 } 198 199 200 public void drawAllLinks(java.awt.Graphics g, java.awt.Point topLeftCornerScreenCoordinate) { 201 java.awt.Graphics2D g2 = (java.awt.Graphics2D ) g; 203 java.awt.Stroke oldStroke = g2.getStroke(); 204 float ratio = 0; 205 switch (drawingStyle) { 206 case PROPORTIONAL_DRAWING_STYLE: 207 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 ratio = ((float)MAX_STROKE_WIDTH_RATIO) / maxCommunicationCounter; 218 break; 219 case FILAIRE_DRAWING_STYLE: 220 default: 221 ratio = -1; 223 break; 224 } 225 java.util.Iterator sourceEntryIterator = panelToPanelsMap.entrySet().iterator(); 227 228 synchronized (panelToPanelsMap) { 230 while (sourceEntryIterator.hasNext()) { 231 java.util.Map.Entry sourceEntry = (java.util.Map.Entry) sourceEntryIterator.next(); 232 ActiveObjectPanel sourcePanel = (ActiveObjectPanel) sourceEntry.getKey(); 234 if (sourcePanel.isDestroyed() || ! sourcePanel.isVisible()) continue; 235 java.util.HashMap destMap = (java.util.HashMap ) sourceEntry.getValue(); 236 java.util.Iterator destEntryIterator = destMap.entrySet().iterator(); 237 synchronized(destMap) { 238 drawOneSourceLinks(destEntryIterator, g2, topLeftCornerScreenCoordinate, oldStroke, ratio, sourcePanel); 239 } 240 } } 242 } 244 245 246 247 251 private void drawOneSourceLinks(java.util.Iterator destEntryIterator, java.awt.Graphics2D g2, java.awt.Point topLeftCornerScreenCoordinate, java.awt.Stroke oldStroke, 252 float ratio, ActiveObjectPanel sourcePanel) { 253 java.awt.Point 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 ActiveObjectPanel destPanel =(ActiveObjectPanel) destEntry.getKey(); 262 if (destPanel.isDestroyed() || ! destPanel.isVisible()) continue; 263 int communicationCount = ((int[]) destEntry.getValue())[0]; 264 java.awt.Point pDest = destPanel.getLocationOnScreen(); 265 int xDest = pDest.x - topLeftCornerScreenCoordinate.x; 266 int yDest = pDest.y - topLeftCornerScreenCoordinate.y; 267 int destWidth = destPanel.getWidth(); 268 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 (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 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 } } 290 291 private void drawArrowHead(int xSource, int ySource, int xDest, int yDest, java.awt.Graphics2D 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 g2) { 311 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 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 g2) { 323 if (ySource > yDest) { 325 drawArrowHead(xDest + destWidth + 100, yDest+13, xDest + destWidth, yDest+13, g2); 327 } else { 328 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 g2) { 334 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 g2) { 345 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 358 private class SourceIterator implements java.util.Iterator { 359 360 private java.util.Iterator 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 next() { 371 return ((java.util.HashMap ) myIterator.next()).values().iterator(); 372 } 373 374 public void remove() { 375 throw new UnsupportedOperationException (); 376 } 377 } 379 } | Popular Tags |