]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.trend/src/org/simantics/trend/impl/HorizRuler.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.trend / src / org / simantics / trend / impl / HorizRuler.java
index 0c0daee8aad4c2dfebb592080e70a4c7b8df2fb0..708a32efcde55d6022b27675264762e31050ca65 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.trend.impl;\r
-\r
-import java.awt.Color;\r
-import java.awt.Graphics2D;\r
-import java.text.Format;\r
-import java.text.NumberFormat;\r
-\r
-import org.simantics.g2d.utils.GridSpacing;\r
-import org.simantics.g2d.utils.GridUtil;\r
-import org.simantics.trend.configuration.TimeWindow;\r
-import org.simantics.trend.configuration.TrendSpec;\r
-import org.simantics.utils.format.TimeFormat;\r
-\r
-public class HorizRuler extends TrendGraphicalNode {\r
-\r
-       private static final long serialVersionUID = 7155812928124259094L;\r
-\r
-       GridSpacing spacing = GridSpacing.SOME_SPACING;\r
-       public double from = 0;\r
-       public double end = 100;\r
-       public boolean autoscroll = true; // Determines if auto-scroll is on/off\r
-       boolean manualscale = false; // Locked in trend spec\r
-       TimeFormat timeFormat = new TimeFormat(0, 0);\r
-       private Format f = timeFormat;\r
-       double iFrom = Double.MAX_VALUE;\r
-       double iEnd  = -Double.MAX_VALUE;\r
-       public double basetime = 0;\r
-       public TimeWindowListener listener;\r
-       public static final Color AUTOSCROLL_ON = new Color(0, 225, 34);\r
-       public static final Color AUTOSCROLL_OFF = new Color(255, 115, 115);\r
-       \r
-               \r
-       public boolean setWidth(double width) {\r
-               if (getWidth()==width) return false;\r
-               this.setSize(width, 20);\r
-               return true;\r
-       }\r
-       \r
-       public boolean setFromEnd(double from, double end) {\r
-               if (this.from!=from || this.end!=end) {\r
-                       this.from = from;\r
-                       this.end = end;\r
-                       getTrend().shapedirty = true;\r
-                       return true;\r
-               }\r
-               return false;\r
-       }\r
-\r
-       public boolean setFromScale(double from, double scaleX) {\r
-               double end = getWidth() * scaleX + from;\r
-               return setFromEnd(from, end);\r
-       }\r
-       \r
-       \r
-       public void fireListener() {\r
-               if (listener != null) {\r
-                       double wid = getWidth();\r
-                       if ( wid == 0.0 ) wid = 0.1;\r
-                       double sx = (end-from) / wid;\r
-                       listener.onNewWindow(from, end, sx);\r
-               }\r
-       }\r
-\r
-       /**\r
-        * Set grid spacing and text formatter;\r
-        */\r
-       public void layout() {\r
-               TrendNode trend = getTrend();\r
-               if (trend.timeFormat == org.simantics.trend.configuration.TimeFormat.Time) {                    \r
-                       // Figure out how many decimals are needed\r
-                       timeFormat.setMaxValue(end);\r
-                       timeFormat.setDecimals(1);\r
-                       \r
-                       double labelWidth = 0;\r
-                       double maxLabelWidth = 0;                       \r
-                       for (int i=0; i<2; i++) {\r
-                               labelWidth = GridUtil.calcLabelWidth(from - basetime, end - basetime, timeFormat, spacing);\r
-                               maxLabelWidth = Math.max(labelWidth, maxLabelWidth);\r
-                               spacing = GridSpacing.makeGridSpacing(end-from, getWidth(), labelWidth+10);\r
-                               timeFormat.setDecimals((int) -spacing.segmentExp);\r
-                       }\r
-\r
-                       if (maxLabelWidth>labelWidth) {\r
-                               spacing = GridSpacing.makeGridSpacing(end-from, getWidth(), maxLabelWidth+10);\r
-                               timeFormat.setDecimals((int) -spacing.segmentExp);\r
-                       }\r
-                       \r
-                       f = timeFormat;\r
-                       \r
-               } else {\r
-                       f = NumberFormat.getInstance();\r
-                       double labelWidth = GridUtil.calcLabelWidth(from - basetime, end, f, spacing);\r
-                       this.spacing = GridSpacing.makeGridSpacing(end-from, getWidth(), labelWidth+10);\r
-               }\r
-       }\r
-       \r
-       @Override\r
-       protected void doRender(Graphics2D g2d) {\r
-               TrendNode trend = getTrend();           \r
-               \r
-               // Draw little "Frozen"\r
-               if ( !trend.printing )\r
-               {\r
-//                     g2d.setColor( Color.LIGHT_GRAY );\r
-                       g2d.setColor( autoscroll ? AUTOSCROLL_ON : AUTOSCROLL_OFF );\r
-                       g2d.setFont( GridUtil.RULER_FONT );\r
-                       String txt = !autoscroll ? (manualscale ? "*" : "Auto-scroll off") : (manualscale ? "" : "Auto-scroll on");\r
-                       // Draw at bottom\r
-//                     g2d.drawString(txt, 0.f, (float)getHeight() + 12.f      );\r
-                       // Draw to right\r
-                       g2d.drawString(txt, (float) getWidth()+20, 20.f         );\r
-               }\r
-               \r
-               g2d.setPaint(Color.GRAY);\r
-               g2d.setStroke( GridUtil.RULER_LINE_STROKE );            \r
-               GridUtil.paintHorizontalRuler(\r
-                       spacing, \r
-                       g2d,\r
-                       from - basetime,\r
-                       getWidth(),\r
-                       f);\r
-       }\r
-       \r
-       /**\r
-        * This method sets iFrom and iEnd values. \r
-        * Read reads values from ItemNodes.\r
-        * So it is good idea to call {@link ItemNode#readMinMaxFromEnd()} first. \r
-        */\r
-       public void setKnownFromEnd() {\r
-               TrendNode trendNode = (TrendNode) getParent();\r
-               \r
-               iFrom = Double.MAX_VALUE;\r
-               iEnd  = -Double.MAX_VALUE;\r
-               for (ItemNode item : trendNode.allItems) {\r
-                       if ( !Double.isNaN(item.from) ) iFrom = Math.min(iFrom, item.from); \r
-                       if ( !Double.isNaN(item.end ) ) iEnd  = Math.max(iEnd , item.end ); \r
-               }\r
-               // Scale to 0..10 if there is no data\r
-               if (iFrom == Double.MAX_VALUE && iEnd == -Double.MAX_VALUE) {\r
-                       iFrom = 0.; \r
-                       iEnd = 10.;\r
-               }\r
-       }       \r
-       \r
-       /**\r
-        * If zoomed, do nothing.\r
-        * If not zoomed, set from and end according to TrendSpec's time window settings. \r
-        */\r
-       public boolean autoscale() {\r
-               if (!autoscroll) return false;\r
-\r
-               setKnownFromEnd();\r
-               TrendNode trendNode = (TrendNode) getParent();\r
-               TrendSpec spec = trendNode.spec;\r
-               \r
-               double nFrom = from;\r
-               double nEnd = end;\r
-                               \r
-               TimeWindow timeWindow = spec.viewProfile.timeWindow;\r
-               double len = timeWindow.timeWindowLength != null ? timeWindow.timeWindowLength : iEnd-iFrom;\r
-                                               \r
-               if (timeWindow.timeWindowStart != null) {\r
-                       \r
-                       if (timeWindow.timeWindowIncrement != null && timeWindow.timeWindowLength==null) {\r
-                               nFrom = timeWindow.timeWindowStart + basetime;\r
-                               if (nFrom>iEnd) {\r
-                                       nEnd = nFrom + 1.0; \r
-                               } else {\r
-                                       len = Math.max(0, iEnd-nFrom);\r
-                                       double f = (100-timeWindow.timeWindowIncrement) / 100;\r
-                                       double b = 1/f;\r
-                                       double x = Math.log( len ) / Math.log( b );\r
-                                       x = Math.ceil( x );\r
-                                       x = Math.pow(b, x);\r
-                                       nEnd = nFrom + x;\r
-                               }\r
-                       } else {\r
-                               nFrom = timeWindow.timeWindowStart + basetime;\r
-                               nEnd = nFrom + len;\r
-                       }\r
-               } else \r
-               {\r
-                       if (timeWindow.timeWindowIncrement == null) {\r
-                               nFrom = iEnd - len;\r
-                               nEnd = iEnd;                                    \r
-                               // Marginal\r
-                               nEnd += len * 0.02; \r
-                       }\r
-                       else\r
-                       {                                       \r
-                               if (timeWindow.timeWindowLength != null) {\r
-                                       double f = timeWindow.timeWindowIncrement / 100;\r
-                                       double fraction = len * f;\r
-                                               \r
-                                       nEnd = Math.floor( (iEnd +fraction)/ fraction ) * fraction;\r
-                                       nFrom = nEnd - len;\r
-                                       if (nFrom<iFrom) {\r
-                                               double diff = iFrom-nFrom;\r
-                                               nFrom += diff;\r
-                                               nEnd += diff;\r
-                                       }\r
-\r
-                                       if (Double.isInfinite(nEnd)) {\r
-                                               nFrom = 0;\r
-                                               nEnd = len; \r
-                                       } else {\r
-                                               // Marginal\r
-                                               nEnd += len * 0.02;\r
-                                       }\r
-                               } else {\r
-                                       double f = (100-timeWindow.timeWindowIncrement) / 100;\r
-                                       double b = 1/f;\r
-                                       double x = Math.log( len ) / Math.log( b );\r
-                                       x = Math.ceil( x );\r
-                                       x = Math.pow(b, x);\r
-                                               \r
-                                       nEnd = iFrom + x;\r
-                                       nFrom = iFrom;\r
-                               }\r
-                       }\r
-               \r
-                       if (nFrom>nEnd) nFrom = nEnd;\r
-               }               \r
-               \r
-               return setFromEnd(nFrom, nEnd);\r
-       }\r
-       \r
-       public void translate(double dx) {\r
-               from += dx;\r
-               end += dx;\r
-               autoscroll = false;\r
-               fireListener();\r
-       }\r
-       \r
-       /**\r
-        * Convert x position in rulers coordinate space to time value.\r
-        * This function does not apply basetime. To apply basetime deduct it from\r
-        * the result.\r
-        * \r
-        * @param x\r
-        * @return time\r
-        */\r
-       public double toTime( double x ) {\r
-               double sx = (end-from) / getWidth();\r
-               return from + x*sx;\r
-       }\r
-       \r
-       public double toX( double time ) {\r
-               double sx = (end-from) / getWidth();\r
-               return (time-from) / sx;\r
-       }\r
-       \r
-       public void zoomIn(double x, double width) {\r
-               autoscroll = false;\r
-               \r
-               double sx = (end-from) / getWidth();\r
-               double newFrom = from + x*sx;\r
-               double newEnd  = from + (x+width)*sx;\r
-               \r
-               from = newFrom;\r
-               end = newEnd;           \r
-               fireListener();\r
-       }\r
-       \r
-       public void zoomOut() {\r
-               autoscroll = true;\r
-               autoscale();\r
-               layout();\r
-               fireListener();\r
-       }       \r
-       \r
-       public double unitsPerPixel() {\r
-               return (end-from) / getWidth();\r
-       }\r
-\r
-       /**\r
-        * @return the current ending sample time calculated from all visible chart\r
-        *         items.\r
-        */\r
-       public double getItemEndTime() {\r
-               return iEnd;\r
-       }\r
-\r
-       public interface TimeWindowListener {\r
-               void onNewWindow(double from, double end, double sx);\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.trend.impl;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.text.Format;
+import java.text.NumberFormat;
+
+import org.simantics.g2d.utils.GridSpacing;
+import org.simantics.g2d.utils.GridUtil;
+import org.simantics.trend.configuration.TimeWindow;
+import org.simantics.trend.configuration.TrendSpec;
+import org.simantics.utils.format.TimeFormat;
+
+public class HorizRuler extends TrendGraphicalNode {
+
+       private static final long serialVersionUID = 7155812928124259094L;
+
+       GridSpacing spacing = GridSpacing.SOME_SPACING;
+       public double from = 0;
+       public double end = 100;
+       public boolean autoscroll = true; // Determines if auto-scroll is on/off
+       boolean manualscale = false; // Locked in trend spec
+       TimeFormat timeFormat = new TimeFormat(0, 0);
+       private Format f = timeFormat;
+       double iFrom = Double.MAX_VALUE;
+       double iEnd  = -Double.MAX_VALUE;
+       public double basetime = 0;
+       public TimeWindowListener listener;
+       public static final Color AUTOSCROLL_ON = new Color(0, 225, 34);
+       public static final Color AUTOSCROLL_OFF = new Color(255, 115, 115);
+       
+               
+       public boolean setWidth(double width) {
+               if (getWidth()==width) return false;
+               this.setSize(width, 20);
+               return true;
+       }
+       
+       public boolean setFromEnd(double from, double end) {
+               if (this.from!=from || this.end!=end) {
+                       this.from = from;
+                       this.end = end;
+                       getTrend().shapedirty = true;
+                       return true;
+               }
+               return false;
+       }
+
+       public boolean setFromScale(double from, double scaleX) {
+               double end = getWidth() * scaleX + from;
+               return setFromEnd(from, end);
+       }
+       
+       
+       public void fireListener() {
+               if (listener != null) {
+                       double wid = getWidth();
+                       if ( wid == 0.0 ) wid = 0.1;
+                       double sx = (end-from) / wid;
+                       listener.onNewWindow(from, end, sx);
+               }
+       }
+
+       /**
+        * Set grid spacing and text formatter;
+        */
+       public void layout() {
+               TrendNode trend = getTrend();
+               if (trend.timeFormat == org.simantics.trend.configuration.TimeFormat.Time) {                    
+                       // Figure out how many decimals are needed
+                       timeFormat.setMaxValue(end);
+                       timeFormat.setDecimals(1);
+                       
+                       double labelWidth = 0;
+                       double maxLabelWidth = 0;                       
+                       for (int i=0; i<2; i++) {
+                               labelWidth = GridUtil.calcLabelWidth(from - basetime, end - basetime, timeFormat, spacing);
+                               maxLabelWidth = Math.max(labelWidth, maxLabelWidth);
+                               spacing = GridSpacing.makeGridSpacing(end-from, getWidth(), labelWidth+10);
+                               timeFormat.setDecimals((int) -spacing.segmentExp);
+                       }
+
+                       if (maxLabelWidth>labelWidth) {
+                               spacing = GridSpacing.makeGridSpacing(end-from, getWidth(), maxLabelWidth+10);
+                               timeFormat.setDecimals((int) -spacing.segmentExp);
+                       }
+                       
+                       f = timeFormat;
+                       
+               } else {
+                       f = NumberFormat.getInstance();
+                       double labelWidth = GridUtil.calcLabelWidth(from - basetime, end, f, spacing);
+                       this.spacing = GridSpacing.makeGridSpacing(end-from, getWidth(), labelWidth+10);
+               }
+       }
+       
+       @Override
+       protected void doRender(Graphics2D g2d) {
+               TrendNode trend = getTrend();           
+               
+               // Draw little "Frozen"
+               if ( !trend.printing )
+               {
+//                     g2d.setColor( Color.LIGHT_GRAY );
+                       g2d.setColor( autoscroll ? AUTOSCROLL_ON : AUTOSCROLL_OFF );
+                       g2d.setFont( GridUtil.RULER_FONT );
+                       String txt = !autoscroll ? (manualscale ? "*" : "Auto-scroll off") : (manualscale ? "" : "Auto-scroll on");
+                       // Draw at bottom
+//                     g2d.drawString(txt, 0.f, (float)getHeight() + 12.f      );
+                       // Draw to right
+                       g2d.drawString(txt, (float) getWidth()+20, 20.f         );
+               }
+               
+               g2d.setPaint(Color.GRAY);
+               g2d.setStroke( GridUtil.RULER_LINE_STROKE );            
+               GridUtil.paintHorizontalRuler(
+                       spacing, 
+                       g2d,
+                       from - basetime,
+                       getWidth(),
+                       f);
+       }
+       
+       /**
+        * This method sets iFrom and iEnd values. 
+        * Read reads values from ItemNodes.
+        * So it is good idea to call {@link ItemNode#readMinMaxFromEnd()} first. 
+        */
+       public void setKnownFromEnd() {
+               TrendNode trendNode = (TrendNode) getParent();
+               
+               iFrom = Double.MAX_VALUE;
+               iEnd  = -Double.MAX_VALUE;
+               for (ItemNode item : trendNode.allItems) {
+                       if ( !Double.isNaN(item.from) ) iFrom = Math.min(iFrom, item.from); 
+                       if ( !Double.isNaN(item.end ) ) iEnd  = Math.max(iEnd , item.end ); 
+               }
+               // Scale to 0..10 if there is no data
+               if (iFrom == Double.MAX_VALUE && iEnd == -Double.MAX_VALUE) {
+                       iFrom = 0.; 
+                       iEnd = 10.;
+               }
+       }       
+       
+       /**
+        * If zoomed, do nothing.
+        * If not zoomed, set from and end according to TrendSpec's time window settings. 
+        */
+       public boolean autoscale() {
+               if (!autoscroll) return false;
+
+               setKnownFromEnd();
+               TrendNode trendNode = (TrendNode) getParent();
+               TrendSpec spec = trendNode.spec;
+               
+               double nFrom = from;
+               double nEnd = end;
+                               
+               TimeWindow timeWindow = spec.viewProfile.timeWindow;
+               double len = timeWindow.timeWindowLength != null ? timeWindow.timeWindowLength : iEnd-iFrom;
+                                               
+               if (timeWindow.timeWindowStart != null) {
+                       
+                       if (timeWindow.timeWindowIncrement != null && timeWindow.timeWindowLength==null) {
+                               nFrom = timeWindow.timeWindowStart + basetime;
+                               if (nFrom>iEnd) {
+                                       nEnd = nFrom + 1.0; 
+                               } else {
+                                       len = Math.max(0, iEnd-nFrom);
+                                       double f = (100-timeWindow.timeWindowIncrement) / 100;
+                                       double b = 1/f;
+                                       double x = Math.log( len ) / Math.log( b );
+                                       x = Math.ceil( x );
+                                       x = Math.pow(b, x);
+                                       nEnd = nFrom + x;
+                               }
+                       } else {
+                               nFrom = timeWindow.timeWindowStart + basetime;
+                               nEnd = nFrom + len;
+                       }
+               } else 
+               {
+                       if (timeWindow.timeWindowIncrement == null) {
+                               nFrom = iEnd - len;
+                               nEnd = iEnd;                                    
+                               // Marginal
+                               nEnd += len * 0.02; 
+                       }
+                       else
+                       {                                       
+                               if (timeWindow.timeWindowLength != null) {
+                                       double f = timeWindow.timeWindowIncrement / 100;
+                                       double fraction = len * f;
+                                               
+                                       nEnd = Math.floor( (iEnd +fraction)/ fraction ) * fraction;
+                                       nFrom = nEnd - len;
+                                       if (nFrom<iFrom) {
+                                               double diff = iFrom-nFrom;
+                                               nFrom += diff;
+                                               nEnd += diff;
+                                       }
+
+                                       if (Double.isInfinite(nEnd)) {
+                                               nFrom = 0;
+                                               nEnd = len; 
+                                       } else {
+                                               // Marginal
+                                               nEnd += len * 0.02;
+                                       }
+                               } else {
+                                       double f = (100-timeWindow.timeWindowIncrement) / 100;
+                                       double b = 1/f;
+                                       double x = Math.log( len ) / Math.log( b );
+                                       x = Math.ceil( x );
+                                       x = Math.pow(b, x);
+                                               
+                                       nEnd = iFrom + x;
+                                       nFrom = iFrom;
+                               }
+                       }
+               
+                       if (nFrom>nEnd) nFrom = nEnd;
+               }               
+               
+               return setFromEnd(nFrom, nEnd);
+       }
+       
+       public void translate(double dx) {
+               from += dx;
+               end += dx;
+               autoscroll = false;
+               fireListener();
+       }
+       
+       /**
+        * Convert x position in rulers coordinate space to time value.
+        * This function does not apply basetime. To apply basetime deduct it from
+        * the result.
+        * 
+        * @param x
+        * @return time
+        */
+       public double toTime( double x ) {
+               double sx = (end-from) / getWidth();
+               return from + x*sx;
+       }
+       
+       public double toX( double time ) {
+               double sx = (end-from) / getWidth();
+               return (time-from) / sx;
+       }
+       
+       public void zoomIn(double x, double width) {
+               autoscroll = false;
+               
+               double sx = (end-from) / getWidth();
+               double newFrom = from + x*sx;
+               double newEnd  = from + (x+width)*sx;
+               
+               from = newFrom;
+               end = newEnd;           
+               fireListener();
+       }
+       
+       public void zoomOut() {
+               autoscroll = true;
+               autoscale();
+               layout();
+               fireListener();
+       }       
+       
+       public double unitsPerPixel() {
+               return (end-from) / getWidth();
+       }
+
+       /**
+        * @return the current starting sample time calculated from all visible chart
+        *         items.
+        */
+       public double getItemFromTime() {
+               return iFrom;
+       }
+
+       /**
+        * @return the current ending sample time calculated from all visible chart
+        *         items.
+        */
+       public double getItemEndTime() {
+               return iEnd;
+       }
+
+       public interface TimeWindowListener {
+               void onNewWindow(double from, double end, double sx);
+       }
+
+}