1 24 package info.monitorenter.gui.chart.traces; 25 26 import info.monitorenter.gui.chart.Chart2D; 27 import info.monitorenter.gui.chart.ITrace2D; 28 import info.monitorenter.gui.chart.ITracePainter; 29 import info.monitorenter.gui.chart.TracePoint2D; 30 import info.monitorenter.gui.chart.traces.painters.TracePainterPolyline; 31 import info.monitorenter.util.StringUtil; 32 33 import java.awt.BasicStroke ; 34 import java.awt.Color ; 35 import java.awt.Stroke ; 36 import java.beans.PropertyChangeListener ; 37 import java.beans.PropertyChangeSupport ; 38 import java.util.Iterator ; 39 import java.util.LinkedList ; 40 import java.util.List ; 41 import java.util.Set ; 42 import java.util.TreeSet ; 43 44 import javax.swing.event.SwingPropertyChangeSupport ; 45 46 150 public abstract class ATrace2D implements ITrace2D { 151 152 155 private static int instanceCount = 0; 156 157 163 public static int getInstanceCount() { 164 return ATrace2D.instanceCount; 165 } 166 167 172 private List m_changeListeners = new LinkedList (); 173 174 177 private Color m_color = Color.black; 178 179 183 private boolean m_firsttime = true; 184 185 188 protected double m_maxX; 189 190 193 protected double m_maxY; 194 195 198 protected double m_minX; 199 200 203 protected double m_minY; 204 205 208 protected String m_name = ""; 209 210 213 protected String m_physicalUnits = ""; 214 215 221 222 protected PropertyChangeSupport m_propertyChangeSupport = new SwingPropertyChangeSupport (this); 223 224 228 protected Object m_renderer = new Object (); 229 230 233 private Stroke m_stroke = new BasicStroke (1f); 234 235 238 private boolean m_visible = true; 239 240 243 private Integer m_zIndex = new Integer (ITrace2D.ZINDEX_MAX); 244 245 250 public ATrace2D() { 251 super(); 252 ATrace2D.instanceCount++; 253 this.m_tracePainters = new TreeSet (); 254 this.m_tracePainters.add(new TracePainterPolyline()); 255 } 256 257 273 public final boolean addPoint(final double x, final double y) { 274 TracePoint2D p = new TracePoint2D(x, y); 275 return this.addPoint(p); 276 } 277 278 298 public final boolean addPoint(final TracePoint2D p) { 299 if (Chart2D.DEBUG_THREADING) { 300 System.out.println("addPoint, 0 locks"); 301 } 302 synchronized (this.m_renderer) { 303 if (Chart2D.DEBUG_THREADING) { 304 System.out.println("addPoint, 1 lock"); 305 } 306 synchronized (this) { 307 if (Chart2D.DEBUG_THREADING) { 308 System.out.println("addPoint, 2 locks"); 309 } 310 boolean accepted = this.addPointInternal(p); 311 if (this.m_firsttime) { 312 this.m_maxX = p.getX(); 313 this.m_minX = this.m_maxX; 314 this.m_maxY = p.getY(); 315 this.m_minY = this.m_maxY; 316 this.firePropertyChange(PROPERTY_MAX_X, this, new Double (this.m_maxX)); 317 this.firePropertyChange(PROPERTY_MIN_X, this, new Double (this.m_minX)); 318 this.firePropertyChange(PROPERTY_MAX_Y, this, new Double (this.m_maxY)); 319 this.firePropertyChange(PROPERTY_MIN_Y, this, new Double (this.m_minY)); 320 321 this.m_firsttime = false; 322 } 323 if (accepted) { 324 this.firePointAdded(p); 325 p.setListener(this); 326 } 327 return accepted; 331 } 332 } 333 } 334 335 360 protected abstract boolean addPointInternal(TracePoint2D p); 361 362 366 public final void addPropertyChangeListener(final String propertyName, 367 final PropertyChangeListener listener) { 368 this.m_propertyChangeSupport.addPropertyChangeListener(propertyName, listener); 369 } 370 371 374 public boolean addTracePainter(final ITracePainter painter) { 375 boolean result = this.m_tracePainters.add(painter); 376 if (result) { 377 this.firePropertyChange(PROPERTY_PAINTERS, null, painter); 378 } 379 return result; 380 } 381 382 389 public void finalize() throws Throwable { 390 super.finalize(); 391 ATrace2D.instanceCount--; 392 } 393 394 413 protected void firePointAdded(final TracePoint2D added) { 414 this.firePointChanged(added, true); 415 this.firePropertyChange(PROPERTY_TRACEPOINT, null, added); 416 } 417 418 442 public void firePointChanged(final TracePoint2D changed, final boolean added) { 443 double tmpx = changed.getX(); 444 double tmpy = changed.getY(); 445 if (added) { 446 if (tmpx > this.m_maxX) { 447 this.m_maxX = tmpx; 448 this.firePropertyChange(PROPERTY_MAX_X, null, new Double (this.m_maxX)); 449 } else if (tmpx < this.m_minX) { 450 this.m_minX = tmpx; 451 this.firePropertyChange(PROPERTY_MIN_X, null, new Double (this.m_minX)); 452 } 453 if (tmpy > this.m_maxY) { 454 this.m_maxY = tmpy; 455 this.firePropertyChange(PROPERTY_MAX_Y, null, new Double (this.m_maxY)); 456 } else if (tmpy < this.m_minY) { 457 this.m_minY = tmpy; 458 this.firePropertyChange(PROPERTY_MIN_Y, null, new Double (this.m_minY)); 459 } 460 } else { 461 if (tmpx > this.m_maxX) { 462 tmpx = this.m_maxX; 463 this.maxXSearch(); 464 this.firePropertyChange(PROPERTY_MAX_X, new Double (tmpx), new Double (this.m_maxX)); 465 466 } else if (tmpx < this.m_minX) { 467 tmpx = this.m_minX; 468 this.minXSearch(); 469 this.firePropertyChange(PROPERTY_MIN_X, new Double (tmpx), new Double (this.m_minX)); 470 } 471 if (tmpy > this.m_maxY) { 472 tmpy = this.m_maxY; 473 this.maxYSearch(); 474 this.firePropertyChange(PROPERTY_MAX_Y, new Double (tmpy), new Double (this.m_maxY)); 475 } else if (tmpy < this.m_minY) { 476 tmpy = this.m_minY; 477 this.minYSearch(); 478 this.firePropertyChange(PROPERTY_MIN_Y, new Double (tmpy), new Double (this.m_minY)); 479 } 480 if (this.getSize() == 0) { 481 this.m_firsttime = true; 482 } 483 } 484 } 485 486 505 protected void firePointRemoved(final TracePoint2D removed) { 506 this.firePointChanged(removed, false); 507 this.firePropertyChange(PROPERTY_TRACEPOINT, removed, null); 508 } 509 510 524 protected final void firePropertyChange(final String property, final Object oldvalue, 525 final Object newvalue) { 526 if (Chart2D.DEBUG_THREADING) { 527 System.out.println("trace.firePropertyChange (" + property + "), 0 locks"); 528 } 529 if (property.equals(PROPERTY_MAX_X) || property.equals(PROPERTY_MAX_Y) 530 || property.equals(PROPERTY_MIN_X) || property.equals(PROPERTY_MIN_Y) 531 || property.equals(PROPERTY_TRACEPOINT)) { 532 if (!Thread.holdsLock(this.m_renderer)) { 533 throw new RuntimeException ("Acquire a look on the corresponding chart first!"); 534 } 535 } 536 537 this.m_propertyChangeSupport.firePropertyChange(property, oldvalue, newvalue); 538 } 539 540 546 public List getChangeListeners() { 547 return new LinkedList (this.m_changeListeners); 548 } 549 550 557 public final Color getColor() { 558 return this.m_color; 559 } 560 561 564 public Number getComparableProperty() { 565 return this.m_zIndex; 566 } 567 568 587 public final String getLable() { 588 String name = this.getName(); 589 String physunit = this.getPhysicalUnits(); 590 if ((name != "") && (physunit != "")) { 591 name = new StringBuffer (name).append(" ").append(physunit).toString(); 592 } else if (physunit != "") { 593 name = new StringBuffer ("unnamed").append(" ").append(physunit).toString(); 594 } 595 return name.toString(); 596 } 597 598 604 public final double getMaxX() { 605 return this.m_maxX; 606 } 607 608 614 615 public final double getMaxY() { 616 return this.m_maxY; 617 } 618 619 625 public final double getMinX() { 626 return this.m_minX; 627 } 628 629 635 public final double getMinY() { 636 return this.m_minY; 637 } 638 639 649 public final String getName() { 650 return this.m_name; 651 } 652 653 656 public final String getPhysicalUnits() { 657 return this.m_physicalUnits; 658 } 659 660 663 public PropertyChangeListener [] getPropertyChangeListeners(final String property) { 664 return this.m_propertyChangeSupport.getPropertyChangeListeners(property); 665 } 666 667 680 public final Chart2D getRenderer() { 681 if (this.m_renderer instanceof Chart2D) { 682 return (Chart2D) this.m_renderer; 683 } else { 684 return null; 685 } 686 } 687 688 698 public final Stroke getStroke() { 699 return this.m_stroke; 700 } 701 702 705 public final boolean isVisible() { 706 return this.m_visible; 707 } 708 709 712 public final Integer getZIndex() { 713 if (Chart2D.DEBUG_THREADING) { 719 System.out.println("trace.getZindex, 0 locks"); 720 } 721 722 synchronized (this.m_renderer) { 723 if (Chart2D.DEBUG_THREADING) { 724 System.out.println("trace.getZindex, 1 locks"); 725 } 726 727 synchronized (this) { 728 if (Chart2D.DEBUG_THREADING) { 729 System.out.println("trace.getZindex, 2 locks"); 730 } 731 732 return new Integer (ZINDEX_MAX - this.m_zIndex.intValue()); 733 } 734 } 735 } 736 737 747 protected void maxXSearch() { 748 if (Chart2D.DEBUG_THREADING) { 749 System.out.println("trace.maxXSearch, 0 locks"); 750 } 751 752 synchronized (this) { 753 if (Chart2D.DEBUG_THREADING) { 754 System.out.println("trace.maxXSearch, 1 locks"); 755 } 756 double ret = -Double.MAX_VALUE; 757 TracePoint2D tmpoint = null; 758 double tmp; 759 Iterator it = this.iterator(); 760 while (it.hasNext()) { 761 tmpoint = (TracePoint2D) it.next(); 762 tmp = tmpoint.getX(); 763 if (tmp > ret) { 764 ret = tmp; 765 } 766 } 767 this.m_maxX = ret; 768 } 769 } 770 771 781 protected void maxYSearch() { 782 if (Chart2D.DEBUG_THREADING) { 783 System.out.println("trace.maxYSearch, 0 locks"); 784 } 785 786 synchronized (this) { 787 if (Chart2D.DEBUG_THREADING) { 788 System.out.println("trace.maxYSearch, 1 lock"); 789 } 790 791 double ret = -Double.MAX_VALUE; 792 TracePoint2D tmpoint = null; 793 double tmp; 794 Iterator it = this.iterator(); 795 while (it.hasNext()) { 796 tmpoint = (TracePoint2D) it.next(); 797 tmp = tmpoint.getY(); 798 if (tmp > ret) { 799 ret = tmp; 800 } 801 } 802 this.m_maxY = ret; 803 } 804 } 805 806 816 protected void minXSearch() { 817 if (Chart2D.DEBUG_THREADING) { 818 System.out.println("trace.minXSearch, 0 locks"); 819 } 820 821 synchronized (this) { 822 if (Chart2D.DEBUG_THREADING) { 823 System.out.println("trace.minXSearch, 1 locks"); 824 } 825 826 double ret = Double.MAX_VALUE; 827 TracePoint2D tmpoint = null; 828 double tmp; 829 Iterator it = this.iterator(); 830 while (it.hasNext()) { 831 tmpoint = (TracePoint2D) it.next(); 832 tmp = tmpoint.getX(); 833 if (tmp < ret) { 834 ret = tmp; 835 } 836 } 837 this.m_minX = ret; 838 } 839 } 840 841 851 protected void minYSearch() { 852 if (Chart2D.DEBUG_THREADING) { 853 System.out.println("trace.minYSearch, 0 locks"); 854 } 855 856 synchronized (this) { 857 if (Chart2D.DEBUG_THREADING) { 858 System.out.println("trace.minYSearch, 1 locks"); 859 } 860 861 double ret = Double.MAX_VALUE; 862 TracePoint2D tmpoint = null; 863 double tmp; 864 Iterator it = this.iterator(); 865 while (it.hasNext()) { 866 tmpoint = (TracePoint2D) it.next(); 867 tmp = tmpoint.getY(); 868 if (tmp < ret) { 869 ret = tmp; 870 } 871 } 872 this.m_minY = ret; 873 } 874 875 } 876 877 884 public final void removeAllPoints() { 885 synchronized (this.m_renderer) { 886 synchronized (this) { 887 888 this.m_firsttime = true; 889 this.removeAllPointsInternal(); 890 this.m_minX = 0; 891 this.m_maxX = 0; 892 this.m_maxY = 0; 893 this.m_minY = 0; 894 this.firePropertyChange(PROPERTY_MAX_X, null, new Double (this.m_maxX)); 895 this.firePropertyChange(PROPERTY_MIN_X, null, new Double (this.m_minX)); 896 this.firePropertyChange(PROPERTY_MAX_Y, null, new Double (this.m_maxY)); 897 this.firePropertyChange(PROPERTY_MIN_Y, null, new Double (this.m_minY)); 898 } 899 } 900 } 901 902 912 protected abstract void removeAllPointsInternal(); 913 914 937 public boolean removePoint(final TracePoint2D point) { 938 synchronized (this.m_renderer) { 939 if (Chart2D.DEBUG_THREADING) { 940 941 System.out.println("removePoint, 0 locks"); 942 } 943 synchronized (this) { 944 if (Chart2D.DEBUG_THREADING) { 945 System.out.println("removePoint, 1 lock"); 946 } 947 boolean success = this.removePointInternal(point); 948 if (success) { 949 this.firePointRemoved(point); 950 } 951 return success; 952 953 } 954 } 955 } 956 957 985 protected abstract boolean removePointInternal(final TracePoint2D point); 986 987 990 public void removePropertyChangeListener(final PropertyChangeListener listener) { 991 this.m_propertyChangeSupport.removePropertyChangeListener(listener); 992 } 993 994 998 public void removePropertyChangeListener(final String property, 999 final PropertyChangeListener listener) { 1000 this.m_propertyChangeSupport.removePropertyChangeListener(property, listener); 1001 } 1002 1003 1014 public boolean removeTracePainter(final ITracePainter painter) { 1015 boolean result = false; 1016 if (this.m_tracePainters.size() > 1) { 1017 result = this.m_tracePainters.remove(painter); 1018 if (result) { 1019 this.firePropertyChange(PROPERTY_PAINTERS, painter, null); 1020 } 1021 1022 } else { 1023 } 1026 return result; 1027 } 1028 1029 1037 public final void setColor(final Color color) { 1038 Color oldValue = this.m_color; 1039 this.m_color = color; 1040 if (!this.m_color.equals(oldValue)) { 1041 this.firePropertyChange(PROPERTY_COLOR, oldValue, this.m_color); 1042 } 1043 } 1044 1045 1048 public void setComparableProperty(final Number n) { 1049 this.m_zIndex = ((Integer ) n); 1050 } 1051 1052 1065 public final void setName(final String name) { 1066 if (!StringUtil.isEmpty(name)) { 1067 String oldValue = this.m_name; 1068 this.m_name = name; 1069 if (!this.m_name.equals(oldValue)) { 1070 this.firePropertyChange(PROPERTY_NAME, oldValue, this.m_name); 1071 } 1072 } 1073 } 1074 1075 1078 public final void setPhysicalUnits(final String xunit, final String yunit) { 1079 String oldValue = this.m_physicalUnits; 1080 if ((xunit == null) && (yunit == null)) { 1081 return; 1082 } 1083 if ((xunit == null) && (yunit != null)) { 1084 this.m_physicalUnits = new StringBuffer ("[x: , y: ").append(yunit).append("]").toString(); 1085 return; 1086 } 1087 if ((xunit != null) && (yunit == null)) { 1088 this.m_physicalUnits = new StringBuffer ("[x: ").append(xunit).append(", y: ]").toString(); 1089 return; 1090 } 1091 this.m_physicalUnits = new StringBuffer ("[x: ").append(xunit).append(", y: ").append(yunit) 1092 .append("]").toString(); 1093 if (!this.m_physicalUnits.equals(oldValue)) { 1094 this.firePropertyChange(PROPERTY_PHYSICALUNITS, oldValue, this.m_physicalUnits); 1095 } 1096 } 1097 1098 1110 public final void setRenderer(final Chart2D renderer) { 1111 this.m_renderer = renderer; 1112 } 1113 1114 1117 public final void setStroke(final Stroke stroke) { 1118 if (stroke == null) { 1119 throw new IllegalArgumentException ("Argument must not be null."); 1120 } 1121 Stroke oldValue = this.m_stroke; 1122 this.m_stroke = stroke; 1123 if (!this.m_stroke.equals(oldValue)) { 1124 this.firePropertyChange(PROPERTY_STROKE, oldValue, this.m_stroke); 1125 } 1126 } 1127 1128 1129 private Set m_tracePainters = new TreeSet (); 1130 1131 1134 public boolean containsTracePainter(final ITracePainter painter) { 1135 return this.m_tracePainters.contains(painter); 1136 } 1137 1138 1152 public final void setVisible(final boolean visible) { 1153 boolean oldValue = this.m_visible; 1154 this.m_visible = visible; 1155 if (oldValue != this.m_visible) { 1156 this.firePropertyChange(PROPERTY_VISIBLE, new Boolean (oldValue), new Boolean (this.m_visible)); 1157 } 1158 } 1159 1160 1170 public Set setTracePainter(final ITracePainter painter) { 1171 Set result = this.m_tracePainters; 1172 this.m_tracePainters = new TreeSet (); 1173 boolean added = this.m_tracePainters.add(painter); 1174 if (added) { 1175 this.firePropertyChange(PROPERTY_PAINTERS, null, painter); 1176 } 1177 return result; 1178 } 1179 1180 1183 public final void setZIndex(final Integer zIndex) { 1184 if (Chart2D.DEBUG_THREADING) { 1185 System.out.println("trace.setZIndex, 0 locks"); 1186 } 1187 1188 if (!zIndex.equals(this.m_zIndex)) { 1189 1190 synchronized (this.m_renderer) { 1191 if (Chart2D.DEBUG_THREADING) { 1192 System.out.println("trace.setZIndex, 1 lock"); 1193 } 1194 1195 Integer oldValue = this.m_zIndex; 1196 synchronized (this) { 1197 if (Chart2D.DEBUG_THREADING) { 1198 System.out.println("trace.setZIndex, 2 locks"); 1199 } 1200 1205 boolean rendered = this.m_renderer instanceof Chart2D; 1206 if (rendered) { 1207 ((Chart2D) this.m_renderer).removeTrace(this); 1208 } 1209 this.m_zIndex = new Integer (ZINDEX_MAX - zIndex.intValue()); 1210 if (rendered) { 1211 ((Chart2D) this.m_renderer).addTrace(this); 1212 } 1213 this.firePropertyChange(PROPERTY_ZINDEX, oldValue, this.m_zIndex); 1214 1215 } 1216 1217 } 1218 } 1219 } 1220 1221 1224 public final Set getTracePainters() { 1225 1226 return this.m_tracePainters; 1227 } 1228 1229 1236 public String toString() { 1237 return this.getName(); 1238 } 1239} 1240 | Popular Tags |