OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | 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
 
63 ilm 16
import org.openconcerto.sql.Configuration;
17 ilm 17
import org.openconcerto.sql.element.SQLElement;
18
import org.openconcerto.sql.model.SQLRowValues;
73 ilm 19
import org.openconcerto.sql.model.SQLTable.ListenerAndConfig;
25 ilm 20
import org.openconcerto.sql.model.SQLTableEvent;
21
import org.openconcerto.sql.model.SQLTableEvent.Mode;
22
import org.openconcerto.sql.model.SQLTableModifiedListener;
174 ilm 23
import org.openconcerto.sql.request.ComboSQLRequest;
17 ilm 24
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
25
 
83 ilm 26
import java.awt.Component;
17 ilm 27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
30
 
83 ilm 31
import javax.swing.JTable;
32
import javax.swing.SwingUtilities;
17 ilm 33
import javax.swing.table.DefaultTableCellRenderer;
34
 
35
public class KeyTableCellRenderer extends DefaultTableCellRenderer {
36
 
37
    private String lastStringValue;
38
    private Object toSelect;
39
    private boolean isLoading = false;
40
    private final SQLElement el;
174 ilm 41
    private final List<String> fields;
83 ilm 42
    private JTable t;
25 ilm 43
    static private final Map<SQLElement, Map<Integer, IComboSelectionItem>> cacheMap = new HashMap<SQLElement, Map<Integer, IComboSelectionItem>>();
17 ilm 44
 
45
    public KeyTableCellRenderer(final SQLElement el) {
174 ilm 46
        this(el, null);
47
    }
48
 
49
    public KeyTableCellRenderer(final SQLElement el, final List<String> fields) {
17 ilm 50
        super();
51
        this.el = el;
174 ilm 52
        this.fields = fields;
17 ilm 53
        if (cacheMap.get(this.el) == null) {
54
            loadCacheAsynchronous();
55
        }
56
    }
57
 
83 ilm 58
    @Override
59
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
60
        this.t = table;
61
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
62
    }
63
 
17 ilm 64
    public void setValue(Object value) {
65
 
66
        if (this.isLoading) {
67
            this.toSelect = value;
68
            setText("Chargement ...");
69
            return;
70
        }
71
 
72
        String newValue = "id non trouvé pour:" + value;
67 ilm 73
        if (value == null) {
74
            setText("");
17 ilm 75
            return;
67 ilm 76
        }
17 ilm 77
        try {
78
 
79
            if (value instanceof SQLRowValues) {
80
                newValue = ((SQLRowValues) value).getString("CODE");
81
            } else {
82
 
94 ilm 83
                final int id = Integer.parseInt(value.toString());
84
                Number undefID = this.el.getTable().getUndefinedIDNumber();
85
                if (undefID == null || id > undefID.intValue()) {
17 ilm 86
                    IComboSelectionItem item = cacheMap.get(this.el).get(id);
87
                    if (item != null) {
88
                        newValue = item.getLabel();
89
                    }
94 ilm 90
 
91
                    // else {
92
 
93
                    // TODO créer une liste des ids à reloader
94
                    // this.toSelect = value;
95
                    // setText("Chargement ...");
96
                    // loadCacheAsynchronous();
97
                    // }
17 ilm 98
                } else {
99
                    newValue = SQLTableElement.UNDEFINED_STRING;
100
                }
101
            }
102
        } catch (NumberFormatException e) {
103
            e.printStackTrace();
104
 
105
        }
106
 
83 ilm 107
        this.lastStringValue = newValue;
108
        setText(newValue);
17 ilm 109
    }
110
 
111
    private void loadCacheAsynchronous() {
112
        this.isLoading = true;
63 ilm 113
        Configuration.getInstance().getNonInteractiveSQLExecutor().execute(new Runnable() {
17 ilm 114
            public void run() {
83 ilm 115
 
174 ilm 116
                final ComboSQLRequest comboRequest = KeyTableCellRenderer.this.fields == null || KeyTableCellRenderer.this.fields.isEmpty() ? KeyTableCellRenderer.this.el.getComboRequest()
117
                        : KeyTableCellRenderer.this.el.createComboRequest(KeyTableCellRenderer.this.fields, null);
118
                List<IComboSelectionItem> items = comboRequest.getComboItems();
17 ilm 119
                final Map<Integer, IComboSelectionItem> m = new HashMap<Integer, IComboSelectionItem>();
120
                for (IComboSelectionItem comboSelectionItem : items) {
121
                    m.put(comboSelectionItem.getId(), comboSelectionItem);
122
                }
123
                cacheMap.put(KeyTableCellRenderer.this.el, m);
73 ilm 124
                KeyTableCellRenderer.this.el.getTable().addPremierTableModifiedListener(new ListenerAndConfig(new SQLTableModifiedListener() {
17 ilm 125
                    @Override
25 ilm 126
                    public void tableModified(SQLTableEvent evt) {
127
                        final int id = evt.getId();
128
                        if (evt.getMode() == Mode.ROW_DELETED)
129
                            m.remove(id);
130
                        else
131
                            m.put(id, KeyTableCellRenderer.this.el.getComboRequest().getComboItem(id));
17 ilm 132
                    }
73 ilm 133
                }, true));
17 ilm 134
 
135
                KeyTableCellRenderer.this.isLoading = false;
83 ilm 136
                SwingUtilities.invokeLater(new Runnable() {
67 ilm 137
 
83 ilm 138
                    @Override
139
                    public void run() {
140
                        setValue(KeyTableCellRenderer.this.toSelect);
141
                        if (t != null)
142
                            t.repaint();
143
                    }
144
                });
67 ilm 145
 
17 ilm 146
            }
147
        });
63 ilm 148
 
17 ilm 149
    }
150
}