OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 63 | 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;
17
import org.openconcerto.sql.model.SQLRowValues;
25 ilm 18
import org.openconcerto.sql.model.SQLTableEvent;
19
import org.openconcerto.sql.model.SQLTableEvent.Mode;
20
import org.openconcerto.sql.model.SQLTableModifiedListener;
17 ilm 21
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
22
 
23
import java.util.HashMap;
24
import java.util.List;
25
import java.util.Map;
26
 
27
import javax.swing.table.DefaultTableCellRenderer;
28
 
29
public class KeyTableCellRenderer extends DefaultTableCellRenderer {
30
 
31
    private String lastStringValue;
32
    private Object toSelect;
33
    private boolean isLoading = false;
34
    private final SQLElement el;
25 ilm 35
    static private final Map<SQLElement, Map<Integer, IComboSelectionItem>> cacheMap = new HashMap<SQLElement, Map<Integer, IComboSelectionItem>>();
17 ilm 36
 
37
    public KeyTableCellRenderer(final SQLElement el) {
38
        super();
39
        this.el = el;
40
 
41
        if (cacheMap.get(this.el) == null) {
42
            loadCacheAsynchronous();
43
        }
44
    }
45
 
46
    public void setValue(Object value) {
47
 
48
        if (this.isLoading) {
49
            this.toSelect = value;
50
            setText("Chargement ...");
51
            return;
52
        }
53
 
54
        String newValue = "id non trouvé pour:" + value;
55
        if (value == null)
56
            return;
57
        try {
58
 
59
            if (value instanceof SQLRowValues) {
60
                newValue = ((SQLRowValues) value).getString("CODE");
61
            } else {
62
 
63
                int id = Integer.parseInt(value.toString());
64
 
65
                if (id > 1) {
66
                    IComboSelectionItem item = cacheMap.get(this.el).get(id);
67
                    if (item != null) {
68
                        newValue = item.getLabel();
69
                    }
70
                } else {
71
                    newValue = SQLTableElement.UNDEFINED_STRING;
72
                }
73
            }
74
        } catch (NumberFormatException e) {
75
            e.printStackTrace();
76
 
77
        }
78
 
79
        if (!newValue.equals(this.lastStringValue)) {
80
 
81
            this.lastStringValue = newValue;
82
            setText(newValue);
83
 
84
        }
85
    }
86
 
87
    private void loadCacheAsynchronous() {
88
        this.isLoading = true;
89
        final Thread thread = new Thread(new Runnable() {
90
            public void run() {
91
                List<IComboSelectionItem> items = KeyTableCellRenderer.this.el.getComboRequest().getComboItems();
92
                final Map<Integer, IComboSelectionItem> m = new HashMap<Integer, IComboSelectionItem>();
93
                for (IComboSelectionItem comboSelectionItem : items) {
94
                    m.put(comboSelectionItem.getId(), comboSelectionItem);
95
                }
96
                cacheMap.put(KeyTableCellRenderer.this.el, m);
25 ilm 97
                KeyTableCellRenderer.this.el.getTable().addPremierTableModifiedListener(new SQLTableModifiedListener() {
17 ilm 98
                    @Override
25 ilm 99
                    public void tableModified(SQLTableEvent evt) {
100
                        final int id = evt.getId();
101
                        if (evt.getMode() == Mode.ROW_DELETED)
102
                            m.remove(id);
103
                        else
104
                            m.put(id, KeyTableCellRenderer.this.el.getComboRequest().getComboItem(id));
17 ilm 105
                    }
106
                });
107
 
108
                KeyTableCellRenderer.this.isLoading = false;
109
                if (KeyTableCellRenderer.this.toSelect != null) {
110
                    setValue(KeyTableCellRenderer.this.toSelect);
111
                }
112
            }
113
        });
114
        thread.start();
115
    }
116
}