svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretable / table / renders / JToggleButtonHeaderCellRenderer.java @ 42775
History | View | Annotate | Download (5.96 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2008 {DiSiD Technologies} {{Task}}
|
27 |
*/
|
28 |
package org.gvsig.fmap.dal.swing.impl.featuretable.table.renders; |
29 |
|
30 |
import java.awt.Component; |
31 |
import java.awt.Insets; |
32 |
import java.awt.event.ActionEvent; |
33 |
import java.awt.event.MouseEvent; |
34 |
import java.awt.event.MouseListener; |
35 |
import java.util.BitSet; |
36 |
|
37 |
import javax.swing.JTable; |
38 |
import javax.swing.JToggleButton; |
39 |
import javax.swing.table.JTableHeader; |
40 |
import javax.swing.table.TableCellRenderer; |
41 |
|
42 |
import org.gvsig.fmap.dal.swing.impl.featuretable.FeatureTable; |
43 |
import org.gvsig.fmap.dal.swing.impl.featuretable.table.notification.ColumnHeaderSelectionChangeNotification; |
44 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
45 |
|
46 |
/**
|
47 |
* A header cell renderer for JTables, which allows to select column headers by
|
48 |
* rendering a JToggleButton on each header cell.
|
49 |
*
|
50 |
* When the selection of column headers changes, Observers are notified through
|
51 |
* a {@link ColumnHeaderSelectionChangeNotification}.
|
52 |
*
|
53 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
54 |
*/
|
55 |
public class JToggleButtonHeaderCellRenderer extends |
56 |
BaseWeakReferencingObservable implements TableCellRenderer, |
57 |
MouseListener {
|
58 |
|
59 |
private BitSet selectedColumns = new BitSet(); |
60 |
|
61 |
// Component to render on each header cell
|
62 |
private JToggleButton button; |
63 |
|
64 |
private FeatureTable table;
|
65 |
|
66 |
/**
|
67 |
* Create a new JToggleButtonCellRenderer for a JTable.
|
68 |
*/
|
69 |
public JToggleButtonHeaderCellRenderer(FeatureTable table) {
|
70 |
this.table = table;
|
71 |
JTableHeader header = table.getTableHeader();
|
72 |
header.addMouseListener(this);
|
73 |
button = new JToggleButton(); |
74 |
|
75 |
// Clone renderization of the default table header
|
76 |
// button.setBackground(header.getBackground());
|
77 |
// button.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, table
|
78 |
// .getGridColor()));
|
79 |
// button.setFont(header.getFont());
|
80 |
// button.setForeground(header.getForeground());
|
81 |
button.setMargin(new Insets(0, 0, 0, 0)); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Returns the selected columns table model position.
|
86 |
*
|
87 |
* @return an array of selected column indices
|
88 |
*/
|
89 |
public int[] getSelectedColumns() { |
90 |
int[] columns = new int[selectedColumns.cardinality()]; |
91 |
int pos = 0; |
92 |
for (int i = selectedColumns.nextSetBit(0); i >= 0; i = selectedColumns |
93 |
.nextSetBit(i + 1)) {
|
94 |
columns[pos] = i; |
95 |
pos++; |
96 |
} |
97 |
return columns;
|
98 |
} |
99 |
|
100 |
public Component getTableCellRendererComponent(JTable table, Object value, |
101 |
boolean isSelected, boolean hasFocus, int row, int column) { |
102 |
|
103 |
// Get the the model position of the column being rendered
|
104 |
int columnModel = table.convertColumnIndexToModel(column);
|
105 |
|
106 |
// Set the column header text and selected status
|
107 |
button.setText(table.getColumnName(column)); |
108 |
button.setSelected(selectedColumns.get(columnModel)); |
109 |
// if (selectedColumns.get(columnModel)){
|
110 |
// button.setBackground(Color.gray);
|
111 |
// }else{
|
112 |
// button.setBackground(Color.lightGray);
|
113 |
// }
|
114 |
return button;
|
115 |
} |
116 |
|
117 |
public void deselectAll() { |
118 |
selectedColumns.clear(); |
119 |
notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
|
120 |
} |
121 |
|
122 |
public void mouseClicked(MouseEvent event) { |
123 |
// Look for the clicked column
|
124 |
JTable jtable = ((JTableHeader) event.getSource()).getTable(); |
125 |
if (jtable.equals(table)) {
|
126 |
int columnViewIndex = table.columnAtPoint(event.getPoint());
|
127 |
if (columnViewIndex >= 0) { |
128 |
int columnModelIndex =
|
129 |
table.convertColumnIndexToModel(columnViewIndex); |
130 |
|
131 |
// Set or add the selected column, depending on the CTRL key
|
132 |
// being
|
133 |
// pressed or not
|
134 |
if (ctrlKeyPressed(event)) {
|
135 |
reverseSelection(columnModelIndex); |
136 |
} else {
|
137 |
setSelection(columnModelIndex); |
138 |
} |
139 |
table.fireColumnSelection(new ActionEvent(table, 1, "ColumnSelection")); |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
public void mouseEntered(MouseEvent e) { |
145 |
// Nothing to do
|
146 |
} |
147 |
|
148 |
public void mouseExited(MouseEvent e) { |
149 |
// Nothing to do
|
150 |
} |
151 |
|
152 |
public void mousePressed(MouseEvent e) { |
153 |
// Nothing to do
|
154 |
} |
155 |
|
156 |
public void mouseReleased(MouseEvent e) { |
157 |
// Nothing to do
|
158 |
} |
159 |
|
160 |
private boolean ctrlKeyPressed(MouseEvent event) { |
161 |
final int ctrlDownMask = MouseEvent.CTRL_DOWN_MASK; |
162 |
return (event.getModifiersEx() & ctrlDownMask) == ctrlDownMask;
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Sets the selection to only the given column.
|
167 |
*/
|
168 |
private void setSelection(int column) { |
169 |
selectedColumns.clear(); |
170 |
selectedColumns.set(column); |
171 |
|
172 |
notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Reverses the selection of a column.
|
177 |
*/
|
178 |
private void reverseSelection(int column) { |
179 |
selectedColumns.flip(column); |
180 |
|
181 |
notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
|
182 |
} |
183 |
} |