1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
14 import java.awt.AlphaComposite;
15 import java.awt.BasicStroke;
16 import java.awt.Color;
18 import java.awt.FontMetrics;
19 import java.awt.Graphics2D;
20 import java.awt.geom.AffineTransform;
21 import java.awt.geom.Rectangle2D;
22 import java.util.Locale;
24 import org.simantics.scenegraph.g2d.G2DNode;
25 import org.simantics.scenegraph.utils.GridUtils;
27 public class RulerNode extends G2DNode {
31 private static final long serialVersionUID = 2490944880914577411L;
34 * FIXME: this is a hack for the map UI that has to be solved some other way.
36 private static final boolean MAP_Y_SCALING = false;
38 private static final Color GRAY = new Color(100, 100, 100);
40 protected Boolean enabled = true;
42 protected double gridSize = 1.0;
45 public void setEnabled(Boolean enabled) {
46 this.enabled = enabled;
49 @SyncField("gridSize")
50 public void setGridSize(double gridSize) {
53 this.gridSize = gridSize;
57 public void render(Graphics2D g) {
61 AffineTransform tr = g.getTransform();
62 double scaleX = Math.abs(tr.getScaleX());
63 double scaleY = Math.abs(tr.getScaleY());
64 if (scaleX <= 0 || scaleY <= 0) {
65 // Make sure that we don't end up in an eternal loop below.
68 double offsetX = tr.getTranslateX();
69 double offsetY = tr.getTranslateY();
70 g.setTransform(new AffineTransform());
72 Font rulerFont = new Font("Tahoma", Font.PLAIN, 9);
74 //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
75 g.setStroke(new BasicStroke(1));
76 g.setColor(new Color(0.9f, 0.9f, 0.9f, 0.75f));
78 Rectangle2D bounds = g.getClipBounds();
79 if(bounds == null) return; // FIXME
81 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
83 Rectangle2D vertical = new Rectangle2D.Double(bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMinY()+20);
86 Rectangle2D horizontal = new Rectangle2D.Double(bounds.getMinX(), bounds.getMinY()+20, bounds.getMinX()+20, bounds.getMaxY());
89 // stepX and stepY should be something between 50 and 100
93 stepX = GridUtils.limitedEvenGridSpacing(stepX, scaleX, 100, gridSize, true);
94 stepY = GridUtils.limitedEvenGridSpacing(stepY, scaleY, 100, gridSize, true);
95 //while(stepX * scaleX > 100) stepX /= 2;
96 //while(stepY * scaleY > 100) stepY /= 2;
98 while(stepX * scaleX < 50) stepX *= 2;
99 while(stepY * scaleY < 50) stepY *= 2;
105 g.setFont(rulerFont);
107 double previousText = -100;
110 for(double x = offsetX%stepX-stepX; x < bounds.getMaxX(); x+=stepX) {
112 double val = (x-offsetX)/scaleX / getTransform().getScaleX();
113 double modifiedValue = modifyHorizontalValue(val);
114 String str = formatValue(modifiedValue);
115 FontMetrics fm = g.getFontMetrics();
116 Rectangle2D r = fm.getStringBounds(str, g);
117 if((x-r.getWidth()/2) > previousText) {
118 g.setColor(Color.BLACK);
119 g.drawString(str, (int)(x-r.getWidth()/2), (int)(bounds.getMinY()+1+r.getHeight()));
120 previousText = x+r.getWidth()/2+stepX/4;
124 g.drawLine((int)x, (int)bounds.getMinY()+12, (int)x, (int)bounds.getMinY()+19);
127 for(double x2 = x+stepX/5; x2 < x+stepX; x2+=stepX/5) {
129 g.drawLine((int)x2, (int)bounds.getMinY()+15, (int)x2, (int)bounds.getMinY()+19);
132 for(double x2 = x+stepX/10; x2 < x+stepX; x2+=stepX/5) {
134 g.drawLine((int)x2, (int)bounds.getMinY()+17, (int)x2, (int)bounds.getMinY()+19);
143 for(double y = offsetY%stepY-stepY; y < bounds.getMaxY(); y+=stepY) {
145 double val = (y-offsetY)/scaleY / getTransform().getScaleY();
146 double modifiedValue = modifyVerticalValue(val);
147 String str = formatValue(modifiedValue);
148 FontMetrics fm = g.getFontMetrics();
149 Rectangle2D r = fm.getStringBounds(str, g);
150 if(y-1+r.getHeight()/2 > previousText) {
151 g.setColor(Color.BLACK);
152 AffineTransform origTr = g.getTransform();
153 g.translate((int)(bounds.getMinX()), (int)(y+r.getWidth()/2));
154 g.rotate(-Math.PI / 2.0);
155 g.drawString(str, 0, (int)r.getHeight());
156 g.setTransform(origTr);
157 previousText = y-1+r.getHeight();
160 g.drawLine((int)bounds.getMinX()+12, (int)y, (int)bounds.getMinX()+19, (int)y);
163 for(double y2 = y+stepY/5; y2 < y+stepY; y2+=stepY/5) {
165 g.drawLine((int)bounds.getMinX()+15, (int)y2, (int)bounds.getMinX()+19, (int)y2);
168 for(double y2 = y+stepY/10; y2 < y+stepY; y2+=stepY/5) {
170 g.drawLine((int)bounds.getMinX()+17, (int)y2, (int)bounds.getMinX()+19, (int)y2);
180 * A method for subclasses to alter the actual X-value of the ruler
183 * @return possibly modified X-value
185 protected double modifyHorizontalValue(double value) {
190 * A method for subclasses to alter the actual Y-value of the ruler
193 * @return possibly modified Y-value
195 protected double modifyVerticalValue(double value) {
199 private static final transient int MAX_DIGITS = 5;
200 private static final transient double EPSILON = 0.01;
201 private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 4);
202 private static final transient String[] SI_UNIT_LARGE_PREFIXES = {
203 "k", "M", "G", "T", "P", "E", "Z", "Y"
206 public static String formatValue(double value) {
207 int magnitude = (int) Math.round(Math.log10(value));
208 //System.out.println("magnitude: " + magnitude + ", " + value);
209 int allowedDecimals = MAX_DIGITS;
210 allowedDecimals -= Math.abs(magnitude);
211 if (allowedDecimals < 0)
214 String valueStr = String.format(Locale.US, "%." + allowedDecimals + "f", value);
215 if (allowedDecimals > 0) {
216 for (int trunc = valueStr.length() - 1; trunc > 0; --trunc) {
217 char ch = valueStr.charAt(trunc);
219 valueStr = valueStr.substring(0, trunc);
222 if (valueStr.charAt(trunc) != '0') {
223 valueStr = valueStr.substring(0, trunc + 1);
227 if (Math.abs(value) + EPSILON > TRIM_THRESHOLD_MAX_VALUE) {
228 // Cut anything beyond a possible decimal dot out since they
229 // should not show anyway. This is a complete hack that tries to
230 // circumvent floating-point inaccuracy problems.
231 int dotIndex = valueStr.lastIndexOf('.');
233 valueStr = valueStr.substring(0, dotIndex);
238 double trimValue = value;
239 if (Math.abs(value)+EPSILON >= TRIM_THRESHOLD_MAX_VALUE) {
240 for (int i = 0; Math.abs(trimValue)+EPSILON >= TRIM_THRESHOLD_MAX_VALUE; ++i) {
241 double trim = trimValue / 1000;
242 if (Math.abs(trim)-EPSILON < TRIM_THRESHOLD_MAX_VALUE) {
243 valueStr = valueStr.substring(0, valueStr.length() - (i + 1) * 3);
244 valueStr += SI_UNIT_LARGE_PREFIXES[i];
251 if (valueStr.equals("-0"))
258 public Rectangle2D getBoundsInLocal() {