01: /*
02: * ScrollToColumnAction.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.actions;
13:
14: import java.awt.EventQueue;
15: import java.awt.Rectangle;
16: import java.awt.event.ActionEvent;
17:
18: import workbench.gui.WbSwingUtilities;
19: import workbench.gui.components.WbTable;
20: import workbench.resource.ResourceMgr;
21: import workbench.resource.Settings;
22: import workbench.util.StringUtil;
23:
24: /**
25: * @author support@sql-workbench.net
26: */
27: public class ScrollToColumnAction extends WbAction {
28: private WbTable client;
29:
30: public ScrollToColumnAction(WbTable aClient) {
31: super ();
32: this .client = aClient;
33: this .initMenuDefinition("MnuTxtFindColumn");
34: this .setIcon(null);
35: }
36:
37: public void executeAction(ActionEvent e) {
38: String lastValue = Settings.getInstance().getProperty(
39: "workbench.gui.findcolumn.lastvalue", null);
40: String col = WbSwingUtilities.getUserInput(client, ResourceMgr
41: .getPlainString("MnuTxtFindColumn"), lastValue);
42: if (col != null) {
43: Settings.getInstance().setProperty(
44: "workbench.gui.findcolumn.lastvalue", col);
45: scrollToColumn(col.toLowerCase());
46: }
47: }
48:
49: protected void scrollToColumn(String toFind) {
50: if (StringUtil.isWhitespaceOrEmpty(toFind))
51: return;
52:
53: for (int idx = 0; idx < client.getModel().getColumnCount(); idx++) {
54: String name = client.getModel().getColumnName(idx);
55: if (name.toLowerCase().indexOf(toFind) > -1) {
56: int row = client.getSelectedRow();
57: if (row < 0) {
58: row = client.getFirstVisibleRow();
59: }
60: final Rectangle rect = client.getCellRect(row, idx,
61: true);
62: EventQueue.invokeLater(new Runnable() {
63: public void run() {
64: client.scrollRectToVisible(rect);
65: client.getTableHeader().repaint();
66: client.repaint();
67: }
68: });
69: }
70: }
71: }
72: }
|