1 19 20 package org.netbeans.modules.tasklist.usertasks.treetable; 21 22 import java.awt.event.MouseAdapter ; 23 import java.awt.event.MouseEvent ; 24 25 import javax.swing.JTable ; 26 import javax.swing.table.JTableHeader ; 27 import javax.swing.table.TableColumnModel ; 28 import org.netbeans.modules.tasklist.core.table.SortingModel; 29 30 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 31 32 37 public class SortableTableHeader extends JTableHeader { 38 private static final long serialVersionUID = 1; 39 40 46 public SortableTableHeader() { 47 this(null); 48 } 49 50 59 public SortableTableHeader(TableColumnModel cm) { 60 super(cm); 61 addMouseListener(new MouseAdapter () { 62 public void mouseClicked(MouseEvent e) { 63 mouseClick(e); 64 } 65 }); 66 setDefaultRenderer(new SortingHeaderRenderer()); 67 } 68 69 74 private void mouseClick(MouseEvent e) { 75 int col = SortableTableHeader.this.columnAtPoint(e.getPoint()); 76 if (col == -1) 77 return; 78 79 JTable 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 |