KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > treetable > SortingHeaderRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.treetable;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Insets JavaDoc;
26
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JTable JavaDoc;
32 import javax.swing.SwingConstants JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
35 import javax.swing.table.JTableHeader JavaDoc;
36 import javax.swing.table.TableCellRenderer JavaDoc;
37 import javax.swing.table.TableColumnModel JavaDoc;
38 import org.netbeans.modules.tasklist.core.table.SortingModel;
39
40 /**
41  * Cell renderer for sorting column header.
42  * Originally copied from org.openide.explorer.view.TreeTableView
43  */

44 public class SortingHeaderRenderer extends JPanel JavaDoc implements TableCellRenderer JavaDoc {
45     /**
46      * A simple layout for an icon and a label
47      *
48      * [-----]
49      * [----] [-----]
50      * [Icon] [Label]
51      * [----] [-----]
52      * [-----]
53      */

54     private static class IconAndLabelLayout implements java.awt.LayoutManager JavaDoc {
55         private Component JavaDoc icon, label;
56         
57         /**
58          * Constructor
59          *
60          * @param icon icon component
61          * @param label label component
62          */

63         public IconAndLabelLayout(Component JavaDoc icon, Component JavaDoc label) {
64             this.icon = icon;
65             this.label = label;
66         }
67         
68         public java.awt.Dimension JavaDoc preferredLayoutSize(java.awt.Container JavaDoc parent) {
69             Dimension JavaDoc d = new Dimension JavaDoc();
70             Dimension JavaDoc iconPref = icon.getPreferredSize();
71             Dimension JavaDoc labelPref = label.getPreferredSize();
72             d.width = iconPref.width + labelPref.width + 2;
73             d.height = Math.max(iconPref.height, labelPref.height);
74             return d;
75         }
76         
77         public void removeLayoutComponent(Component JavaDoc comp) {
78         }
79         
80         public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
81         }
82         
83         public void layoutContainer(java.awt.Container JavaDoc parent) {
84             Dimension JavaDoc iconPref = icon.getPreferredSize();
85             Dimension JavaDoc labelPref = label.getPreferredSize();
86             Insets JavaDoc insets = parent.getInsets();
87             
88             int y = (parent.getHeight() - insets.top - insets.bottom -
89                 iconPref.height) / 2;
90             int x = (parent.getWidth() - insets.left - insets.right -
91                 iconPref.width - 2 - labelPref.width) / 2;
92             if (y < 0)
93                 y = 0;
94             if (x < 0)
95                 x = 0;
96             
97             x += insets.left;
98             y += insets.top;
99             
100             int w = parent.getWidth() - iconPref.width - 2;
101             if (w < 0)
102                 w = 0;
103             
104             icon.setBounds(x, y, iconPref.width, iconPref.height);
105             
106             if (iconPref.width != 0)
107                 x = x + iconPref.width + 2;
108             label.setBounds(x, 0, w, parent.getHeight());
109         }
110         
111         public java.awt.Dimension JavaDoc minimumLayoutSize(java.awt.Container JavaDoc parent) {
112             return new Dimension JavaDoc(0, 0);
113         }
114     }
115     
116     private static final long serialVersionUID = 1;
117
118     private static ImageIcon JavaDoc SORT_DESC_ICON =
119         new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(
120         "org/netbeans/modules/tasklist/usertasks/treetable/columnsSortedDesc.gif")); // NOI18N
121
private static ImageIcon JavaDoc SORT_ASC_ICON =
122         new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(
123         "org/netbeans/modules/tasklist/usertasks/treetable/columnsSortedAsc.gif")); // NOI18N
124

125     private JLabel JavaDoc label = new JLabel JavaDoc();
126     private DefaultTableCellRenderer JavaDoc renderer = new DefaultTableCellRenderer JavaDoc();
127     
128     /**
129      * Constructor
130      */

131     public SortingHeaderRenderer() {
132 /* setBorder(new CompoundBorder(
133             UIManager.getBorder("TableHeader.cellBorder"),
134             new EmptyBorder(0, 2, 0, 2)));*/

135         
136         renderer.setHorizontalTextPosition(SwingConstants.LEFT);
137         renderer.setOpaque(false);
138         
139         add(label);
140         add(renderer);
141         
142         setLayout(new SortingHeaderRenderer.IconAndLabelLayout(label, renderer));
143     }
144     
145     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
146             boolean isSelected, boolean hasFocus, int row, int column) {
147         if (table != null) {
148             JTableHeader JavaDoc header = table.getTableHeader();
149             if (header != null) {
150                 setForeground(header.getForeground());
151                 setBackground(header.getBackground());
152                 renderer.setFont(header.getFont());
153             }
154             TableColumnModel JavaDoc tcm = header.getColumnModel();
155             int modelIndex = tcm.getColumn(column).getModelIndex();
156             SortingModel tableModel = ((TreeTable) table).getSortingModel();
157             if (tableModel != null) {
158                 if (tableModel.getSortedColumn() == modelIndex) {
159                     renderer.setIcon(
160                         tableModel.isSortOrderDescending() ?
161                         SORT_DESC_ICON : SORT_ASC_ICON);
162                     renderer.setFont(this.getFont().deriveFont(Font.BOLD));
163                 } else {
164                     renderer.setIcon(null);
165                 }
166             }
167         }
168
169         renderer.setText((value == null) ? "" : value.toString()); // NOI18N
170
setBorder(UIManager.getBorder("TableHeader.cellBorder")); // NOI18N
171

172         return this;
173     }
174     
175     /**
176      * Sets an icon
177      *
178      * @param new icon
179      */

180     public void setIcon(Icon JavaDoc icon) {
181         label.setIcon(icon);
182     }
183     
184     /**
185      * Returns the icon
186      *
187      * @return icon
188      */

189     public Icon JavaDoc getIcon() {
190         return label.getIcon();
191     }
192 }
193
194
195
Popular Tags