1 23 package org.objectweb.clif.scenario.util.isac.loadprofile.gui; 24 25 import java.util.Vector ; 26 27 import org.apache.log4j.Category; 28 import org.objectweb.clif.scenario.util.isac.loadprofile.GroupDescription; 29 import org.objectweb.clif.scenario.util.isac.loadprofile.Point; 30 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription; 31 32 40 public class ScaleConvertor { 41 static Category cat = Category.getInstance(ScaleConvertor.class.getName()); 43 44 47 private static final int DEFAULT_NB_THREADS = 1000; 48 private static final int DEFAULT_NB_SEC = 3600; 49 private static final Size DEFAULT_SCALE = new Size(DEFAULT_NB_SEC, 50 DEFAULT_NB_THREADS);; 51 public static final Size DEFAULT_MIN_CANVAS_SIZE = new Size(300, 200); 52 53 56 public static final int MARGIN_TOP = 20; 57 public static final int MARGIN_BACK = 50; 58 public static final int MARGIN_LEFT = 50; 59 public static final int MARGIN_RIGHT = 20; 60 61 public static final int GRAD_SIZE = 5 ; 62 63 66 private static final int ERROR_RATE = DrawableCanvas.SELECTION_SIZE; 67 private static final Point ERROR_POINT = new Point(-1, -1); 68 69 private Size canvasSize; 70 private Point visibleLocation; 71 private Size scale; 72 73 76 public ScaleConvertor() { 77 this.canvasSize = new Size(); 79 this.visibleLocation = new Point(-1, -1); 80 this.scale = DEFAULT_SCALE; 82 } 83 84 88 95 public Point editorPointToRealPoint(Point editor) { 96 int realEditorHeight = this.canvasSize.getHeight() 98 - (MARGIN_BACK + MARGIN_TOP); 99 int realEditorWidth = this.canvasSize.getWidth() 100 - (MARGIN_LEFT + MARGIN_RIGHT); 101 double propHeight = (double) this.scale.getHeight() 103 / (double) realEditorHeight; 104 double propWidth = (double) this.scale.getWidth() 105 / (double) realEditorWidth; 106 Point editorWithCorrectHeightDirectionWithoutMargins = new Point( 108 editor.x - MARGIN_LEFT, canvasSize.getHeight() 109 - (editor.y + MARGIN_BACK)); 110 Point real = new Point( 111 (int) ((double) editorWithCorrectHeightDirectionWithoutMargins.x * propWidth), 112 (int) ((double) editorWithCorrectHeightDirectionWithoutMargins.y * propHeight)); 113 return real; 114 } 115 116 123 public Point realPointToEditorPoint(Point real) { 124 int realEditorHeight = this.canvasSize.getHeight() 126 - (MARGIN_BACK + MARGIN_TOP); 127 int realEditorWidth = this.canvasSize.getWidth() 128 - (MARGIN_LEFT + MARGIN_RIGHT); 129 double propHeight = (double) this.scale.getHeight() 131 / (double) realEditorHeight; 132 double propWidth = (double) this.scale.getWidth() 133 / (double) realEditorWidth; 134 Point editor = new Point((int) ((double) real.x / propWidth), 136 (int) ((double) real.y / propHeight)); 137 Point editorWithCorrectDirectionAndMargins = new Point(editor.x 139 + MARGIN_LEFT, canvasSize.getHeight() 140 - (editor.y + MARGIN_BACK)); 141 return editorWithCorrectDirectionAndMargins; 142 } 143 144 151 public RampDescription realRampToEditorRamp(RampDescription real) { 152 switch (real.getType()) { 154 case RampDescription.LINE : 155 return realLineRampToEditorLineRamp(real); 156 case RampDescription.ARC : 157 return realArcRampToEditorArcRamp(real); 158 case RampDescription.CRENEL_HV : 159 case RampDescription.CRENEL_VH : 160 return realCrenelRampToEditorCrenelRamp(real) ; 161 default : 162 cat.warn("UNKNOW RAMP TYPE... " + real.getType()); 163 return null; 164 } 165 } 166 167 174 public RampDescription editorRampToRealRamp(RampDescription editor) { 175 switch (editor.getType()) { 177 case RampDescription.LINE : 178 return editorLineRampToRealLineRamp(editor); 179 case RampDescription.ARC : 180 return editorArcRampToRealArcRamp(editor); 181 case RampDescription.CRENEL_HV : 182 case RampDescription.CRENEL_VH : 183 return editorCrenelRampToRealCrenelRamp(editor) ; 184 default : 185 cat.warn("UNKNOW RAMP TYPE... " + editor.getType()); 186 return null; 187 } 188 } 189 190 197 private RampDescription editorCrenelRampToRealCrenelRamp(RampDescription editor) { 198 RampDescription real = new RampDescription(editor.getRampId(), editor 200 .getType()); 201 Point start = this.editorPointToRealPoint(editor.getStart()); 203 Point end = this.editorPointToRealPoint(editor.getEnd()); 204 real.setStart(start); 206 real.setEnd(end); 207 return real; 209 } 210 211 218 private RampDescription realCrenelRampToEditorCrenelRamp(RampDescription real) { 219 RampDescription editor = new RampDescription(real.getRampId(), real 221 .getType()); 222 Point start = this.realPointToEditorPoint(real.getStart()); 224 Point end = this.realPointToEditorPoint(real.getEnd()); 225 editor.setStart(start); 227 editor.setEnd(end); 228 return editor; 230 } 231 232 239 private RampDescription editorLineRampToRealLineRamp(RampDescription editor) { 240 RampDescription real = new RampDescription(editor.getRampId(), editor 242 .getType()); 243 Point start = this.editorPointToRealPoint(editor.getStart()); 245 Point end = this.editorPointToRealPoint(editor.getEnd()); 246 real.setStart(start); 248 real.setEnd(end); 249 return real; 251 } 252 253 260 private RampDescription realLineRampToEditorLineRamp(RampDescription real) { 261 RampDescription editor = new RampDescription(real.getRampId(), real 263 .getType()); 264 Point start = this.realPointToEditorPoint(real.getStart()); 266 Point end = this.realPointToEditorPoint(real.getEnd()); 267 editor.setStart(start); 269 editor.setEnd(end); 270 return editor; 272 } 273 274 281 private RampDescription realArcRampToEditorArcRamp(RampDescription real) { 282 return null; 284 } 285 286 291 private RampDescription editorArcRampToRealArcRamp(RampDescription editor) { 292 return null; 294 } 295 296 301 public GroupDescription realGroupToEditorGroup(GroupDescription real) { 302 GroupDescription editor = new GroupDescription(real.getGroupId()) ; 304 editor.setBehaviorId(real.getBehaviorId()) ; 305 editor.setCurveColor(real.getCurveColor()) ; 306 Vector ramps = new Vector () ; 307 for (int i=0;i<real.getRamps().size();i++) { 308 RampDescription editorRamp = this.realRampToEditorRamp((RampDescription)real.getRamps().elementAt(i)) ; 309 ramps.add(editorRamp) ; 310 } 311 editor.setRamps(ramps) ; 313 return editor ; 314 } 315 316 321 public GroupDescription editorGroupToRealGroup(GroupDescription editor) { 322 GroupDescription real = new GroupDescription(editor.getGroupId()) ; 324 real.setBehaviorId(editor.getBehaviorId()) ; 325 real.setCurveColor(editor.getCurveColor()) ; 326 Vector ramps = new Vector () ; 327 for (int i=0;i<real.getRamps().size();i++) { 328 RampDescription realRamp = this.editorRampToRealRamp((RampDescription)editor.getRamps().elementAt(i)) ; 329 ramps.add(realRamp) ; 330 } 331 real.setRamps(ramps) ; 333 return real ; 334 } 335 336 340 347 public boolean isInMarginPoint(Point p) { 348 if (p.x < MARGIN_LEFT) 350 return true; 351 if (p.x > (this.canvasSize.getWidth() - MARGIN_RIGHT)) 352 return true; 353 if (p.y < MARGIN_TOP) 354 return true; 355 if (p.y > (this.canvasSize.getHeight() - MARGIN_BACK)) 356 return true; 357 return false; 359 } 360 361 371 public Point editorPointIsSelectableRealGroupPoint(Point editor, 372 GroupDescription gd) { 373 Point result = new Point(-1, -1); 375 Vector ramps = gd.getRamps(); 377 for (int i = 0; i < ramps.size(); i++) { 378 Point temp = editorPointIsSelectableRampPoint(editor, 379 (RampDescription) ramps.elementAt(i)); 380 if (!temp.equals(new Point(-1, -1))) 381 return temp; 382 } 383 return result; 384 } 385 386 396 private Point editorPointIsSelectableRampPoint(Point editor, 397 RampDescription rd) { 398 Point result = new Point(-1, -1); 400 switch (rd.getType()) { 402 case RampDescription.LINE : 403 Point temp = this.editorPointIsSelectableLineRampPoint(editor, 404 rd); 405 return temp; 406 case RampDescription.ARC : 407 break; 408 case RampDescription.CRENEL_HV : 409 case RampDescription.CRENEL_VH : 410 Point tempCrenel = this.editorPointIsSelectableCrenelRampPoint(editor, 411 rd); 412 return tempCrenel ; 413 default : 414 cat.warn("UNKNOW DRAW TYPE : " + rd.getType()); 415 } 416 return result; 417 } 418 419 429 private Point editorPointIsSelectableLineRampPoint(Point editor, 430 RampDescription rd) { 431 Point result = new Point(-1, -1); 433 Point editorStart = this.realPointToEditorPoint(rd.getStart()); 436 if (editorStart.x > editor.x - ERROR_RATE 437 && editorStart.x < editor.x + ERROR_RATE) 438 if (editorStart.y > editor.y - ERROR_RATE 439 && editorStart.y < editor.y + ERROR_RATE) 440 return rd.getStart(); 441 Point editorEnd = this.realPointToEditorPoint(rd.getEnd()); 444 if (editorEnd.x > editor.x - ERROR_RATE 445 && editorEnd.x < editor.x + ERROR_RATE) 446 if (editorEnd.y > editor.y - ERROR_RATE 447 && editorEnd.y < editor.y + ERROR_RATE) 448 return rd.getEnd(); 449 450 return result; 451 } 452 453 463 private Point editorPointIsSelectableCrenelRampPoint(Point editor, 464 RampDescription rd) { 465 Point result = new Point(-1, -1); 467 Point editorStart = this.realPointToEditorPoint(rd.getStart()); 470 if (editorStart.x > editor.x - ERROR_RATE 471 && editorStart.x < editor.x + ERROR_RATE) 472 if (editorStart.y > editor.y - ERROR_RATE 473 && editorStart.y < editor.y + ERROR_RATE) 474 return rd.getStart(); 475 Point editorEnd = this.realPointToEditorPoint(rd.getEnd()); 478 if (editorEnd.x > editor.x - ERROR_RATE 479 && editorEnd.x < editor.x + ERROR_RATE) 480 if (editorEnd.y > editor.y - ERROR_RATE 481 && editorEnd.y < editor.y + ERROR_RATE) 482 return rd.getEnd(); 483 484 return result; 485 } 486 487 497 public String editorPointIsMemberOfGroup(Point editor, GroupDescription gd) { 498 Vector ramps = gd.getRamps(); 499 for (int i = 0; i < ramps.size(); i++) { 501 if (editorPointIsMemberOfCurve(editor, (RampDescription) ramps 502 .elementAt(i))) 503 return ((RampDescription) ramps.elementAt(i)).getRampId(); 504 } 505 return null; 507 } 508 509 519 private boolean editorPointIsMemberOfCurve(Point editor, RampDescription rd) { 520 switch (rd.getType()) { 522 case RampDescription.LINE : 523 if (editorPointIsMemberOfLineRamp(editor, rd)) 524 return true; 525 break; 526 case RampDescription.ARC : 527 break; 528 default : 529 cat.warn("UNKNOW DRAW TYPE : " + rd.getType()); 530 } 531 return false; 532 } 533 534 544 private boolean editorPointIsMemberOfLineRamp(Point editor, 545 RampDescription rd) { 546 Point start = rd.getStart(); 547 Point end = rd.getEnd(); 548 Point real = editorPointToRealPoint(editor); 550 if (real.x > end.x || real.x < start.x) { 553 return false; 554 } 555 Double a = calculateLineEquationA(start, end); 557 if (a == null) { 558 if (start.y < end.y) { 561 if (editor.y > start.y && editor.y < end.y) 562 return true; 563 else 564 return false; 565 } else { 566 if (editor.y < start.y && editor.y > end.y) 567 return true; 568 else 569 return false; 570 } 571 } 572 Double b = calculateLineEquationB(start, end, a); 574 int y = calculateYAxisPoint(a, b, real.x); 577 if (real.y > y - ERROR_RATE && real.y < y + ERROR_RATE) 580 return true; 581 582 return false; 584 } 585 586 590 600 public static Double calculateLineEquationA(Point p, Point q) { 601 if (p.x == q.x) { 603 return null; 604 } 605 double a = ((double) (q.y - p.y)) / ((double) (q.x - p.x)); 607 return new Double (a); 608 } 609 610 621 public static Double calculateLineEquationB(Point p, Point q, Double a) { 622 return new Double (q.y - (q.x * a.doubleValue())); 623 } 624 625 630 public static int calculateYAxisPoint(Double a, Double b, int x) { 631 return (int) (a.doubleValue() * x + b.doubleValue()); 632 } 633 634 639 public static int logTen(int n) { 640 if (n <= 10) return 1; 641 return 1+logTen(n/10) ; 642 } 643 644 648 651 public Size getCanvasSize() { 652 return canvasSize; 653 } 654 655 659 public void setCanvasSize(Size canvasSize) { 660 this.canvasSize = canvasSize; 661 } 662 663 666 public Point getVisibleLocation() { 667 return visibleLocation; 668 } 669 673 public void setVisibleLocation(Point visibleLocation) { 674 this.visibleLocation = visibleLocation; 675 } 676 677 680 public Size getScale() { 681 return scale; 682 } 683 684 688 public void setScale(Size scale) { 689 this.scale = scale; 690 } 691 } | Popular Tags |