1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans-Common.
5 *
6 * CosyBeans-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.components.gauger;
21
22 import java.awt.Graphics;
23 import java.awt.Rectangle;
24 import java.awt.geom.Point2D;
25
26
27 /**
28 * This transform defines shape of horizontal linear gauger. It imposes
29 * constraints on minimum and maximum sizes and defines borders, as well as
30 * position of value label.
31 *
32 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
33 * @version $id$
34 */
35 public class LinearHorizontalTransform extends ScaleTransform
36 {
37 /** Maximum height to which the scale will grow */
38 private static final int MAXIMUM_HEIGHT = 80;
39
40 /** Minimum height to which the scale will shrink */
41 private static final int MINIMUM_HEIGHT = 5;
42
43 // /** Horizontal offset of scale from border */
44 // private static final double RELATIVE_X_OFFSET = 0.05;
45
46 // /** Vertical offset of scale from border */
47 // private static final double RELATIVE_Y_OFFSET = 0.15;
48
49 private int width = 0;
50 private int height = 0;
51 private double xOffset = 0;
52 private double yOffset = 0;
53 private double scaleWidth;
54 private int tickOffset = 0;
55
56 /**
57 * Sets the dimensions of this scale and calculates internal parameters.
58 *
59 * @param w int Gauger width.
60 * @param h int Gauger height.
61 * @param marginX int Horizontal margin from border in pixels.
62 * @param marginY int Vertical margin from border in pixels.
63 * @param tickOffset int Minimum offset of first and last tick from scale
64 * in pixels.
65 *
66 * @see ScaleTransform#setParameters(int, int, int, int)
67 */
68 public void setParameters(int w, int h, int marginX, int marginY,
69 int tickOffset)
70 {
71 this.tickOffset = tickOffset;
72
73 width = w - 2 * (marginX + tickOffset);
74 height = h - 2 * marginY;
75
76 // width = w - (int) (2 * w * RELATIVE_X_OFFSET) - 5;
77 // height = h - (int) (2 * h * RELATIVE_Y_OFFSET) - 5;
78 height = Math.min(height, MAXIMUM_HEIGHT);
79 height = Math.max(height, MINIMUM_HEIGHT);
80
81 xOffset = (w - width) / 2.0;
82 yOffset = (h - height - marginY) / 2.0;
83
84 scaleWidth = Math.abs(width - 2 * tickOffset);
85
86 Rectangle outline = new Rectangle((int)xOffset, (int)yOffset, width,
87 height);
88 addSegment(new RectangleSegment(outline));
89
90 setLabelPosition(w / 2, (int)(h - (h - height - yOffset) / 2));
91 }
92
93 /**
94 * Converts point in scale space to point in gauger space. Conversion is
95 * linear.
96 *
97 * @param scaleSpace PointDouble Point in scale space.
98 * @param cartesianSpace Point Point in gauger space.
99 *
100 * @see ScaleTransform#mapUVtoXY(PointDouble, Point)
101 */
102 public void mapUVtoXY(Point2D scaleSpace, Point2D cartesianSpace)
103 {
104 double x = (xOffset + scaleWidth(0.0) * scaleSpace.getX() + tickOffset);
105 double y = (yOffset + (height - 1) * (1 - scaleSpace.getY()) - 1);
106 cartesianSpace.setLocation(x, y);
107 }
108
109 /**
110 * Not implemented.
111 *
112 * @param cartesianSpace Point
113 * @param scaleScape PointDouble
114 *
115 * @see ScaleTransform#maxXYtoUV(Point, PointDouble)
116 */
117 public void mapXYtoUV(Point2D cartesianSpace, Point2D scaleScape)
118 {
119 }
120
121 /**
122 * Returns width of scale at relative vertical position. Returns constant
123 * width.
124 *
125 * @param v double Relative vertical position in range 0.0 to 1.0
126 *
127 * @return double Width in pixels.
128 *
129 * @see ScaleTransform#scaleWidth(double)
130 */
131 public double scaleWidth(double v)
132 {
133 return Math.abs(width - 2 * tickOffset);
134 }
135
136 /**
137 * Returns scale height at relative horizontal position. Returns constant
138 * height.
139 *
140 * @param u double Relative horizontal position in range 0.0 to 1.0.
141 *
142 * @return double Heightin pixels.
143 *
144 * @see ScaleTransform#scaleHeight(double)
145 */
146 public double scaleHeight(double u)
147 {
148 return height;
149 }
150
151 /**
152 * Calculates tick label width when rendered.
153 *
154 * @param g Graphics Context to render to.
155 * @param x double Relative value of tick position.
156 * @param text String Text to render.
157 *
158 * @return int Width in pixels.
159 *
160 * @see com.cosylab.gui.components.gauger.ScaleTransform#measureTick(Graphics,
161 * double, String)
162 */
163 public int measureTick(Graphics g, double x, String text)
164 {
165 return g.getFontMetrics().stringWidth(text);
166 }
167 }
168
169 /* __oOo__ */