Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / JToggleButtonHeaderCellRenderer.java @ 38564

History | View | Annotate | Download (5.78 KB)

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