OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
156 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.
156 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.ui.light;
15
 
16
import org.openconcerto.utils.io.JSONConverter;
17
 
18
import java.util.List;
19
import java.util.Optional;
20
 
21
import net.minidev.json.JSONObject;
22
 
23
public class LightUIAutoCompleteComboBox extends LightUIComboBox {
24
    private static final String FILTER = "filter";
25
 
26
    private String filter;
27
 
28
    private LightUIComboRequest request;
29
 
30
    public LightUIAutoCompleteComboBox() {
31
    }
32
 
33
    public LightUIAutoCompleteComboBox(final String id) {
34
        super(id);
35
        this.setType(TYPE_AUTOCOMPLETE_COMBOBOX);
36
    }
37
 
38
    public void setComboRequest(final LightUIComboRequest request) {
39
        this.request = request;
40
        this.setFilter("");
41
        this.setAlreadyFilled(true);
42
    }
43
 
44
    public LightUIComboRequest getComboRequest() {
45
        return this.request;
46
    }
47
 
48
    public String getFilter() {
49
        return this.filter;
50
    }
51
 
52
    public void setFilter(final String filter) {
53
        this.filter = filter;
54
        this.applyFilter();
55
    }
56
 
57
    private void applyFilter() {
58
        if (this.request != null) {
59
            Integer selectedId = null;
60
            if (this.hasSelectedValue()) {
61
                selectedId = this.getSelectedValue().getId();
62
            }
63
 
64
            this.clearValues();
65
            if (this.hasNotSpecifedLine()) {
66
                this.addValue(LightUIComboBox.getDefaultValue());
67
            }
68
 
69
            final Optional<LightUIComboBoxElement> sel = Optional.ofNullable(this.hasSelectedValue() ? this.getSelectedValue() : null);
70
            final List<LightUIComboBoxElement> items = this.request.getItems(this.filter, sel);
71
 
72
            for (final LightUIComboBoxElement item : items) {
73
                this.addValue(item);
74
            }
75
 
76
            this.setSelectedId(selectedId);
77
        }
78
    }
79
 
80
    @Override
81
    public JSONObject toJSON() {
82
        final JSONObject json = super.toJSON();
83
        json.put(FILTER, this.filter);
84
        return json;
85
    }
86
 
87
    @Override
88
    public void fromJSON(final JSONObject json) {
89
        super.fromJSON(json);
90
 
91
        this.filter = JSONConverter.getParameterFromJSON(json, FILTER, String.class);
92
    }
93
}
94