KickJava   Java API By Example, From Geeks To Geeks.

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


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

31
32 package org.antlr.xjlib.appkit.swing;
33
34 import javax.swing.*;
35 import javax.swing.event.ListSelectionEvent JavaDoc;
36 import javax.swing.event.ListSelectionListener JavaDoc;
37 import javax.swing.table.DefaultTableModel JavaDoc;
38 import java.awt.*;
39 import java.awt.dnd.Autoscroll JavaDoc;
40
41 public class XJTable extends JTable implements Autoscroll JavaDoc {
42
43     private int margin = 12;
44
45     public XJTableDelegate delegate;
46
47     public int selectionRow = -1;
48     public boolean ignoreSelectionEvent = false;
49
50     public boolean allowEmptySelection = true;
51     public boolean rememberSelection = false;
52     public boolean autoresizeColumn = false;
53
54     public XJTable() {
55         super();
56         init();
57     }
58
59     public XJTable(DefaultTableModel JavaDoc model) {
60         super(model);
61         init();
62     }
63
64     public void init() {
65         getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
66             public void valueChanged(ListSelectionEvent JavaDoc e) {
67                 if(e.getValueIsAdjusting())
68                     return;
69
70                 if(!ignoreSelectionEvent) {
71                     setSelectedRow(getSelectedRow());
72                     if(delegate != null)
73                         delegate.tableSelectionChanged(XJTable.this, selectionRow);
74                 }
75             }
76         });
77     }
78
79     public void setDelegate(XJTableDelegate delegate) {
80         this.delegate = delegate;
81     }
82
83     public void setAllowEmptySelection(boolean flag) {
84         allowEmptySelection = flag;
85     }
86
87     public void setRememberSelection(boolean flag) {
88         rememberSelection = flag;
89     }
90
91     public void setAutoresizeColumn(boolean flag) {
92         autoresizeColumn = flag;
93     }
94
95     public void setSelectedRow(int row) {
96         if(row == -1 && !allowEmptySelection)
97             row = 0;
98
99         selectionRow = Math.min(row, getRowCount()-1);
100         if(selectionRow >= 0) {
101             setRowSelectionInterval(selectionRow, selectionRow);
102         }
103     }
104
105     public void reload() {
106         ignoreSelectionEvent = true;
107         try {
108             DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc) getModel();
109             model.fireTableDataChanged();
110
111             setSelectedRow(selectionRow);
112         } finally {
113             ignoreSelectionEvent = false;
114         }
115     }
116
117     public Insets getAutoscrollInsets() {
118         Rectangle outer = getBounds();
119         Rectangle inner = getParent().getBounds();
120
121         return new Insets(inner.y-outer.y+margin, inner.x-outer.x+margin,
122                 outer.height-inner.height+margin,
123                 outer.width-inner.width+margin);
124     }
125
126     public void autoscroll(Point point) {
127         int row = rowAtPoint(point);
128         Rectangle outer = getBounds();
129         Rectangle r2 = getCellRect(point.y+outer.y<=margin?row-1:row+1, 0, true);
130         scrollRectToVisible(r2);
131     }
132
133     public void selectLastRow() {
134         setSelectedRow(getRowCount()-1);
135         scrollRectToVisible(getCellRect(getRowCount()-1, 0, true));
136     }
137
138 }
139
Popular Tags