View Javadoc

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.Font;
23  import java.awt.FontMetrics;
24  import java.awt.Graphics2D;
25  import java.awt.Rectangle;
26  
27  import com.cosylab.gui.components.Gauger;
28  import com.cosylab.util.PrintfFormat;
29  
30  
31  /**
32  /**
33   * Value label displays value of gauger in textual form.
34   *
35   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
36   * @version $id$
37   */
38  public class ValueLabel
39  {
40  	private boolean unitsVisible = true;
41  
42  	private Gauger gauger = null;
43  
44  	//	private Font font = new Font("dialog", Font.PLAIN, 12);
45  	private String DEFAULT_FORMAT = "%0.2f";
46  	private String DEFAULT_TEXT = "val";
47  	private String text = DEFAULT_TEXT;
48  //	private String oldText = DEFAULT_TEXT;
49  	private String formatString = DEFAULT_FORMAT;
50  	private String units = "";
51  	private PrintfFormat formatter;
52  	private double lastValue = 0.0;
53  	private Rectangle bounds;
54  	private boolean changeSignificant = true;
55  	private int x;
56  	private int y;
57  
58  	/**
59  	 * Constructor for ValueLabel.
60  	 *
61  	 * @param gauger Gauger on which this value will display.
62  	 */
63  	public ValueLabel(Gauger gauger)
64  	{
65  		super();
66  		this.gauger = gauger;
67  		
68  		/**
69  		 * This is initial height for bounding box, since first time, text has
70  		 * not been drawn yet and height is not properly set. It is initialized
71  		 * to height of font as later in draw().
72  		 */
73  		bounds = new Rectangle(0, 0, 1, 16);
74  	}
75  
76  	/**
77  	 * Returns bounds of this label.
78  	 *
79  	 * @return Bounds of text.
80  	 */
81  	public Rectangle getBounds()
82  	{
83  		return bounds;
84  	}
85  
86  	/**
87  	 * Returns format used by this label.
88  	 *
89  	 * @return C-Style format.
90  	 */
91  	public String getFormat()
92  	{
93  		return formatString;
94  	}
95  
96  	/**
97  	 * Returns units symbol to append to text.
98  	 *
99  	 * @return Units.
100 	 */
101 	public String getUnits()
102 	{
103 		return units;
104 	}
105 
106 	private PrintfFormat getFormatter()
107 	{
108 		if(formatter == null) {
109 			formatter = new PrintfFormat(formatString + " " + (unitsVisible ? units : ""));
110 		}
111 
112 		return formatter;
113 	}
114 
115 	/**
116 	 * Sets new value to display. Value will be formatted according to the
117 	 * format specified and will have units symbol appended to it.
118 	 *
119 	 * @param value New value to display.
120 	 */
121 	public void setValue(double value)
122 	{
123 		String newText;
124 		try {
125 			newText = getFormatter().sprintf(value);
126 		} catch (Exception e) {
127 			newText = Double.toString(value);
128 		}
129 		lastValue = value;
130 
131 		if(!newText.equals(text)) {
132 			text = newText;
133 
134 			changeSignificant = true;
135 		} else {
136 			changeSignificant = false;
137 		}
138 	}
139 
140 	/**
141 	 * Returns whether the change is signifficant.
142 	 *
143 	 * @return True if text has changed.
144 	 */
145 	public boolean isChangeSignificant()
146 	{
147 		return changeSignificant;
148 	}
149 
150 	/**
151 	 * Renders this component.
152 	 *
153 	 * @param g Context to render to.
154 	 */
155 	public void draw(Graphics2D g)
156 	{
157 		Font f = gauger.getValueLabelFont();
158 		g.setFont(f);
159 		
160 		
161 		FontMetrics metrics = g.getFontMetrics();
162 		
163 		
164 		//		metrics.getStringBounds(outText, g);
165 		int width = metrics.stringWidth(text);
166 		int height = metrics.getHeight(); //+metrics.getDescent();
167 		bounds.setBounds((int)(x - width / 2),
168 		    (int)(y - (metrics.getAscent() + metrics.getDescent()) / 2), width,
169 		    height);
170 		g.drawString(text, x - width/2, y + height/2 );
171 	}
172 
173 	/**
174 	 * Sets center position of this component. 
175 	 *
176 	 * @param x coordinate of label center.
177 	 * @param y coordinate of label center.
178 	 */
179 	public void setPosition(int x, int y)
180 	{
181 		this.x = x;
182 		this.y = y;
183 	}
184 
185 	/**
186 	 * Sets format for this label.
187 	 *
188 	 * @param newFormat C-Style format.
189 	 * @see setValue()
190 	 */
191 	public void setFormat(String newFormat)
192 	{
193 		if(newFormat != formatString) {
194 			formatString = newFormat;
195 
196 			formatter = null;
197 			setValue(lastValue);
198 		}
199 	}
200 
201 	/**
202 	 * Sets units to append to text.
203 	 *
204 	 * @param newUnits Units symbol.
205 	 * @see setValue()
206 	 */
207 	public void setUnits(String newUnits)
208 	{
209 		units = newUnits;
210 		formatter = null;
211 		setValue(lastValue);
212 	}
213 
214 	/**
215 	 * Sets the units visibility.
216 	 * 
217 	 * @param b
218 	 */
219 	public void setUnitsVisible(boolean b) {
220 		this.unitsVisible = b;
221 		formatter = null;
222 		setValue(lastValue);
223 	}
224 
225 	/**
226 	 * Returns true if units are visible.
227 	 * 
228 	 * @return
229 	 */
230 	public boolean isUnitsVisible() {
231 		return unitsVisible;
232 	}
233 }
234 
235 /* __oOo__ */