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 java.awt.*;
044: import javax.swing.*;
045: import javax.swing.table.TableCellRenderer;
046:
047: /**
048: * This class is used for rendering the JTable header. It also holds information about current sorting column and sorting order.
049: * The column header is rendered by the JButton using the appropriate icon.
050: * @author Jiri Sedlacek
051: */
052: public class CustomSortableHeaderRenderer implements TableCellRenderer {
053: //~ Instance fields ----------------------------------------------------------------------------------------------------------
054:
055: private ImageIcon ascIcon;
056: private ImageIcon descIcon;
057: private boolean sortOrder = SortableTableModel.SORT_ORDER_DESC;
058:
059: /**
060: * The column which is currently being pressed.
061: * The button has to be pressed programatically because the mouse events are not delivered to the JButton from the table header.
062: */
063: private int pressedColumn = -1;
064:
065: /**
066: * The column which currently defines the sorting order. Only this column header contains the appropriate icon.
067: */
068: private int sortingColumn = -1;
069:
070: //~ Constructors -------------------------------------------------------------------------------------------------------------
071:
072: /**
073: * Creates a new instance of CustomSortableHeaderRenderer
074: * @param asc The icon representing ascending sort order.
075: * @param desc The icon representing descending sort order.
076: */
077: public CustomSortableHeaderRenderer(ImageIcon asc, ImageIcon desc) {
078: ascIcon = asc;
079: descIcon = desc;
080: }
081:
082: //~ Methods ------------------------------------------------------------------------------------------------------------------
083:
084: public void setPressedColumn(int column) {
085: pressedColumn = column;
086: }
087:
088: public int getPressedColumn() {
089: return pressedColumn;
090: }
091:
092: public void setSortingColumn(int column) {
093: sortingColumn = column;
094: }
095:
096: public int getSortingColumn() {
097: return sortingColumn;
098: }
099:
100: public void setSortingOrder(boolean order) {
101: sortOrder = order;
102: }
103:
104: public boolean getSortingOrder() {
105: return sortOrder;
106: }
107:
108: public Component getTableCellRendererComponent(JTable table,
109: Object value, boolean isSelected, boolean hasFocus,
110: int row, int column) {
111: TableCellRenderer tableCellRenderer = table.getTableHeader()
112: .getDefaultRenderer();
113: Component c = tableCellRenderer.getTableCellRendererComponent(
114: table, value, isSelected, hasFocus, row, column);
115:
116: if (c instanceof JLabel) {
117: JLabel l = (JLabel) c;
118:
119: if (column == sortingColumn) { // only for sorting column the icon is displayed
120: l
121: .setIcon((sortOrder == SortableTableModel.SORT_ORDER_ASC) ? ascIcon
122: : descIcon);
123: l.setFont(l.getFont().deriveFont(Font.BOLD));
124: } else {
125: l.setIcon(null);
126: }
127:
128: l.setHorizontalTextPosition(JLabel.LEFT);
129: }
130:
131: return c;
132: }
133:
134: public void reverseSortingOrder() {
135: sortOrder = !sortOrder;
136: }
137: }
|