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.Color;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.WindowAdapter;
25  import java.awt.event.WindowEvent;
26  import java.beans.Beans;
27  import java.beans.PropertyVetoException;
28  import java.util.Map;
29  import java.util.logging.Level;
30  import java.util.logging.Logger;
31  
32  import javax.swing.AbstractAction;
33  import javax.swing.JComponent;
34  import javax.swing.JFrame;
35  import javax.swing.SwingConstants;
36  import javax.swing.TransferHandler;
37  
38  import com.cosylab.gui.components.AbstractDisplayerPanel;
39  import com.cosylab.gui.components.ResizableTextLabel;
40  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
41  import com.cosylab.gui.components.util.CosyTransferHandler;
42  import com.cosylab.gui.components.util.CosyUIElements;
43  import com.cosylab.gui.components.util.PopupManageable;
44  import com.cosylab.gui.components.util.PopupManager;
45  import com.cosylab.gui.displayers.DataConsumer;
46  import com.cosylab.gui.displayers.DataSource;
47  import com.cosylab.gui.displayers.DataState;
48  import com.cosylab.gui.displayers.StringConsumerMulticaster;
49  import com.cosylab.gui.displayers.StringDisplayer;
50  import com.cosylab.gui.util.UserSettingsProtection;
51  import com.cosylab.logging.DebugLogger;
52  import com.cosylab.util.CommonException;
53  
54  
55  /**
56   * A displayer GUI widget that can uniformly display changable string value.
57   * Displayer is build primarely as <code>StringConsumer</code> but can act as
58   * any other consumer by help of <code>StringConsumerMulticaster</code>
59   * utility class. By look is similar to number displayer. User can display
60   * arbytrary value by implementing own way to turn it in to String.
61   *
62   * @author <a href="mailto:igor.verstovsek@cosylab.com">Igor Verstovsek</a>
63   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
64   * @version $id$
65   */
66  public class LabelDisplayer extends AbstractDisplayerPanel
67  	implements StringDisplayer, PopupManageable
68  {
69  	private static final long serialVersionUID = 1L;
70  	private DataSource dataSource;
71  	private DataState dataState = new DataState(DataState.UNDEFINED);
72  	private ResizableTextLabel label = null;
73  	private int suspendCount;
74  	private String value;
75  	private final Logger logger = DebugLogger.getLogger("LabelDisplayer",
76  		    Level.OFF);
77  	private PopupManager popupManager;
78  	private AbstractCustomizerPanel customizer;
79  	private InfoDialog infoDialog;
80  	
81  	/**
82  	 * Constructor for LabelDisplayer.
83  	 */
84  	public LabelDisplayer()
85  	{
86  		super();
87  		internalCleanup();
88  	}
89  
90  	/* (non-Javadoc)
91  	 * @see com.cosylab.gui.displayers.CommonDisplayer#cleanup()
92  	 */
93  	public void cleanup()
94  	{
95  		internalCleanup();
96  		updateDataState(new DataState(DataState.NOT_INITIALIZED));
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see com.cosylab.gui.core.CosyComponent#destroy()
101 	 */
102 	public void destroy()
103 	{
104 		internalCleanup();
105 	}
106 
107 	/**
108 	 * Returns number of character columns. Fixes the length of displayed string to fixed value.  
109 	 *
110 	 * @return number of characters in displayed string
111 	 */
112 	public int getColumns()
113 	{
114 		return getValueLabel().getColumns();
115 	}
116 
117 	/* (non-Javadoc)
118 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
119 	 */
120 	public DataConsumer getDataConsumer(Class type)
121 	{
122 		return StringConsumerMulticaster.createDataConsumer(type, this);
123 	}
124 
125 	/* (non-Javadoc)
126 	 * @see com.cosylab.gui.displayers.Displayer#getDataSource()
127 	 */
128 	public DataSource getDataSource()
129 	{
130 		return dataSource;
131 	}
132 
133 	/* (non-Javadoc)
134 	 * @see com.cosylab.gui.displayers.CommonDisplayer#getDataState()
135 	 */
136 	public DataState getDataState()
137 	{
138 		return dataState;
139 	}
140 
141 	/* (non-Javadoc)
142 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
143 	 */
144 	public DataConsumer getDefaultDataConsumer()
145 	{
146 		return this;
147 	}
148 
149 	/**
150 	 * A label which is used to display value. Extended class can access
151 	 * directly to customize how label will display value. Note that some
152 	 * label properties are overriden by default displayer policies when
153 	 * policy is changed.
154 	 *
155 	 * @return the label which displays string value
156 	 */
157 	protected ResizableTextLabel getValueLabel()
158 	{
159 		if (label == null) {
160 			label = new ResizableTextLabel();
161 			label.setHorizontalAlignment(SwingConstants.CENTER);
162 			label.setEnabled(isEnabled());
163 			label.setEnhanced(isEnhanced());
164 			label.setResizable(isResizable());
165 			internalSetValue();
166 			label.setOpaque(true);
167 			label.setBorder(CosyUIElements.getFlushBorder());
168 			label.setBackground(Color.LIGHT_GRAY);
169 		}
170 
171 		return label;
172 	}
173 	
174 	@Override
175 	public void setBackground(Color bg) {
176 		Color labelBg = getValueLabel().getBackground();
177 		super.setBackground(bg);
178 		getValueLabel().setBackground(labelBg);
179 	}
180 
181 	/* (non-Javadoc)
182 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
183 	 */
184 	public String[] getSupportedCharacteristics()
185 	{
186 		return new String[]{ C_DISPLAY_NAME };
187 	}
188 
189 	/* (non-Javadoc)
190 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
191 	 */
192 	@SuppressWarnings("unchecked")
193 	public Class[] getSupportedConsumerTypes()
194 	{
195 		return StringConsumerMulticaster.SUPPORTED_CONSUMER_TYPES;
196 	}
197 
198 	/**
199 	 * Returns the string value.
200 	 *
201 	 * @return the string value
202 	 */
203 	public String getValue()
204 	{
205 		return value;
206 	}
207 
208 	/* (non-Javadoc)
209 	 * @see com.cosylab.gui.components.DisplayerPanel#getValueComponent()
210 	 */
211 	protected JComponent getValueComponent()
212 	{
213 		return getValueLabel();
214 	}
215 
216 	private void internalCleanup()
217 	{
218 		logger.info("Cleanup");
219 
220 		//setLayoutOrientation(AbstractDisplayerPanel.DYNAMIC_LAYOUT);
221 		///setResizable(true);
222 		//setEnhanced(true);
223 		//setTitleVisible(true);
224 		//setTitle("Label Displayer");
225 		//setValue("value");
226 		setEnhanced(true);
227 		setResizable(true);
228 		setValue("value");
229 		setTitle("Label Displayer");
230 		setLayoutOrientation(AbstractDisplayerPanel.DYNAMIC_LAYOUT);
231 		setTitleVisible(true);
232 		setColumns(5);
233 		setPopupEnabled(true);
234 	}
235 
236 	/* (non-Javadoc)
237 	 * @see com.cosylab.gui.components.DisplayerPanel#internalSetEnabled()
238 	 */
239 	protected void internalSetEnabled()
240 	{
241 		super.internalSetEnabled();
242 		getValueLabel().setEnabled(isEnabled());
243 	}
244 
245 	/* (non-Javadoc)
246 	 * @see com.cosylab.gui.components.DisplayerPanel#internalSetEnhanced()
247 	 */
248 	protected void internalSetEnhanced()
249 	{
250 		super.internalSetEnhanced();
251 		getValueLabel().setEnhanced(isEnhanced());
252 	}
253 
254 	/* (non-Javadoc)
255 	 * @see com.cosylab.gui.components.DisplayerPanel#internalSetResizable()
256 	 */
257 	protected void internalSetResizable()
258 	{
259 		super.internalSetResizable();
260 		getValueLabel().setResizable(isResizable());
261 	}
262 
263 	private void internalSetValue()
264 	{
265 		String tValue = value;
266 
267 		if (tValue == null && Beans.isDesignTime()) {
268 			tValue = "<value>";
269 		}
270 
271 		if (getValueLabel().isEnabled()) {
272 			if (logger!=null) logger.fine("'" + tValue + "'");
273 			getValueLabel().setText(tValue);
274 		}
275 	}
276 
277 	/* (non-Javadoc)
278 	 * @see com.cosylab.gui.displayers.Displayer#isEditable()
279 	 */
280 	public boolean isEditable()
281 	{
282 		return false;
283 	}
284 
285 	/* (non-Javadoc)
286 	 * @see com.cosylab.gui.displayers.CommonDisplayer#isSuspended()
287 	 */
288 	public boolean isSuspended()
289 	{
290 		return suspendCount > 0;
291 	}
292 
293 	/* (non-Javadoc)
294 	 * @see com.cosylab.gui.displayers.CommonDisplayer#resume()
295 	 */
296 	public void resume()
297 	{
298 		if (suspendCount > 0) {
299 			suspendCount--;
300 		}
301 
302 		if (suspendCount == 0) {
303 			setEnabled(true);
304 		}
305 	}
306 
307 	/* (non-Javadoc)
308 	 * @see com.cosylab.gui.displayers.DataConsumer#setCharacteristics(java.util.Map)
309 	 */
310 	public void setCharacteristics(Map characteristics)
311 	{
312 		Object o = characteristics.get(C_DISPLAY_NAME);
313 
314 		if (o != null) {
315 			try {
316 				UserSettingsProtection.setUnprotected(this,"title",o.toString());
317 			} catch (Exception e) {
318 				e.printStackTrace();
319 			}
320 		}
321 	}
322 
323 	/**
324 	 * DOCUMENT ME!
325 	 *
326 	 * @param c DOCUMENT ME!
327 	 */
328 	public void setColumns(int c)
329 	{
330 		getValueLabel().setColumns(c);
331 	}
332 
333 	/* (non-Javadoc)
334 	 * @see com.cosylab.gui.displayers.Displayer#setDataSource(com.cosylab.gui.displayers.DataSource)
335 	 */
336 	public void setDataSource(DataSource dataSource)
337 		throws PropertyVetoException
338 	{
339 		if (this.dataSource != null) {
340 			this.dataSource.removeConsumer(this);
341 		}
342 
343 		DataSource old = this.dataSource;
344 		this.dataSource = dataSource;
345 		
346 		if (this.dataSource != null) {
347 			this.dataSource.addConsumer(this);
348 		}		
349 
350 		firePropertyChange(DATA_SOURCE, old, dataSource);
351 	}
352 
353 	/**
354 	 * @see com.cosylab.gui.displayers.StringDisplayer#getValue()
355 	 */
356 	public void setValue(String value)
357 	{
358 		logger.info("'" + value + "'");
359 
360 		if (value == null && this.value == null
361 		    || value != null && value.equals(this.value)) {
362 			return;
363 		}
364 
365 		String oldValue = this.value;
366 		this.value = value;
367 		internalSetValue();
368 		firePropertyChange("value", oldValue, value);
369 	}
370 
371 	/* (non-Javadoc)
372 	 * @see com.cosylab.gui.displayers.CommonDisplayer#suspend()
373 	 */
374 	public void suspend()
375 	{
376 		setEnabled(false);
377 		suspendCount++;
378 	}
379 
380 	/* (non-Javadoc)
381 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
382 	 */
383 	public void updateDataState(DataState state)
384 	{
385 		DataState old = dataState;
386 		dataState = state;
387 		firePropertyChange(DATA_STATE, old, dataState);
388 	}
389 
390 	/* (non-Javadoc)
391 	 * @see com.cosylab.gui.displayers.StringConsumer#updateValue(long, java.lang.String)
392 	 */
393 	public void updateValue(long timestamp, String value)
394 		throws CommonException
395 	{
396 		logger.info(timestamp + ",'" + value + "'");
397 		setValue(value);
398 	}
399 	
400 	/*
401 	 * (non-Javadoc)
402 	 * @see com.cosylab.gui.components.AbstractDisplayerPanel#getPopupManager()
403 	 */
404 	@Override
405 	public PopupManager getPopupManager()
406 	{
407 		if (popupManager == null) {
408 			popupManager = super.getPopupManager();
409 			popupManager.addAction(new AbstractAction("Info...") {
410 				private static final long serialVersionUID = 1L;
411 
412 					public void actionPerformed(ActionEvent e)
413 					{
414 						getInfoDialog().setVisible(true);
415 					}
416 				});
417 		}
418 		
419 		return popupManager;
420 	}
421 	
422 	public InfoDialog getInfoDialog() {
423 		if (infoDialog == null) {
424 			infoDialog = new InfoDialog(this);
425 		}
426 		return infoDialog;
427 	}
428 	
429 	/**
430 	 * Loads the default
431 	 * <code>com.cosylab.gui.components.customizer.Customizer</code> for this
432 	 * displayer and returns it.
433 	 *
434 	 * @return the Customizer intance for this displayer
435 	 */
436 	public AbstractCustomizerPanel getCustomizer()
437 	{
438 		if (customizer == null) {
439 			customizer = AbstractCustomizerPanel.findCustomizer(this);
440 		}
441 
442 		return customizer;
443 	}
444 	
445 	public static void main(String[] args) {
446 		try {
447 			JFrame frame= new JFrame();
448 			frame.addWindowListener(new WindowAdapter() {
449 				public void windowClosing(WindowEvent e) {
450 					System.exit(0);
451 				}
452 			});
453 			frame.setSize(200,200);
454 			
455 			LabelDisplayer d= new LabelDisplayer();
456 			d.setPopupEnabled(true);
457 			frame.setContentPane(d);
458 			
459 			//frame.show();
460 			frame.setVisible(true);
461 			
462 		} catch (Exception e) {
463 			e.printStackTrace();
464 		}
465 	}
466     /* (non-Javadoc)
467      * @see javax.swing.JComponent#setTransferHandler(javax.swing.TransferHandler)
468      */
469     @Override
470     public void setTransferHandler(TransferHandler newHandler) {
471     	super.setTransferHandler(newHandler);
472     	CosyTransferHandler.registerTransferHandler(this,newHandler,new JComponent[]{getValueComponent()});
473     }
474 
475 }
476 
477 /* __oOo__ */