1
2
3
4
5
6
7 package net.sourceforge.jsh3modtool.gui.imagetable.utils;
8
9 import java.awt.*;
10
11 import javax.swing.*;
12
13 /***
14 * Util class for the JTable.
15 *
16 * @author erma
17 */
18 public class JTableUtil
19 {
20 /***
21 * Scrolls the table so the specified cell is visible in the table,
22 * Note that the table must be inside a JViewport
23 * note this may be anywhere in the visible viewport.
24 * @param table the table to scroll
25 * @param row the row
26 * @param column the column
27 */
28 public static void scrollToVisible(JTable table, int row, int column)
29 {
30 if (!(table.getParent() instanceof JViewport))
31 {
32 return;
33 }
34 JViewport viewport = (JViewport)table.getParent();
35
36
37
38 Rectangle rect = table.getCellRect(row, column, true);
39
40
41 Point pt = viewport.getViewPosition();
42
43
44
45
46 rect.setLocation(rect.x-pt.x, rect.y-pt.y);
47
48
49 viewport.scrollRectToVisible(rect);
50 }
51
52 /***
53 * Scrolls the table so the specified cell is visible in the table,
54 * Note that the table must be inside a JViewport
55 * note this may be anywhere in the visible viewport.
56 * @param table the table to scroll
57 * @param cell the cell
58 */
59 public static void scrollToVisible(JTable table, JTableUtil.Cell cell)
60 {
61 scrollToVisible( table, cell.row, cell.column );
62 }
63
64 /***
65 * Scrolls the table (its Viewport) so the indicated cell is in the center
66 * Note that the table must be inside a JViewport
67 * @param table the table to scroll
68 * @param row the row
69 * @param column the column
70 */
71 public static void scrollToCenter(JTable table, int row, int column)
72 {
73 if ( !( table.getParent() instanceof JViewport ) )
74 {
75 return;
76 }
77 JViewport viewport = (JViewport)table.getParent();
78
79
80
81 Rectangle rect = table.getCellRect(row, column, true);
82
83
84 Rectangle viewRect = viewport.getViewRect();
85
86
87
88
89 rect.setLocation(rect.x-viewRect.x, rect.y-viewRect.y);
90
91
92 int centerX = (viewRect.width-rect.width)/2;
93 int centerY = (viewRect.height-rect.height)/2;
94
95
96
97 if (rect.x < centerX)
98 {
99 centerX = -centerX;
100 }
101 if (rect.y < centerY)
102 {
103 centerY = -centerY;
104 }
105 rect.translate(centerX, centerY);
106
107
108 viewport.scrollRectToVisible(rect);
109 }
110
111 /***
112 * Scrolls the table (its Viewport) so the indicated cell is in the center
113 * Note that the table must be inside a JViewport
114 * @param table the table to scroll
115 * @param cell the cell
116 */
117 public static void scrollToCenter(JTable table, JTableUtil.Cell cell)
118 {
119 scrollToCenter( table, cell.row, cell.column );
120 }
121
122
123 /***
124 * Returns the Cell that spans over the pixel
125 * @param table the table
126 * @param x the pixels x value
127 * @param y the pixels y value
128 * @return the Cell
129 */
130 public static Cell getCell( JTable table, int x, int y )
131 {
132 int rowHeight = table.getRowHeight();
133 int colWidth = table.getColumnModel().getColumn(0).getWidth();
134 int row = 0;
135 int col = 0;
136 if ( rowHeight != 0 )
137 {
138 row = y / rowHeight;
139 }
140 if ( colWidth != 0 )
141 {
142 col = x / colWidth;
143 }
144
145 Cell cell = new Cell( row, col);
146 return cell;
147 }
148
149 /***
150 * Public internal class for describing a Cell in a Table
151 */
152 public static class Cell
153 {
154 public int row;
155 public int column;
156 public Cell( int r, int c )
157 {
158 row = r;
159 column = c;
160 }
161 }
162 }
163
164 /***
165 * Revision history
166 *
167 * $Log: P:/scm_db/archives/iWIP/iWIP Client Java/src/com/imbridge/iwip/client/swing/JTableUtil.java-arc $
168 *
169 * Rev 1.1 Jun 02 2003 15:20:56 erik lindgren
170 * Fixed imports.
171 *
172 * Rev 1.0 Feb 05 2003 10:32:20 Erik Mattsson
173 * Initial revision.
174 */