OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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