OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
142 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.ui.light;
15
 
16
import org.openconcerto.sql.model.Where;
17
import org.openconcerto.sql.request.ComboSQLRequest;
18
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
19
import org.openconcerto.ui.light.LightUIComboBox;
20
import org.openconcerto.ui.light.LightUIComboBoxElement;
21
import org.openconcerto.utils.io.JSONConverter;
22
 
23
import java.util.Arrays;
24
import java.util.List;
25
import java.util.Locale;
26
import java.util.regex.Pattern;
27
 
28
import net.minidev.json.JSONObject;
29
 
30
public class LightAutoCompleteComboBox extends LightUIComboBox {
31
    private static final String FILTER = "filter";
32
 
33
    private final static Pattern QUERY_SPLIT_PATTERN = Pattern.compile("\\s+");
34
 
35
    private String filter;
36
 
37
    private ComboSQLRequest request;
38
 
39
    public LightAutoCompleteComboBox(final JSONObject json) {
40
        super(json);
41
    }
42
 
43
    public LightAutoCompleteComboBox(final String id) {
44
        super(id);
45
        this.setType(TYPE_AUTOCOMPLETE_COMBOBOX);
46
    }
47
 
48
    public void setComboRequest(final ComboSQLRequest request) {
49
        this.request = request;
50
        this.setFilter("");
51
        this.setAlreadyFilled(true);
52
    }
53
 
54
    public ComboSQLRequest getComboRequest() {
55
        return this.request;
56
    }
57
 
58
    public String getFilter() {
59
        return this.filter;
60
    }
61
 
62
    public void setFilter(final String filter) {
63
        this.filter = filter;
64
        this.applyFilter();
65
    }
66
 
67
    private void applyFilter() {
68
        if (this.request != null) {
69
            Integer selectedId = null;
70
            if (this.hasSelectedValue()) {
71
                selectedId = this.getSelectedValue().getId();
72
            }
73
 
74
            this.clearValues();
75
            if (this.hasNotSpecifedLine()) {
76
                this.addValue(LightUIComboBox.getDefaultValue());
77
            }
78
 
79
            final Where where = this.hasSelectedValue() ? new Where(this.request.getPrimaryTable().getKey(), "=", this.getSelectedValue().getId()) : null;
80
            final List<IComboSelectionItem> items = this.request.getComboItems(true, Arrays.asList(QUERY_SPLIT_PATTERN.split(this.filter)), Locale.getDefault(), where);
81
 
82
            System.err.println("LightAutoCompleteComboBox.applyFilter() - items count: " + items.size());
83
            for (final IComboSelectionItem item : items) {
144 ilm 84
                int id = item.getId();
85
                String label = item.getLabel();
86
                System.err.println("LightAutoCompleteComboBox.applyFilter() " + id + " : " + label);
87
                this.addValue(new LightUIComboBoxElement(id, label));
142 ilm 88
            }
89
 
90
            this.setSelectedId(selectedId);
91
        }
92
    }
93
 
94
    @Override
95
    public JSONObject toJSON() {
96
        final JSONObject json = super.toJSON();
97
        json.put(FILTER, this.filter);
98
        return json;
99
    }
100
 
101
    @Override
102
    public void fromJSON(final JSONObject json) {
103
        super.fromJSON(json);
104
 
105
        this.filter = JSONConverter.getParameterFromJSON(json, FILTER, String.class);
106
    }
107
}