View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans 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 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.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui;
21  
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.event.ActionEvent;
25  import java.beans.PropertyVetoException;
26  import java.util.Map;
27  
28  import javax.swing.AbstractAction;
29  import javax.swing.JFrame;
30  
31  import com.cosylab.gui.adapters.Converter;
32  import com.cosylab.gui.components.LabelledGauger;
33  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
34  import com.cosylab.gui.components.range2.TrimValuePolicy;
35  import com.cosylab.gui.components.util.PopupManager;
36  import com.cosylab.gui.displayers.ConvertibleDisplayer;
37  import com.cosylab.gui.displayers.DataConsumer;
38  import com.cosylab.gui.displayers.DataSource;
39  import com.cosylab.gui.displayers.DataState;
40  import com.cosylab.gui.displayers.DisplayerUtilities;
41  import com.cosylab.gui.displayers.DoubleConsumer;
42  import com.cosylab.gui.displayers.DoubleConsumerMulticaster;
43  import com.cosylab.gui.displayers.DoubleDisplayer;
44  import com.cosylab.util.CommonException;
45  
46  /**
47   * Gauger displayer.
48   *
49   * @version 1.0
50   */
51  public class GaugerDisplayer extends LabelledGauger
52  	implements DoubleDisplayer, ConvertibleDisplayer 
53  {
54  	private static final long serialVersionUID = 1L;
55  	private int suspendCount = 0;
56  	private double value;
57  	private DataState dataState = new DataState(DataState.UNDEFINED);
58  	private DataSource dataSource;  //  @jve:decl-index=0:
59  	private Converter converter;  //  @jve:decl-index=0:
60  	private AbstractCustomizerPanel customizer;
61  	private InfoDialog infoDialog;
62  	private PopupManager popupManager;
63  	
64  	/** 
65  	 * Creates new gauger displayer. It defaults to initial values as specified
66  	 * in the gauger component.
67  	 */
68  	public GaugerDisplayer()
69  	{
70  		super();
71  		try {
72  			initialize();
73  		} catch (PropertyVetoException e) {
74  			e.printStackTrace();
75  		}
76  	}
77  
78  	/* (non-Javadoc)
79  	 * @see com.cosylab.gui.displayers.DataConsumer#setCharacteristics(java.util.Map)
80  	 */
81  	public void setCharacteristics(Map characteristics)
82  	{
83  		if (characteristics == null) {
84  			throw new NullPointerException("characteristics");
85  		}
86  
87  		DisplayerUtilities.setCharacteristics(characteristics, this);
88  	}
89  
90  	/* (non-Javadoc)
91  	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
92  	 */
93  	public DataConsumer getDataConsumer(Class type)
94  	{
95  		if (type == DoubleConsumer.class) {
96  			return this;
97  		}
98  
99  		return DoubleConsumerMulticaster.createDataConsumer(type, this);
100 	}
101 
102 	/* (non-Javadoc)
103 	 * @see com.cosylab.gui.displayers.CommonDisplayer#getDataState()
104 	 */
105 	public DataState getDataState()
106 	{
107 		return dataState;
108 	}
109 
110 	/* (non-Javadoc)
111 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
112 	 */
113 	public DataConsumer getDefaultDataConsumer()
114 	{
115 		return this;
116 	}
117 
118 	/* (non-Javadoc)
119 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
120 	 */
121 	public String[] getSupportedCharacteristics()
122 	{
123 		return DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_CHARACTERISTICS;
124 	}
125 
126 	/* (non-Javadoc)
127 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
128 	 */
129 	@SuppressWarnings("unchecked")
130 	public Class[] getSupportedConsumerTypes()
131 	{
132 		return DoubleConsumerMulticaster.PREFERED_CONSUMER_TYPES;
133 	}
134 
135 	/**
136 	 * @see DoubleDisplayer#setValue(double)
137 	 */
138 	public void setValue(double value)
139 	{
140 		if (this.value == value) {
141 			return;
142 		}
143 
144 		double oldValue = this.value;
145 		this.value = value;
146 		firePropertyChange("value", oldValue, value);
147 
148 		if (!isSuspended()) {
149 				super.setValue(value);
150 		}
151 	}
152 
153 	/* (non-Javadoc)
154 	 * @see com.cosylab.gui.displayers.CommonDisplayer#cleanup()
155 	 */
156 	public void cleanup()
157 	{
158 		internalCleanup();
159 		updateDataState(new DataState(DataState.NOT_INITIALIZED));
160 	}
161 
162 	/* (non-Javadoc)
163 	 * @see com.cosylab.gui.core.CosyComponent#destroy()
164 	 */
165 	public void destroy()
166 	{
167 		suspend();
168 		cleanup();
169 		dataState = null;
170 	}
171 
172 	/* (non-Javadoc)
173 	 * @see com.cosylab.gui.displayers.CommonDisplayer#resume()
174 	 */
175 	public void resume()
176 	{
177 		if (suspendCount > 0) {
178 			suspendCount--;
179 		}
180 		
181 		if (suspendCount==0) {
182 			setEnabled(true);
183 			super.setValue(value);
184 		}
185 	}
186 
187 	/* (non-Javadoc)
188 	 * @see com.cosylab.gui.displayers.CommonDisplayer#suspend()
189 	 */
190 	public void suspend()
191 	{
192 		suspendCount++;
193 		setEnabled(false);
194 	}
195 
196 	/* (non-Javadoc)
197 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
198 	 */
199 	public void updateDataState(DataState state)
200 	{
201 		DataState old = dataState;
202 		dataState = state;
203 		firePropertyChange(DATA_STATE, old, dataState);
204 	}
205 
206 	/* (non-Javadoc)
207 	 * @see com.cosylab.gui.displayers.DoubleConsumer#updateValue(long, double)
208 	 */
209 	public void updateValue(long timestamp, double value)
210 		throws CommonException
211 	{
212 		setValue(value);
213 	}
214 
215 	private void initialize() throws PropertyVetoException
216 	{
217 		
218 		internalCleanup();
219 	}
220 
221 	private void internalCleanup() 
222 	{
223 		setTitle("Gauger");
224 		setUnits(null);
225 		setUnitsVisible(true);
226 		setFormat("%3.2f");
227 		setResizable(true);
228 		setTitleVisible(true);
229 		setLogarithmicScale(false); 
230 		setValuePolicy(new TrimValuePolicy());
231 		setPopupEnabled(true);
232 	}
233 	/* (non-Javadoc)
234 	 * @see com.cosylab.gui.displayers.CommonDisplayer#isSuspended()
235 	 */
236 	public boolean isSuspended()
237 	{
238 		return (suspendCount > 0);
239 	}
240 
241 	public double getValue()
242 	{
243 		return value;
244 	}
245 
246 	/* (non-Javadoc)
247 	 * @see com.cosylab.gui.displayers.Displayer#getDataSource()
248 	 */
249 	public DataSource getDataSource()
250 	{
251 		return dataSource;
252 	}
253 
254 	/* (non-Javadoc)
255 	 * @see com.cosylab.gui.displayers.Displayer#setDataSource(com.cosylab.gui.displayers.DataSource)
256 	 */
257 	public void setDataSource(DataSource dataSource)
258 		throws PropertyVetoException
259 	{
260 		DisplayerUtilities.prepareNewDataSource(dataSource,this);
261 		
262 		DataSource old= this.dataSource;
263 		this.dataSource = dataSource;
264 		
265 		firePropertyChange(DATA_SOURCE,old,dataSource);
266 	}
267 
268 	
269 	/**
270 	 * Returns the converter.
271 	 * @return Returns the converter.
272 	 */
273 	public Converter getConverter() {
274 		return converter;
275 	}
276 
277 	
278 	/**
279 	 * Sets new converter.
280 	 * @param converter The converter to set.
281 	 * @throws PropertyVetoException if set fails
282 	 */
283 	public void setConverter(Converter converter) throws PropertyVetoException {
284 		if (this.converter != null && this.converter.equals(converter) ||
285 				(this.converter == null && converter == null)) return;
286 		DisplayerUtilities.prepareNewConverter(converter,this);
287 		
288 		Converter old= this.converter;
289 		this.converter = converter;
290 		
291 		firePropertyChange(CONVERTER_PROPERTY,old,this.converter);    
292 	}
293 
294 	@Override
295 	public AbstractCustomizerPanel getCustomizer() {
296 		if (customizer == null) {
297 			customizer = AbstractCustomizerPanel.findCustomizer(this);
298 		}
299    
300 		return customizer;
301 	}
302 	
303 	public InfoDialog getInfoDialog() {
304 		if (infoDialog == null) {
305 			infoDialog = new InfoDialog(this);
306 		}
307 		return infoDialog;
308 	}
309 	
310 	public PopupManager getPopupManager()
311 	{
312 		if (popupManager == null) {
313 			popupManager = super.getPopupManager();
314 			popupManager.addAction(new AbstractAction("Info...") {
315 	
316 				private static final long serialVersionUID = 1L;
317 
318 					public void actionPerformed(ActionEvent e)
319 					{
320 						getInfoDialog().setVisible(true);
321 					}
322 				});
323 		}
324 		
325 		return popupManager;
326 	}
327 
328 	/**
329 	 * Run test applet.
330 	 *
331 	 * @param args command line parameters
332 	 */
333 	public static void main(String[] args)
334 	{
335 		JFrame dialog = new JFrame();
336 
337 		dialog.getContentPane().setLayout(new GridBagLayout());
338 
339 		GridBagConstraints c = new GridBagConstraints();
340 		c.fill = GridBagConstraints.BOTH;
341 		c.weightx = 1.0;
342 		c.weighty = 1.0;
343 
344 		//dialog.setModal(true);
345 
346 		final GaugerDisplayer gd = new GaugerDisplayer();
347 		gd.setPopupEnabled(false);
348 		
349 		try {
350 			gd.setConverter(new com.cosylab.gui.adapters.MultiplierConverter(2));
351 		} catch (PropertyVetoException e) {
352 			e.printStackTrace();
353 		}
354 		dialog.setSize(100, 400);
355 
356 		dialog.getContentPane().add(gd, c);
357 		dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
358 		gd.setValue(1.8);
359 
360 //		Timer t = new Timer();
361 //		t.schedule(new TimerTask(){
362 //			int i = 0;
363 //			public void run() {
364 //				gd.setValue(i);
365 //				i = (i + 1)%20;
366 //			}
367 //			
368 //		}, 500, 500);
369 		
370 		//Timer t = new Timer();
371 		//t.schedule(new TimerTask() {
372 		//	public void run() {
373 		//		if (gd.isSuspended()) {
374 		//			gd.resume();
375 		//		} else {
376 		//			gd.suspend();
377 		//		}
378 		//		Debug.out("Gauger#isSuspended="+gd.isSuspended());
379 		//	}
380 		//},500,500);
381 
382 		//dialog.show();
383 		dialog.setVisible(true);
384 		
385 		
386 		//System.exit(0);
387 
388 		/*javax.swing.JDialog dialog = new javax.swing.JDialog();
389 
390 		dialog.getContentPane().setLayout(new GridBagLayout());
391 
392 		GridBagConstraints c = new GridBagConstraints();
393 		c.fill = GridBagConstraints.BOTH;
394 		c.weightx = 1.0;
395 		c.weighty = 1.0;
396 
397 		dialog.setModal(true);
398 
399 		GaugerDisplayer gd = new GaugerDisplayer();
400 
401 		dialog.setSize(400, 400);
402 
403 		dialog.getContentPane().add(gd, c);
404 
405 		gd.gauger.setValue(1.8);
406 
407 		dialog.show();
408 
409 		System.exit(0);
410 		*/
411 	}	
412 }
413 /* __oOo__ */