root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / data / feature / swing / table / JToggleButtonHeaderCellRenderer.java @ 25842
History | View | Annotate | Download (5.58 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Gobernment (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 {DiSiD Technologies} {{Task}}
|
26 |
*/
|
27 |
package org.gvsig.fmap.data.feature.swing.table; |
28 |
|
29 |
import java.awt.Component; |
30 |
import java.awt.Insets; |
31 |
import java.awt.event.MouseEvent; |
32 |
import java.awt.event.MouseListener; |
33 |
import java.util.BitSet; |
34 |
|
35 |
import javax.swing.BorderFactory; |
36 |
import javax.swing.JTable; |
37 |
import javax.swing.JToggleButton; |
38 |
import javax.swing.table.JTableHeader; |
39 |
import javax.swing.table.TableCellRenderer; |
40 |
|
41 |
import org.gvsig.fmap.data.feature.swing.FeatureTable; |
42 |
import org.gvsig.fmap.data.feature.swing.table.notification.ColumnHeaderSelectionChangeNotification; |
43 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
44 |
|
45 |
/**
|
46 |
* A header cell renderer for JTables, which allows to select column headers by
|
47 |
* rendering a JToggleButton on each header cell.
|
48 |
*
|
49 |
* When the selection of column headers changes, Observers are notified through
|
50 |
* a {@link ColumnHeaderSelectionChangeNotification}.
|
51 |
*
|
52 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
53 |
*/
|
54 |
public class JToggleButtonHeaderCellRenderer extends |
55 |
BaseWeakReferencingObservable implements TableCellRenderer, |
56 |
MouseListener {
|
57 |
|
58 |
private BitSet selectedColumns = new BitSet(); |
59 |
|
60 |
// Component to render on each header cell
|
61 |
private JToggleButton button; |
62 |
|
63 |
private FeatureTable table;
|
64 |
|
65 |
/**
|
66 |
* Create a new JToggleButtonCellRenderer for a JTable.
|
67 |
*/
|
68 |
public JToggleButtonHeaderCellRenderer(FeatureTable table) {
|
69 |
this.table = table;
|
70 |
JTableHeader header = table.getTableHeader();
|
71 |
header.addMouseListener(this);
|
72 |
button = new JToggleButton(); |
73 |
|
74 |
// Clone renderization of the default table header
|
75 |
button.setBackground(header.getBackground()); |
76 |
button.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, table |
77 |
.getGridColor())); |
78 |
button.setFont(header.getFont()); |
79 |
button.setForeground(header.getForeground()); |
80 |
button.setMargin(new Insets(0, 0, 0, 0)); |
81 |
} |
82 |
|
83 |
/**
|
84 |
* Returns the selected columns table model position.
|
85 |
*
|
86 |
* @return an array of selected column indices
|
87 |
*/
|
88 |
public int[] getSelectedColumns() { |
89 |
int[] columns = new int[selectedColumns.cardinality()]; |
90 |
int pos = 0; |
91 |
for (int i = selectedColumns.nextSetBit(0); i >= 0; i = selectedColumns |
92 |
.nextSetBit(i + 1)) {
|
93 |
columns[pos] = i; |
94 |
pos++; |
95 |
} |
96 |
return columns;
|
97 |
} |
98 |
|
99 |
public Component getTableCellRendererComponent(JTable table, Object value, |
100 |
boolean isSelected, boolean hasFocus, int row, int column) { |
101 |
|
102 |
// Get the the model position of the column being rendered
|
103 |
int columnModel = table.convertColumnIndexToModel(column);
|
104 |
|
105 |
// Set the column header text and selected status
|
106 |
button.setText(table.getColumnName(column)); |
107 |
button.setSelected(selectedColumns.get(columnModel)); |
108 |
return button;
|
109 |
} |
110 |
|
111 |
public void deselectAll() { |
112 |
selectedColumns.clear(); |
113 |
notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
|
114 |
} |
115 |
|
116 |
public void mouseClicked(MouseEvent event) { |
117 |
// Look for the clicked column
|
118 |
JTable jtable = ((JTableHeader) event.getSource()).getTable(); |
119 |
if (jtable.equals(table)) {
|
120 |
int columnViewIndex = table.columnAtPoint(event.getPoint());
|
121 |
int columnModelIndex = table
|
122 |
.convertColumnIndexToModel(columnViewIndex); |
123 |
|
124 |
// Set or add the selected column, depending on the CTRL key being
|
125 |
// pressed or not
|
126 |
if (ctrlKeyPressed(event)) {
|
127 |
reverseSelection(columnModelIndex); |
128 |
} else {
|
129 |
setSelection(columnModelIndex); |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
public void mouseEntered(MouseEvent e) { |
135 |
// Nothing to do
|
136 |
} |
137 |
|
138 |
public void mouseExited(MouseEvent e) { |
139 |
// Nothing to do
|
140 |
} |
141 |
|
142 |
public void mousePressed(MouseEvent e) { |
143 |
// Nothing to do
|
144 |
} |
145 |
|
146 |
public void mouseReleased(MouseEvent e) { |
147 |
// Nothing to do
|
148 |
} |
149 |
|
150 |
private boolean ctrlKeyPressed(MouseEvent event) { |
151 |
final int ctrlDownMask = MouseEvent.CTRL_DOWN_MASK; |
152 |
return (event.getModifiersEx() & ctrlDownMask) == ctrlDownMask;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Sets the selection to only the given column.
|
157 |
*/
|
158 |
private void setSelection(int column) { |
159 |
selectedColumns.clear(); |
160 |
selectedColumns.set(column); |
161 |
|
162 |
notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Reverses the selection of a column.
|
167 |
*/
|
168 |
private void reverseSelection(int column) { |
169 |
selectedColumns.flip(column); |
170 |
|
171 |
notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
|
172 |
} |
173 |
} |