OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 182
Line 1... Line 1...
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.
Line 34... Line 34...
34
import org.openconcerto.utils.checks.EmptyObj;
34
import org.openconcerto.utils.checks.EmptyObj;
35
import org.openconcerto.utils.checks.ValidListener;
35
import org.openconcerto.utils.checks.ValidListener;
36
import org.openconcerto.utils.checks.ValidState;
36
import org.openconcerto.utils.checks.ValidState;
37
 
37
 
38
import java.awt.event.ActionEvent;
38
import java.awt.event.ActionEvent;
-
 
39
import java.awt.event.HierarchyEvent;
-
 
40
import java.awt.event.HierarchyListener;
39
import java.awt.event.MouseEvent;
41
import java.awt.event.MouseEvent;
40
import java.awt.event.MouseListener;
42
import java.awt.event.MouseListener;
41
import java.beans.PropertyChangeListener;
43
import java.beans.PropertyChangeListener;
42
import java.beans.PropertyChangeSupport;
44
import java.beans.PropertyChangeSupport;
43
import java.sql.SQLException;
45
import java.sql.SQLException;
Line 67... Line 69...
67
 
69
 
68
    private ITreeSelectionNode rootNode;
70
    private ITreeSelectionNode rootNode;
69
    private DefaultTreeModel model;
71
    private DefaultTreeModel model;
70
 
72
 
71
    // Map <Id, Node>
73
    // Map <Id, Node>
72
    private Map<Integer, ITreeSelectionNode> mapNode = new HashMap<Integer, ITreeSelectionNode>();
74
    private final Map<Integer, ITreeSelectionNode> mapNode = new HashMap<Integer, ITreeSelectionNode>();
73
 
75
 
74
    protected static final int EMPTY_ID = SQLRow.MIN_VALID_ID - 1;
76
    protected static final int EMPTY_ID = SQLRow.MIN_VALID_ID - 1;
75
    private final EmptyChangeSupport helper;
77
    private final EmptyChangeSupport helper;
76
    private final PropertyChangeSupport supp;
78
    private final PropertyChangeSupport supp;
-
 
79
    private final SQLTableListener listener;
77
 
80
 
78
    public ITreeSelection() {
81
    public ITreeSelection() {
79
        this(null);
82
        this(null);
80
    }
83
    }
81
 
84
 
Line 101... Line 104...
101
                ITreeSelection.this.supp.firePropertyChange("value", null, getValue());
104
                ITreeSelection.this.supp.firePropertyChange("value", null, getValue());
102
                ITreeSelection.this.helper.fireEmptyChange(isEmpty());
105
                ITreeSelection.this.helper.fireEmptyChange(isEmpty());
103
            }
106
            }
104
        });
107
        });
105
 
108
 
-
 
109
        this.listener = new SQLTableListener() {
-
 
110
            public void rowModified(SQLTable table, int id) {
-
 
111
                final ITreeSelectionNode node = ITreeSelection.this.mapNode.get(Integer.valueOf(id));
-
 
112
                if (node != null) {
-
 
113
                    modifyNode(table.getRow(id), node);
-
 
114
                }
-
 
115
            }
-
 
116
 
-
 
117
            public void rowAdded(SQLTable table, int id) {
-
 
118
                final SQLRow row = table.getRow(id);
-
 
119
                int idPere = row.getInt("ID_" + element.getTable().getName() + "_PERE");
-
 
120
                addNewNode(null, row, idPere, new HashSet<>());
-
 
121
            }
-
 
122
 
-
 
123
            public void rowDeleted(SQLTable table, int id) {
-
 
124
                final ITreeSelectionNode node = ITreeSelection.this.mapNode.get(Integer.valueOf(id));
-
 
125
                for (int i = 0; i < node.getChildCount(); i++) {
-
 
126
                    removeNode(table.getRow(id), node);
-
 
127
                }
-
 
128
            }
-
 
129
        };
-
 
130
 
106
    }
131
    }
107
 
132
 
108
    private void initTree() {
133
    private void initTree() {
109
        if (this.element == null) {
134
        if (this.element == null) {
110
            this.rootNode = new ITreeSelectionNode(null);
135
            this.rootNode = new ITreeSelectionNode(null);
111
        } else {
136
        } else {
112
            SQLRowValues row = UndefinedRowValuesCache.getInstance().getDefaultRowValues(element.getTable());
137
            SQLRowValues row = UndefinedRowValuesCache.getInstance().getDefaultRowValues(this.element.getTable());
113
            this.rootNode = new ITreeSelectionNode(row);
138
            this.rootNode = new ITreeSelectionNode(row);
114
        }
139
        }
115
        this.model = new DefaultTreeModel(this.rootNode);
140
        this.model = new DefaultTreeModel(this.rootNode);
116
        this.setModel(this.model);
141
        this.setModel(this.model);
117
        loadTree();
142
        loadTree();
118
        setTableListener();
-
 
119
        this.addMouseListener(this);
143
        this.addMouseListener(this);
-
 
144
        this.element.getTable().addTableListener(this.listener);
-
 
145
        this.addHierarchyListener(new HierarchyListener() {
-
 
146
 
-
 
147
            @Override
-
 
148
            public void hierarchyChanged(HierarchyEvent e) {
-
 
149
                // check for Hierarchy event
-
 
150
                if (e.getChangeFlags() == HierarchyEvent.DISPLAYABILITY_CHANGED) {
-
 
151
                    // do the required action upon close
-
 
152
                    if (!isDisplayable()) {
-
 
153
                        ITreeSelection.this.element.getTable().removeTableListener(ITreeSelection.this.listener);
-
 
154
                    }
-
 
155
 
-
 
156
                }
-
 
157
 
-
 
158
            }
-
 
159
        });
120
    }
160
    }
121
 
161
 
122
    public void mouseClicked(MouseEvent e) {
162
    public void mouseClicked(MouseEvent e) {
123
    }
163
    }
124
 
164
 
Line 181... Line 221...
181
 
221
 
182
    /**
222
    /**
183
     * Ajouter une feuille
223
     * Ajouter une feuille
184
     */
224
     */
185
    public void addElement(int idRoot) {
225
    public void addElement(int idRoot) {
186
        EditFrame frameAdd = new EditFrame(element, EditFrame.CREATION);
226
        EditFrame frameAdd = new EditFrame(this.element, EditFrame.CREATION);
187
        SQLRowValues rowVals = new SQLRowValues(element.getTable());
227
        SQLRowValues rowVals = new SQLRowValues(this.element.getTable());
188
        if (idRoot > 1) {
228
        if (idRoot > 1) {
189
            rowVals.put("ID_" + element.getTable().getName() + "_PERE", idRoot);
229
            rowVals.put("ID_" + this.element.getTable().getName() + "_PERE", idRoot);
190
            frameAdd.getSQLComponent().select(rowVals);
230
            frameAdd.getSQLComponent().select(rowVals);
191
        }
231
        }
192
        FrameUtil.showPacked(frameAdd);
232
        FrameUtil.showPacked(frameAdd);
193
 
233
 
194
    }
234
    }
Line 197... Line 237...
197
     * Supprimer la feuille
237
     * Supprimer la feuille
198
     */
238
     */
199
    public void removeElement(int id) {
239
    public void removeElement(int id) {
200
        if (id > 1) {
240
        if (id > 1) {
201
            try {
241
            try {
202
                element.archive(id);
242
                this.element.archive(id);
203
            } catch (SQLException e1) {
243
            } catch (SQLException e1) {
204
                e1.printStackTrace();
244
                e1.printStackTrace();
205
            }
245
            }
206
        }
246
        }
207
    }
247
    }
Line 209... Line 249...
209
    /**
249
    /**
210
     * Modifier la feuille
250
     * Modifier la feuille
211
     */
251
     */
212
    public void modifyElement(int id) {
252
    public void modifyElement(int id) {
213
        if (id > 1) {
253
        if (id > 1) {
214
            EditFrame frameModFamille = new EditFrame(element, EditFrame.MODIFICATION);
254
            EditFrame frameModFamille = new EditFrame(this.element, EditFrame.MODIFICATION);
215
            frameModFamille.selectionId(id, 1);
255
            frameModFamille.selectionId(id, 1);
216
            FrameUtil.showPacked(frameModFamille);
256
            FrameUtil.showPacked(frameModFamille);
217
        }
257
        }
218
    }
258
    }
219
 
259
 
Line 300... Line 340...
300
 
340
 
301
    private void loadTree() {
341
    private void loadTree() {
302
        new SwingWorker<List<SQLRow>, Object>() {
342
        new SwingWorker<List<SQLRow>, Object>() {
303
            @Override
343
            @Override
304
            protected List<SQLRow> doInBackground() throws Exception {
344
            protected List<SQLRow> doInBackground() throws Exception {
305
                final SQLTable table = element.getTable();
345
                final SQLTable table = ITreeSelection.this.element.getTable();
306
                final SQLSelect sel = new SQLSelect();
346
                final SQLSelect sel = new SQLSelect();
307
                sel.addSelectStar(table);
347
                sel.addSelectStar(table);
308
                return SQLRowListRSH.execute(sel);
348
                return SQLRowListRSH.execute(sel);
309
            }
349
            }
310
 
350
 
Line 319... Line 359...
319
                            SQLRow row = l.get(i);
359
                            SQLRow row = l.get(i);
320
                            familles.put(row.getID(), row);
360
                            familles.put(row.getID(), row);
321
                        }
361
                        }
322
                        for (int i = 0; i < l.size(); i++) {
362
                        for (int i = 0; i < l.size(); i++) {
323
                            SQLRow row = l.get(i);
363
                            SQLRow row = l.get(i);
324
                            addNewNode(familles, row, row.getInt("ID_" + element.getTable().getName() + "_PERE"), new HashSet<>());
364
                            addNewNode(familles, row, row.getInt("ID_" + ITreeSelection.this.element.getTable().getName() + "_PERE"), new HashSet<>());
325
                        }
365
                        }
326
                        expandRow(0);
366
                        expandRow(0);
327
                    }
367
                    }
328
                } catch (InterruptedException e) {
368
                } catch (InterruptedException e) {
329
                    ExceptionHandler.handle("", e);
369
                    ExceptionHandler.handle("", e);
Line 346... Line 386...
346
            if (idPere > 1 && nodePere == null && familles != null) {
386
            if (idPere > 1 && nodePere == null && familles != null) {
347
                final SQLRow rowPere = familles.get(idPere);
387
                final SQLRow rowPere = familles.get(idPere);
348
                if (idPere != row.getID()) {
388
                if (idPere != row.getID()) {
349
                    if (!addedIDs.contains(idPere)) {
389
                    if (!addedIDs.contains(idPere)) {
350
                        addedIDs.add(idPere);
390
                        addedIDs.add(idPere);
351
                        addNewNode(familles, rowPere, rowPere.getInt("ID_" + element.getTable().getName() + "_PERE"), addedIDs);
391
                        addNewNode(familles, rowPere, rowPere.getInt("ID_" + this.element.getTable().getName() + "_PERE"), addedIDs);
352
                    }
392
                    }
353
                }
393
                }
354
                nodePere = this.mapNode.get(Integer.valueOf(idPere));
394
                nodePere = this.mapNode.get(Integer.valueOf(idPere));
355
            }
395
            }
356
            final ITreeSelectionNode newNode = new ITreeSelectionNode(row);
396
            final ITreeSelectionNode newNode = new ITreeSelectionNode(row);
Line 358... Line 398...
358
            if (!row.isUndefined()) {
398
            if (!row.isUndefined()) {
359
                if (nodePere != null && idPere > 1) {
399
                if (nodePere != null && idPere > 1) {
360
                    addNode(newNode, nodePere);
400
                    addNode(newNode, nodePere);
361
                } else {
401
                } else {
362
                    if (idPere == 1) {
402
                    if (idPere == 1) {
363
                        addNode(newNode, rootNode);
403
                        addNode(newNode, this.rootNode);
364
                    }
404
                    }
365
                }
405
                }
366
            }
406
            }
367
        }
407
        }
368
    }
408
    }
Line 379... Line 419...
379
        for (; n < childCount; n++) {
419
        for (; n < childCount; n++) {
380
            if (nodeToAdd.toString().compareToIgnoreCase(nodeParent.getChildAt(n).toString()) < 0) {
420
            if (nodeToAdd.toString().compareToIgnoreCase(nodeParent.getChildAt(n).toString()) < 0) {
381
                break;
421
                break;
382
            }
422
            }
383
        }
423
        }
384
        model.insertNodeInto(nodeToAdd, nodeParent, n);
424
        this.model.insertNodeInto(nodeToAdd, nodeParent, n);
385
    }
425
    }
386
 
426
 
387
    /**
427
    /**
388
     * 
428
     * 
389
     * @param row
429
     * @param row
390
     * @param nodeParent
430
     * @param nodeParent
391
     */
431
     */
392
    private void modifyNode(SQLRow row, ITreeSelectionNode node) {
432
    private void modifyNode(SQLRow row, ITreeSelectionNode node) {
393
        if (row.isArchived() && node.getParent() != null) {
433
        if (row.isArchived() && node.getParent() != null) {
394
            model.removeNodeFromParent(node);
434
            this.model.removeNodeFromParent(node);
395
        } else {
435
        } else {
396
            node.setRow(row);
436
            node.setRow(row);
397
            model.nodeChanged(node);
437
            this.model.nodeChanged(node);
398
        }
438
        }
399
    }
439
    }
400
 
440
 
401
    /**
441
    /**
402
     * Suppression d'un noeud
442
     * Suppression d'un noeud
Line 407... Line 447...
407
    private void removeNode(SQLRow row, ITreeSelectionNode nodeParent) {
447
    private void removeNode(SQLRow row, ITreeSelectionNode nodeParent) {
408
        final int childCount = nodeParent.getChildCount();
448
        final int childCount = nodeParent.getChildCount();
409
        for (int i = 0; i < childCount; i++) {
449
        for (int i = 0; i < childCount; i++) {
410
            final ITreeSelectionNode v = (ITreeSelectionNode) nodeParent.getChildAt(i);
450
            final ITreeSelectionNode v = (ITreeSelectionNode) nodeParent.getChildAt(i);
411
            if (v.getId() == row.getID()) {
451
            if (v.getId() == row.getID()) {
412
                model.removeNodeFromParent(v);
452
                this.model.removeNodeFromParent(v);
413
            }
453
            }
414
        }
454
        }
415
    }
455
    }
416
 
456
 
417
    /**
457
    /**
418
     * Table listener permettant de rafraichir l'arbre
458
     * Table listener permettant de rafraichir l'arbre
419
     * 
459
     * 
420
     */
460
     */
421
    private void setTableListener() {
461
    private void setTableListener() {
422
        final SQLTableListener listener = new SQLTableListener() {
-
 
423
            public void rowModified(SQLTable table, int id) {
-
 
424
                final ITreeSelectionNode node = mapNode.get(Integer.valueOf(id));
-
 
425
                if (node != null) {
-
 
426
                    modifyNode(table.getRow(id), node);
-
 
427
                }
-
 
428
            }
-
 
429
 
-
 
430
            public void rowAdded(SQLTable table, int id) {
-
 
431
                final SQLRow row = table.getRow(id);
-
 
432
                int idPere = row.getInt("ID_" + element.getTable().getName() + "_PERE");
-
 
433
                addNewNode(null, row, idPere, new HashSet<>());
-
 
434
            }
-
 
435
 
462
 
436
            public void rowDeleted(SQLTable table, int id) {
-
 
437
                final ITreeSelectionNode node = mapNode.get(Integer.valueOf(id));
-
 
438
                for (int i = 0; i < node.getChildCount(); i++) {
-
 
439
                    removeNode(table.getRow(id), node);
-
 
440
                }
-
 
441
            }
-
 
442
        };
-
 
443
        this.element.getTable().addTableListener(listener);
463
        this.element.getTable().addTableListener(this.listener);
444
    }
464
    }
445
}
465
}