OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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