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.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.Font;
25  import java.awt.FontMetrics;
26  import java.awt.GridBagConstraints;
27  import java.awt.GridBagLayout;
28  import java.awt.Insets;
29  import java.awt.Rectangle;
30  import java.awt.event.ActionEvent;
31  import java.util.Map;
32  
33  import javax.swing.AbstractAction;
34  import javax.swing.Icon;
35  import javax.swing.JFrame;
36  import javax.swing.JLabel;
37  import javax.swing.JScrollPane;
38  import javax.swing.JTable;
39  import javax.swing.SwingConstants;
40  import javax.swing.plaf.metal.MetalLabelUI;
41  import javax.swing.table.DefaultTableColumnModel;
42  import javax.swing.table.TableColumn;
43  
44  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
45  import com.cosylab.gui.components.introspection.MapTableModel;
46  import com.cosylab.gui.components.introspection.PropertiesTableModel;
47  import com.cosylab.gui.components.util.CosyUIElements;
48  import com.cosylab.gui.components.util.PopupManager;
49  import com.cosylab.gui.displayers.AbstractDisplayerPanel;
50  import com.cosylab.gui.displayers.DataConsumer;
51  import com.cosylab.gui.displayers.DataState;
52  import com.cosylab.gui.displayers.ObjectConsumer;
53  import com.cosylab.gui.displayers.ObjectConsumerMulticaster;
54  import com.cosylab.gui.displayers.ObjectDisplayer;
55  import com.cosylab.util.ArrayHelper;
56  import com.cosylab.util.CommonException;
57  
58  
59  /**
60   * <code>CharacteristicsMapPanel</code> ...  DOCUMENT ME!
61   *
62   * @author <a href="mailto:anze.zupanc@cosylab.com">Anze Zupanc</a>
63   * @version $Id: CharacteristicsMapPanel.java,v 1.15 2008-04-22 12:31:02 jbobnar Exp $
64   *
65   * @since Mar 29, 2004.
66   */
67  public  class CharacteristicsMapPanel extends AbstractDisplayerPanel
68  	implements ObjectDisplayer 
69  {
70  	private static final long serialVersionUID = 1L;
71  	protected PopupManager popupManager;
72  	private boolean initialized = false;
73  	public  static MapTableModel tableModel = null;
74  	private JLabel title = null;
75  	public static JTable table; 
76  	private AbstractCustomizerPanel customizer;
77  	private int suspendCount = 0;
78  	private Object lastValue=null;
79  	/**
80  	 * Creates a new CharacteristicsMapPanel object.
81  	 * 
82  	 */
83  	public CharacteristicsMapPanel()
84  	{
85  		super();
86  		initialize();
87  	}
88  
89  	/* (non-Javadoc)
90  	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
91  	 */
92  	public DataConsumer getDataConsumer(Class type)
93  	{
94  		if (type == ObjectConsumer.class) {
95  			return this;
96  		}
97  
98  		return ObjectConsumerMulticaster.createDataConsumer(type, this);
99  	}
100 
101 	public void resume()
102 	{
103 		if (suspendCount > 0) {
104 			suspendCount--;
105 		}
106 		
107 		if (suspendCount==0) {
108 			setEnabled(true);
109 		    setValue(lastValue);
110 		}
111 	}
112 
113 	public void setEnabled(boolean enabled){
114 	    table.setEnabled(enabled);
115 	    title.setEnabled(enabled);   
116 	}
117 	
118 	
119 	public void suspend()
120 	{
121 		suspendCount++;
122 		setEnabled(false);
123 	}
124 
125 
126 	/* (non-Javadoc)
127 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
128 	 */
129 	public DataConsumer getDefaultDataConsumer()
130 	{
131 		return this;
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see com.cosylab.gui.core.CosyComponent#getName()
136 	 */
137 	public String getName()
138 	{
139 		return "CharacteristicsMapPanel";
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
144 	 */
145 	public String[] getSupportedCharacteristics()
146 	{
147 		return null;
148 	}
149 
150 	/* (non-Javadoc)
151 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
152 	 */
153 	public Class[] getSupportedConsumerTypes()
154 	{
155 		return ObjectConsumerMulticaster.SUPPORTED_CONSUMER_TYPES;
156 	}
157 
158 	/* (non-Javadoc)
159 	 * @see com.cosylab.gui.displayers.Displayer#getTitle()
160 	 */
161 	public String getTitle()
162 	{
163 		return title.getText();
164 	}
165 
166 	/* (non-Javadoc)
167 	 * @see com.cosylab.gui.displayers.ObjectDisplayer#getValue()
168 	 */
169 	public Object getValue()
170 	{
171 		return null;
172 	}
173 	
174 	/**
175 	 * costomizer for settings
176 	 * @return costomizer
177 	 */
178 	public AbstractCustomizerPanel getCustomizer()
179 		{
180 			if (customizer == null) {
181 				customizer = AbstractCustomizerPanel.findCustomizer(this);
182 			}
183 
184 			return customizer;
185 		}
186 
187 	
188 	private void initialize()
189 	{
190 	  if (initialized) {
191 			return;
192 		}
193 
194 		
195 		initialized = true;
196 
197 		setLayout(new GridBagLayout());
198 		setBorder(CosyUIElements.getPlainBorder(true));
199 
200 		GridBagConstraints c = new GridBagConstraints();
201 				
202 		getPopupManager().addAction(new AbstractAction("Preferences...") {
203 						public void actionPerformed(ActionEvent e)
204 						{
205 							getCustomizer().showDialog();
206 						}
207 			
208 					});
209 		this.addMouseListener(getPopupManager().getMouseHook());
210 		
211 		//end adding tomo
212 
213 		title = new JLabel();
214 		title.setHorizontalAlignment(SwingConstants.CENTER);
215 		title.setHorizontalTextPosition(SwingConstants.CENTER);
216 		title.setFont(title.getFont().deriveFont(Font.BOLD));
217 
218 		// Trim text on the left side
219 		title.setUI(new MetalLabelUI() {
220 				protected String layoutCL(JLabel label,
221 				    FontMetrics fontMetrics, String text, Icon icon,
222 				    Rectangle viewR, Rectangle iconR, Rectangle textR)
223 				{
224 					char[] chars = text.toCharArray();
225 					ArrayHelper.flip(chars);
226 
227 					String prevText = String.copyValueOf(chars);
228 					String modText = super.layoutCL(label, fontMetrics,
229 						    prevText, icon, viewR, iconR, textR);
230 					chars = modText.toCharArray();
231 					ArrayHelper.flip(chars);
232 
233 					return String.copyValueOf(chars);
234 				}
235 			});
236 
237 		c.anchor = GridBagConstraints.NORTH;
238 		c.weightx = 1.0;
239 		c.weighty = 0.0;
240 		c.fill = GridBagConstraints.BOTH;
241 		c.insets = new Insets(4, 4, 2, 4);
242 		add(title, c);
243 
244 		
245 		tableModel = new MapTableModel(new String[]{ "property", "value"});
246 		tableModel.setValueEditable(false);
247 		tableModel.setKeyEditable(false);
248 
249 		final JScrollPane scroll = new JScrollPane();
250 		table = new JTable(tableModel);
251 
252 		
253 		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
254 		table.setSize(new Dimension(200,200));
255 				
256 		scroll.setMinimumSize(new Dimension(100,50));		
257 		scroll.setViewportView(table);
258 
259 		table.addMouseListener(getPopupManager().getMouseHook());
260 		scroll.addMouseListener(getPopupManager().getMouseHook());
261 
262 		c.gridy = 1;
263 		c.weighty = 1.0;
264 		c.insets = new Insets(0, 4, 4, 4);
265 		add(scroll, c);
266 		revalidate();
267 		
268 		/**
269 		 * we set all row width to 50% larger 
270 		 */
271 		setRowWidthMultiplayer(1.5);
272 		
273 	
274 	}
275 	
276 	private void setRowWidthMultiplayer(double i){
277 	
278 		DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
279         for(int j=0;j<colModel.getColumnCount();j++){
280         	TableColumn col = colModel.getColumn(j);
281         	
282         	col.setPreferredWidth((int)(col.getWidth()*i));
283         }
284         	
285 	}
286 
287 	/* (non-Javadoc)
288 	 * @see com.cosylab.gui.displayers.Displayer#isEditable()
289 	 */
290 	public boolean isEditable()
291 	{
292 		return false;
293 	}
294 
295 	/* (non-Javadoc)
296 	 * @see com.cosylab.gui.displayers.DataConsumer#setCharacteristics(java.util.Map)
297 	 */
298 	public void setCharacteristics(Map characteristics)
299 	{
300 		tableModel.setMap(characteristics);
301 		Object o= characteristics.get(C_DISPLAY_NAME);
302 		if (o==null) {
303 			o="N/A";
304 		}
305 		String t= o.toString();
306 		setTitle(t);
307 		revalidate();
308 		repaint();
309 	}
310 
311 	/* (non-Javadoc)
312 	 * @see com.cosylab.gui.displayers.Displayer#setTitle(java.lang.String)
313 	 */
314 	public void setTitle(String label)
315 	{
316 		//String old= title.getText();
317 		title.setText(label);
318 	}
319 
320 	/* (non-Javadoc)
321 	 * @see com.cosylab.gui.displayers.ObjectDisplayer#setValue(java.lang.Object)
322 	 */
323 	public void setValue(Object value)
324 	{
325 		tableModel.put("VALUE",value);
326 	}
327 
328 	/* (non-Javadoc)
329 	 * @see com.cosylab.gui.displayers.ObjectConsumer#updateValue(long, java.lang.Object)
330 	 */
331 	public void updateValue(long timestamp, Object value)
332 		throws CommonException
333 	{
334 	    lastValue=value;
335 	    if(!isSuspended()){
336 	        setValue(value);
337 	    }
338 	}
339 	/* (non-Javadoc)
340 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
341 	 */
342 	public void updateDataState(DataState state) {
343 		super.updateDataState(state);
344 		tableModel.put("DATA_STATE",state);
345 	}
346 	
347 	/**
348 	 * added by tomo
349 	 * @param args
350 	 */
351 	public static void main(String[] arg0) {
352 		try {
353 			JFrame bla = new JFrame("CharacteristicsMapPanel");
354 			CharacteristicsMapPanel cmp = new CharacteristicsMapPanel();
355 			bla.getContentPane().setLayout(new BorderLayout());
356 			bla.getContentPane().add(cmp);
357 			bla.setSize(350,150);
358 			cmp.setTitle("aha");
359 			
360 			bla.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
361 			
362 			bla.setVisible(true);
363 
364 			/**
365 			 * we create two rows in table,... just to see if suspend&resume works...
366 			 */
367 			
368 			String[] names = new String[2];
369 			Class[] types = new Class[2];
370 			Object[] values = new Object[2];
371 
372 			names[0] = C_DISPLAY_NAME;
373 			types[0] = String.class;
374 			values[0] = "SOME/DUMMY/DATA/SOURCE";
375 
376 			names[1] = C_DESCRIPTION;
377 			types[1] = String.class;
378 			values[1] = "This is simple DataSource implementation.";
379 
380 			
381 
382 			PropertiesTableModel model = null;
383 			model = new PropertiesTableModel(names, types, values);
384 			
385 			table.setModel(model);
386 		//TODO
387 			for(int i = 0 ; i<15; i++){
388 
389 				values[0] = new Integer(i);
390 				values[1] = "This "+i;
391 
392 					cmp.updateValue(1,values);
393 				Thread.sleep(1000);
394 				if(i==5){
395 				    cmp.suspend();
396 				}
397 				if (i==10){
398 				    cmp.resume();
399 				}
400 			}			
401 		} catch (Exception e) {
402 			e.printStackTrace();
403 		}
404 		
405 	}
406 
407 	// samo za test
408 	private boolean resizable, titleVisible;
409 	private int titleMinimumFontSize, titleMaximumFontSize;
410 	public boolean isResizable() {
411 		return resizable;
412 	}
413 
414 	public void setResizable(boolean resizable) {
415 		this.resizable = resizable;
416 	}
417 
418 	public int getTitleMaximumFontSize() {
419 		return titleMaximumFontSize;
420 	}
421 
422 	public void setTitleMaximumFontSize(int titleMaximumFontSize) {
423 		this.titleMaximumFontSize = titleMaximumFontSize;
424 	}
425 
426 	public int getTitleMinimumFontSize() {
427 		return titleMinimumFontSize;
428 	}
429 
430 	public void setTitleMinimumFontSize(int titleMinimumFontSize) {
431 		this.titleMinimumFontSize = titleMinimumFontSize;
432 	}
433 
434 	public boolean isTitleVisible() {
435 		return titleVisible;
436 	}
437 
438 	public void setTitleVisible(boolean titleVisible) {
439 		this.titleVisible = titleVisible;
440 	}
441 	// do tu je samo za test
442 
443 }
444 
445 /* __oOo__ */