1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.Point ; 22 import java.awt.event.InputEvent ; 23 import java.awt.event.MouseAdapter ; 24 import java.awt.event.MouseEvent ; 25 import javax.swing.JComponent ; 26 import javax.swing.ListSelectionModel ; 27 import javax.swing.event.ListSelectionListener ; 28 import javax.swing.table.DefaultTableCellRenderer ; 29 import javax.swing.table.JTableHeader ; 30 import javax.swing.table.TableCellRenderer ; 31 import javax.swing.table.TableColumnModel ; 32 import org.apache.log4j.Logger; 33 import org.objectweb.jac.aspects.gui.*; 34 import org.objectweb.jac.core.rtti.CollectionItem; 35 import org.objectweb.jac.core.rtti.MemberItem; 36 37 public class SwingTableView extends AbstractCollection 38 implements ListSelectionListener 39 { 40 static Logger logger = Logger.getLogger("gui.table"); 41 42 JTable table; 44 45 public SwingTableView(ViewFactory factory, DisplayContext context, 46 CollectionItem collection, Object substance, 47 ExtendedTableModel model, 48 org.objectweb.jac.aspects.gui.CollectionItemView itemView) 49 { 50 super(factory,context,collection,substance,model,itemView); 51 52 getInnerComponent(model); 54 setCellRenderers(table); 55 table.setPreferredRowHeights(1); 56 addMouseListenerToHeaderInTable((TableSorter)model); 57 } 58 59 64 void addMouseListenerToHeaderInTable(final TableSorter sorter) { 65 final JTable tableView = table; 66 tableView.setColumnSelectionAllowed(false); 67 MouseAdapter listMouseListener = new MouseAdapter () { 68 public void mouseClicked(MouseEvent e) { 69 TableColumnModel columnModel = tableView.getColumnModel(); 70 int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 71 int column = tableView.convertColumnIndexToModel(viewColumn); 72 if (e.getClickCount() == 1 && column != -1) { 73 boolean shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) != 0; 75 if (shiftPressed) { 76 sorter.sortByColumn(column, false); 77 } else { 78 SortCriteria criteria = sorter.getSortCriteria(column); 79 if (criteria!=null) { 80 sorter.sortByColumn(-1,criteria.isAscending()); 81 } else { 82 sorter.sortByColumn(column, true); 83 } 84 } 85 } 86 } 87 }; 88 JTableHeader th = tableView.getTableHeader(); 89 th.addMouseListener(listMouseListener); 90 } 91 92 95 protected void setCellRenderers(JTable table) { 96 ExtendedTableModel tableModel = (ExtendedTableModel)table.getModel(); 97 MemberItem[] members = tableModel.getMembers(); 99 String [] headers = tableModel.getHeaders(); 100 101 for (int i=0; i<members.length; i++) { 102 Object cellRenderer = tableModel.getCellRenderer(this,i,factory,context); 104 if (cellRenderer instanceof TableCellRenderer ) { 105 logger.debug("setCellRenderer for "+ 106 members[i].getLongName()+" -> "+cellRenderer); 107 table.getColumnModel().getColumn(i).setCellRenderer( 108 (TableCellRenderer )cellRenderer); 109 } else { 110 logger.debug("setCellRenderer for "+members[i].getLongName()); 111 table.getColumnModel().getColumn(i).setCellRenderer(new ToStringTableCellRenderer()); 112 } 113 } 114 } 115 116 protected JComponent getInnerComponent(Model model) { 117 if (table==null) { 118 table = new JTable(); 119 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 120 BetterListSelectionModel selModel = new BetterListSelectionModel(table); 121 table.setSelectionModel(selModel); 122 table.setModel((javax.swing.table.TableModel )model); 123 selModel.addListSelectionListener(this); 124 } 125 return table; 126 } 127 128 protected void onRemove() { 129 table.clearSelection(); 130 } 131 132 protected int[] getSelectedIndices() { 133 return table.getSelectedRows(); 134 } 135 136 protected Model getModel() { 137 return (Model )table.getModel(); 138 } 139 140 protected ListSelectionModel getSelectionModel() { 141 return table.getSelectionModel(); 142 } 143 144 protected CollectionUpdate getCollectionUpdate() { 145 return (CollectionUpdate)((TableSorter)model).getModel(); 146 } 147 148 int locationToIndex(Point location) { 149 return table.rowAtPoint(location); 150 } 151 152 154 public void setSelected(int index) { 155 table.changeSelection(index,-1,false,false); 156 } 157 } 158 | Popular Tags |