01: /*
02: * ColumnHider.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.components;
13:
14: import java.util.ArrayList;
15: import java.util.List;
16: import javax.swing.table.TableColumn;
17: import javax.swing.table.TableColumnModel;
18:
19: /**
20: *
21: * @author support@sql-workbench.net
22: */
23: public class ColumnHider {
24:
25: private List<TableColumn> hiddenColumns;
26: private TableColumnModel columnModel;
27:
28: public ColumnHider(TableColumnModel model) {
29: columnModel = model;
30: hiddenColumns = new ArrayList<TableColumn>(model
31: .getColumnCount());
32: }
33:
34: public void hideColumn(Object identifier) {
35: if (isHidden(identifier))
36: return;
37:
38: int index = columnModel.getColumnIndex(identifier);
39: if (index > -1) {
40: TableColumn col = columnModel.getColumn(index);
41: this .hiddenColumns.add(col);
42: this .columnModel.removeColumn(col);
43: }
44: }
45:
46: public void showColumn(Object identifier) {
47: TableColumn col = getHiddenColumn(identifier);
48: if (col != null) {
49: columnModel.addColumn(col);
50: }
51: }
52:
53: private boolean isHidden(Object identifier) {
54: return getHiddenColumn(identifier) != null;
55: }
56:
57: private TableColumn getHiddenColumn(Object identifier) {
58: if (identifier == null)
59: return null;
60: for (TableColumn col : hiddenColumns) {
61: Object id = col.getIdentifier();
62: if (id != null) {
63: if (col.getIdentifier().equals(identifier))
64: return col;
65: }
66: }
67: return null;
68: }
69:
70: }
|