KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.MouseAdapter JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24
25 import javax.swing.JTable JavaDoc;
26 import javax.swing.table.JTableHeader JavaDoc;
27 import javax.swing.table.TableColumnModel JavaDoc;
28 import org.netbeans.modules.tasklist.core.table.SortingModel;
29
30 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
31
32 /**
33  * A table header that can work together with SortingModel
34  *
35  * @author tl
36  */

37 public class SortableTableHeader extends JTableHeader JavaDoc {
38     private static final long serialVersionUID = 1;
39
40     /**
41      * Constructs a <code>SortableTableHeader</code> with a default
42      * <code>TableColumnModel</code>.
43      *
44      * @see #createDefaultColumnModel
45      */

46     public SortableTableHeader() {
47     this(null);
48     }
49
50     /**
51      * Constructs a <code>SortableTableHeader</code> which is initialized with
52      * <code>cm</code> as the column model. If <code>cm</code> is
53      * <code>null</code> this method will initialize the table header
54      * with a default <code>TableColumnModel</code>.
55      *
56      * @param cm the column model for the table
57      * @see #createDefaultColumnModel
58      */

59     public SortableTableHeader(TableColumnModel JavaDoc cm) {
60     super(cm);
61         addMouseListener(new MouseAdapter JavaDoc() {
62             public void mouseClicked(MouseEvent JavaDoc e) {
63                 mouseClick(e);
64             }
65         });
66         setDefaultRenderer(new SortingHeaderRenderer());
67     }
68     
69     /**
70      * Mouse click handler
71      *
72      * @param e an event
73      */

74     private void mouseClick(MouseEvent JavaDoc e) {
75         int col = SortableTableHeader.this.columnAtPoint(e.getPoint());
76         if (col == -1)
77             return;
78         
79         JTable JavaDoc t = SortableTableHeader.this.getTable();
80         if (!(t instanceof TreeTable))
81             return;
82         
83         SortingModel sm = ((TreeTable) t).getSortingModel();
84         if (sm == null)
85             return;
86         
87         int index = getColumnModel().getColumn(col).getModelIndex();
88         if (sm.getColumnComparator(index) == null)
89             return;
90
91         int cur = sm.getSortedColumn();
92         if (index == cur) {
93             if (sm.isSortOrderDescending())
94                 sm.setSortOrderDescending(false);
95             else
96                 sm.setSortedColumn(-1);
97         } else {
98             sm.setSortOrderDescending(true);
99             sm.setSortedColumn(index);
100         }
101     }
102 }
103
Popular Tags