OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Rev 177 | Go to most recent revision | 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
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.sql.view.list;
15
 
16
import org.openconcerto.sql.element.SQLElement;
93 ilm 17
import org.openconcerto.sql.model.Constraint;
17 ilm 18
import org.openconcerto.sql.model.SQLField;
19
import org.openconcerto.sql.model.SQLRow;
73 ilm 20
import org.openconcerto.sql.model.SQLRowAccessor;
67 ilm 21
import org.openconcerto.sql.model.SQLRowListRSH;
17 ilm 22
import org.openconcerto.sql.model.SQLRowValues;
174 ilm 23
import org.openconcerto.sql.model.SQLRowValuesCluster.StoreMode;
17 ilm 24
import org.openconcerto.sql.model.SQLSelect;
93 ilm 25
import org.openconcerto.sql.model.SQLSyntax.ConstraintType;
67 ilm 26
import org.openconcerto.sql.model.SQLTable;
17 ilm 27
import org.openconcerto.sql.model.UndefinedRowValuesCache;
28
import org.openconcerto.sql.model.Where;
93 ilm 29
import org.openconcerto.sql.utils.AlterTable;
90 ilm 30
import org.openconcerto.utils.CompareUtils;
67 ilm 31
import org.openconcerto.utils.ExceptionHandler;
17 ilm 32
import org.openconcerto.utils.OrderedSet;
33
 
34
import java.math.BigDecimal;
35
import java.sql.SQLException;
36
import java.util.ArrayList;
93 ilm 37
import java.util.Arrays;
17 ilm 38
import java.util.Collection;
39
import java.util.HashMap;
93 ilm 40
import java.util.HashSet;
17 ilm 41
import java.util.List;
42
import java.util.Map;
93 ilm 43
import java.util.Set;
17 ilm 44
 
45
import javax.swing.SwingUtilities;
46
import javax.swing.event.EventListenerList;
47
import javax.swing.event.TableModelEvent;
48
import javax.swing.event.TableModelListener;
49
import javax.swing.table.AbstractTableModel;
50
 
51
public class RowValuesTableModel extends AbstractTableModel {
65 ilm 52
    // modification of rowValues MUST be done in AWT EDT
67 ilm 53
    // methods that perform requests, MUST use the runnableQueue
54
    // synchronized is used to protect list access
65 ilm 55
    private List<SQLRowValues> rowValues = new ArrayList<SQLRowValues>();
17 ilm 56
 
57
    private OrderedSet<TableModelListener> tableModelListeners = new OrderedSet<TableModelListener>();
58
 
59
    protected SQLElement element;
60
 
61
    private int nbColumn;
62
 
63
    private List<SQLTableElement> list; // Liste de SQLTableElement
65 ilm 64
    private Map<String, Integer> mapColumnField = new HashMap<String, Integer>();
17 ilm 65
 
73 ilm 66
    private List<SQLField> requiredFields;
67
 
17 ilm 68
    private SQLField requiredField, validationField;
69
 
65 ilm 70
    private SQLRowValues defautRow;
71
    private List<SQLRowValues> rowValuesDeleted = new ArrayList<SQLRowValues>();
17 ilm 72
 
73
    private boolean editable = true;
74
 
83 ilm 75
    private Where fillWhere;
76
 
17 ilm 77
    public RowValuesTableModel() {
78
 
79
    }
80
 
81
    public RowValuesTableModel(final SQLElement e, final List<SQLTableElement> list, SQLField validField) {
82
        this(e, list, validField, true);
83
    }
84
 
85
    public RowValuesTableModel(final SQLElement e, final List<SQLTableElement> list, SQLField validField, boolean addDefault, SQLRowValues rowParDefaut) {
86
        this(e, list, validField, addDefault, rowParDefaut, null);
87
 
88
    }
89
 
90
    public RowValuesTableModel(final SQLElement e, final List<SQLTableElement> list, SQLField validField, boolean addDefault, SQLRowValues rowParDefaut, SQLField validationField) {
91
        init(e, list, validField, addDefault, rowParDefaut, validationField);
92
 
93
    }
94
 
95
    public RowValuesTableModel(final SQLElement e, final List<SQLTableElement> list, SQLField validField, boolean addDefault) {
96
        this(e, list, validField, addDefault, null);
97
    }
98
 
99
    protected void init(final SQLElement e, final List<SQLTableElement> list, SQLField validField, boolean addDefault, SQLRowValues rowParDefaut) {
100
        init(e, list, validField, addDefault, rowParDefaut, null);
101
    }
102
 
103
    /**
104
     * @param e
105
     * @param list
106
     * @param validField
107
     * @param addDefault
108
     * @param rowParDefaut
109
     */
110
    protected void init(final SQLElement e, final List<SQLTableElement> list, SQLField validField, boolean addDefault, SQLRowValues rowParDefaut, SQLField validationField) {
111
        this.element = e;
112
        this.requiredField = validField;
73 ilm 113
        this.requiredFields = new ArrayList<SQLField>();
114
 
115
        this.requiredFields.add(validField);
17 ilm 116
        this.validationField = validationField;
117
        this.list = list;
65 ilm 118
        this.nbColumn = list.size();
17 ilm 119
 
120
        if (rowParDefaut != null) {
65 ilm 121
            this.defautRow = rowParDefaut;
17 ilm 122
        } else {
65 ilm 123
            this.defautRow = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
17 ilm 124
        }
125
 
126
        // rowParDefaut
127
        if (addDefault) {
80 ilm 128
            final SQLRowValues row = new SQLRowValues(this.defautRow);
129
            final BigDecimal maxOrder = RowValuesTableModel.this.element.getTable().getMaxOrder();
130
            row.put(RowValuesTableModel.this.element.getTable().getOrderField().getName(), maxOrder.add(BigDecimal.ONE));
131
            RowValuesTableModel.this.rowValues.add(row);
65 ilm 132
        }
17 ilm 133
    }
134
 
83 ilm 135
    public void setFillWhere(Where fillWhere) {
136
        this.fillWhere = fillWhere;
137
    }
138
 
73 ilm 139
    public void addRequiredField(SQLField f) {
140
        this.requiredFields.add(f);
141
    }
142
 
28 ilm 143
    public SQLRowValues getDefaultRowValues() {
65 ilm 144
        return this.defautRow;
28 ilm 145
    }
146
 
17 ilm 147
    public synchronized void addColumn(SQLTableElement e) {
148
        this.nbColumn++;
149
        this.list.add(e);
150
    }
151
 
152
    public synchronized int getColumnCount() {
153
        return this.nbColumn;
154
    }
155
 
67 ilm 156
    public int getRowCount() {
157
        checkEDT();
17 ilm 158
        return this.rowValues.size();
159
    }
160
 
161
    public boolean isCellEditable(int rowIndex, int columnIndex) {
162
        if (!this.editable)
163
            return false;
164
 
165
        SQLTableElement elt = this.list.get(columnIndex);
166
        boolean validate = false;
167
        boolean fieldValidate = false;
168
        if (this.validationField != null) {
169
            fieldValidate = elt.getField().getName().equalsIgnoreCase(this.validationField.getName());
170
            validate = this.getRowValuesAt(rowIndex).getBoolean(this.validationField.getName());
171
        }
172
        if (validate && fieldValidate) {
132 ilm 173
            return this.list.get(columnIndex).isCellEditable(this.getRowValuesAt(rowIndex), rowIndex, columnIndex);
17 ilm 174
        } else {
132 ilm 175
            return (!validate) && this.list.get(columnIndex).isCellEditable(this.getRowValuesAt(rowIndex), rowIndex, columnIndex);
17 ilm 176
        }
177
    }
178
 
179
    public synchronized Class<?> getColumnClass(int columnIndex) {
180
        return this.list.get(columnIndex).getElementClass();
181
    }
182
 
183
    public Object getValueAt(int rowIndex, int columnIndex) {
67 ilm 184
        checkEDT();
65 ilm 185
        final Object result;
17 ilm 186
 
187
        if (rowIndex >= this.rowValues.size()) {
65 ilm 188
            System.err.println("RowValuesTableModel: get(" + rowIndex + "," + columnIndex + ") rowIndex>" + this.rowValues.size());
17 ilm 189
            Thread.dumpStack();
65 ilm 190
            result = new Integer(0);
191
        } else {
192
            SQLRowValues val = this.rowValues.get(rowIndex);
193
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
132 ilm 194
            Object storedObject = val.getObject(sqlTableElem.getRowField());
65 ilm 195
            result = sqlTableElem.getValueFrom(val);
132 ilm 196
            if (sqlTableElem.getElementClass() != null && Number.class.isAssignableFrom(sqlTableElem.getElementClass()) && !CompareUtils.equals(result, storedObject)) {
197
                fireTableDataChanged();
198
            }
199
 
17 ilm 200
        }
201
 
202
        return result;
203
    }
204
 
67 ilm 205
    private void checkEDT() {
65 ilm 206
        if (!SwingUtilities.isEventDispatchThread())
207
            Thread.dumpStack();
67 ilm 208
    }
209
 
210
    public void putValue(Object value, int rowIndex, String fieldName) {
93 ilm 211
        putValue(value, rowIndex, fieldName, false);
212
    }
213
 
214
    public void putValue(Object value, int rowIndex, String fieldName, boolean forcedFire) {
67 ilm 215
        checkEDT();
65 ilm 216
        final SQLRowValues rowVal = this.rowValues.get(rowIndex);
73 ilm 217
        Object oldValue = rowVal.getObject(fieldName);
93 ilm 218
        if (!forcedFire) {
219
            if (oldValue == value) {
220
                return;
221
            }
222
            if (oldValue != null && oldValue.equals(value)) {
223
                return;
224
            }
73 ilm 225
        }
17 ilm 226
        rowVal.put(fieldName, value);
227
        for (SQLTableElement sqlTableElem : this.list) {
228
            sqlTableElem.fireModification(rowVal);
229
        }
230
        fireTableModelModified(rowIndex);
231
    }
232
 
233
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
67 ilm 234
        checkEDT();
17 ilm 235
        if (!this.editable)
236
            return;
67 ilm 237
        if (this.list.size() <= columnIndex) {
238
            return;
239
        }
73 ilm 240
 
241
        Object oldValue = getValueAt(rowIndex, columnIndex);
242
        if (aValue == oldValue) {
243
            return;
244
        }
245
        if (oldValue != null && oldValue.equals(aValue)) {
246
            return;
247
        }
83 ilm 248
        try {
249
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
73 ilm 250
 
83 ilm 251
            SQLRowValues rowVal = this.rowValues.get(rowIndex);
252
            Object realVal = sqlTableElem.convertEditorValueToModel(aValue, rowVal);
253
            if (realVal == null || realVal.getClass() == this.getColumnClass(columnIndex)) {
254
                sqlTableElem.setValueFrom(rowVal, realVal);
255
                fireTableChanged(new TableModelEvent(this, rowIndex, rowIndex, columnIndex));
256
            } else {
257
                System.err.println("RowValuesTableModel:setValueAt:" + realVal + "(" + realVal.getClass() + ") at (row:" + rowIndex + "/col:" + columnIndex + ") column class : "
258
                        + this.getColumnClass(columnIndex));
259
                Thread.dumpStack();
260
            }
261
        } catch (Exception e) {
262
            // can append when stop editing occur while removing a line
263
            e.printStackTrace();
17 ilm 264
        }
265
    }
266
 
267
    /**
268
     *
269
     */
67 ilm 270
    public void dumpValues() {
17 ilm 271
        for (int i = 0; i < this.rowValues.size(); i++) {
272
            SQLRowValues val = this.rowValues.get(i);
273
            System.out.println("Item" + i + ":" + val);
274
        }
275
    }
276
 
277
    public String getColumnName(int columnIndex) {
278
        SQLTableElement sqlTableElem = this.list.get(columnIndex);
279
        return sqlTableElem.getColumnName();
280
    }
281
 
282
    /**
283
     * Valider les modifications dans la base
284
     */
285
    public void commitData() {
67 ilm 286
        checkEDT();
287
        final List<SQLRowValues> rowsToCommmit = new ArrayList<SQLRowValues>();
288
        rowsToCommmit.addAll(this.rowValues);
73 ilm 289
        try {
93 ilm 290
            final SQLTable table = getDefaultRowValues().getTable();
291
            // Remove constraint on ORDRE for private
292
            final Constraint constraint = table.getConstraint(ConstraintType.UNIQUE, Arrays.asList(table.getOrderField().getName()));
293
            if (constraint != null) {
294
                final String req = new AlterTable(table).dropConstraint(constraint.getName()).asString();
295
                table.getDBSystemRoot().getDataSource().execute(req);
296
                table.fetchFields();
297
                table.getSchema().updateVersion();
298
            }
299
            BigDecimal o = table.getMaxOrder(Boolean.FALSE);
73 ilm 300
            final int size = rowsToCommmit.size();
301
            for (int i = 0; i < size; i++) {
302
                final SQLRowValues r = rowsToCommmit.get(i);
83 ilm 303
                r.put(r.getTable().getOrderField().getFieldName(), o.add(new BigDecimal(i + 1)));
174 ilm 304
                final SQLRow row = r.getGraph().store(StoreMode.COMMIT).getStoredRow(r);
73 ilm 305
                r.setID(row.getIDNumber());
17 ilm 306
            }
73 ilm 307
        } catch (SQLException e) {
308
            ExceptionHandler.handle("Unable to commit rows", e);
309
        }
17 ilm 310
    }
311
 
312
    public void addTableModelListener(TableModelListener l) {
313
        this.tableModelListeners.add(l);
314
    }
315
 
316
    public void removeTableModelListener(TableModelListener l) {
317
        this.tableModelListeners.remove(l);
318
    }
319
 
320
    public void fireTableModelModified(int line) {
83 ilm 321
        checkEDT();
17 ilm 322
        this.fireTableRowsUpdated(line, line);
323
 
324
    }
325
 
326
    public int getColumnForField(String to) {
327
        if (this.mapColumnField.get(to) == null) {
328
 
329
            for (int columnIndex = 0; columnIndex < this.list.size(); columnIndex++) {
330
                SQLTableElement sqlTableElem = this.list.get(columnIndex);
331
                if (sqlTableElem.getField() != null) {
332
                    if (sqlTableElem.getField().getName().equalsIgnoreCase(to)) {
333
                        this.mapColumnField.put(to, columnIndex);
334
                        return columnIndex;
335
                    }
336
                }
337
            }
338
            this.mapColumnField.put(to, -1);
339
            return -1;
340
        } else {
341
            return this.mapColumnField.get(to);
342
        }
343
    }
344
 
67 ilm 345
    public void addNewRowAt(final int index) {
346
        checkEDT();
347
        if (index > getRowCount()) {
348
            throw new IllegalArgumentException(index + " > row count: " + getRowCount());
349
        } else if (index < 0) {
350
            throw new IllegalArgumentException(index + " <0");
351
        }
17 ilm 352
 
73 ilm 353
        final SQLRowValues newRowParDefaut = new SQLRowValues(RowValuesTableModel.this.defautRow);
132 ilm 354
        if (index > 0 && index < getRowCount() && newRowParDefaut.getTable().contains("NIVEAU")) {
355
            newRowParDefaut.put("NIVEAU", this.rowValues.get(index - 1).getObject("NIVEAU"));
356
        }
83 ilm 357
        RowValuesTableModel.this.rowValues.add(index, newRowParDefaut);
17 ilm 358
 
83 ilm 359
        final int size = RowValuesTableModel.this.tableModelListeners.size();
360
        for (int i = 0; i < size; i++) {
361
            final TableModelListener l = RowValuesTableModel.this.tableModelListeners.get(i);
362
            l.tableChanged(new TableModelEvent(RowValuesTableModel.this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
363
        }
17 ilm 364
 
365
    }
366
 
93 ilm 367
    public void addRowAt(final int index, SQLRowValues rowVals) {
368
        checkEDT();
369
        if (index > getRowCount()) {
370
            throw new IllegalArgumentException(index + " > row count: " + getRowCount());
371
        } else if (index < 0) {
372
            throw new IllegalArgumentException(index + " <0");
373
        }
374
 
375
        RowValuesTableModel.this.rowValues.add(index, rowVals);
376
 
377
        final int size = RowValuesTableModel.this.tableModelListeners.size();
378
        for (int i = 0; i < size; i++) {
379
            final TableModelListener l = RowValuesTableModel.this.tableModelListeners.get(i);
380
            l.tableChanged(new TableModelEvent(RowValuesTableModel.this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
381
        }
382
 
383
    }
384
 
17 ilm 385
    /**
386
     * Suppression d'une ligne de la table
387
     *
388
     * @param index index de la ligne
389
     */
67 ilm 390
    public void removeRowAt(final int index) {
391
        checkEDT();
83 ilm 392
        if (index < 0) {
17 ilm 393
            return;
83 ilm 394
        }
395
        final SQLRowValues removedLine = this.rowValues.remove(index);
396
        this.rowValuesDeleted.add(removedLine);
67 ilm 397
        fireTableRowsDeleted(index, index);
17 ilm 398
    }
399
 
400
    /**
401
     * Suppression de plusieurs ligne de la table
402
     *
403
     * @param index tableau des index de ligne à supprimer
404
     */
67 ilm 405
    public void removeRowsAt(final int[] index) {
406
        checkEDT();
17 ilm 407
        if (index.length <= 0)
408
            return;
409
 
67 ilm 410
        final List<SQLRowValues> rowVals = new ArrayList<SQLRowValues>(index.length);
411
        for (int i : index) {
412
            final SQLRowValues rowValues2 = RowValuesTableModel.this.rowValues.get(i);
413
            rowVals.add(rowValues2);
414
            RowValuesTableModel.this.rowValuesDeleted.add(rowValues2);
415
        }
93 ilm 416
 
417
        // Ne pas utiliser removeAll, supprime toutes les lignes equals
418
        // RowValuesTableModel.this.rowValues.removeAll(rowVals);
419
        for (SQLRowValues sqlRowValues2Remove : rowVals) {
420
            for (int i = 0; i < getRowCount(); i++) {
421
                SQLRowValues rowValsList = RowValuesTableModel.this.rowValues.get(i);
422
                if (rowValsList == sqlRowValues2Remove) {
423
                    RowValuesTableModel.this.rowValues.remove(i);
424
                    break;
425
                }
426
            }
427
        }
67 ilm 428
        fireTableDataChanged();
17 ilm 429
 
430
    }
431
 
67 ilm 432
    public void addNewRow() {
17 ilm 433
        addNewRowAt(getRowCount());
434
    }
435
 
436
    public boolean isLastRowValid() {
437
        return isRowValid(this.rowValues.size() - 1);
438
    }
439
 
440
    public boolean isRowValid(int index) {
67 ilm 441
        checkEDT();
17 ilm 442
        if (this.rowValues.size() == 0)
443
            return true;
444
 
445
        if (index < 0 || index >= this.rowValues.size()) {
446
            return false;
447
        }
448
 
449
        SQLRowValues row = this.rowValues.get(index);
450
 
73 ilm 451
        boolean valid = true;
452
        for (SQLField f : this.requiredFields) {
453
            if (f.isKey()) {
454
                valid &= !row.isForeignEmpty(f.getName());
455
            } else {
456
                final Object object = row.getObject(f.getName());
457
                valid &= object != null && object.toString().trim().length() > 0;
458
            }
459
            if (!valid) {
460
                break;
461
            }
17 ilm 462
        }
73 ilm 463
        return valid;
17 ilm 464
    }
465
 
466
    public boolean isValidated() {
467
        boolean b = true;
468
        for (int i = 0; i < getRowCount(); i++) {
469
            b &= isRowValid(i);
470
        }
471
        return b;
472
    }
473
 
474
    public final List<SQLTableElement> getList() {
475
        return this.list;
476
    }
477
 
478
    public void updateField(String field, SQLRowValues rowVals, String fieldCondition) {
67 ilm 479
        checkEDT();
17 ilm 480
        if (rowVals != null) {
481
            int stop = this.rowValues.size();
65 ilm 482
 
483
            // FIXME check à faire sur l'ensemble des rows avec la methode isValid(). Quand
484
            // RowValuesTable deviendra un RowItemView.
485
            // if (!isLastRowValid()) {
486
            // stop--;
487
            // }
17 ilm 488
            int id = rowVals.getID();
489
 
490
            for (int i = 0; i < stop; i++) {
491
 
492
                SQLRowValues r = this.rowValues.get(i);
493
 
494
                if (fieldCondition != null) {
495
                    Object o = r.getObject(fieldCondition);
496
                    if (o != null && ((Boolean) o)) {
497
                        if (id != SQLRow.NONEXISTANT_ID) {
498
                            r.put(field, id);
499
                        } else {
500
                            r.put(field, rowVals);
501
                        }
502
                    } else {
503
                        r.put(field, 1);
504
                    }
505
                } else {
506
                    if (id != SQLRow.NONEXISTANT_ID) {
507
                        r.put(field, id);
508
                    } else {
509
                        r.put(field, rowVals);
510
                    }
511
                }
512
            }
83 ilm 513
            // FIXME : must be done in commit not here
17 ilm 514
            List<SQLRowValues> l = new ArrayList<SQLRowValues>(this.rowValuesDeleted);
93 ilm 515
            Set<SQLRowValues> toArchive = new HashSet<SQLRowValues>();
17 ilm 516
            for (int i = 0; i < l.size(); i++) {
517
                SQLRowValues rowVals2 = l.get(i);
518
                int idRow = rowVals2.getID();
519
                if (idRow != SQLRow.NONEXISTANT_ID) {
93 ilm 520
                    toArchive.add(rowVals2);
17 ilm 521
                } else {
522
                    rowVals2.putEmptyLink(field);
523
                }
524
            }
65 ilm 525
            this.rowValuesDeleted.clear();
93 ilm 526
            try {
527
                this.element.archive(toArchive);
528
            } catch (SQLException e) {
529
                throw new IllegalStateException("Unable to archive rows : " + toArchive, e);
530
            }
531
 
17 ilm 532
            if (id != SQLRow.NONEXISTANT_ID) {
533
                this.commitData();
534
            }
535
        }
536
    }
537
 
538
    public void updateField(String field, SQLRowValues rowVals) {
539
        updateField(field, rowVals, null);
540
    }
541
 
542
    public void updateField(String field, int id) {
543
        updateField(field, id, null);
544
    }
545
 
546
    public void updateField(String field, int id, String fieldCondition) {
547
        if (id > 0) {
548
            updateField(field, this.element.getTable().getForeignTable(field).getRow(id).createUpdateRow(), fieldCondition);
549
        }
550
    }
551
 
552
    public void insertFrom(String field, int id) {
553
        insertFrom(field, id, SQLRow.NONEXISTANT_ID);
554
    }
555
 
556
    public void insertFrom(final String field, final int id, final int exceptID) {
67 ilm 557
        checkEDT();
17 ilm 558
        if (id > 0) {
559
 
93 ilm 560
            SQLSelect sel = new SQLSelect();
561
            final SQLTable table = RowValuesTableModel.this.element.getTable();
562
            sel.addSelectStar(table);
563
            Where w = new Where(table.getField(field), "=", id);
564
            w = w.and(new Where(table.getKey(), "!=", exceptID));
565
            if (fillWhere != null) {
566
                w = w.and(fillWhere);
567
            }
568
            sel.setWhere(w);
569
            sel.addFieldOrder(table.getOrderField());
17 ilm 570
 
93 ilm 571
            final List<SQLRow> listOfRows = SQLRowListRSH.execute(sel);
17 ilm 572
 
93 ilm 573
            RowValuesTableModel.this.rowValues.clear();
574
            final int size = listOfRows.size();
575
            for (int i = 0; i < size; i++) {
576
                SQLRow sqlRow = listOfRows.get(i);
577
                SQLRowValues row = sqlRow.createUpdateRow();
578
                RowValuesTableModel.this.rowValues.add(row);
579
            }
580
            fireTableModelModified(RowValuesTableModel.this.rowValues.size());
17 ilm 581
 
582
        }
90 ilm 583
 
17 ilm 584
    }
585
 
90 ilm 586
    public void insertFrom(final SQLRowAccessor rowVals) {
587
        insertFrom(rowVals, null);
588
    }
589
 
590
    public void insertFrom(final SQLRowAccessor rowVals, final SQLField referentField) {
591
        insertFrom(rowVals, referentField, null, null);
592
    }
593
 
83 ilm 594
    // Remplit la table à partir de la SQLRow parente
90 ilm 595
    public void insertFrom(final SQLRowAccessor rowVals, final SQLField referentField, final SQLField fieldWhere, final Object value) {
17 ilm 596
        if (!SwingUtilities.isEventDispatchThread()) {
597
            Thread.dumpStack();
598
        }
599
        if (rowVals != null) {
600
 
93 ilm 601
            final List<SQLRowValues> newRows = new ArrayList<SQLRowValues>();
17 ilm 602
 
93 ilm 603
            if (rowVals.getID() > 1) {
604
                SQLRow row = rowVals.getTable().getRow(rowVals.getID());
605
                List<SQLRow> rowSet;
606
                if (referentField == null) {
607
                    rowSet = row.getReferentRows(RowValuesTableModel.this.element.getTable());
608
                } else {
609
                    rowSet = row.getReferentRows(referentField);
610
                }
611
                for (SQLRow row2 : rowSet) {
612
                    if (fieldWhere == null || CompareUtils.equals(row2.getObject(fieldWhere.getName()), value)) {
613
                        SQLRowValues rowVals2 = new SQLRowValues(RowValuesTableModel.this.element.getTable());
614
                        rowVals2.loadAbsolutelyAll(row2);
615
                        newRows.add(rowVals2);
616
                    }
617
                }
17 ilm 618
 
93 ilm 619
            } else {
620
                final Collection<? extends SQLRowAccessor> colRows;
621
                if (referentField == null) {
622
                    colRows = rowVals.getReferentRows(RowValuesTableModel.this.element.getTable());
623
                } else {
624
                    colRows = rowVals.getReferentRows(referentField);
625
                }
626
                for (SQLRowAccessor rowValues : colRows) {
627
                    if (fieldWhere == null || CompareUtils.equals(rowValues.getObject(fieldWhere.getName()), value)) {
628
                        newRows.add(rowValues.asRowValues());
17 ilm 629
                    }
630
                }
93 ilm 631
            }
632
 
633
            RowValuesTableModel.this.rowValues.clear();
634
            RowValuesTableModel.this.rowValues.addAll(newRows);
635
            fireTableModelModified(RowValuesTableModel.this.rowValues.size());
636
 
17 ilm 637
        }
638
    }
639
 
640
    public void addRow(SQLRowValues row) {
93 ilm 641
        checkEDT();
17 ilm 642
        addRow(row, true);
643
    }
644
 
83 ilm 645
    public void addRow(final SQLRowValues row, final boolean fireModified) {
93 ilm 646
        checkEDT();
83 ilm 647
        final List<SQLRowValues> rows = new ArrayList<SQLRowValues>(1);
648
        rows.add(row);
649
        addRows(rows, true);
650
    }
651
 
80 ilm 652
    public void submit(Runnable r) {
93 ilm 653
        checkEDT();
654
        r.run();
80 ilm 655
    }
656
 
83 ilm 657
    public void addRows(final List<SQLRowValues> rows, final boolean fireModified) {
67 ilm 658
        checkEDT();
83 ilm 659
        if (rows.isEmpty()) {
660
            return;
661
        }
17 ilm 662
 
93 ilm 663
        addRowsSync(rows, fireModified);
17 ilm 664
 
665
    }
666
 
667
    public void clearRows() {
83 ilm 668
        checkEDT();
67 ilm 669
        final int size = RowValuesTableModel.this.rowValues.size();
670
        if (size > 0) {
671
            RowValuesTableModel.this.rowValues.clear();
672
            fireTableRowsDeleted(0, size - 1);
673
        }
65 ilm 674
 
17 ilm 675
    }
676
 
67 ilm 677
    public synchronized SQLTableElement getSQLTableElementAt(int columnIndex) {
93 ilm 678
        checkEDT();
17 ilm 679
        if (columnIndex >= 0 && columnIndex < this.list.size()) {
680
            return this.list.get(columnIndex);
681
        } else {
682
            return null;
683
        }
684
    }
685
 
67 ilm 686
    public synchronized int getColumnIndexForElement(SQLTableElement e) {
93 ilm 687
        checkEDT();
17 ilm 688
        for (int columnIndex = 0; columnIndex < this.list.size(); columnIndex++) {
689
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
690
            if (sqlTableElem.equals(e)) {
691
                return columnIndex;
692
            }
693
        }
694
        return -1;
695
    }
696
 
697
    public SQLRowValues getRowValuesAt(int rowIndex) {
67 ilm 698
        checkEDT();
17 ilm 699
        return this.rowValues.get(rowIndex);
700
    }
701
 
702
    public final int id2index(int id) {
83 ilm 703
        checkEDT();
17 ilm 704
        for (int i = 0; i < this.getRowCount(); i++) {
705
            if (this.getRowValuesAt(i).getID() == id)
706
                return i;
707
        }
708
        return -1;
709
    }
710
 
93 ilm 711
    public final int row2index(SQLRowAccessor row) {
83 ilm 712
        checkEDT();
93 ilm 713
        return this.rowValues.indexOf(row);
83 ilm 714
    }
715
 
17 ilm 716
    public void fireTableChanged(TableModelEvent event) {
67 ilm 717
        checkEDT();
17 ilm 718
        for (int i = 0; i < this.tableModelListeners.size(); i++) {
719
            TableModelListener l = this.tableModelListeners.get(i);
720
            l.tableChanged(event);
721
        }
722
    }
723
 
724
    /**
725
     * Notifies all listeners that all cell values in the table's rows may have changed. The number
726
     * of rows may also have changed and the <code>JTable</code> should redraw the table from
727
     * scratch. The structure of the table (as in the order of the columns) is assumed to be the
728
     * same.
729
     *
730
     * @see TableModelEvent
731
     * @see EventListenerList
732
     * @see javax.swing.JTable#tableChanged(TableModelEvent)
733
     */
734
    public void fireTableDataChanged() {
735
        fireTableChanged(new TableModelEvent(this));
736
    }
737
 
738
    /**
739
     * Notifies all listeners that the table's structure has changed. The number of columns in the
740
     * table, and the names and types of the new columns may be different from the previous state.
741
     * If the <code>JTable</code> receives this event and its
742
     * <code>autoCreateColumnsFromModel</code> flag is set it discards any table columns that it had
743
     * and reallocates default columns in the order they appear in the model. This is the same as
744
     * calling <code>setModel(TableModel)</code> on the <code>JTable</code>.
745
     *
746
     * @see TableModelEvent
747
     * @see EventListenerList
748
     */
749
    public void fireTableStructureChanged() {
750
        fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
751
    }
752
 
753
    /**
754
     * Notifies all listeners that rows in the range <code>[firstRow, lastRow]</code>, inclusive,
755
     * have been inserted.
756
     *
757
     * @param firstRow the first row
758
     * @param lastRow the last row
759
     *
760
     * @see TableModelEvent
761
     * @see EventListenerList
762
     *
763
     */
764
    public void fireTableRowsInserted(int firstRow, int lastRow) {
765
        fireTableChanged(new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
766
    }
767
 
768
    /**
769
     * Notifies all listeners that rows in the range <code>[firstRow, lastRow]</code>, inclusive,
770
     * have been updated.
771
     *
772
     * @param firstRow the first row
773
     * @param lastRow the last row
774
     *
775
     * @see TableModelEvent
776
     * @see EventListenerList
777
     */
778
    public void fireTableRowsUpdated(int firstRow, int lastRow) {
779
        fireTableChanged(new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
780
    }
781
 
782
    /**
783
     * Notifies all listeners that rows in the range <code>[firstRow, lastRow]</code>, inclusive,
784
     * have been deleted.
785
     *
786
     * @param firstRow the first row
787
     * @param lastRow the last row
788
     *
789
     * @see TableModelEvent
790
     * @see EventListenerList
791
     */
792
    public void fireTableRowsDeleted(int firstRow, int lastRow) {
793
        fireTableChanged(new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
794
    }
795
 
796
    /**
797
     * Notifies all listeners that the value of the cell at <code>[row, column]</code> has been
798
     * updated.
799
     *
800
     * @param row row of cell which has been updated
801
     * @param column column of cell which has been updated
802
     * @see TableModelEvent
803
     * @see EventListenerList
804
     */
805
    public void fireTableCellUpdated(int row, int column) {
806
        fireTableChanged(new TableModelEvent(this, row, row, column));
807
    }
808
 
809
    /**
810
     * Déplacer une ligne
811
     *
812
     * @param rowIndex ligne à déplacer
813
     * @param inc incrémentation +1 ou -1
814
     * @return le nouvel index
815
     */
816
    public int moveBy(int rowIndex, int inc) {
67 ilm 817
        checkEDT();
17 ilm 818
        int destIndex = rowIndex + inc;
819
 
820
        // On vérifie que l'on reste dans le tableau
821
        if (rowIndex >= 0 && destIndex >= 0) {
822
            if (rowIndex < this.rowValues.size() && destIndex < this.rowValues.size()) {
823
                SQLRowValues rowValues1 = this.rowValues.get(rowIndex);
824
                SQLRowValues rowValues2 = this.rowValues.get(destIndex);
825
                this.rowValues.set(rowIndex, rowValues2);
826
                this.rowValues.set(destIndex, rowValues1);
827
                this.fireTableRowsUpdated(rowIndex, destIndex);
828
                this.fireTableDataChanged();
829
            }
830
        }
831
        return destIndex;
832
    }
833
 
67 ilm 834
    public List<SQLRowValues> getCopyOfValues() {
835
        checkEDT();
836
        List<SQLRowValues> vals = new ArrayList<SQLRowValues>(this.rowValues.size());
837
        for (SQLRowValues sqlRowValues : this.rowValues) {
838
            vals.add(sqlRowValues.asRowValues());
839
        }
840
        return vals;
841
    }
842
 
17 ilm 843
    /**
844
     * Rendre une colonne éditable ou nom
845
     *
846
     * @param b
847
     * @param column
848
     */
849
    public void setEditable(boolean b, int column) {
93 ilm 850
        checkEDT();
17 ilm 851
        this.list.get(column).setEditable(b);
852
    }
853
 
854
    public void setEditable(boolean b) {
855
        this.editable = b;
856
 
857
    }
858
 
859
    public SQLElement getSQLElement() {
860
        return this.element;
861
    }
862
 
863
    public SQLField getRequiredField() {
864
        return this.requiredField;
865
    }
73 ilm 866
 
867
    public List<SQLField> getRequiredsField() {
868
        return this.requiredFields;
869
    }
83 ilm 870
 
871
    public void addRowsSync(final List<SQLRowValues> rows, final boolean fireModified) {
872
        checkEDT();
873
        if (rows.isEmpty()) {
874
            return;
875
        }
876
        RowValuesTableModel.this.rowValues.addAll(rows);
877
        if (fireModified) {
878
            for (SQLRowValues row : rows) {
879
                for (SQLTableElement sqlTableElem : RowValuesTableModel.this.list) {
880
                    sqlTableElem.fireModification(row);
881
                }
882
            }
883
            fireTableRowsInserted(RowValuesTableModel.this.rowValues.size() - rows.size(), RowValuesTableModel.this.rowValues.size() - 1);
884
        }
885
    }
17 ilm 886
}