Statistics
| Revision:

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

History | View | Annotate | Download (5.78 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (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.mapcontrol.dal.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.JTable;
36
import javax.swing.JToggleButton;
37
import javax.swing.table.JTableHeader;
38
import javax.swing.table.TableCellRenderer;
39

    
40
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable;
41
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.notification.ColumnHeaderSelectionChangeNotification;
42
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
 *
48
 * When the selection of column headers changes, Observers are notified through
49
 * a {@link ColumnHeaderSelectionChangeNotification}.
50
 *
51
 * @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
    private FeatureTable table;
63

    
64
    /**
65
     * Create a new JToggleButtonCellRenderer for a JTable.
66
     */
67
    public JToggleButtonHeaderCellRenderer(FeatureTable table) {
68
        this.table = table;
69
        JTableHeader header = table.getTableHeader();
70
        header.addMouseListener(this);
71
                button = new JToggleButton();
72

    
73
        // Clone renderization of the default table header
74
                // 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
        button.setMargin(new Insets(0, 0, 0, 0));
80
    }
81

    
82
    /**
83
     * Returns the selected columns table model position.
84
     *
85
     * @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

    
98
    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
                // if (selectedColumns.get(columnModel)){
108
                // button.setBackground(Color.gray);
109
                // }else{
110
                // button.setBackground(Color.lightGray);
111
                // }
112
        return button;
113
    }
114

    
115
    public void deselectAll() {
116
        selectedColumns.clear();
117
        notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
118
    }
119

    
120
    public void mouseClicked(MouseEvent event) {
121
        // Look for the clicked column
122
        JTable jtable = ((JTableHeader) event.getSource()).getTable();
123
        if (jtable.equals(table)) {
124
            int columnViewIndex = table.columnAtPoint(event.getPoint());
125
            if (columnViewIndex >= 0) {
126
                int columnModelIndex =
127
                    table.convertColumnIndexToModel(columnViewIndex);
128

    
129
                // 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
            }
138
        }
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
    private void setSelection(int column) {
166
        selectedColumns.clear();
167
        selectedColumns.set(column);
168

    
169
        notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
170
    }
171

    
172
    /**
173
     * Reverses the selection of a column.
174
     */
175
    private void reverseSelection(int column) {
176
        selectedColumns.flip(column);
177

    
178
        notifyObservers(new ColumnHeaderSelectionChangeNotification(table));
179
    }
180
}