001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components.table;
042:
043: import org.netbeans.lib.profiler.ui.UIUtils;
044: import java.awt.*;
045: import javax.swing.*;
046: import javax.swing.border.Border;
047: import javax.swing.table.TableCellRenderer;
048:
049: /** An abstract superclass for table cell renderers to be used throughout the profiler.
050: * It implements alrenating background colors for odd and even rows, and simplifies the writing of
051: * concrete renderers by masking Swing's ugly CellRenderer API (by forcing "this" to be returned as the renderer).
052: *
053: * @author Ian Formanek
054: * @author Jiri Sedlacek
055: */
056: public abstract class EnhancedTableCellRenderer extends JPanel
057: implements TableCellRendererPersistent {
058: //~ Instance fields ----------------------------------------------------------------------------------------------------------
059:
060: protected boolean supportsFocusBorder;
061: private Border originalBorder;
062: private Color darkerUnselectedBackground;
063: private Color unselectedBackground;
064: private Color unselectedForeground;
065: private Insets originalBorderInsets;
066: private int horizontalAlignment;
067:
068: //~ Constructors -------------------------------------------------------------------------------------------------------------
069:
070: /** Creates a default table cell renderer with LEADING horizontal alignment showing border when focused.
071: * Rendering of focused cell border is disabled by default, to enable it, use setSupportsFocusBorder(true).
072: */
073: public EnhancedTableCellRenderer() {
074: setOpaque(true);
075: supportsFocusBorder = false;
076: horizontalAlignment = SwingConstants.LEADING;
077: unselectedBackground = UIUtils.getProfilerResultsBackground();
078: darkerUnselectedBackground = UIUtils
079: .getDarker(unselectedBackground);
080: }
081:
082: //~ Methods ------------------------------------------------------------------------------------------------------------------
083:
084: public void setBorder(Border border) {
085: super .setBorder(border);
086: originalBorder = border;
087:
088: if (originalBorder != null) {
089: originalBorderInsets = originalBorder.getBorderInsets(this );
090: }
091: }
092:
093: public void setHorizontalAlignment(int horizontalAlignment) {
094: this .horizontalAlignment = horizontalAlignment;
095: }
096:
097: public int getHorizontalAlignment() {
098: return horizontalAlignment;
099: }
100:
101: /** Sets whether or not the renderer supports drawing border around the focused cell */
102: public void setSupportsFocusBorder(boolean supportsFocusBorder) {
103: this .supportsFocusBorder = supportsFocusBorder;
104:
105: if ((supportsFocusBorder) && (originalBorder == null)) {
106: setBorder(BorderFactory.createEmptyBorder());
107: }
108: }
109:
110: public boolean getSupportsFocusBorder() {
111: return supportsFocusBorder;
112: }
113:
114: // ----------------------------------------------------------------------------
115: // Cell renderer functionality
116:
117: /**
118: * Returns the default table cell renderer.
119: *
120: * @param table the <code>JTable</code>
121: * @param value the value to assign to the cell at
122: * <code>[row, column]</code>
123: * @param isSelected true if cell is selected
124: * @param hasFocus true if cell has focus
125: * @param row the row of the cell to render
126: * @param column the column of the cell to render
127: * @return the default table cell renderer
128: */
129: public final Component getTableCellRendererComponent(JTable table,
130: Object value, boolean isSelected, boolean hasFocus,
131: int row, int column) {
132: if (supportsFocusBorder) {
133: if ((hasFocus) && (isSelected) && (originalBorder != null)) {
134: Border focusBorder = BorderFactory
135: .createCompoundBorder(
136: BorderFactory
137: .createLineBorder(UIUtils
138: .getDarkerLine(
139: table
140: .getSelectionBackground(),
141: 0.65f)),
142: BorderFactory.createEmptyBorder(
143: originalBorderInsets.top,
144: originalBorderInsets.left - 1,
145: originalBorderInsets.bottom,
146: originalBorderInsets.right));
147: super .setBorder(focusBorder);
148: } else {
149: super .setBorder(originalBorder);
150: }
151: }
152:
153: if (isSelected && table.isEnabled()) {
154: setRowForeground(table.isFocusOwner() ? table
155: .getSelectionForeground() : UIUtils
156: .getUnfocusedSelectionForeground());
157: setRowBackground(table.isFocusOwner() ? table
158: .getSelectionBackground() : UIUtils
159: .getUnfocusedSelectionBackground());
160: } else if (!table.isEnabled()) {
161: setRowForeground(UIManager
162: .getColor("TextField.inactiveForeground")); // NOI18N
163: setRowBackground(UIManager
164: .getColor("TextField.inactiveBackground")); // NOI18N
165: } else {
166: if ((row & 0x1) == 0) { //even row
167: setRowForeground((unselectedForeground != null) ? unselectedForeground
168: : table.getForeground());
169: setRowBackground((darkerUnselectedBackground != null) ? darkerUnselectedBackground
170: : UIUtils.getDarker(table.getBackground()));
171: } else {
172: setRowForeground((unselectedForeground != null) ? unselectedForeground
173: : table.getForeground());
174: setRowBackground((unselectedBackground != null) ? unselectedBackground
175: : table.getBackground());
176: }
177: }
178:
179: setState(table, value, isSelected, hasFocus, row, column);
180: setValue(table, value, row, column);
181:
182: return this ;
183: }
184:
185: /**
186: * Returns persistent (new) table cell renderer with provided parameters.
187: *
188: * @param table the <code>JTable</code>
189: * @param value the value to assign to the cell at
190: * <code>[row, column]</code>
191: * @param isSelected true if cell is selected
192: * @param hasFocus true if cell has focus
193: * @param row the row of the cell to render
194: * @param column the column of the cell to render
195: * @return the default table cell renderer
196: */
197: public abstract Component getTableCellRendererComponentPersistent(
198: JTable table, Object value, boolean isSelected,
199: boolean hasFocus, int row, int column);
200:
201: // ----------------------------------------------------------------------------
202: // Private impl
203: public static Color getDarker(Color c) {
204: return UIUtils.getSafeColor((int) (c.getRed() - 30), (int) (c
205: .getGreen() - 30), (int) (c.getBlue() - 30));
206: }
207:
208: /**
209: * Called each time this renderer is to be used to render a specific row, with the color
210: * to be used for painting background of this row. The default implementation sets the
211: * background of the panel to this color, so this method does not need to be overriden unless
212: * the subclass has any opaque components placed into the panel that should alternate their background
213: * on each line and change on selected lines.
214: *
215: * @param c the color to be used for row background
216: */
217: protected void setRowBackground(Color c) {
218: setBackground(c);
219: }
220:
221: // ----------------------------------------------------------------------------
222: // API for subclasses
223:
224: /**
225: * Called each time this renderer is to be used to render a specific row, with the color
226: * to be used for painting foreground of this row (based on table's getSelectionForeground).
227: * The default implementation sets the foreground of the panel to this color.
228: * Subclasses should override this to meaningfully change the rendering when the selection changes.
229: *
230: * @param c the color to be used for row foreground
231: */
232: protected void setRowForeground(Color c) {
233: setForeground(c);
234: }
235:
236: /**
237: * Called each time this renderer is to be used to render a specific value on specified row/column.
238: * Subclasses need to implement this method to render the value.
239: *
240: * @param table the table in which the rendering occurs
241: * @param value the value to be rendered
242: * @param row the row at which the value is located
243: * @param column the column at which the value is located
244: */
245: protected abstract void setValue(JTable table, Object value,
246: int row, int column);
247:
248: /**
249: * Called each time this renderer is to be used to render a specific value on specified row/column.
250: * Subclasses can override this method to set their states according to provided values.
251: *
252: * @param table the <code>JTable</code>
253: * @param value the value to assign to the cell at
254: * <code>[row, column]</code>
255: * @param isSelected true if cell is selected
256: * @param hasFocus true if cell has focus
257: * @param row the row of the cell to render
258: * @param column the column of the cell to render
259: */
260: protected void setState(JTable table, Object value,
261: boolean isSelected, boolean hasFocus, int row, int column) {
262: }
263: }
|