1 /*********************************************************************************
2 * *
3 * Raptor - Rapid prototyping of Swing GUIs based on JavaBeans like Java objects *
4 * Copyright (C) 2003 XCOM AG *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 * *
20 *********************************************************************************/
21 package net.sf.raptor.ui.tables;
22
23 import java.awt.Cursor;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import java.lang.reflect.Method;
27 import java.util.Vector;
28
29 import javax.swing.JTable;
30 import javax.swing.ListSelectionModel;
31 import javax.swing.table.TableColumnModel;
32 import javax.swing.table.TableModel;
33
34 import net.sf.raptor.logging.Trace;
35
36 import org.apache.commons.beanutils.MethodUtils;
37
38 /**
39 * @author XCOM AG
40 *
41 * To change the template for this generated type comment go to
42 * Window>Preferences>Java>Code Generation>Code and Comments
43 */
44 public class SortableTable extends JTable {
45
46 private boolean allowSortByColumns = true;
47
48 /**
49 *
50 */
51 public SortableTable() {
52 super();
53 installTableHeaderHandler();
54 }
55
56 /**
57 * @param dm
58 */
59 public SortableTable(TableModel dm) {
60 super(dm);
61 installTableHeaderHandler();
62 }
63
64 /**
65 * @param dm
66 * @param cm
67 */
68 public SortableTable(TableModel dm, TableColumnModel cm) {
69 super(dm, cm);
70 installTableHeaderHandler();
71 }
72
73 /**
74 * @param dm
75 * @param cm
76 * @param sm
77 */
78 public SortableTable(
79 TableModel dm,
80 TableColumnModel cm,
81 ListSelectionModel sm) {
82 super(dm, cm, sm);
83 installTableHeaderHandler();
84 }
85
86 /**
87 * @param numRows
88 * @param numColumns
89 */
90 public SortableTable(int numRows, int numColumns) {
91 super(numRows, numColumns);
92 installTableHeaderHandler();
93 }
94
95 /**
96 * @param rowData
97 * @param columnNames
98 */
99 public SortableTable(Vector rowData, Vector columnNames) {
100 super(rowData, columnNames);
101 installTableHeaderHandler();
102 }
103
104 /**
105 * @param rowData
106 * @param columnNames
107 */
108 public SortableTable(Object[][] rowData, Object[] columnNames) {
109 super(rowData, columnNames);
110 installTableHeaderHandler();
111 }
112
113 /**
114 * main
115 * @param args
116 */
117 public static void main(String[] args) {
118 }
119
120 /**
121 * hilfsmethode um die sortierung nach spalten
122 * ?ber mausklicks zu realisieren und die ver?nderung
123 * der spaltenbreiten zu erlauben
124 */
125 protected void installTableHeaderHandler () {
126
127 getTableHeader().addMouseListener(new MouseAdapter() {
128
129 /**
130 * put your documentation comment here
131 * @param e
132 */
133 public void mouseClicked (MouseEvent e) {
134 TableColumnModel columnModel = getColumnModel();
135 Cursor cursor = getTableHeader().getCursor();
136 switch (cursor.getType()) {
137 case Cursor.DEFAULT_CURSOR:
138 {
139 int columnNumber = columnModel.getColumnIndexAtX(e.getX());
140 int column = convertColumnIndexToModel(columnNumber);
141
142 try {
143 Class [] args = { ListSelectionModel.class, Integer.class };
144 Method method = MethodUtils.getAccessibleMethod(getModel().getClass(), "sortByColumn", args);
145 if(method!=null) {
146 Object [] params = { getSelectionModel(), new Integer(column) };
147 method.invoke(getModel(), params );
148 } else {
149 Trace.debug("wenn die methode sortByColumn(ListSelectionModel, Integer) im TableModel implementiert waere, dann koennte die Tabelle ueber das Anklicken der Spaltenkoepfe sortiert werden");
150 }
151 } catch (Exception e1) {
152 e1.printStackTrace();
153 }
154 break;
155 }
156 case Cursor.E_RESIZE_CURSOR:
157 {
158 // Offset von -3, um sicher die Spalte links von der Cursorposition zu ermitteln
159 int columnNumber = columnModel.getColumnIndexAtX(
160 e.getX() - 3);
161 int column = convertColumnIndexToModel(columnNumber);
162 if (e.getClickCount() == 2 && column != -1) {
163 if (getAutoResizeMode() == JTable.AUTO_RESIZE_ALL_COLUMNS)
164 //AutoResize ausschalten, damit das Framework die genaue Spaltenbreite spezifizieren kann
165 setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
166 // ((ArrayTableModel)getModel()).setWidthToLongestCell(column);
167 }
168 }
169 }
170 }
171
172 });
173 }
174
175 /**
176 * zeigt an, ob die tabellensortierung durch anklicken der
177 * spaltenk?pfe aktiviert ist
178 *
179 * @param allowSortByColumnsValue true, wenn tabellensortierung ?ber anklicken
180 * der spaltenk?pfe eingeschaltet ist, sonst false
181 */
182 public boolean isSortByColumnsAllowed() {
183 return allowSortByColumns;
184 }
185
186 /**
187 * erlaubt das ein/ausschalten der tabellensortierung durch anklicken der
188 * spaltenk?pfe
189 *
190 * @param allowSortByColumnsValue
191 */
192 public void setAllowSortByColumns(boolean allowSortByColumnsValue) {
193 this.allowSortByColumns = allowSortByColumnsValue;
194 }
195
196
197 }