OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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.search;
15
 
16
import org.openconcerto.sql.view.list.ITableModel;
17
import org.openconcerto.sql.view.list.SQLTableModelColumn;
18
import org.openconcerto.utils.FormatGroup;
19
 
20
import java.awt.GridBagConstraints;
21
import java.awt.GridBagLayout;
22
import java.awt.Insets;
23
import java.util.ArrayList;
24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27
 
28
import javax.swing.JPanel;
29
import javax.swing.SwingUtilities;
30
import javax.swing.table.TableModel;
31
 
32
public class SearchListComponent extends JPanel {
33
 
34
    private ITableModel model;
35
 
36
    private final List<SearchItemComponent> items;
37
 
38
    private final GridBagConstraints c;
39
    private Runnable r;
40
 
41
    private final Map<Class<?>, FormatGroup> formats;
42
 
43
    public SearchListComponent(ITableModel model) {
44
        super();
45
        this.r = null;
182 ilm 46
        this.items = new ArrayList<>();
47
        this.formats = new HashMap<>();
17 ilm 48
 
49
        this.setLayout(new GridBagLayout());
50
        this.c = new GridBagConstraints();
51
        this.c.gridx = 0;
52
        this.c.gridy = 0;
53
        this.c.insets = new Insets(0, 2, 0, 2);
54
        this.c.fill = GridBagConstraints.HORIZONTAL;
55
        this.c.weightx = 1;
56
 
57
        this.reset(model);
58
    }
59
 
60
    protected final TableModel getTableModel() {
61
        return this.model;
62
    }
63
 
64
    // never contains null
65
    final String[] getColumnNames(int index) {
66
        final SQLTableModelColumn col = this.model.getLinesSource().getParent().getColumn(index);
67
        final String[] res = new String[] { col.getName(), col.getToolTip(), col.getIdentifier() };
68
        assert res[res.length - 1] != null : "Null identifier for " + col;
69
        for (int i = res.length - 2; i >= 0; i--) {
70
            if (res[i] == null)
71
                res[i] = res[i + 1];
72
        }
73
        return res;
74
    }
75
 
76
    private SearchItemComponent getItem(int i) {
77
        return this.items.get(i);
78
    }
79
 
80
    // we're a JComponent so all of our methods must be called from the EDT
81
    // plus no need to synchronize
82
    private void checkEDT() {
83
        if (!SwingUtilities.isEventDispatchThread())
84
            Thread.dumpStack();
85
    }
86
 
87
    public void reset(ITableModel newModel) {
88
        this.model = newModel;
89
        this.reset();
90
    }
91
 
92
    private void reset() {
93
        this.checkEDT();
94
        this.removeAll();
95
        this.items.clear();
96
        this.c.gridy = 0;
97
 
98
        if (this.getTableModel() != null) {
99
            this.addNewSearchItem();
100
            this.updateSearch();
101
        } else
102
            this.revalidate();
103
    }
104
 
105
    public void addNewSearchItem() {
106
        this.checkEDT();
107
 
108
        final SearchItemComponent item = new SearchItemComponent(this);
109
        add(item, this.c);
110
        this.c.gridy++;
111
        this.items.add(item);
112
 
113
        this.revalidate();
114
    }
115
 
116
    public void updateSearch() {
117
        checkEDT();
118
        // ne pas passer par model.invokeLater() sinon la recherche risque de lagger
119
        // (attendre après la base)
120
        this.model.search(getSearchList(), this.r);
121
    }
122
 
123
    /**
124
     * Créé et retourne la searchList correspondant
125
     *
126
     * @return la searchList remplie
127
     */
128
    private SearchList getSearchList() {
129
        final SearchList l = new SearchList();
130
        for (int i = 0; i < this.items.size(); i++) {
131
            final SearchItemComponent component = this.items.get(i);
132
            l.addSearchItem(component.getSearchItem());
133
        }
134
        return l;
135
    }
136
 
137
    public void removeSearchItem(SearchItemComponent item) {
138
        this.checkEDT();
139
 
140
        if (this.items.size() > 1) {
141
            this.items.remove(item);
142
            this.remove(item);
143
            this.revalidate();
144
            updateSearch();
145
        } else {
146
            getItem(0).resetState();
147
        }
148
    }
149
 
150
    public void clear() {
151
        this.reset();
152
    }
153
 
154
    public void setSearchString(String s) {
155
        this.setSearchString(s, null);
156
    }
157
 
158
    /**
159
     * Recherche <code>s</code> puis exécute r.
160
     *
161
     * @param s la chaîne à rechercher.
162
     * @param r sera exécuter dans la thread de la recherche.
163
     */
164
    public void setSearchString(String s, Runnable r) {
165
        checkEDT();
166
 
167
        this.clear();
168
        // DO NOT assign r before clear() otherwise it will be run twice
169
        this.r = r;
170
        this.getItem(0).setText(s);
171
        this.r = null;
172
    }
173
 
174
    public void setSearchFullMode(boolean b) {
175
        this.checkEDT();
176
 
177
        for (int i = 0; i < this.items.size(); i++) {
182 ilm 178
            final SearchItemComponent component = this.items.get(i);
17 ilm 179
            component.setSearchFullMode(b);
180
        }
181
    }
182
 
183
    public final void setFormats(Map<Class<?>, FormatGroup> formats) {
184
        this.formats.clear();
185
        this.formats.putAll(formats);
186
    }
187
 
188
    public final Map<Class<?>, FormatGroup> getFormats() {
189
        return this.formats;
190
    }
191
}