KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > UIUtil


1 package org.objectstyle.cayenne.modeler.util;
2
3 import java.awt.Rectangle JavaDoc;
4
5 import javax.swing.JTable JavaDoc;
6 import javax.swing.JViewport JavaDoc;
7
8 /**
9  * @author Andrei Adamchik
10  * @since 1.1
11  */

12 public class UIUtil {
13
14     /**
15      * Scrolls table within JViewport to the selected row if there is one.
16      */

17     public static void scrollToSelectedRow(JTable JavaDoc table) {
18         int row = table.getSelectedRow();
19         if (row >= 0) {
20             scroll(table, row, 0);
21         }
22     }
23
24     /**
25     * Scrolls view if it is located in a JViewport, so that the specified cell
26     * is displayed in the center.
27     */

28     public static void scroll(JTable JavaDoc table, int rowIndex, int vColIndex) {
29         if (!(table.getParent() instanceof JViewport JavaDoc)) {
30             return;
31         }
32
33         JViewport JavaDoc viewport = (JViewport JavaDoc) table.getParent();
34         Rectangle JavaDoc rect = table.getCellRect(rowIndex, vColIndex, true);
35         Rectangle JavaDoc viewRect = viewport.getViewRect();
36
37         if (viewRect.intersects(rect)) {
38             return;
39         }
40
41         // Translate the cell location so that it is relative
42
// to the view, assuming the northwest corner of the
43
// view is (0,0).
44
rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);
45
46         // Calculate location of rect if it were at the center of view
47
int centerX = (viewRect.width - rect.width) / 2;
48         int centerY = (viewRect.height - rect.height) / 2;
49
50         // Fake the location of the cell so that scrollRectToVisible
51
// will move the cell to the center
52
if (rect.x < centerX) {
53             centerX = -centerX;
54         }
55         if (rect.y < centerY) {
56             centerY = -centerY;
57         }
58         rect.translate(centerX, centerY);
59
60         // Scroll the area into view.
61
viewport.scrollRectToVisible(rect);
62     }
63 }
64
Popular Tags