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.displayers;
21  
22  import java.awt.Color;
23  import java.awt.Dimension;
24  import java.awt.event.ActionEvent;
25  import java.beans.PropertyVetoException;
26  
27  import javax.swing.AbstractAction;
28  import javax.swing.JLabel;
29  import javax.swing.JPanel;
30  
31  import com.cosylab.gui.adapters.Converter;
32  import com.cosylab.gui.components.AbstractDisplayerPanelCustomizer;
33  import com.cosylab.gui.components.ResizableTextLabel;
34  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
35  import com.cosylab.gui.components.util.CosyUIElements;
36  import com.cosylab.gui.components.util.PopupManageable;
37  import com.cosylab.gui.components.util.PopupManager;
38  import com.cosylab.gui.components.util.ScreenCapturer;
39  
40  
41  /**
42   * <code>AbstractDisplayerPanel</code> is a displayer that provides common
43   * feature of a Displayer interface.
44   *
45   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
46   * @version $id$
47   */
48  public abstract class AbstractDisplayerPanel extends JPanel implements Displayer,
49  	PopupManageable, ConvertibleDisplayer
50  {
51  	private DataState dataState = new DataState(DataState.UNDEFINED);
52  	private int suspendCount = 0;
53  	private PopupManager popupManager;
54  	private boolean popupEnabled;
55  	private DataSource dataSource;
56  	private Converter converter;
57  	
58  	private ResizableTextLabel titleLabel;
59  	private AbstractCustomizerPanel customizer;
60  
61  	/**
62  	 * Constructor for AbstractDisplayerPanel.
63  	 */
64  	public AbstractDisplayerPanel()
65  	{
66  		super();
67  		setBorder(CosyUIElements.getPlainBorder(true));
68  
69  		Dimension size = new Dimension(200, 50);
70  		setPreferredSize(size);
71  		setPopupEnabled(true);
72  	}
73  	
74  	/**
75  	 * Generates the customizer for the displayer.
76  	 * 
77  	 * @return
78  	 */
79  	public AbstractCustomizerPanel getCustomizer()
80  	{
81  		if (customizer == null) {
82  			customizer = AbstractCustomizerPanel.findCustomizer(this);
83  		}
84  
85  		return customizer;
86  	}
87  	
88  	protected ResizableTextLabel getTitleLabel() {
89  		if (titleLabel == null) {
90  			titleLabel = new ResizableTextLabel();
91  			titleLabel.setHorizontalAlignment(JLabel.CENTER);
92  			titleLabel.addMouseListener(getPopupManager().getMouseHook());
93  			titleLabel.setEnabled(isEnabled());
94  		}
95  
96  		return titleLabel;
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
101 	 */
102 	public DataConsumer getDefaultDataConsumer()
103 	{
104 		return this;
105 	}
106 
107 	/* (non-Javadoc)
108 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
109 	 */
110 	public String[] getSupportedCharacteristics()
111 	{
112 		return DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_CHARACTERISTICS;
113 	}
114 
115 	/* (non-Javadoc)
116 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
117 	 */
118 	public void updateDataState(DataState state)
119 	{
120 		DataState old = dataState;
121 		dataState = state;
122 		firePropertyChange(DATA_STATE, old, dataState);
123 	}
124 
125 	/* (non-Javadoc)
126 	 * @see com.cosylab.gui.displayers.CommonDisplayer#cleanup()
127 	 */
128 	public void cleanup()
129 	{
130 		updateDataState(new DataState(DataState.NOT_INITIALIZED));
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.CommonDisplayer#isSuspended()
143 	 */
144 	public boolean isSuspended()
145 	{
146 		return suspendCount > 0;
147 	}
148 
149 	/* (non-Javadoc)
150 	 * @see com.cosylab.gui.displayers.CommonDisplayer#resume()
151 	 */
152 	public void resume()
153 	{
154 		suspendCount--;
155 	}
156 
157 	/* (non-Javadoc)
158 	 * @see com.cosylab.gui.displayers.CommonDisplayer#suspend()
159 	 */
160 	public void suspend()
161 	{
162 		suspendCount++;
163 	}
164 
165 	/**
166 	 * Returns popup manager for adding popup actions.
167 	 *
168 	 * @see com.cosylab.gui.components.util.PopupManageable#getPopupManager()
169 	 */
170 	public PopupManager getPopupManager()
171 	{
172 		if (popupManager == null) {
173 			popupManager = new PopupManager(this, false);
174 			popupManager.addAction(new AbstractAction("Preferences...") {
175 				private static final long serialVersionUID = 1L;
176 					public void actionPerformed(ActionEvent e)
177 					{
178 						getCustomizer().showDialog();
179 					}
180 				});
181 			popupManager.addAction(new AbstractAction("Capture screen..."){
182 				private static final long serialVersionUID = 1L;
183 
184 				public void actionPerformed(ActionEvent e){
185 				   	ScreenCapturer sc = new ScreenCapturer(AbstractDisplayerPanel.this);
186 					sc.showScreenDialog();
187 				   }
188 			});
189 		}
190 
191 		return popupManager;
192 	}
193 	
194 	/**
195 	 * Return true if the popup menu is enabled or false otherwise.
196 	 * 
197 	 * @return true if popup is enabled
198 	 */
199 	public boolean isPopupEnabled() {
200 		return popupEnabled;
201 	}
202 	
203 	/**
204 	 * Enables or disables the popup menu.
205 	 * 
206 	 * @param enabled true if enable or false if disableds
207 	 */
208 	public void setPopupEnabled(boolean enabled) {
209 		if (popupEnabled == enabled) return;
210 		popupEnabled = enabled;
211 		if (enabled) {
212 			addMouseListener(getPopupManager().getMouseHook());
213 		} else {
214 			removeMouseListener(getPopupManager().getMouseHook());
215 		}
216 		firePropertyChange("popupEnabled",!popupEnabled,popupEnabled);
217 	}
218 
219 	/* (non-Javadoc)
220 	 * @see com.cosylab.gui.core.CosyComponent#destroy()
221 	 */
222 	public void destroy()
223 	{
224 		popupManager = null;
225 		dataState = null;
226 	}
227 
228 	/* (non-Javadoc)
229 	 * @see com.cosylab.gui.displayers.Displayer#getDataSource()
230 	 */
231 	public DataSource getDataSource() {
232 		return dataSource;
233 	}
234 	
235 	/* (non-Javadoc)
236 	 * @see com.cosylab.gui.displayers.Displayer#setDataSource(com.cosylab.gui.displayers.DataSource)
237 	 */
238 	public void setDataSource(DataSource dataSource)
239 		throws PropertyVetoException
240 	{
241 		DisplayerUtilities.prepareNewDataSource(dataSource,this);
242 		
243 		DataSource old= this.dataSource;
244 		this.dataSource = dataSource;
245 		
246 		firePropertyChange(DATA_SOURCE,old,dataSource);
247 	}
248 	
249 	/**
250 	 * Returns the converter.
251 	 * @return Returns the converter.
252 	 */
253 	public Converter getConverter() {
254 		return converter;
255 	}
256 	
257 	/**
258 	 * Sets new converter.
259 	 * @param converter new converter
260 	 * @throws PropertyVetoException if set fails
261 	 */
262 	public void setConverter(Converter converter) throws PropertyVetoException {
263 		DisplayerUtilities.prepareNewConverter(converter,this);
264 		
265 		Converter old= this.converter;
266 		this.converter = converter;
267 		
268 		firePropertyChange(CONVERTER_PROPERTY,old,dataSource);
269 	}
270 	
271 	/**
272 	 * Returns the maximum title font size.
273 	 *
274 	 * @return font size in pixels
275 	 */
276 	public int getTitleMaximumFontSize()
277 	{
278 		return getTitleLabel().getMaximumFontSize();
279 	}
280 
281 	/**
282 	 * Returns the minimum title font size.
283 	 *
284 	 * @return font size in pixels
285 	 */
286 	public int getTitleMinimumFontSize()
287 	{
288 		return getTitleLabel().getMinimumFontSize();
289 	}
290 
291 	/**
292 	 * Sets the maximum title font size.
293 	 *
294 	 * @param max new font size in pixels
295 	 */
296 	public void setTitleMaximumFontSize(int max)
297 	{
298 		int old= getTitleMaximumFontSize();
299 		getTitleLabel().setMaximumFontSize(max);
300 		firePropertyChange(AbstractDisplayerPanelCustomizer.MAX_TITLE_FONT_SIZE,
301 		    old, max);
302 	}
303 
304 	/**
305 	 * Sets the minimum title font size.
306 	 *
307 	 * @param min new font size in pixels
308 	 */
309 	public void setTitleMinimumFontSize(int min)
310 	{
311 		int old= getTitleMinimumFontSize();
312 		getTitleLabel().setMaximumFontSize(min);
313 		firePropertyChange(AbstractDisplayerPanelCustomizer.MIN_TITLE_FONT_SIZE,
314 		    old, min);
315 	}
316 	/**
317      * Sets visibility of title label.
318      * 
319      * @param value
320      *        True if title should be visible.
321      */
322 	public void setTitleVisible(boolean value) {
323 		getTitleLabel().setVisible(value);
324 	}
325 
326 	/**
327      * Returns visibility of title.
328      * 
329      * @return True if title is visible.
330      */
331 	public boolean isTitleVisible() {
332 		return getTitleLabel().isVisible();
333 	}
334 
335 	/**
336      * Returns title of this component.
337      * 
338      * @see com.cosylab.gui.displayers.Displayer#getTitle()
339      */
340 	public String getTitle() {
341 		return getTitleLabel().getText();
342 	}
343 
344 	/**
345      * Sets the title of component.
346      * 
347      * @see com.cosylab.gui.displayers.Displayer#setTitle(java.lang.String)
348      */
349 	public void setTitle(String value) {
350 		getTitleLabel().setText(value);
351 	}
352 	
353 	/*
354 	 * (non-Javadoc)
355 	 * @see javax.swing.JComponent#setBackground(java.awt.Color)
356 	 */
357 	@Override
358 	public void setBackground(Color bg) {
359 		super.setBackground(bg);
360 		getTitleLabel().setBackground(bg);
361 		
362 	}
363 	
364 	/*
365 	 * (non-Javadoc)
366 	 * @see javax.swing.JComponent#setForeground(java.awt.Color)
367 	 */
368 	@Override
369 	public void setForeground(Color fg) {
370 		super.setForeground(fg);
371 		getTitleLabel().setForeground(fg);
372 	}
373 
374 
375 }
376 
377 /* __oOo__ */