OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 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.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
 
19
import java.awt.GridBagConstraints;
20
import java.awt.GridBagLayout;
21
import java.awt.event.MouseAdapter;
22
import java.awt.event.MouseEvent;
23
 
24
import javax.swing.JList;
25
import javax.swing.JPanel;
26
import javax.swing.ListSelectionModel;
27
import javax.swing.event.ListSelectionEvent;
28
import javax.swing.event.ListSelectionListener;
29
 
30
public class CategorieSelector extends JPanel implements ListSelectionListener, CaisseListener {
31
    private CategorieModel model;
32
    private JList list;
33
    private StatusBar comp;
34
    private Categorie previous;
35
    private ArticleModel articleModel;
36
    private CaisseControler controller;
37
 
38
    CategorieSelector(CaisseControler controller, final ArticleModel articleModel) {
39
        this.articleModel = articleModel;
40
        this.controller = controller;
41
 
42
        controller.addCaisseListener(this);
43
 
44
        this.setLayout(new GridBagLayout());
45
        GridBagConstraints c = new GridBagConstraints();
46
        c.gridx = 0;
47
        c.gridy = 0;
48
        c.weightx = 1;
49
        c.fill = GridBagConstraints.BOTH;
50
        this.comp = new StatusBar();
51
        this.comp.setTitle("Catégories");
52
        this.add(this.comp, c);
53
 
54
        c.weighty = 1;
55
        c.gridy++;
56
        this.model = new CategorieModel();
57
        this.model.setRoot(null);
58
        this.list = new JList(this.model);
59
        this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
60
        this.list.setCellRenderer(new CategorieListCellRenderer());
61
        this.list.setFixedCellHeight(64);
62
        this.add(this.list, c);
63
        this.list.getSelectionModel().addListSelectionListener(this);
64
 
65
        this.comp.addMouseListener(new MouseAdapter() {
66
 
67
            @Override
68
            public void mousePressed(MouseEvent e) {
69
                final Categorie prev = CategorieSelector.this.previous;
70
                CategorieSelector.this.model.setRoot(prev);
71
                CategorieSelector.this.list.clearSelection();
72
                if (prev == null) {
73
                    CategorieSelector.this.comp.setTitle("Catégories");
74
                    CategorieSelector.this.comp.setPrevious(false);
75
                    articleModel.setCategorie(null);
76
                } else {
77
                    CategorieSelector.this.comp.setTitle(prev.getName());
78
                    CategorieSelector.this.comp.setPrevious(true);
79
                }
80
            }
81
 
82
        });
83
    }
84
 
85
    @Override
86
    public void valueChanged(ListSelectionEvent e) {
87
        Object sel = this.list.getSelectedValue();
88
        if (sel != null && !e.getValueIsAdjusting()) {
89
            Categorie c = (Categorie) sel;
90
            if (!c.getSubCategories().isEmpty()) {
91
                // Descend la hierarchie
92
                this.previous = this.model.getRoot();
93
                this.model.setRoot(c);
94
                this.comp.setTitle(c.getName());
95
                this.comp.setPrevious(true);
96
                this.list.clearSelection();
97
            }
98
            this.articleModel.setCategorie(c);
99
            this.controller.setArticleSelected(null);
100
        }
101
    }
102
 
103
    @Override
104
    public void caisseStateChanged() {
105
        final Article articleSelected = this.controller.getArticleSelected();
106
        if (articleSelected != null) {
107
            Categorie c = articleSelected.getCategorie();
108
            if (c.getParent() != null) {
109
                this.previous = c.getParent().getParent();
110
 
111
                this.model.setRoot(c.getParent());
112
                this.comp.setTitle(c.getParent().getName());
113
            }
114
            this.comp.setPrevious(true);
115
            this.list.setSelectedValue(c, true);
116
        }
117
 
118
    }
119
}