]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RulerNode.java
Add max digits for ruler node for subclasses to override
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / RulerNode.java
index f7e0d1e5c6b5cb05d280b9a5ae514758723b2370..7e962976e52f1b67ec5f489af713c25357e90a2b 100644 (file)
@@ -57,7 +57,7 @@ public class RulerNode extends G2DNode {
     public void render(Graphics2D g) {
         if (!enabled)
             return;
-
+        
         AffineTransform tr = g.getTransform();
         double scaleX = Math.abs(tr.getScaleX());
         double scaleY = Math.abs(tr.getScaleY());
@@ -109,7 +109,9 @@ public class RulerNode extends G2DNode {
         // Vertical ruler
         for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) {
             if(x > 20) {
-                String str = formatValue((x-offsetX)/scaleX);
+                double val = (x-offsetX)/scaleX / getTransform().getScaleX();
+                double modifiedValue = modifyHorizontalValue(val);
+                String str = formatValue(modifiedValue, getMaxDigits());
                 FontMetrics fm = g.getFontMetrics();
                 Rectangle2D r = fm.getStringBounds(str, g);
                 if((x-r.getWidth()/2) > previousText) {
@@ -140,10 +142,9 @@ public class RulerNode extends G2DNode {
         previousText = -100;
         for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) {
             if(y > 20) {
-                double val = (y-offsetY)/scaleY;
-                if (MAP_Y_SCALING)
-                    val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
-                String str = formatValue(val);
+                double val = (y-offsetY)/scaleY / getTransform().getScaleY();
+                double modifiedValue = modifyVerticalValue(val);
+                String str = formatValue(modifiedValue, getMaxDigits());
                 FontMetrics fm = g.getFontMetrics();
                 Rectangle2D r = fm.getStringBounds(str, g);
                 if(y-1+r.getHeight()/2 > previousText) {
@@ -175,17 +176,41 @@ public class RulerNode extends G2DNode {
         g.setTransform(tr);
     }
 
+    /**
+     * A method for subclasses to alter the actual X-value of the ruler 
+     * 
+     * @param value
+     * @return possibly modified X-value 
+     */
+    protected double modifyHorizontalValue(double value) {
+        return value;
+    }
+
+    /**
+     * A method for subclasses to alter the actual Y-value of the ruler 
+     * 
+     * @param value
+     * @return possibly modified Y-value 
+     */
+    protected double modifyVerticalValue(double value) {
+        return value;
+    }
+
     private static final transient int    MAX_DIGITS = 5;
     private static final transient double EPSILON    = 0.01;
     private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 4);
     private static final transient String[] SI_UNIT_LARGE_PREFIXES = {
         "k", "M", "G", "T", "P", "E", "Z", "Y"
     };
+    
+    protected int getMaxDigits() {
+        return MAX_DIGITS;
+    }
 
-    public static String formatValue(double value) {
+    public static String formatValue(double value, int maxDigits) {
         int magnitude = (int) Math.round(Math.log10(value));
         //System.out.println("magnitude: " + magnitude + ", " + value);
-        int allowedDecimals = MAX_DIGITS;
+        int allowedDecimals = maxDigits;
         allowedDecimals -= Math.abs(magnitude);
         if (allowedDecimals < 0)
             allowedDecimals = 0;