1 package org.enhydra.jawe.xml.panels.tablesorting; 2 3 import java.awt.event.*; 4 import java.awt.event.MouseListener ; 5 import java.awt.*; 6 import java.util.Vector ; 7 import javax.swing.*; 8 import javax.swing.table.*; 9 import javax.swing.table.TableColumnModel ; 10 import org.enhydra.jawe.xml.*; 11 import org.enhydra.jawe.xml.XMLElement; 12 import org.enhydra.jawe.xml.XMLElementDialog; 13 import org.enhydra.jawe.xml.panels.XMLTablePanel; 14 import java.net.*; 15 16 public class SortingTable extends JTable { 17 18 protected int sortedColIndex = -1; 19 protected boolean ascending = true; 20 21 XMLTablePanel owner; 22 23 public SortingTable(XMLTablePanel p,Vector data, Vector names) { 24 super(new SortingTableModel((XMLCollection)p.getOwner(),data, names)); 25 this.owner=p; 26 27 JTableHeader h = getTableHeader(); 28 h.setDefaultRenderer(new SHRenderer()); 29 MouseListener ml=new MouseAdapter () { 30 public void mouseReleased(MouseEvent event) { 31 performSorting(event); 32 } 33 }; 34 35 h.addMouseListener(ml); 36 } 37 38 public int getSortedColumnIndex() { 39 return sortedColIndex; 40 } 41 42 public boolean isSortedColumnAscending() { 43 return ascending; 44 } 45 46 47 public void performSorting (MouseEvent event) { 48 if(owner.getOwner().isReadOnly() && ((XMLCollection)owner.getOwner()).getOwner().isReadOnly()) return; 49 if (event==null && sortedColIndex==-1) return; 50 51 TableColumnModel colModel = getColumnModel(); 52 int index = sortedColIndex; 53 if (event!=null) { 54 index = colModel.getColumnIndexAtX(event.getX()); 55 } 56 int modelIndex = colModel.getColumn(index).getModelIndex(); 57 58 SortingTableModel model = (SortingTableModel)getModel(); 59 60 Object selEl=null; 61 int sr=getSelectedRow(); 62 if (sr>=0) { 63 selEl=model.getValueAt(sr,0); 64 } 65 66 if (sortedColIndex == index && event!=null) { 67 ascending = !ascending; 68 } 69 sortedColIndex = index; 70 71 model.sortByColumn(modelIndex, ascending); 72 XMLCollection c=(XMLCollection)owner.getOwner(); 73 int cs=c.size(); 74 if (cs>0) { 75 c.toCollection().clear(); 76 for (int i=0; i<cs; i++) { 77 Object el=model.getValueAt(i,0); 78 c.toCollection().add(el); 79 } 80 ((XMLElementDialog)owner.getDialog()).notifyListeners((XMLElement)model.getValueAt(0,0)); } 82 83 if (selEl!=null) { 84 try { 85 owner.setSelectedElement(selEl); 86 } catch (Exception ex) {} 87 } 88 update(getGraphics()); 89 getTableHeader().update(getTableHeader().getGraphics()); 90 } 91 92 static class SHRenderer extends DefaultTableCellRenderer { 93 94 static Icon NONSORTED = null; 95 static Icon ASCENDING = null; 96 static Icon DESCENDING = null; 97 98 static { 99 URL url = XMLUtil.getResource("ASCENDINGImage"); 100 if ( url != null ) { 101 ASCENDING = new ImageIcon( url ); 102 } 103 url = XMLUtil.getResource("DESCENDINGImage"); 104 if ( url != null ) { 105 DESCENDING = new ImageIcon( url ); 106 } 107 } 108 109 public SHRenderer() { 110 setHorizontalTextPosition(LEFT); 111 setHorizontalAlignment(CENTER); 112 } 113 114 public Component getTableCellRendererComponent(JTable table, 115 Object value, 116 boolean isSelected, 117 boolean hasFocus, 118 int row, 119 int col) { 120 int index = -1; 121 boolean ascending = true; 122 SortingTable sortTable = (SortingTable)table; 123 index = sortTable.getSortedColumnIndex(); 124 ascending = sortTable.isSortedColumnAscending(); 125 126 if (table != null) { 127 JTableHeader header = table.getTableHeader(); 128 if (header != null) { 129 setForeground(header.getForeground()); 130 setBackground(header.getBackground()); 131 setFont(header.getFont()); 132 } 133 } 134 135 Icon icon = null; 136 if (ascending) { 137 icon=ASCENDING; 138 } else { 139 icon=DESCENDING; 140 } 141 142 if (col==index) { 143 setIcon(icon); 144 } else { 145 setIcon(NONSORTED); 146 } 147 148 if (value==null) { 149 setText(""); 150 } else { 151 setText(value.toString()); 152 } 153 154 setBorder(UIManager.getBorder("TableHeader.cellBorder")); 155 return this; 156 } 157 } 158 159 } 160 161 | Popular Tags |