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.table.renderers;
21  
22  import com.cosylab.gui.components.table.cells.DoubleCell;
23  
24  import com.cosylab.util.FormatCache;
25  import com.cosylab.util.PrintfFormat;
26  
27  import java.awt.Component;
28  
29  import javax.swing.JTable;
30  
31  
32  /**
33   * This is the default renderer for ObjectTable cells implementing DoubleCell
34   * interface.
35   *
36   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
37   * @version $id$
38   */
39  public class DoubleCellRenderer extends DefaultTableCellRenderer
40  {
41  	
42  	private static final long serialVersionUID = 1L;
43  
44  	/**
45  	 * Constructs a new DoubleCellRenderer. 
46  	 */
47  	public DoubleCellRenderer() {
48  		this(false,true);
49  	}
50  	
51  	/**
52  	 * Constructs a new DoubleCellRenderer.
53  	 * 
54  	 * @param decorateBorder if true decorated border will be used
55  	 * @param decorateBackground if true decorated background will be used
56  	 */
57  	public DoubleCellRenderer(boolean decorateBorder, boolean decorateBackground)
58  	{
59  		super(decorateBorder,decorateBackground);
60  		setOpaque(true);
61  	}
62  
63  	/*
64  	 * (non-Javadoc)
65  	 * @see com.cosylab.gui.components.table.renderers.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
66  	 */
67  	public Component getTableCellRendererComponent(JTable table, Object cell,
68  	    boolean isSelected, boolean hasFocus, int row, int column)
69  	{
70  		super.getTableCellRendererComponent(table, cell, isSelected, hasFocus,
71  		    row, column);
72  
73  		if (cell instanceof DoubleCell) {
74  			if (((DoubleCell)cell).isSuspended()) {
75  				setText("");
76  			} else {
77  				PrintfFormat formatter = FormatCache.getFormatter(((DoubleCell)cell)
78  					    .getFormat());
79  
80  				if (formatter == null) {
81  					formatter = FormatCache.getDefaultFormatter();
82  				}
83  				
84  				String value = formatter.sprintf(((DoubleCell)cell).getDoubleValue());
85  				if (((DoubleCell)cell).isUnitsVisible()) {
86  					value += " " + ((DoubleCell)cell).getUnits();
87  				}
88  				setText(value);
89  			}
90  		}
91  
92  		return this;
93  	}
94  }
95  
96  /* __oOo__ */