001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components;
020:
021: import java.awt.Container;
022:
023: import javax.swing.JScrollPane;
024: import javax.swing.JTable;
025: import javax.swing.JViewport;
026: import javax.swing.table.JTableHeader;
027: import javax.swing.table.TableColumn;
028: import javax.swing.table.TableColumnModel;
029: import javax.swing.table.TableModel;
030:
031: /**
032: * This class contains utility methods for doing common tasks with JTables
033: *
034: * @author Jeff Tassin
035: */
036: public class TableUtils {
037:
038: /**
039: * Clears the column header icons for a given table
040: */
041: public static void clearColumnHeaders(JTable table) {
042: if (table == null)
043: return;
044:
045: TableColumnModel columnmodel = table.getColumnModel();
046: // reset all other column sort modes to NONE at this point
047: int count = columnmodel.getColumnCount();
048:
049: for (int index = 0; index < count; index++) {
050: TableColumn tc = columnmodel.getColumn(index);
051: Object robj = tc.getHeaderRenderer();
052: if (robj instanceof SortedColumnHeaderRenderer)
053: ((SortedColumnHeaderRenderer) robj)
054: .setSortMode(SortMode.NONE);
055: }
056: JTableHeader th = table.getTableHeader();
057: th.repaint();
058: }
059:
060: /**
061: * Converts a table row the the corresponding model index. This is needed
062: * when the table is sorted. If the table is not sorted, the table index
063: * will equal the model index
064: *
065: * @param table
066: * the table whose index to convert
067: * @param index
068: * the table index to convert
069: * @return the corresponding model index
070: */
071: public static int convertTableToModelIndex(JTable table, int index) {
072: if (index >= 0 && (table.getModel() instanceof TableSorter)) {
073: TableSorter sorter = (TableSorter) table.getModel();
074: return sorter.getModelRow(index);
075: } else
076: return index;
077: }
078:
079: /**
080: * Creates a panel that contains a table that can be sorted can be sorted.
081: * The table is scrollable and has automatically resized subsequent columns
082: *
083: * @param model
084: * the data model for the table. Don't send in a TableSorter
085: * here.
086: * @param sortable
087: * if true, then we will return a table that has sortable
088: * columns. This will wrap the tablemodel with a TableSorter.
089: * @return a container that contains the table.
090: */
091: public static JTable createBasicTablePanel(TableModel model,
092: boolean sortable) {
093: JTable table = null;
094: if (sortable) {
095: TableSorter sorter = new TableSorter(model);
096: table = new JTable(sorter);
097: initializeTableSorter(sorter, table);
098: } else {
099: table = new JTable(model);
100: }
101:
102: table.setCellSelectionEnabled(true);
103: return table;
104: }
105:
106: /**
107: * Intializes a table and a table sorter
108: */
109: public static void initializeTableSorter(TableSorter sorter,
110: JTable table) {
111: sorter.addMouseListenerToHeaderInTable(table);
112:
113: // set the renderer for each column
114: TableColumnModel columnmodel = table.getColumnModel();
115: int count = columnmodel.getColumnCount();
116: for (int index = 0; index < count; index++) {
117: TableColumn column = columnmodel.getColumn(index);
118: column.setHeaderRenderer(new SortedColumnHeaderRenderer());
119: }
120: }
121:
122: /**
123: * * Creates row header for table with row number (starting with 1)
124: * displayed
125: */
126: public static void removeRowHeader(JTable table) {
127: Container p = table.getParent();
128: if (p instanceof JViewport) {
129: Container gp = p.getParent();
130: if (gp instanceof JScrollPane) {
131: JScrollPane scrollPane = (JScrollPane) gp;
132: scrollPane.setRowHeader(null);
133: }
134: }
135: }
136:
137: /** Creates row header for table with row number (starting with 1) displayed */
138: public static TableRowHeader setRowHeader(JTable table) {
139: return setRowHeader(table, -1);
140: }
141:
142: /**
143: * Creats a row header for the given table. The row number is displayed to
144: * the left of the table ( starting with row 1).
145: *
146: * @param table
147: * the table to create the row header for
148: * @param headerWidth
149: * the number of characters to size the header
150: */
151: public static TableRowHeader setRowHeader(JTable table,
152: int headerWidth) {
153: boolean isok = false;
154:
155: TableRowHeader result = null;
156: Container p = table.getParent();
157: if (p instanceof JViewport) {
158: Container gp = p.getParent();
159: if (gp instanceof JScrollPane) {
160: JScrollPane scrollPane = (JScrollPane) gp;
161: result = new TableRowHeader(table);
162: scrollPane.setRowHeaderView(result);
163: isok = true;
164: }
165: }
166: assert (isok);
167: return result;
168: }
169:
170: /**
171: * Stops any editing for a given cell on a table.
172: */
173: public static void stopEditing(JTable table) {
174: if (table == null) {
175: assert (false);
176: return;
177: }
178:
179: if (table.isEditing()) {
180: int row = table.getEditingColumn();
181: int col = table.getEditingRow();
182: if (row >= 0 && col >= 0) {
183: javax.swing.CellEditor editor = table.getCellEditor(
184: row, col);
185: editor.stopCellEditing();
186: }
187: }
188: }
189: }
|