1 24 25 package org.objectweb.clif.console.lib.gui; 26 27 import java.awt.BasicStroke ; 28 import java.awt.Color ; 29 import java.awt.Graphics ; 30 import java.awt.Graphics2D ; 31 import java.awt.FontMetrics ; 32 import java.awt.geom.Line2D ; 33 import java.awt.geom.Point2D ; 34 import java.util.Vector ; 35 import javax.swing.JPanel ; 36 37 43 public class Graph extends JPanel 44 { 45 public GraphArea graphArea; 47 public XAxis xAxis; 49 public YAxis yAxis; 51 public Vector hostsToDisplay = new Vector (); 53 public InjectorsGraph[] allHosts; 55 private InjectorsGraph[] oneHost; 57 public int nbElements; 59 public int VIEW = 0; 61 public boolean isLimited; 64 private GraphColorChooser colorChooser = new GraphColorChooser(); 66 private Point2D.Double newPoint = null; 67 68 74 public Graph(String [] injectors, int nbElements, boolean isLimited) { 75 76 this.nbElements = nbElements; 77 this.isLimited = isLimited; 78 79 84 allHosts = new InjectorsGraph[injectors.length]; 85 for (int i = 0; i < injectors.length; i++) { 86 allHosts[i] = new InjectorsGraph(nbElements, isLimited); 87 allHosts[i].name = injectors[i]; 88 allHosts[i].color = colorChooser.getNextColor(); 89 } 93 94 setLayout(null); 95 96 graphArea = new GraphArea(isLimited); 97 xAxis = new XAxis(graphArea); 98 yAxis = new YAxis(graphArea); 99 100 yAxis.setSize(YAxis.width, 300); 102 xAxis.setSize(100, 20); 103 yAxis.setLocation(0, 0); 104 xAxis.setLocation(YAxis.width, 280); 105 graphArea.setLocation(YAxis.width, 0); 106 107 add(yAxis); 108 add(xAxis); 109 add(graphArea); 110 doLayout(); 111 } 112 113 118 public void setView(int view) { 119 this.VIEW = view; 120 graphArea.resetScale(); 121 } 122 123 124 131 public void addPoint(String injector, int type, int time, long value) { 132 133 newPoint = new Point2D.Double (time, value); 134 135 for (int i = 0; i < allHosts.length; i++) { 137 if (allHosts[i].name.equals(injector)) { 138 allHosts[i].addPoint(newPoint, type); 139 break; 140 } 141 } 142 } 143 144 148 public void updateXAxis(int totalTime) { 149 graphArea.xMax = totalTime; 150 } 151 152 156 public void updateGraph() 157 { 158 graphArea.calculateScale(hostsToDisplay, allHosts); 159 yAxis.repaint(); 160 xAxis.repaint(); 161 graphArea.repaint(); 162 } 163 164 168 public void addPointsOnDisplay(String injector) { 169 170 for (int i = 0; i < allHosts.length; i++) { 172 if (allHosts[i].name.equals(injector)) { 173 oneHost = new InjectorsGraph[1]; 175 oneHost[0] = new InjectorsGraph(1, isLimited); 176 oneHost[0].name = injector; 177 oneHost[0].color = allHosts[i].color; 178 oneHost[0].points[0] = allHosts[i].points[VIEW]; 179 if (isLimited) 180 oneHost[0].maxElements = allHosts[i].getMaxElements(); 181 hostsToDisplay.addElement(oneHost); 182 break; 183 } 184 } 185 } 186 187 191 public void addAllPointsOnDisplay(Object [] injectors) { 192 193 for (int i = 0; i < injectors.length; i++) { 194 addPointsOnDisplay((String ) injectors[i]); 195 } 196 } 197 198 202 public void removePointsFromDisplay(String injector) { 203 204 for (int i = 0; i < hostsToDisplay.size(); i++) { 206 oneHost = (InjectorsGraph[]) hostsToDisplay.elementAt(i); 207 if (oneHost[0].name.equals(injector)) { 208 hostsToDisplay.removeElementAt(i); 209 break; 210 } 211 } 212 } 213 214 218 public void removeAllPointsFromDisplay(Object [] injectors) { 219 220 for (int i = 0; i < injectors.length; i++) { 221 removePointsFromDisplay((String ) injectors[i]); 222 } 223 } 224 225 public void paintComponent(Graphics g) { 226 yAxis.setSize(YAxis.width, getHeight()); 227 xAxis.setSize(getWidth(), 20); 228 xAxis.setLocation(YAxis.width, getHeight() - 20); 229 graphArea.setSize(getWidth() - YAxis.width, getHeight() - 20); 230 updateGraph(); 231 } 232 233 237 public void addInjector(String name) { 238 InjectorsGraph[] _allInjectors = allHosts; 239 int _size = allHosts.length; 240 241 allHosts = new InjectorsGraph[_size + 1]; 243 for (int i = 0; i < _allInjectors.length; i++) 244 { 245 allHosts[i] = new InjectorsGraph(nbElements, isLimited); 246 allHosts[i].name = _allInjectors[i].name; 247 allHosts[i].color = _allInjectors[i].color; 248 } 249 allHosts[_size] = new InjectorsGraph(nbElements, isLimited); 251 allHosts[_size].name = name; 252 allHosts[_size].color = colorChooser.getNextColor(); 253 } 254 255 258 public void clear() { 259 for (int i = 0; i < allHosts.length; i++) { 260 allHosts[i].clearAllPoints(); 261 } 262 updateXAxis(0); 263 updateGraph(); 264 } 265 266 271 public void setNbPoints(int nbPoints) { 272 for (int i = 0; i < allHosts.length; i++) { 273 allHosts[i].setMaxElements(nbPoints); 274 } 275 updateGraph(); 276 } 277 278 279 public int getGraphAreaWidth() 280 { 281 return graphArea.getWidth(); 282 } 283 } 284 285 288 class XAxis extends JPanel 289 { 290 private GraphArea graphArea; 291 private FontMetrics fontMet; 292 293 public XAxis(GraphArea graph) 294 { 295 this.graphArea = graph; 296 fontMet = getFontMetrics(getFont()); 297 } 298 299 public void paint(Graphics g) 300 { 301 g.clearRect(0, 0, getWidth(), getHeight()); 302 g.drawLine(0, 1, getWidth(), 1); 303 String time = String.valueOf(graphArea.xMax); 304 g.drawString( 305 time, 306 getWidth() - fontMet.stringWidth(time) - YAxis.width, 307 fontMet.getHeight()); 308 } 309 } 310 311 314 class YAxis extends JPanel 315 { 316 static final int width = 90; 317 private GraphArea graphArea; 318 private FontMetrics fontMet; 319 320 public YAxis(GraphArea graphArea) 321 { 322 this.graphArea = graphArea; 323 fontMet = getFontMetrics(getFont()); 324 } 325 326 public void paint(Graphics g) 327 { 328 g.clearRect(0, 0, getWidth(), getHeight()); 329 g.drawLine(YAxis.width - 1, 0, YAxis.width - 1, getHeight() - 20); 330 if (graphArea.maxValue > graphArea.minValue) 331 { 332 long middleValue = (graphArea.maxValue + graphArea.minValue) / 2; 333 g.drawString( 335 String.valueOf(graphArea.maxValue), 336 1, 337 fontMet.getAscent()); 338 g.drawString( 340 String.valueOf(middleValue), 341 1, 342 (graphArea.getHeight() - fontMet.getLeading() )/ 2); 343 long quartDiff = (middleValue - graphArea.minValue) / 2; 345 g.drawString( 346 String.valueOf(graphArea.minValue + quartDiff), 347 1, 348 (3*graphArea.getHeight()) / 4); 349 g.drawString( 351 String.valueOf(graphArea.maxValue - quartDiff), 352 1, 353 graphArea.getHeight() / 4); 354 g.drawString( 356 String.valueOf(graphArea.minValue), 357 1, 358 graphArea.getHeight()); 359 } 360 } 361 } 362 363 366 class GraphArea extends JPanel { 367 368 private BasicStroke defaultStroke = new BasicStroke (); 369 private BasicStroke dash; 370 private Point2D.Double first = null; 371 private Point2D.Double last = null; 372 private Point2D.Double firstTransf = null; 373 private Point2D.Double lastTransf = null; 374 protected long yMax = 0; 376 protected long yMin = 0; 377 public int xMax = 0; 378 public double xScale; 380 public double yScale; 381 public boolean isLimited; 382 private Vector injectorsToDisplay = new Vector (); 383 private InjectorsGraph[] oneInjector; 384 protected long maxValue; 385 protected long minValue; 386 387 public GraphArea(boolean isLimited) { 388 this.isLimited = isLimited; 389 xScale = 0; 390 yScale = 0; 391 dash = 392 new BasicStroke ( 393 defaultStroke.getLineWidth(), 394 defaultStroke.getEndCap(), 395 defaultStroke.getLineJoin(), 396 defaultStroke.getMiterLimit(), 397 new float[] { 8, 8 }, 398 0); 399 oneInjector = new InjectorsGraph[1]; 400 oneInjector[0] = new InjectorsGraph(1, isLimited); 401 } 402 403 public void paint(Graphics g) 404 { 405 Graphics2D g2 = (Graphics2D ) g; 406 g2.setStroke(defaultStroke); 407 g2.setBackground(Color.WHITE); 408 409 g2.clearRect(0, 0, getWidth(), getHeight()); 411 412 if (yMax > yMin) 413 { 414 for (int i = 0; i < injectorsToDisplay.size(); i++) { 416 oneInjector = (InjectorsGraph[]) injectorsToDisplay.elementAt(i); 417 g2.setColor(oneInjector[0].color); 418 419 424 for (int j = 0; j < oneInjector[0].points[0].size() - 1; j++) { 425 first = (Point2D.Double ) oneInjector[0].points[0].elementAt(j); 426 last = 427 (Point2D.Double ) oneInjector[0].points[0].elementAt(j + 1); 428 429 firstTransf = (Point2D.Double ) first.clone(); 430 lastTransf = (Point2D.Double ) last.clone(); 431 432 if (!isLimited) { 435 firstTransf.setLocation( 436 first.getX() * xScale, 437 getHeight() - ((first.getY() - yMin) * yScale)); 438 lastTransf.setLocation( 439 last.getX() * xScale, 440 getHeight() - ((last.getY() - yMin) * yScale)); 441 } else { 442 firstTransf.setLocation( 443 j * xScale, 444 getHeight() - ((first.getY() - yMin) * yScale)); 445 lastTransf.setLocation( 446 (j + 1) * xScale, 447 getHeight() - ((last.getY() - yMin) * yScale)); 448 } 449 g2.draw(new Line2D.Double (firstTransf, lastTransf)); 450 } 451 } 452 maxValue = yMax; 454 minValue = yMin; 455 g2.setStroke(dash); 456 g2.setColor(Color.BLACK); 457 g2.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); 458 g2.drawLine(0, getHeight() / 4, getWidth(), getHeight() / 4); 459 g2.drawLine(0, 3 * getHeight() / 4, getWidth(), 3 * getHeight() / 4); 460 } 461 } 462 463 464 467 public void resetScale() 468 { 469 yMax = Integer.MIN_VALUE; 470 yMin = Integer.MAX_VALUE; 471 } 472 473 474 479 public void calculateScale( 480 Vector injectorsToDisplay, 481 InjectorsGraph[] allInjectors) { 482 483 this.injectorsToDisplay = injectorsToDisplay; 484 485 for (int i = 0; i < injectorsToDisplay.size(); i++) { 487 oneInjector = (InjectorsGraph[]) injectorsToDisplay.elementAt(i); 488 489 for (int j = 0; j < oneInjector[0].points[0].size(); j++) 491 { 492 Point2D.Double currPoint = (Point2D.Double ) oneInjector[0].points[0].elementAt(j); 493 if (currPoint.getY() > yMax) 494 { 495 yMax = new Double (currPoint.getY()).longValue(); 496 } 497 if (currPoint.getY() < yMin) 498 { 499 yMin = new Double (currPoint.getY()).longValue(); 500 } 501 if (yMax - yMin < 4) 502 { 503 yMax = yMin + 4; 504 } 505 } 506 } 507 508 if (xMax != 0) { 510 if (isLimited) { 511 xScale = 512 new Double (getWidth()).doubleValue() 513 / (oneInjector[0].getMaxElements() - 1); 514 } else { 515 xScale = 516 new Double (getWidth()).doubleValue() 517 / (new Double (xMax).doubleValue()); 518 } 519 } else 520 xScale = 0; 521 522 yScale = 523 new Double (getHeight()).doubleValue() 524 / new Double (yMax - yMin).doubleValue(); 525 } 526 } 527 | Popular Tags |