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.TableCell;
23  import com.cosylab.gui.components.util.ColorHelper;
24  import com.cosylab.gui.components.util.PaintHelper;
25  
26  import java.awt.Color;
27  import java.awt.Component;
28  import java.awt.Graphics;
29  
30  import java.util.GregorianCalendar;
31  
32  import javax.swing.JTable;
33  
34  
35  /**
36   * Default renderer used for all <code>PropertyCell</code> objects. Uses color
37   * coding to indicate state of cell, but does not render value.
38   *
39   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
40   * @version $id$
41   */
42  public class DefaultTableCellRenderer
43  	extends javax.swing.table.DefaultTableCellRenderer
44  {
45  	private static final long serialVersionUID = 1L;
46  
47  	/**
48  	 * Default constructor.
49  	 */
50  	public DefaultTableCellRenderer()
51  	{
52  		this(false,true);
53  	}
54  	/**
55  	 * Default constructor.
56  	 */
57  	public DefaultTableCellRenderer(boolean decorateBorder, boolean decorateBackground)
58  	{
59  		super();
60  		setOpaque(true);
61  		this.decorateBorder=decorateBorder;
62  		this.decorateBackground=decorateBackground;
63  	}
64  
65  	protected boolean timeout;
66  	protected boolean error;
67  	protected boolean alarm;
68  	protected boolean active;
69  	protected String severity;
70  	protected boolean decorateBorder=false;
71  	protected boolean decorateBackground=true;
72  
73  	/**
74  	 * Returns cell renderer component. This implementation is specific to
75  	 * <code>PropertyCell</code> objects. Color coding will be used to
76  	 * indicate states. If cell indicates timeout, background will be rendered
77  	 * gray, if value is out of bounds as defined by <code>BoundedCell</code>,
78  	 * background will be colored red.
79  	 *
80  	 * @param table JTable
81  	 * @param cell Object
82  	 * @param isSelected boolean
83  	 * @param hasFocus boolean
84  	 * @param row int
85  	 * @param column int
86  	 *
87  	 * @return Component
88  	 *
89  	 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(JTable,
90  	 *      Object, boolean, boolean, int, int)
91  	 */
92  	public Component getTableCellRendererComponent(JTable table, Object cell,
93  	    boolean isSelected, boolean hasFocus, int row, int column)
94  	{
95  		Component comp = super.getTableCellRendererComponent(table, cell,
96  			    isSelected, hasFocus, row, column);
97  
98  		// Workaround for ugly default behaviour of DefaultTableCellRenderer
99  		if (isSelected) {
100 			comp.setBackground(table.getSelectionBackground());
101 		} else {
102 			comp.setBackground(table.getBackground());
103 		}
104 
105 		if (cell instanceof TableCell) {
106 			TableCell tc = (TableCell)cell;
107 
108 			if (tc.isOutOfBounds()) {
109 				comp.setBackground(ColorHelper.getWarning());
110 			}
111 
112 			timeout = tc.isTimeout();
113 			error = tc.isError();
114 			active = !tc.isSuspended();
115 			alarm = tc.isAlarm();
116 			severity = (String)tc.getCharacteristic(TableCell.SEVERITY);
117 
118 			String t = tc.getTooltip();
119 			setToolTipText(t);
120 			
121 			if (decorateBackground) {
122 				if (error || timeout) {
123 					setBackground(ColorHelper.getAlarmInvalidBackground());
124 				}
125 
126 				if (alarm) {
127 					if (severity == null) {
128 						setBackground(Color.WHITE);
129 					} else if (severity.equals("MAJOR_ALARM")) {
130 						setBackground(ColorHelper.getAlarmMajorBackground());
131 					} else if (severity.equals("MINOR_ALARM")) {
132 						setBackground(ColorHelper.getAlarmMinorBackground());
133 					} else if (severity.equals("INVALID_ALARM")) {
134 						setBackground(ColorHelper.getAlarmInvalidBackground());
135 					}
136 				}
137 
138 			}
139 		}
140 
141 		return comp;
142 	}
143 
144 	/* (non-Javadoc)
145 	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
146 	 */
147 	protected void paintComponent(Graphics g)
148 	{
149 		super.paintComponent(g);
150 
151 		paintDecorations(g);
152 	}
153 
154 	protected void paintSuperComponent(Graphics g)
155 	{
156 		super.paintComponent(g);
157 	}
158 
159 	protected void paintDecorations(Graphics g)
160 	{
161 		if (!active) {
162 			PaintHelper.paintDisabled(g, 0, 0, getWidth(), getHeight());
163 		}
164 		
165 		if (!decorateBorder) {
166 			return;
167 		}
168 
169 		int size = getHeight();
170 		int y = 0;
171 		int x = (getWidth() - size) / 2;
172 
173 		if (error && !timeout) {
174 			PaintHelper.paintAlarm(g, x, y, size);
175 		}
176 
177 		if (!error && timeout) {
178 			PaintHelper.paintTimeout(g, x, y, size, new GregorianCalendar(1900,0,0,3,0).getTime());
179 		}
180 
181 		if (error && timeout) {
182 			x = (getWidth() - 2 * size) / 2;
183 			PaintHelper.paintAlarm(g, x, y, size);
184 			x += size;
185 			PaintHelper.paintTimeout(g, x, y, size, new GregorianCalendar(1900,0,0,3,0).getTime());
186 		}
187 
188 		if (alarm) {
189 			if (severity == null) {
190 				PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
191 				    ColorHelper.getAlarm(), 2);
192 			} else if (severity.equals("MAJOR_ALARM")) {
193 				PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
194 				    ColorHelper.getAlarmMajor(), 2);
195 			} else if (severity.equals("MINOR_ALARM")) {
196 				PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
197 				    ColorHelper.getAlarmMinor(), 2);
198 			} else if (severity.equals("INVALID_ALARM")) {
199 				PaintHelper.paintRectangle(g, 0, 0, getWidth(), getHeight(),
200 				    ColorHelper.getAlarmInvalid(), 2);
201 			}
202 		}
203 	}
204 	public boolean isDecorateBackground() {
205 		return decorateBackground;
206 	}
207 	public void setDecorateBackground(boolean decorateBackground) {
208 		this.decorateBackground = decorateBackground;
209 	}
210 	public boolean isDecorateBorder() {
211 		return decorateBorder;
212 	}
213 	public void setDecorateBorder(boolean decorateBorder) {
214 		this.decorateBorder = decorateBorder;
215 	}
216 }
217 
218 /* __oOo__ */