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;
21  
22  import java.awt.Color;
23  import java.awt.Dimension;
24  import java.awt.Font;
25  
26  import javax.swing.JComponent;
27  import javax.swing.JFrame;
28  import javax.swing.TransferHandler;
29  
30  import com.cosylab.application.state.State;
31  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
32  import com.cosylab.gui.components.range2.RangedValuePolicy;
33  import com.cosylab.gui.components.util.CosyTransferHandler;
34  import com.cosylab.gui.components.util.PopupManager;
35  
36  
37  /**
38   * <code>LabelledGauger</code> is an extension of AbstractDisplayerPanel which
39   * provides <code>Gauger</code> as the numerical value displayer.  
40   * 
41   * @see AbstractDisplayerPanel
42   * @see Gauger
43   * @version 1.0
44   */
45  public class LabelledGauger extends AbstractDisplayerPanel
46  {
47  	private static final long serialVersionUID = 1L;
48  	private Gauger gauger = null;
49  	private AbstractCustomizerPanel customizer;
50  	
51  	/**
52  	 * Creates new gauger displayer with the given title.
53  	 */
54  	public LabelledGauger(String title)
55  	{
56  		super();
57  		setTitle(title);
58  		setTitleVisible(true);
59  		setLayoutOrientation(VERTICAL_LAYOUT);
60  		setResizable(true);
61  		setEnhanced(true);
62  		setValue(5);
63  		setFormat("%1.1f");
64  		setUnits("");
65  	}
66  
67  	/**
68  	 * Creates new gauger displayer.
69  	 */
70  	public LabelledGauger()
71  	{
72  		this(null);
73  	}
74  
75  	/* (non-Javadoc)
76  	 * @see com.cosylab.gui.displayers.Displayer#isEditable()
77  	 */
78  	public boolean isEditable()
79  	{
80  		return false;
81  	}
82  
83  	/* (non-Javadoc)
84  	 * @see com.cosylab.gui.displayers.DoubleDisplayer#setFormat(java.lang.String)
85  	 */
86  	public void setFormat(String value)
87  	{
88  		String oldValue = getFormat();
89  		getGauger().setFormat(value);
90  		firePropertyChange("format", oldValue, value);
91  	}
92  
93  	/* (non-Javadoc)
94  	 * @see com.cosylab.gui.displayers.DoubleDisplayer#getFormat()
95  	 */
96  	public String getFormat()
97  	{
98  		return getGauger().getFormat();
99  	}
100 
101 	/**
102 	 * Switches between the logarithmic and linear scale.
103 	 * 
104 	 * @param b true if scale should be logarithmic
105 	 */
106 	public void setLogarithmicScale(boolean b)
107 	{
108 		if (b) {
109 			setLogarithmicScale();
110 		} else {
111 			setLinearScale();
112 		}
113 	}
114 
115 	/**
116 	 * Returns true if logarithmic scale is used.
117 	 * 
118 	 * @return
119 	 */
120 	public boolean isLogarithmicScale()
121 	{
122 		return getGauger().isLogarithmicScale();
123 	}
124 
125 	/**
126 	 * Sets the maximum value used by the displayer.
127 	 * 
128 	 * @param value new maximum
129 	 */
130 	public void setMaximum(double value)
131 	{	
132 		double oldValue = getMaximum();
133 		getGauger().setMaximum(value);
134 		firePropertyChange("maximum", oldValue, value);
135 	}
136 
137 	/**
138 	 * Returns the maximum value shown by displayer.
139 	 * 
140 	 * @return the maximum
141 	 */
142 	public double getMaximum()
143 	{
144 		return getGauger().getMaximum();
145 	}
146 
147 	/**
148 	 * Sets the minimum value shown by the displayer.
149 	 * 
150 	 * @param value new minimum
151 	 */
152 	public void setMinimum(double value)
153 	{
154 		double oldValue = getMinimum();
155 		getGauger().setMinimum(value);
156 		firePropertyChange("minimum", oldValue, value);
157 	}
158 
159 	/**
160 	 * Returns the minimum value shown by displayer.
161 	 * 
162 	 * @return the minimum
163 	 */
164 	public double getMinimum()
165 	{
166 		return getGauger().getMinimum();
167 	}
168 
169 	/**
170 	 * Sets the scale mode for the gauger.
171 	 * 
172 	 * @param policy
173 	 * @deprecated use {@link #setValuePolicy(RangedValuePolicy)}
174 	 * @see Gauger#setScaleMode(int)
175 	 */
176 	public void setScaleMode(int policy)
177 	{
178 		int oldValue = getScaleMode();
179 		getGauger().setScaleMode(policy);
180 		firePropertyChange("scaleMode", oldValue, policy);
181 	}
182 
183 	/**
184 	 * Returns the scale mode of the gauger.
185 	 * 
186 	 * @return the scale mode
187 	 * @deprecated use {@link #getValuePolicy()}
188 	 * @see Gauger#getScaleMode()
189 	 */
190 	public int getScaleMode()
191 	{
192 		return getGauger().getScaleMode();
193 	}
194 	
195 	/**
196 	 * Sets a new value policy.
197 	 * 
198 	 * @param policy new value policy
199 	 */
200 	public void setValuePolicy(RangedValuePolicy policy) {
201 		RangedValuePolicy oldValue = getValuePolicy();
202 		getGauger().setValuePolicy(policy);
203 		firePropertyChange("valuePolicy", oldValue, policy);
204 	}
205 	
206 	/**
207 	 * Returns the value policy.
208 	 * 
209 	 * @return value policy
210 	 */
211 	public RangedValuePolicy getValuePolicy() {
212 		return getGauger().getValuePolicy();
213 	}
214 
215 	/* (non-Javadoc)
216 	 * @see com.cosylab.application.state.StateOriginator#setState(com.cosylab.application.state.State)
217 	 */
218 	public void setState(State state)
219 	{
220 		super.setState(state);
221 		getGauger().setState(state.getState("gauger"));
222 	}
223 
224 	/* (non-Javadoc)
225 	 * @see com.cosylab.application.state.StateOriginator#getState()
226 	 */
227 	public State getState()
228 	{
229 		State state = super.getState(); 
230 		state.putState("gauger",getGauger().getState());
231 		return state;
232 	}
233 
234 	/**
235 	 * Toggles the units visibility.
236 	 * 
237 	 * @param b true if units are visible
238 	 */
239 	public void setUnitsVisible(boolean b) {
240 		boolean oldB = isUnitsVisible();
241 		
242 		if (oldB == b) return;
243 		
244 		getGauger().setUnitsVisible(b);
245 		
246 		firePropertyChange("unitsVisible",oldB,b);
247 	}
248 	
249 	/**
250 	 * Returns true if units are visible.
251 	 * 
252 	 * @return true if units are displayed.
253 	 */
254 	public boolean isUnitsVisible() {
255 		return getGauger().isUnitsVisible();
256 	}
257 	
258 	/**
259 	 * Returns the Gauger displayer associated with this component.
260 	 * 
261 	 * @return
262 	 */
263 	protected Gauger getGauger() {
264 		if (gauger==null) {
265 			gauger = new Gauger(){
266 				private static final long serialVersionUID = 1L;
267 				
268 				public AbstractCustomizerPanel getCustomizer()
269 				{
270 					if (customizer == null) {
271 						customizer = AbstractCustomizerPanel.findCustomizer(LabelledGauger.this);
272 					}
273 
274 					return customizer;
275 				}
276 			};
277 			Dimension size = new Dimension(250, 150);
278 			gauger.setPreferredSize(size);
279 			gauger.setSize(size);
280 			gauger.setMinimumSize(new Dimension(120,85));
281 		}
282 		return gauger;
283 	}
284 	
285 	/*
286 	 * (non-Javadoc)
287 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#getValueComponent()
288 	 */
289 	@Override
290 	protected JComponent getValueComponent() {
291 		return getGauger();
292 	}
293 		
294 	/**
295 	 * Returns the actual value on the displayer.
296 	 * 
297 	 * @return the value
298 	 */
299 	public double getValue() {
300 		return getGauger().getValue();
301 	}
302 	
303 	/**
304 	 * Sets the value to the displayer.
305 	 * 
306 	 * @param d new value
307 	 */
308 	public void setValue(double d) {
309 		double oldValue = getValue();
310 		getGauger().setValue(d);
311 		firePropertyChange("value", oldValue, d);
312 	}
313 	
314 	/*
315 	 * (non-Javadoc)
316 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#layoutDisplayer()
317 	 */
318 	@Override
319 	protected void layoutDisplayer() {
320 		super.layoutDisplayer();
321 		getGauger().doLayout();
322 	}
323 	
324 	/**
325 	 * Returns the string representing the units.
326 	 * 
327 	 * @return the units
328 	 */
329 	public String getUnits() {
330 		return getGauger().getUnits();
331 	}
332 	
333 	/**
334 	 * Sets the units shown by this displayer.
335 	 * 
336 	 * @param u new units
337 	 */
338 	public void setUnits(String u) {
339 		String oldValue = getUnits();
340 		getGauger().setUnits(u);
341 		firePropertyChange("units", oldValue, u);
342 	}
343 	
344 	/*
345 	 * (non-Javadoc)
346 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#getCustomizer()
347 	 */
348 	public AbstractCustomizerPanel getCustomizer()
349 	{
350 		if (customizer == null) {
351 			customizer = AbstractCustomizerPanel.findCustomizer(this);
352 		}
353 
354 		return customizer;
355 	}
356 	
357 	/*
358 	 * (non-Javadoc)
359 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#getPopupManager()
360 	 */
361 	public PopupManager getPopupManager()
362 	{
363 		return getGauger().getPopupManager();
364 	}
365 	
366 	/*
367 	 * (non-Javadoc)
368 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#setPopupEnabled(boolean)
369 	 */
370 	@Override
371 	public void setPopupEnabled(boolean enabled) {
372 		getGauger().setPopupEnabled(enabled);
373 		super.setPopupEnabled(enabled);
374 	}
375 
376 	/**
377 	 * Returns the font used to display value.
378 	 * 
379 	 * @return the value font
380 	 */
381 	public Font getValueLabelFont() {
382 	    return gauger.getValueLabelFont();
383     }
384 	
385 	/**
386 	 * Sets the font used to display value.
387 	 * 
388 	 * @param font new font
389 	 */
390 	public void setValueLabelFont(Font font) {
391 		Font oldValue = getValueLabelFont();
392 		gauger.setValueLabelFont(font);
393 		firePropertyChange("valueLabelFont", oldValue, font);
394 	}
395 	
396 	/**
397 	 * Convenient method to set logarithmic scale.
398 	 *
399 	 */
400 	public void setLogarithmicScale() {
401 		boolean oldValue = isLogarithmicScale();
402 		gauger.setLogarithmicScale();
403 		firePropertyChange("logarithmicScale", oldValue, true);
404 	}
405 	
406 	/**
407 	 * Convenient method to set linear scale.
408 	 *
409 	 */
410 	public void setLinearScale() {
411 		boolean oldValue = isLinearScale();
412 		gauger.setLinearScale();
413 		firePropertyChange("linearScale", oldValue, true);
414 	}
415 	
416 	/**
417 	 * Convenient method to see if linear scale is used.
418 	 * 
419 	 * @return
420 	 */
421 	public boolean isLinearScale() {
422 		return gauger.isLinearScale();
423 	}
424 	
425 	/**
426 	 * Sets the color which indicates that the value is out of bounds.
427 	 * 
428 	 * @param color new out of bounds color
429 	 */
430 	public void setOutOfBoundsColor(Color color) {
431 		Color oldValue = gauger.getOutOfBoundsColor();
432 		gauger.setOutOfBoundsColor(color);
433 		firePropertyChange("outOfBoundsColor", oldValue, color);
434 	}
435 	
436 	/**
437 	 * Returns the value which indicates that the value is out of bounds.
438 	 * 
439 	 * @return the out of bounds color
440 	 */
441 	public Color getOutOfBoundsColor() {
442 		return gauger.getOutOfBoundsColor();
443 	}
444 	
445 	/**
446 	 * Returns the color which indicates the warning.
447 	 * 
448 	 * @return the warning color
449 	 */
450 	public Color getWarningColor() {
451 		return gauger.getWarningColor();
452 	}
453 	
454 	/**
455 	 * Sets the warning color for the gauger.
456 	 * 
457 	 * @param color new warning color
458 	 */
459 	public void setWarningColor(Color color) {
460 		Color oldValue = gauger.getWarningColor();
461 		gauger.setWarningColor(color);
462 		firePropertyChange("warningColor", oldValue, color);
463 	}
464 	
465 	/**
466 	 * Sets new upper warning limit. At this value the needle will 
467 	 * change its color to the set warning color. The limit should be given 
468 	 * in percentage of the current span of the gauger.
469 	 *  
470 	 * @param value new warning limit
471 	 */
472 	public void setHighWarningLimit(double value) {
473 		double oldValue = gauger.getHighWarningLimit();
474 		gauger.setHighWarningLimit(value);
475 		firePropertyChange("highWarningLimit", oldValue, value);
476 	}
477 	
478 	/**
479 	 * Returns the upper warning limit.
480 	 * 
481 	 * @return upper warning limit
482 	 */
483 	public double getHighWarningLimit() {
484 		return gauger.getHighWarningLimit();
485 	}
486 	
487 	/**
488 	 * Sets new lower warning limit. At this value the needle will 
489 	 * change its color to the set warning color. The limit should be given 
490 	 * in percentage of the current span of the gauger.
491 	 *  
492 	 * @param value new warning limit
493 	 */
494 	public void setLowWarningLimit(double value) {
495 		double oldValue = gauger.getLowWarningLimit();
496 		gauger.setLowWarningLimit(value);
497 		firePropertyChange("lowWarningLimit", oldValue, value);
498 	}
499 	
500 	/**
501 	 * Returns the lower warning limit.
502 	 * 
503 	 * @return upper warning limit
504 	 */
505 	public double getLowWarningLimit() {
506 		return gauger.getLowWarningLimit();
507 	}
508 	
509 	/**
510 	 * Sets new value and bounds to the displayer.
511 	 * 
512 	 * @param newMin new minimum
513 	 * @param newMax new maximum
514 	 * @param newValue new maximum
515 	 */
516 	public void setValue(double newMin, double newMax, double newValue) {
517 		double oldValue = getValue();
518 		gauger.setValue(newMin, newMax, newValue);
519 		firePropertyChange("value", oldValue, newValue);
520 	}
521 	
522     /* (non-Javadoc)
523      * @see javax.swing.JComponent#setTransferHandler(javax.swing.TransferHandler)
524      */
525     @Override
526     public void setTransferHandler(TransferHandler newHandler) {
527     	super.setTransferHandler(newHandler);
528     	CosyTransferHandler.registerTransferHandler(this,newHandler,new JComponent[]{getValueComponent()});
529     }
530     
531     /**
532 	 * Run test applet.
533 	 *
534 	 * @param args command line parameters
535 	 */
536 	public static void main(String[] args)
537 	{
538 		Thread t = new Thread(){
539 			public void run(){
540 				JFrame f= new JFrame();
541 				f.setSize(400,400);
542 				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
543 				
544 				LabelledGauger g= new LabelledGauger("Labelled Gauger");
545 //				g.setMaximum(100);
546 //				g.setMinimum(0);
547 //				g.setValue(50);
548 				g.setResizable(true);
549 				g.setUnits("V");
550 				g.setFormat("%1.2f");
551 				f.getContentPane().add(g);
552 				
553 				f.setVisible(true);
554 //				while (true){
555 //					g.setValue(g.getMinimum() + Math.random() * (g.getMaximum() - g.getMinimum()));
556 //					
557 //					try {
558 //						sleep(500);
559 //					} catch (InterruptedException e) {
560 //						break;
561 //					}
562 //				}
563 			}
564 		};
565 		t.run();
566 	}
567 
568 }
569 
570 /* __oOo__ */