OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.table;
15
 
16
import java.util.Collections;
17
import java.util.List;
18
import java.util.Vector;
19
 
20
import javax.swing.table.DefaultTableColumnModel;
21
import javax.swing.table.TableColumn;
22
 
23
/**
24
 * <code>XTableColumnModel</code> extends the DefaultTableColumnModel . It provides a comfortable
25
 * way to hide/show columns. Columns keep their positions when hidden and shown again.
26
 *
27
 * In order to work with JTable it cannot add any events to <code>TableColumnModelListener</code>.
28
 * Therefore hiding a column will result in <code>columnRemoved</code> event and showing it again
29
 * will notify listeners of a <code>columnAdded</code>, and possibly a <code>columnMoved</code>
30
 * event. For the same reason the following methods still deal with visible columns only:
31
 * getColumnCount(), getColumns(), getColumnIndex(), getColumn() There are overloaded versions of
32
 * these methods that take a parameter <code>onlyVisible</code> which let's you specify wether you
33
 * want invisible columns taken into account.
34
 *
35
 * @version 0.9 04/03/01
36
 * @author Stephen Kelvin, mail@StephenKelvin.de
37
 * @see DefaultTableColumnModel
38
 */
39
@SuppressWarnings("unqualified-field-access")
40
public class XTableColumnModel extends DefaultTableColumnModel {
41
    /**
42
     * Array of TableColumn objects in this model. Holds all column objects, regardless of their
43
     * visibility
44
     */
45
    protected List<TableColumn> allTableColumns = new Vector<TableColumn>();
46
 
47
    /**
48
     * Creates an extended table column model.
49
     */
50
    public XTableColumnModel() {
51
    }
52
 
53
    /**
54
     * Sets the visibility of the specified TableColumn. The call is ignored if the TableColumn is
55
     * not found in this column model or its visibility status did not change.
56
     * <p>
57
     *
58
     * @param column the column to show/hide
59
     * @param visible its new visibility status
60
     */
61
    // listeners will receive columnAdded()/columnRemoved() event
62
    public void setColumnVisible(TableColumn column, boolean visible) {
63
        if (!visible) {
64
            super.removeColumn(column);
65
        } else {
66
            // find the visible index of the column:
67
            // iterate through both collections of visible and all columns, counting
68
            // visible columns up to the one that's about to be shown again
69
            int noVisibleColumns = tableColumns.size();
70
            int noInvisibleColumns = allTableColumns.size();
71
            int visibleIndex = 0;
72
 
73
            for (int invisibleIndex = 0; invisibleIndex < noInvisibleColumns; ++invisibleIndex) {
74
                TableColumn visibleColumn = (visibleIndex < noVisibleColumns ? (TableColumn) tableColumns.get(visibleIndex) : null);
75
                TableColumn testColumn = allTableColumns.get(invisibleIndex);
76
 
77
                if (testColumn == column) {
78
                    if (visibleColumn != column) {
79
                        super.addColumn(column);
80
                        super.moveColumn(tableColumns.size() - 1, visibleIndex);
81
                    }
82
                    return; // ####################
83
                }
84
                if (testColumn == visibleColumn) {
85
                    ++visibleIndex;
86
                }
87
            }
88
        }
89
    }
90
 
91
    /**
92
     * Makes all columns in this model visible
93
     */
94
    public void setAllColumnsVisible() {
95
        int noColumns = allTableColumns.size();
96
 
97
        for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
98
            TableColumn visibleColumn = (columnIndex < tableColumns.size() ? (TableColumn) tableColumns.get(columnIndex) : null);
99
            TableColumn invisibleColumn = allTableColumns.get(columnIndex);
100
 
101
            if (visibleColumn != invisibleColumn) {
102
                super.addColumn(invisibleColumn);
103
                super.moveColumn(tableColumns.size() - 1, columnIndex);
104
            }
105
        }
106
    }
107
 
108
    /**
109
     * Maps the index of the column in the table model at <code>modelColumnIndex</code> to the
110
     * TableColumn object. There may me multiple TableColumn objects showing the same model column,
111
     * though this is uncommon. This method will always return the first visible or else the first
112
     * invisible column with the specified index.
113
     *
114
     * @param modelColumnIndex index of column in table model
115
     * @return table column object or null if no such column in this column model
116
     */
117
    public TableColumn getColumnByModelIndex(int modelColumnIndex) {
118
        for (int columnIndex = 0; columnIndex < allTableColumns.size(); ++columnIndex) {
119
            TableColumn column = allTableColumns.get(columnIndex);
120
            if (column.getModelIndex() == modelColumnIndex) {
121
                return column;
122
            }
123
        }
124
        return null;
125
    }
126
 
127
    /**
128
     * Checks wether the specified column is currently visible.
129
     *
130
     * @param aColumn column to check
131
     * @return visibility of specified column (false if there is no such column at all. [It's not
132
     *         visible, right?])
133
     */
134
    public boolean isColumnVisible(TableColumn aColumn) {
135
        return (tableColumns.indexOf(aColumn) >= 0);
136
    }
137
 
138
    /**
139
     * Append <code>column</code> to the right of exisiting columns. Posts <code>columnAdded</code>
140
     * event.
141
     *
142
     * @param column The column to be added
143
     * @see #removeColumn
144
     * @exception IllegalArgumentException if <code>column</code> is <code>null</code>
145
     */
146
    public void addColumn(TableColumn column) {
147
        allTableColumns.add(column);
148
        super.addColumn(column);
149
    }
150
 
151
    /**
152
     * Removes <code>column</code> from this column model. Posts <code>columnRemoved</code> event.
153
     * Will do nothing if the column is not in this model.
154
     *
155
     * @param column the column to be added
156
     * @see #addColumn
157
     */
158
    public void removeColumn(TableColumn column) {
159
        int allColumnsIndex = allTableColumns.indexOf(column);
160
        if (allColumnsIndex != -1) {
161
            allTableColumns.remove(allColumnsIndex);
162
        }
163
        super.removeColumn(column);
164
    }
165
 
166
    /**
167
     * Moves the column from <code>oldIndex</code> to <code>newIndex</code>. Posts
168
     * <code>columnMoved</code> event. Will not move any columns if <code>oldIndex</code> equals
169
     * <code>newIndex</code>.
170
     *
171
     * @param oldIndex index of column to be moved
172
     * @param newIndex new index of the column
173
     * @exception IllegalArgumentException if either <code>oldIndex</code> or <code>newIndex</code>
174
     *            are not in [0, getColumnCount() - 1]
175
     */
176
    public void moveColumn(int oldIndex, int newIndex) {
177
        if ((oldIndex < 0) || (oldIndex >= getColumnCount()) || (newIndex < 0) || (newIndex >= getColumnCount()))
178
            throw new IllegalArgumentException("moveColumn() - Index out of range");
179
 
180
        TableColumn fromColumn = tableColumns.get(oldIndex);
181
        TableColumn toColumn = tableColumns.get(newIndex);
182
 
183
        int allColumnsOldIndex = allTableColumns.indexOf(fromColumn);
184
        int allColumnsNewIndex = allTableColumns.indexOf(toColumn);
185
 
186
        if (oldIndex != newIndex) {
187
            allTableColumns.remove(allColumnsOldIndex);
188
            allTableColumns.add(allColumnsNewIndex, fromColumn);
189
        }
190
 
191
        super.moveColumn(oldIndex, newIndex);
192
    }
193
 
194
    /**
195
     * Returns the total number of columns in this model.
196
     *
197
     * @param onlyVisible if set only visible columns will be counted
198
     * @return the number of columns in the <code>tableColumns</code> array
199
     * @see #getColumns
200
     */
201
    public int getColumnCount(boolean onlyVisible) {
202
        return getColumnsFast(onlyVisible).size();
203
    }
204
 
205
    /**
206
     * Returns all the columns in the model.
207
     *
208
     * @param onlyVisible if set all invisible columns will be missing from the result.
209
     * @return the columns in the model
210
     */
211
    public List<TableColumn> getColumns(boolean onlyVisible) {
212
        return Collections.unmodifiableList(getColumnsFast(onlyVisible));
213
    }
214
 
215
    private final List<TableColumn> getColumnsFast(boolean onlyVisible) {
216
        return onlyVisible ? tableColumns : allTableColumns;
217
    }
218
 
219
    /**
220
     * Returns the position of the first column whose identifier equals <code>identifier</code>.
221
     * Position is the the index in all visible columns if <code>onlyVisible</code> is true or else
222
     * the index in all columns.
223
     *
224
     * @param identifier the identifier object to search for
225
     * @param onlyVisible if set searches only visible columns
226
     *
227
     * @return the index of the first column whose identifier equals <code>identifier</code>
228
     *
229
     * @exception IllegalArgumentException if <code>identifier</code> is <code>null</code>, or if no
230
     *            <code>TableColumn</code> has this <code>identifier</code>
231
     * @see #getColumn
232
     */
233
    public int getColumnIndex(Object identifier, boolean onlyVisible) {
234
        if (identifier == null) {
235
            throw new IllegalArgumentException("Identifier is null");
236
        }
237
 
238
        List<TableColumn> columns = getColumnsFast(onlyVisible);
239
        int noColumns = columns.size();
240
        TableColumn column;
241
 
242
        for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
243
            column = columns.get(columnIndex);
244
 
245
            if (identifier.equals(column.getIdentifier()))
246
                return columnIndex;
247
        }
248
 
249
        throw new IllegalArgumentException("Identifier not found");
250
    }
251
 
252
    /**
253
     * Returns the <code>TableColumn</code> object for the column at <code>columnIndex</code>.
254
     *
255
     * @param columnIndex the index of the column desired
256
     * @param onlyVisible if set columnIndex is meant to be relative to all visible columns only
257
     *        else it is the index in all columns
258
     *
259
     * @return the <code>TableColumn</code> object for the column at <code>columnIndex</code>
260
     */
261
    public TableColumn getColumn(int columnIndex, boolean onlyVisible) {
262
        return getColumnsFast(onlyVisible).get(columnIndex);
263
    }
182 ilm 264
 
265
    @Override
266
    public TableColumn getColumn(int columnIndex) {
267
        if (columnIndex < 0) {
268
            // Fix OpenJDK with FlatUI
269
            columnIndex = 0;
270
        }
271
        return super.getColumn(columnIndex);
272
    }
17 ilm 273
}