--- /dev/null
+/*******************************************************************************\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