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.PatternCell;
23  import com.cosylab.gui.components.util.PaintHelper;
24  import com.cosylab.util.BitSetUtilities;
25  
26  import java.awt.Color;
27  import java.awt.Component;
28  import java.awt.Graphics;
29  import java.awt.Point;
30  import java.awt.event.MouseEvent;
31  
32  import javax.swing.JTable;
33  import javax.swing.ToolTipManager;
34  
35  
36  /**
37   * Renderer for <code>com.cosylab.gui.components.table.cells.PatternCell</code>
38   * cells. Divides available area into horizontal regions and displays values
39   * as rectangular areas matching the color specified in <code>PatternCell
40   * </code>.
41   *
42   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
43   * @version $id$
44   */
45  public class PatternCellRenderer extends DefaultTableCellRenderer
46  {
47  	private static final long serialVersionUID = 1L;
48  	protected TimeoutBorder border = new TimeoutBorder();
49  	private Point position = null;
50  	private int size;
51  	private int h;
52  	private int w;
53  	private long value;
54  	private Color[] colorsOn;
55  	private Color[] colorsOff;
56  	private String[] descriptions;
57  	public static final Color DEFAULT_ON = Color.GREEN;
58  	public static final Color DEFAULT_OFF = Color.GRAY;
59  
60  	/**
61  	 * Creates a new PatternCellRenderer object.
62  	 */
63  	public PatternCellRenderer()
64  	{
65  		super(true, false);
66  		setToolTipText("");
67  		w = getWidth();
68  		ToolTipManager.sharedInstance().setInitialDelay(0);
69  		ToolTipManager.sharedInstance().setDismissDelay(1000);
70  
71  		//setBorder(border);
72  	}
73  
74  	/*
75  	 * (non-Javadoc)
76  	 * @see com.cosylab.gui.components.table.renderers.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
77  	 */
78  	public Component getTableCellRendererComponent(JTable table,
79  	    Object patternCell, boolean isSelected, boolean hasFocus, int row,
80  	    int column)
81  	{
82  		super.getTableCellRendererComponent(table, patternCell, isSelected,
83  		    hasFocus, row, column);
84  
85  		if (patternCell instanceof PatternCell) {
86  			PatternCell cell = (PatternCell)patternCell;
87  			descriptions = cell.getDescriptions();
88  			value = cell.getLongValue();
89  			
90  			if (descriptions == null) {
91  				size = BitSetUtilities.forLong(value).length();
92  				if (size == 0) size = 1;
93  			} else {
94  				size = descriptions.length;
95  			}
96  			
97  			colorsOn = cell.getColorsWhenOn();
98  			colorsOff = cell.getColorsWhenOff();
99  	
100 			//border.setEnabled(cell.isTimeout());
101 				
102 			setText("");
103 		}
104 		return this;
105 	}
106 
107 	/**
108 	 * Returns tool tip text based on area the mouse is over.
109 	 *
110 	 * @param e event
111 	 *
112 	 * @return description of selected led.
113 	 */
114 	public String getToolTipText(MouseEvent e)
115 	{
116 		position = e.getPoint();
117 
118 		for (int i = 0; i < size; i++) {
119 			if (position.getX() > (i * w) / size
120 			    && position.getX() < ((i + 1) * w) / size) {
121 				if (descriptions != null) {
122 					return descriptions[i];
123 				} else {
124 					return String.valueOf(i);
125 				}
126 			}
127 		}
128 
129 		return null;
130 	}
131 
132 	/**
133 	 * Paints the component.
134 	 *
135 	 * @param g graphics
136 	 */
137 	public void paintComponent(Graphics g)
138 	{
139 		paintSuperComponent(g);
140 
141 		if (active) {
142 			w = getWidth() - 1;
143 			h = getHeight() - 1;
144 
145 			for (int i = 0; i < size; i++) {
146 				if ((value & 1) == 1) {
147 					if (colorsOn != null && colorsOn.length > i) {
148 						g.setColor(colorsOn[i]);
149 					} else {
150 						g.setColor(DEFAULT_ON);
151 					}					
152 				} else {
153 					if (colorsOn != null && colorsOn.length > i) {
154 						g.setColor(colorsOff[i]);
155 					} else {
156 						g.setColor(DEFAULT_OFF);
157 					}
158 				}
159 
160 				value >>= 1;
161 				int bin= w/size+1;
162 				g.fillRect((i * w) / size, 0, bin, h);
163 				g.setColor(Color.WHITE);
164 				g.drawRect((i * w) / size, 0, bin, h);
165 			}
166 		}
167 
168 		paintDecorations(g);
169 
170 		if (alarm) {
171 				PaintHelper.paintRectangle(g, 2, 2, getWidth()-5, getHeight()-5,
172 				    Color.WHITE, 1);
173 		}
174 }
175 }
176 
177 /* __oOo__ */