KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > swing > XJTableView


1 package org.antlr.xjlib.appkit.swing;
2
3 import org.antlr.xjlib.foundation.XJSystem;
4
5 import javax.swing.*;
6 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
7 import javax.swing.table.TableCellRenderer JavaDoc;
8 import javax.swing.table.TableColumn JavaDoc;
9 import javax.swing.table.TableColumnModel JavaDoc;
10 import java.awt.*;
11 import java.awt.event.ComponentAdapter JavaDoc;
12 import java.awt.event.ComponentEvent JavaDoc;
13 /*
14
15 [The "BSD licence"]
16 Copyright (c) 2005-2007 Jean Bovet
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
21 are met:
22
23 1. Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
25 2. Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
28 3. The name of the author may not be used to endorse or promote products
29 derived from this software without specific prior written permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 */

43
44 public class XJTableView extends JScrollPane {
45
46     private XJTable table = new XJTable();
47     private boolean alternateBackground = true;
48
49     public XJTableView() {
50         setViewportView(table);
51
52         addComponentListener(new ComponentAdapter JavaDoc() {
53             public void componentResized(ComponentEvent JavaDoc e) {
54                 autoresizeColumns();
55             }
56         });
57
58         table.setDefaultRenderer(Object JavaDoc.class, new XJTableAlternateRenderer());
59         table.setShowGrid(false);
60         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
61
62         setWheelScrollingEnabled(true);
63         getViewport().setBackground(Color.white);
64     }
65
66     public XJTable getTable() {
67         return table;
68     }
69
70     public boolean isAlternateBackground() {
71         return alternateBackground;
72     }
73
74     public void setAlternateBackground(boolean alternateBackground) {
75         this.alternateBackground = alternateBackground;
76         if(alternateBackground) {
77             table.setDefaultRenderer(Object JavaDoc.class, new XJTableAlternateRenderer());
78         } else {
79             table.setDefaultRenderer(Object JavaDoc.class, new DefaultTableCellRenderer JavaDoc());
80         }
81     }
82
83     public void autoresizeColumns() {
84         resizeTableColumnsToFitContent(table, 20);
85
86         TableColumnModel JavaDoc model = table.getColumnModel();
87         int columnTotalWidth = 0;
88         for(int i=0; i<model.getColumnCount()-1; i++) {
89             columnTotalWidth += model.getColumn(i).getPreferredWidth();
90         }
91
92         // todo this offset should be UI dependent
93
int offset;
94         if(getVerticalScrollBar().isVisible()) {
95             if(XJSystem.isWindows()) {
96                 offset = 20;
97             } else {
98                 offset = 20;
99             }
100         } else {
101             if(XJSystem.isWindows()) {
102                 offset = 3;
103             } else {
104                 offset = 4;
105             }
106         }
107
108         int spWidth = getWidth();
109         if(spWidth == 0) {
110             spWidth = getPreferredSize().width;
111         }
112
113         if(model.getColumnCount() > 0) {
114             TableColumn JavaDoc c = model.getColumn(model.getColumnCount()-1);
115             int prefWidth = c.getPreferredWidth();
116             c.setPreferredWidth(Math.max(spWidth - columnTotalWidth - offset, prefWidth));
117         }
118     }
119
120     public static void resizeTableColumnsToFitContent(JTable table, int margin) {
121         for(int c = 0; c < table.getColumnCount(); c++) {
122             resizeColumnToFitContent(table, c, margin);
123         }
124     }
125
126     public static void resizeColumnToFitContent(JTable table, int columnIndex, int margin) {
127         TableColumn JavaDoc column = table.getColumnModel().getColumn(columnIndex);
128         TableCellRenderer JavaDoc renderer = column.getHeaderRenderer();
129         if (renderer == null) {
130             renderer = table.getTableHeader().getDefaultRenderer();
131         }
132         Component c = renderer.getTableCellRendererComponent(table,
133                 column.getHeaderValue(),
134                 false, false,
135                 0, 0);
136         int maxWidth = c.getPreferredSize().width;
137
138         for(int row=0; row<table.getRowCount(); row++) {
139             renderer = table.getCellRenderer(row, columnIndex);
140             c = renderer.getTableCellRendererComponent(table,
141                     table.getValueAt(row, columnIndex),
142                     false, false,
143                     row, columnIndex);
144             maxWidth = Math.max(maxWidth, c.getPreferredSize().width);
145         }
146
147         column.setPreferredWidth(maxWidth+margin);
148     }
149
150     public void scrollToLastRow() {
151         table.scrollRectToVisible(table.getCellRect(table.getRowCount()-1, 0, true));
152     }
153 }
154
Popular Tags