OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 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.
83 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.erp.core.sales.pos.ui;
15
 
16
import org.openconcerto.erp.core.sales.pos.model.Article;
17
import org.openconcerto.erp.core.sales.pos.model.Categorie;
18
import org.openconcerto.utils.StringUtils;
19
 
20
import java.util.ArrayList;
21
import java.util.Collections;
22
import java.util.Comparator;
23
import java.util.List;
24
import java.util.Stack;
25
 
26
import javax.swing.AbstractListModel;
27
import javax.swing.SwingUtilities;
28
 
29
public class FilteredListModel extends AbstractListModel {
149 ilm 30
    private final List<Object> items = new ArrayList<>();
31
    private final Stack<String> searches = new Stack<>();
32
    private final Thread t;
83 ilm 33
 
149 ilm 34
    public FilteredListModel() {
35
        items.addAll(Categorie.getFavoriteProducts());
83 ilm 36
        final List<Categorie> l = new ArrayList<Categorie>(Categorie.getAllCategories());
37
        Collections.sort(l, new Comparator<Categorie>() {
38
 
39
            @Override
40
            public int compare(Categorie o1, Categorie o2) {
41
                return o1.getName().compareToIgnoreCase(o2.getName());
42
            }
43
        });
44
        for (Categorie categorie : l) {
45
            final List<Article> articles = categorie.getArticles();
46
            if (!articles.isEmpty()) {
47
                items.add(categorie);
48
                items.addAll(articles);
49
            }
50
        }
51
        t = new Thread(new Runnable() {
52
 
53
            @Override
54
            public void run() {
55
                while (true) {
56
 
57
                    String s = null;
58
                    synchronized (searches) {
59
                        if (!searches.isEmpty()) {
60
                            s = searches.lastElement().toLowerCase();
61
                            searches.clear();
62
                        }
63
                    }
64
                    if (s != null) {
65
                        final List<Object> newitems = new ArrayList<Object>();
66
                        for (Categorie categorie : l) {
67
                            final List<Article> allArticles = categorie.getArticles();
68
 
69
                            if (s.trim().isEmpty()) {
70
                                if (!allArticles.isEmpty()) {
71
                                    newitems.add(categorie);
72
                                    newitems.addAll(allArticles);
73
                                }
74
 
75
                            } else {
76
                                String[] parts = StringUtils.fastSplit(s, ' ').toArray(new String[] {});
77
                                final int length = parts.length;
78
                                final List<Article> articles = new ArrayList<Article>();
79
                                int size = allArticles.size();
80
                                for (int i = 0; i < size; i++) {
81
                                    Article a = allArticles.get(i);
182 ilm 82
                                    final String name = (a.getName() + a.getCode() + a.getBarCode() + a.getDeclinaison()).toLowerCase();
83 ilm 83
                                    for (int j = 0; j < length; j++) {
84
 
85
                                        if (name.contains(parts[j])) {
86
                                            if (j == length - 1) {
87
                                                articles.add(a);
88
                                            }
89
 
90
                                        } else {
91
                                            break;
92
                                        }
93
                                    }
94
                                }
95
 
96
                                if (!articles.isEmpty()) {
97
                                    newitems.add(categorie);
98
                                    newitems.addAll(articles);
99
                                }
100
                            }
101
                        }
102
                        SwingUtilities.invokeLater(new Runnable() {
103
 
104
                            @Override
105
                            public void run() {
106
                                items.clear();
149 ilm 107
                                items.addAll(Categorie.getFavoriteProducts());
83 ilm 108
                                items.addAll(newitems);
109
                                fireContentsChanged(this, 0, items.size());
110
                            }
111
                        });
112
                    } else {
113
                        try {
114
                            Thread.sleep(100);
115
                        } catch (InterruptedException e) {
116
                            e.printStackTrace();
117
                        }
118
                    }
119
 
120
                }
121
            }
122
        });
123
        t.setName("FilteredListModel search");
124
        t.setPriority(Thread.MIN_PRIORITY);
125
        t.setDaemon(true);
126
        t.start();
127
    }
128
 
129
    @Override
130
    public int getSize() {
131
        return items.size();
132
    }
133
 
134
    @Override
135
    public Object getElementAt(int index) {
136
        return items.get(index);
137
    }
138
 
139
    public void setFilter(String text) {
140
        System.err.println("FilteredListModel.setFilter() " + text);
141
        items.clear();
142
        synchronized (searches) {
143
            searches.add(text);
144
        }
145
    }
146
 
147
}