KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > SwingTableView


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
3                           Laurent Martelli <laurent@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.Point JavaDoc;
22 import java.awt.event.InputEvent JavaDoc;
23 import java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.ListSelectionModel JavaDoc;
27 import javax.swing.event.ListSelectionListener JavaDoc;
28 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
29 import javax.swing.table.JTableHeader JavaDoc;
30 import javax.swing.table.TableCellRenderer JavaDoc;
31 import javax.swing.table.TableColumnModel JavaDoc;
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 JavaDoc
39 {
40     static Logger logger = Logger.getLogger("gui.table");
41
42     // swing table component
43
JTable table;
44
45     public SwingTableView(ViewFactory factory, DisplayContext context,
46                           CollectionItem collection, Object JavaDoc substance,
47                           ExtendedTableModel model,
48                           org.objectweb.jac.aspects.gui.CollectionItemView itemView)
49     {
50         super(factory,context,collection,substance,model,itemView);
51       
52         // ensure that 'table' is not null
53
getInnerComponent(model);
54         setCellRenderers(table);
55         table.setPreferredRowHeights(1);
56         addMouseListenerToHeaderInTable((TableSorter)model);
57     }
58
59     /**
60      * Sets some mouse listeners on the columns headers. <p>"Click"
61      * sorts in ascending order, "Shift+Click" sort in descending
62      * order.
63      */

64     void addMouseListenerToHeaderInTable(final TableSorter sorter) {
65         final JTable tableView = table;
66         tableView.setColumnSelectionAllowed(false);
67         MouseAdapter JavaDoc listMouseListener = new MouseAdapter JavaDoc() {
68                 public void mouseClicked(MouseEvent JavaDoc e) {
69                     TableColumnModel JavaDoc columnModel = tableView.getColumnModel();
70                     int viewColumn = columnModel.getColumnIndexAtX(e.getX());
71                     int column = tableView.convertColumnIndexToModel(viewColumn);
72                     if (e.getClickCount() == 1 && column != -1) {
73                         //System.out.println("Sorting ...");
74
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 JavaDoc th = tableView.getTableHeader();
89         th.addMouseListener(listMouseListener);
90     }
91
92     /**
93      * Set the cell renderer to use for each column of the table.
94      */

95     protected void setCellRenderers(JTable table) {
96         ExtendedTableModel tableModel = (ExtendedTableModel)table.getModel();
97         // ClassItem[] viewerClasses = tableModel.getViewerClasses();
98
MemberItem[] members = tableModel.getMembers();
99         String JavaDoc[] headers = tableModel.getHeaders();
100       
101         for (int i=0; i<members.length; i++) {
102             //if(members[i] instanceof FieldItem)
103
Object JavaDoc cellRenderer = tableModel.getCellRenderer(this,i,factory,context);
104             if (cellRenderer instanceof TableCellRenderer JavaDoc) {
105                 logger.debug("setCellRenderer for "+
106                              members[i].getLongName()+" -> "+cellRenderer);
107                 table.getColumnModel().getColumn(i).setCellRenderer(
108                     (TableCellRenderer JavaDoc)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 JavaDoc getInnerComponent(Model JavaDoc 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 JavaDoc)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 JavaDoc getModel() {
137         return (Model JavaDoc)table.getModel();
138     }
139
140     protected ListSelectionModel JavaDoc getSelectionModel() {
141         return table.getSelectionModel();
142     }
143
144     protected CollectionUpdate getCollectionUpdate() {
145         return (CollectionUpdate)((TableSorter)model).getModel();
146     }
147
148     int locationToIndex(Point JavaDoc location) {
149         return table.rowAtPoint(location);
150     }
151
152     // CollectionView interface
153

154     public void setSelected(int index) {
155         table.changeSelection(index,-1,false,false);
156     }
157 }
158
Popular Tags