001: /*
002: * ColumnSelectorPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.GridBagConstraints;
017: import java.awt.GridBagLayout;
018: import java.awt.Insets;
019: import java.awt.event.ActionListener;
020: import java.util.ArrayList;
021: import java.util.List;
022: import javax.swing.JButton;
023: import javax.swing.JCheckBox;
024:
025: import javax.swing.JLabel;
026: import javax.swing.JPanel;
027: import javax.swing.JTable;
028: import javax.swing.border.EmptyBorder;
029: import javax.swing.event.TableModelEvent;
030: import javax.swing.table.TableColumn;
031: import javax.swing.table.TableColumnModel;
032: import javax.swing.table.TableModel;
033:
034: import workbench.db.ColumnIdentifier;
035: import workbench.resource.ResourceMgr;
036:
037: /**
038: *
039: * @author support@sql-workbench.net
040: */
041: public class ColumnSelectorPanel extends JPanel implements
042: ActionListener {
043: private JTable selectTable;
044: protected JPanel infoPanel;
045: private ColumnSelectTableModel model;
046: private JButton selectAll;
047: private JButton selectNone;
048: private JCheckBox selectedOnlyCheckBox;
049: private JCheckBox includeHeaderCheckBox;
050:
051: public ColumnSelectorPanel(ColumnIdentifier[] columns) {
052: this (columns, false, false, false, false);
053: }
054:
055: public ColumnSelectorPanel(ColumnIdentifier[] columns,
056: boolean includeHeader, boolean selectedOnly,
057: boolean showHeaderSelection, boolean showSelectedCheckBox) {
058: this .setLayout(new BorderLayout());
059: this .selectTable = new JTable();
060: this .selectTable
061: .setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
062: this .selectTable.setRowSelectionAllowed(false);
063: this .selectTable.setColumnSelectionAllowed(false);
064: this .model = new ColumnSelectTableModel(columns);
065: this .selectTable.setModel(this .model);
066:
067: TableColumnModel colMod = this .selectTable.getColumnModel();
068: TableColumn col = colMod.getColumn(0);
069: col.setPreferredWidth(150);
070: col.setMinWidth(50);
071: col = colMod.getColumn(1);
072: col.setPreferredWidth(65);
073: col.setMinWidth(35);
074:
075: WbScrollPane scroll = new WbScrollPane(this .selectTable);
076: this .infoPanel = new JPanel();
077: configureInfoPanel();
078: this .add(this .infoPanel, BorderLayout.NORTH);
079: this .add(scroll, BorderLayout.CENTER);
080:
081: selectAll = new FlatButton(ResourceMgr
082: .getString("LblSelectAll"));
083: selectNone = new FlatButton(ResourceMgr
084: .getString("LblSelectNone"));
085: selectAll.addActionListener(this );
086: selectNone.addActionListener(this );
087:
088: if (showSelectedCheckBox) {
089: this .selectedOnlyCheckBox = new JCheckBox(ResourceMgr
090: .getString("LblSelectedRowsOnly"));
091: this .selectedOnlyCheckBox.setSelected(selectedOnly);
092: this .selectedOnlyCheckBox.setEnabled(true);
093: }
094:
095: if (showHeaderSelection) {
096: this .includeHeaderCheckBox = new JCheckBox(ResourceMgr
097: .getString("LblExportIncludeHeaders"));
098: this .includeHeaderCheckBox.setSelected(includeHeader);
099: }
100:
101: JPanel optionPanel = new JPanel();
102: optionPanel.setLayout(new GridBagLayout());
103: GridBagConstraints c = new GridBagConstraints();
104: c.gridx = 0;
105: c.gridy = 0;
106: c.weightx = 0.5;
107: c.anchor = java.awt.GridBagConstraints.EAST;
108: c.insets = new Insets(0, 0, 0, 5);
109: optionPanel.add(selectAll, c);
110:
111: c.gridx = 1;
112: c.gridy = 0;
113: c.anchor = java.awt.GridBagConstraints.WEST;
114: c.weightx = 0.5;
115: c.insets = new Insets(0, 5, 0, 0);
116: optionPanel.add(selectNone, c);
117:
118: if (showSelectedCheckBox) {
119: c.gridx = 0;
120: c.gridy = 1;
121: c.insets = new Insets(3, 0, 0, 0);
122: c.anchor = (showHeaderSelection ? java.awt.GridBagConstraints.EAST
123: : java.awt.GridBagConstraints.CENTER);
124: c.gridwidth = (showHeaderSelection ? 1 : 2);
125: c.weightx = (showHeaderSelection ? 0.5 : 1.0);
126: optionPanel.add(selectedOnlyCheckBox, c);
127: }
128:
129: if (showHeaderSelection) {
130: c.gridx = (showSelectedCheckBox ? 1 : 0);
131: c.gridy = 1;
132: c.gridwidth = (showSelectedCheckBox ? java.awt.GridBagConstraints.EAST
133: : java.awt.GridBagConstraints.CENTER);
134: c.gridwidth = (showSelectedCheckBox ? 1 : 2);
135: c.weightx = (showSelectedCheckBox ? 0.5 : 1.0);
136: c.anchor = java.awt.GridBagConstraints.WEST;
137: optionPanel.add(includeHeaderCheckBox, c);
138: }
139:
140: optionPanel.setBorder(new EmptyBorder(5, 0, 10, 0));
141: this .add(optionPanel, BorderLayout.SOUTH);
142: Dimension d = new Dimension(300, 250);
143: this .setPreferredSize(d);
144: }
145:
146: protected void configureInfoPanel() {
147: String msg = ResourceMgr.getString("MsgSelectColumns");
148: JLabel infoLabel = new JLabel(msg);
149: this .infoPanel.add(infoLabel);
150: }
151:
152: public void setSelectionLabel(String label) {
153: this .model.selectLabel = label;
154: TableColumnModel colMod = this .selectTable.getColumnModel();
155: TableColumn col = colMod.getColumn(1);
156: col.setHeaderValue(label);
157: }
158:
159: public boolean selectedOnly() {
160: if (this .selectedOnlyCheckBox == null)
161: return false;
162: return selectedOnlyCheckBox.isSelected();
163: }
164:
165: public boolean includeHeader() {
166: if (this .includeHeaderCheckBox == null)
167: return false;
168: return includeHeaderCheckBox.isSelected();
169: }
170:
171: /**
172: * Check if the column with the specified index is selected.
173: * @param index the index to be checked
174: * @return true if selected, false otherwise
175: */
176: public boolean isColumnSelected(int index) {
177: return this .model.selected[index];
178: }
179:
180: /**
181: * Return the number of selected columns.
182: * @return int
183: */
184: public int getSelectedCount() {
185: int selected = 0;
186: for (int i = 0; i < this .model.selected.length; i++) {
187: if (this .model.selected[i])
188: selected++;
189: }
190: return selected;
191: }
192:
193: /**
194: * Return the columns that have been selected.
195: * @return the selected columns
196: */
197: public List<ColumnIdentifier> getSelectedColumns() {
198: int selected = this .getSelectedCount();
199: List<ColumnIdentifier> result = new ArrayList<ColumnIdentifier>(
200: selected);
201: for (int i = 0; i < this .model.selected.length; i++) {
202: if (this .model.selected[i]) {
203: result.add(this .model.columns[i].createCopy());
204: }
205: }
206: return result;
207: }
208:
209: public void selectAll() {
210: this .model.selectAll();
211: }
212:
213: public void selectNone() {
214: this .model.selectNone();
215: }
216:
217: public void setColumnSelected(int i, boolean flag) {
218: this .model.selected[i] = flag;
219: }
220:
221: public void selectColumns(List columns) {
222: if (columns == null) {
223: this .selectAll();
224: } else {
225: int cols = this .model.columns.length;
226: for (int i = 0; i < cols; i++) {
227: this .model.selected[i] = columns
228: .contains(this .model.columns[i]);
229: }
230: }
231: }
232:
233: public void actionPerformed(java.awt.event.ActionEvent e) {
234: if (this .model == null)
235: return;
236:
237: if (e.getSource() == this .selectAll) {
238: this .model.selectAll();
239: } else if (e.getSource() == this .selectNone) {
240: this .model.selectNone();
241: }
242: TableModelEvent evt = new TableModelEvent(model);
243: this .selectTable.tableChanged(evt);
244: }
245:
246: }
247:
248: class ColumnSelectTableModel implements TableModel {
249: ColumnIdentifier[] columns;
250: boolean[] selected;
251: String colLabel = ResourceMgr
252: .getString("LblHeaderKeyColumnColName");
253: String selectLabel = ResourceMgr.getString("LblHeaderUseColumn");
254:
255: private int rows;
256:
257: public ColumnSelectTableModel(ColumnIdentifier[] cols) {
258: this .rows = cols.length;
259: this .columns = cols;
260: this .selected = new boolean[this .rows];
261: }
262:
263: public int getColumnCount() {
264: return 2;
265: }
266:
267: public Object getValueAt(int row, int column) {
268: if (column == 0) {
269: return this .columns[row].getColumnName();
270: } else if (column == 1) {
271: return Boolean.valueOf(this .selected[row]);
272: }
273: return "";
274: }
275:
276: public void selectAll() {
277: this .setFlagForAll(true);
278: }
279:
280: public void selectNone() {
281: this .setFlagForAll(false);
282: }
283:
284: private void setFlagForAll(boolean flag) {
285: for (int i = 0; i < this .rows; i++) {
286: this .selected[i] = flag;
287: }
288: }
289:
290: public int getRowCount() {
291: return this .rows;
292: }
293:
294: public void addTableModelListener(
295: javax.swing.event.TableModelListener l) {
296: }
297:
298: public Class getColumnClass(int columnIndex) {
299: if (columnIndex == 0)
300: return String.class;
301: else
302: return Boolean.class;
303: }
304:
305: public String getColumnName(int columnIndex) {
306: if (columnIndex == 0)
307: return this .colLabel;
308: return this .selectLabel;
309: }
310:
311: public boolean isCellEditable(int rowIndex, int columnIndex) {
312: return (columnIndex == 1);
313: }
314:
315: public void removeTableModelListener(
316: javax.swing.event.TableModelListener l) {
317: }
318:
319: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
320: if (columnIndex == 1 && aValue instanceof Boolean) {
321: Boolean b = (Boolean) aValue;
322: this.selected[rowIndex] = b.booleanValue();
323: }
324: }
325:
326: }
|