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  import java.awt.BorderLayout;
22  import java.awt.Color;
23  import java.awt.event.WindowAdapter;
24  import java.awt.event.WindowEvent;
25  
26  import javax.swing.JComponent;
27  import javax.swing.JFrame;
28  
29  import com.cosylab.application.state.State;
30  import com.cosylab.events.SetListener;
31  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
32  import com.cosylab.gui.components.util.PopupManageable;
33  import com.cosylab.gui.components.wheelswitch.AbstractWheelswitchFormatter;
34  import com.cosylab.gui.components.wheelswitch.WheelswitchFormatter;
35  
36  
37  /**
38   * <code>LabelledWheelswitch</code> is extended <code>Wheelswitch</code> with
39   * title label and unit label in same style.
40   *
41   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
42   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
43   * @version $id$
44   */
45  public class LabelledWheelswitch extends AbstractNumericDisplayerPanel implements PopupManageable
46  {
47  	private static final long serialVersionUID = 1L;
48  	/** Value property name. */
49  	public static final String VALUE = Wheelswitch.VALUE;
50  	private Wheelswitch number;
51  	private AbstractCustomizerPanel customizer;
52  	/**
53  	 * Creates a new LabelledWheelswitch object.
54  	 */
55  	public LabelledWheelswitch()
56  	{
57  		this(0.0, "3.2f", null, null);
58  	}
59  
60  	/**
61  	 * Creates a new LabelledWheelswitch object.
62  	 *
63  	 * @param value initial double value
64  	 */
65  	public LabelledWheelswitch(double value)
66  	{
67  		this(value, "3.2f" , null, null);
68  	}
69  
70  	/**
71  	 * Creates a new LabelledWheelswitch object.
72  	 *
73  	 * @param format initial number (C-style) format
74  	 */
75  	public LabelledWheelswitch(String format)
76  	{
77  		this(0.0, format, null, null);
78  	}
79  
80  	/**
81  	 * Creates a new LabelledWheelswitch object.
82  	 *
83  	 * @param value initial double value
84  	 * @param format initial number (C-style) format
85  	 * @param title initial title
86  	 * @param units initial units
87  	 */
88  	public LabelledWheelswitch(double value, String format, String title,
89  	    String units)
90  	{
91  		super();
92  
93  		setValue(value);
94  		setFormat(format);
95  		setUnits(units);
96  		setTitle(title);
97  
98  		setUnitsVisible(true);
99  		setTitleVisible(true);
100 		setLayoutOrientation(HORIZONTAL_LAYOUT);
101 		setResizable(true);
102 		setEnhanced(true);
103 		addMouseListener(getPopupManager().getMouseHook());	
104 		//Ike's FR by jbobnar RT#41405
105 		getTitleComponent().addMouseListener(getWheelswitch().handler);
106 	}
107 		
108 	/*
109 	 * (non-Javadoc)
110 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#getCustomizer()
111 	 */
112 	public AbstractCustomizerPanel getCustomizer()
113 	{
114 		if (customizer == null) {
115 			customizer = AbstractCustomizerPanel.findCustomizer(this);
116 		}
117 
118 		return customizer;
119 	}
120 			
121 
122 	/**
123 	 * Sets the editability of the wheelswitch.
124 	 *
125 	 * @param newEditable
126 	 */
127 	public void setEditable(boolean newEditable)
128 	{
129 		if (newEditable == isEditable()) return;
130 		getWheelswitch().setEditable(newEditable);
131 		//has to fire event EVERY time!
132 		firePropertyChange("editable",!newEditable,newEditable);
133 	}
134 
135 	/**
136 	 * Returns whether the wheelswitch can be edited  by the user.
137 	 *
138 	 * @return boolean
139 	 */
140 	public boolean isEditable()
141 	{
142 		return getWheelswitch().isEditable();
143 	}
144 
145 	/**
146 	 * Sets the maximum allowed value.
147 	 *
148 	 * @param newValue
149 	 *
150 	 * @see WheelswitchFormatter#setMaximum(double)
151 	 */
152 	public void setMaximum(double newValue)
153 	{
154 		double oldValue = getMaximum();
155 		if (newValue == oldValue) return;
156 		getWheelswitch().setGraphMax(newValue);
157 		super.setMaximumValue(new Double(getWheelswitch().getGraphMax()));
158 		newValue = getMaximum();
159 		firePropertyChange("maximum", oldValue, newValue);
160 	}
161 
162 	/**
163 	 * Returns the maximum alowed value.
164 	 *
165 	 * @return double
166 	 *
167 	 * @see WheelswitchFormatter#getMaximum()
168 	 */
169 	public double getMaximum()
170 	{
171 		return getWheelswitch().getGraphMax();
172 	}
173 
174 	/**
175 	 * Sets the minimum allowed value.
176 	 *
177 	 * @param newValue
178 	 *
179 	 * @see WheelswitchFormatter#setMinimum(double)
180 	 */
181 	public void setMinimum(double newValue)
182 	{
183 		double oldValue = getMinimum();
184 		if (newValue == oldValue) return;
185 		getWheelswitch().setGraphMin(newValue);
186 		super.setMinimumValue(new Double(getWheelswitch().getGraphMin()));
187 		newValue = getMinimum();
188 		firePropertyChange("minimum", oldValue, newValue);
189 	}
190 
191 	/**
192 	 * Returns the minimum alowed value.
193 	 *
194 	 * @return double
195 	 *
196 	 * @see WheelswitchFormatter#getMaximum()
197 	 */
198 	public double getMinimum()
199 	{
200 		return getWheelswitch().getGraphMin();
201 	}
202 
203 	/**
204 	 * Sets the maximum and minimum allowed values.
205 	 *
206 	 * @param max
207 	 * @param min
208 	 *
209 	 * @see com.cosylab.gui.components.Wheelswitch#setMaxMin(double, double)
210 	 */
211 	public void setMaxMin(double max, double min)
212 	{
213 		setMaximum(max);
214 		setMinimum(min);
215 	}
216 
217 	/*
218 	 * (non-Javadoc)
219 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#setState(com.cosylab.application.state.State)
220 	 */
221 	public void setState(State state)
222 	{
223 		super.setState(state);
224 
225 		setTiltingEnabled(state.getBoolean("tiltingEnabled", isTiltingEnabled()));
226 		setEditable(state.getBoolean("editable", isEditable()));
227 		setMaximum(state.getDouble("maximum", getMaximum()));
228 		setMinimum(state.getDouble("minimum", getMinimum()));
229 	}
230 
231 	/*
232 	 * (non-Javadoc)
233 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#getState()
234 	 */
235 	public State getState()
236 	{
237 		State state = super.getState();
238 
239 		state.putDouble("maximum", getMaximum());
240 		state.putDouble("minimum", getMinimum());
241 		state.putBoolean("editable", isEditable());
242 		state.putBoolean("tiltingEnabled", isTiltingEnabled());
243 
244 		return state;
245 	}
246 
247 	/**
248 	 * Enables/disables tilting of the displayer. Displayer tilts when out of bounds.
249 	 *
250 	 * @param b true if tilting is enabled
251 	 * @see Wheelswitch#setTiltingEnabled(boolean);
252 	 */
253 	public void setTiltingEnabled(boolean b)
254 	{	
255 		if (isTiltingEnabled() == b) return; 
256 		getWheelswitch().setTiltingEnabled(b);
257 		firePropertyChange("tiltingEnabled", !b, b);
258 	}
259 
260 	/**
261 	 * Returns true if tilting is enabled.
262 	 *
263 	 * @return true if tilting is enabled
264 	 */
265 	public boolean isTiltingEnabled()
266 	{
267 		return getWheelswitch().isTiltingEnabled();
268 	}
269 
270 	/**
271 	 * Sets the value and displays it in the wheelswitch. The method may  also
272 	 * change the current digit selection if neccessary in order to point to
273 	 * the same decimal digit of the displayed value.
274 	 *
275 	 * @param newValue
276 	 *
277 	 * @see WheelswitchFormatter#setValue(double)
278 	 */
279 	public void setValue(double newValue)
280 	{	
281 		double oldValue = getValue();
282 		getWheelswitch().setValue(newValue);
283 		firePropertyChange("value", oldValue, newValue);
284 	}
285 	
286 	/**
287 	 * Sets the animated property.
288 	 * 
289 	 * @param animated
290 	 * @see Wheelswitch#setAnimated(boolean)
291 	 */
292 	public void setAnimated(boolean animated) {
293 		if (isAnimated() == animated) return;
294 		getWheelswitch().setAnimated(animated);
295 		firePropertyChange("animated", !animated, animated);
296 	}
297 	
298 	/**
299 	 * Returns weather the labelled wheelswitch is animated.
300 	 * 
301 	 * @return animated property
302 	 * @see Wheelswitch#isAnimated()
303 	 */
304 	public boolean isAnimated() {
305 		return getWheelswitch().isAnimated();
306 	}
307 
308 	/**
309 	 * Returns the value displayed by the <code>Wheelswitch</code> and stored by
310 	 * the <code>formatter</code>.
311 	 *
312 	 * @return double
313 	 *
314 	 * @see WheelswitchFormatter#getValue()
315 	 */
316 	public double getValue()
317 	{
318 		return getWheelswitch().getValue();
319 	}
320 
321 	/**
322 	 * Adds set listener to the <code>NumberField</code>.
323 	 *
324 	 * @see NumberField#addSetListener(SetListener)
325 	 */
326 	public void addSetListener(SetListener l)
327 	{
328 		getWheelswitch().addSetListener(l);
329 	}
330 
331 	/**
332 	 * Removes set listener from the <code>NumberField</code>.
333 	 *
334 	 * @see NumberField#removeSetListener(SetListener)
335 	 */
336 	public void removeSetListener(SetListener l)
337 	{
338 		getWheelswitch().removeSetListener(l);
339 	}
340 
341 	protected Wheelswitch getWheelswitch()
342 	{
343 		if (number == null) {
344 			number = new Wheelswitch();
345 			
346 			//			number.addPropertyChangeListener(new PropertyChangeListener() {
347 			//					/**
348 			//					 * @see java.beans.PropertyChangeListener#propertyChange(PropertyChangeEvent)
349 			//					 */
350 			//					public void propertyChange(PropertyChangeEvent evt)
351 			//					{
352 			//						firePropertyChange(evt.getPropertyName(),
353 			//						    evt.getOldValue(), evt.getNewValue());
354 			//					}
355 			//				});
356 			number.addMouseListener(getPopupManager().getMouseHook());
357 			number.setUnitSeparate(true);
358 			number.setPopupEnabled(false);
359 			//Ike's FR by jbobnar RT#41405
360 			addMouseListener(number.handler);
361 			getUnitsComponent().addMouseListener(number.handler);
362 		}
363 
364 		return number;
365 	}
366 
367 	/*
368 	 * (non-Javadoc)
369 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#getValueComponent()
370 	 */
371 	protected JComponent getValueComponent()
372 	{
373 		return getWheelswitch();
374 	}
375 	
376 	/*
377 	 * (non-Javadoc)
378 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#internalSetEnabled()
379 	 */
380 	protected void internalSetEnabled()
381 	{
382 		super.internalSetEnabled();
383 		getWheelswitch().setEnabled(isEnabled());
384 	}
385 
386 	/*
387 	 * (non-Javadoc)
388 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#internalSetEnhanced()
389 	 */
390 	protected void internalSetEnhanced()
391 	{
392 		super.internalSetEnhanced();
393 		getWheelswitch().setEnhanced(isEnhanced());
394 	}
395 
396 	/*
397 	 * (non-Javadoc)
398 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#internalSetFormat()
399 	 */
400 	protected void internalSetFormat()
401 	{
402 		super.internalSetFormat();
403 
404 		String tFormat = getFormat();
405 
406 		if (tFormat == null) {
407 			tFormat = NumberField.createDefaultFormat(Double.class);
408 		}
409 
410 		getWheelswitch().setFormat(WheelswitchFormatter.transformFormat(tFormat));
411 	}
412 
413 	/*
414 	 * (non-Javadoc)
415 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#internalSetResizable()
416 	 */
417 	protected void internalSetResizable()
418 	{
419 		super.internalSetResizable();
420 		getWheelswitch().revalidate();
421 	}
422 
423 	/* (non-Javadoc)
424 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#setUnitsVisible(boolean)
425 	 */
426 	public void setUnitsVisible(boolean b) {
427 		if (b) getWheelswitch().setUnit(getUnits()); 
428 		else getWheelswitch().setUnit(null);
429 		super.setUnitsVisible(b);
430 	}
431 
432 	/* (non-Javadoc)
433 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#setUnits(java.lang.String)
434 	 */
435 	public void setUnits(String value) {
436 		if (isUnitsVisible()) {
437 			getWheelswitch().setUnit(value);
438 		}
439 		super.setUnits(value);
440 	}
441 	/* (non-Javadoc)
442 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#layoutValueAndTitleAndUnits()
443 	 */
444 	protected void layoutValueAndTitleAndUnits() {
445 		super.layoutValueAndTitle();
446 	}
447 
448 	/* (non-Javadoc)
449 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#layoutValueAndTitleAndUnitsAndBounds()
450 	 */
451 	protected void layoutValueAndTitleAndUnitsAndBounds() {
452 		super.layoutValueAndTitleAndBounds();
453 	}
454 	
455 	/* (non-Javadoc)
456 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#layoutValueAndUnits()
457 	 */
458 	protected void layoutValueAndUnits() {
459 		super.layoutValue();
460 	}
461 	
462 	/* (non-Javadoc)
463 	 * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#layoutValueAndUnitsAndBounds()
464 	 */
465 	protected void layoutValueAndUnitsAndBounds() {
466 		super.layoutValueAndBounds();
467 	}
468 	
469 	/* (non-Javadoc)
470 	 * @see javax.swing.JComponent#setBackground(java.awt.Color)
471 	 */
472 	public void setBackground(Color bg) {
473 		getWheelswitch().setBackground(bg);
474 		super.setBackground(bg);
475 	}
476 	/**
477 	 * Set the formatter fot the value.
478 	 * 
479 	 * @param formatter new formatter
480 	 * @see Wheelswitch#setFormatter(AbstractWheelswitchFormatter)
481 	 */
482 	public void setFormatter(AbstractWheelswitchFormatter formatter) {
483 		AbstractWheelswitchFormatter oldValue = getFormatter();
484 		getWheelswitch().setFormatter(formatter);
485 		firePropertyChange("formatter", oldValue, formatter);
486 	}
487 	/**
488 	 * Returns the formatter employed by the displayer.
489 	 * 
490 	 * @see Wheelswitch#getFormatter()
491 	 */
492 	public AbstractWheelswitchFormatter getFormatter() {
493 		return getWheelswitch().getFormatter();
494 	}
495 	
496 	/* (non-Javadoc)
497      * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#setTitleMaximumFontSize(int)
498      */
499     public void setTitleMaximumFontSize(int max) {
500         getWheelswitch().setUnitsMaximumFontSize(max);
501         super.setTitleMaximumFontSize(max);
502     }
503     
504     /* (non-Javadoc)
505      * @see com.cosylab.gui.components.AbstractNumericDisplayerPanel#setTitleMinimumFontSize(int)
506      */
507     public void setTitleMinimumFontSize(int min) {
508         getWheelswitch().setUnitsMinimumFontSize(min);        
509         super.setTitleMinimumFontSize(min);
510     }
511     
512     /**
513      * Set <code>true</code> if digits should take up all space, set 
514      * <code>false</code> if place for not visible digits should be reserved
515      * (there is no resizing of digits and labels when 9 changes to 10 etc.).
516      * 
517      * @param bool boolean value
518      */
519     public void setDigitsTakeUpAllSpace(boolean bool) {
520     	if(getWheelswitch().getDigitsTakeUpAllSpace() == bool)
521     		return;
522     	
523     	firePropertyChange("digitsTakeUpAllSpace", getWheelswitch().getDigitsTakeUpAllSpace(), bool);
524     	
525     	getWheelswitch().setDigitsTakeUpAllSpace(bool);
526     }
527     
528     /**
529      * <code>true</code> if digits take up all space,
530      *  otherwise <code>false</code>
531      * @return
532      */
533     public boolean getDigitsTakeUpAllSpace() {
534     	return getWheelswitch().getDigitsTakeUpAllSpace();
535     }
536     
537     /**
538      * If {@link #setDigitsTakeUpAllSpace(boolean)} is set to <code>false</code>
539      * the place for <code>numberOfAllDigits</code> is reserved. Not needed 
540      * additional digits are not shown.
541      * 
542      * @param numberOfAllDigits number of all digits
543      */
544     public void setNumberOfAllDigits(int numberOfAllDigits) {
545     	if(getWheelswitch().getNumberOfAllDigits() == numberOfAllDigits)
546     		return;
547     	
548     	firePropertyChange("numberOfAllDigits", getWheelswitch().getNumberOfAllDigits(), numberOfAllDigits);
549     	getWheelswitch().setNumberOfAllDigits(numberOfAllDigits);
550     }
551     
552     /**
553      * @return The number of reserved places for digits.
554      */
555     public int getNumberOfAllDigits() {
556     	return(getWheelswitch().getNumberOfAllDigits());
557     }
558     
559     /*
560      * (non-Javadoc)
561      * @see com.cosylab.gui.components.AbstractDisplayerPanel#setPopupEnabled(boolean)
562      */
563     @Override
564     public void setPopupEnabled(boolean enabled) {
565     	super.setPopupEnabled(enabled);
566     	getWheelswitch().setPopupEnabled(enabled);
567     }
568 
569     /**
570 	 * Runs simple test applet.
571 	 *
572 	 * @param args String[]
573 	 */
574 	public static void main(String[] args)
575 	{
576 		LabelledWheelswitch ws = new LabelledWheelswitch(2.449, "%2.1f",
577 			    "Current", "A");
578 		ws.setDigitsTakeUpAllSpace(false);
579 		ws.setNumberOfAllDigits(5);
580 		JFrame frame = new JFrame("Wheelwsitch Testing Applet");
581 		frame.getContentPane().setLayout(new BorderLayout(10, 10));
582 		frame.getContentPane().add(ws);
583 		frame.setSize(300, 100);
584 		frame.addWindowListener(new WindowAdapter() {
585 				public void windowClosing(WindowEvent e)
586 				{
587 					System.exit(0);
588 				}
589 			});
590 		frame.setVisible(true);
591 	}
592 }
593 
594 /* __oOo__ */