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;
|
83 |
ilm |
18 |
import org.openconcerto.ui.touch.ScrollableList;
|
18 |
ilm |
19 |
|
83 |
ilm |
20 |
import java.awt.Color;
|
|
|
21 |
import java.awt.Font;
|
|
|
22 |
import java.awt.Graphics;
|
|
|
23 |
import java.awt.Graphics2D;
|
18 |
ilm |
24 |
import java.awt.GridBagConstraints;
|
|
|
25 |
import java.awt.GridBagLayout;
|
83 |
ilm |
26 |
import java.awt.RenderingHints;
|
18 |
ilm |
27 |
import java.awt.event.MouseAdapter;
|
|
|
28 |
import java.awt.event.MouseEvent;
|
|
|
29 |
|
|
|
30 |
import javax.swing.JPanel;
|
|
|
31 |
import javax.swing.event.ListSelectionEvent;
|
|
|
32 |
import javax.swing.event.ListSelectionListener;
|
|
|
33 |
|
|
|
34 |
public class CategorieSelector extends JPanel implements ListSelectionListener, CaisseListener {
|
|
|
35 |
private CategorieModel model;
|
83 |
ilm |
36 |
private ScrollableList list;
|
18 |
ilm |
37 |
private StatusBar comp;
|
|
|
38 |
private Categorie previous;
|
|
|
39 |
private ArticleModel articleModel;
|
|
|
40 |
private CaisseControler controller;
|
|
|
41 |
|
132 |
ilm |
42 |
CategorieSelector(final CaisseControler controller, final ArticleModel articleModel) {
|
83 |
ilm |
43 |
this.setBackground(Color.WHITE);
|
18 |
ilm |
44 |
this.articleModel = articleModel;
|
|
|
45 |
this.controller = controller;
|
|
|
46 |
|
|
|
47 |
controller.addCaisseListener(this);
|
|
|
48 |
|
|
|
49 |
this.setLayout(new GridBagLayout());
|
|
|
50 |
GridBagConstraints c = new GridBagConstraints();
|
|
|
51 |
c.gridx = 0;
|
|
|
52 |
c.gridy = 0;
|
|
|
53 |
c.weightx = 1;
|
|
|
54 |
c.fill = GridBagConstraints.BOTH;
|
|
|
55 |
this.comp = new StatusBar();
|
|
|
56 |
this.comp.setTitle("Catégories");
|
|
|
57 |
this.add(this.comp, c);
|
|
|
58 |
|
|
|
59 |
c.weighty = 1;
|
|
|
60 |
c.gridy++;
|
|
|
61 |
this.model = new CategorieModel();
|
|
|
62 |
this.model.setRoot(null);
|
83 |
ilm |
63 |
final Font f = new Font("Arial", Font.PLAIN, 36);
|
|
|
64 |
final Font smallFont = new Font("Arial", Font.PLAIN, 23);
|
|
|
65 |
this.list = new ScrollableList(this.model) {
|
|
|
66 |
int maxWidth = 0;
|
|
|
67 |
|
|
|
68 |
@Override
|
|
|
69 |
public void paint(Graphics g) {
|
|
|
70 |
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
|
|
71 |
if (maxWidth == 0) {
|
|
|
72 |
g.setFont(f);
|
|
|
73 |
int w = this.getWidth();
|
|
|
74 |
|
|
|
75 |
int maxW = w - 2 * getLeftMargin();
|
|
|
76 |
String str = "a";
|
|
|
77 |
int strW;
|
|
|
78 |
do {
|
|
|
79 |
strW = (int) g.getFontMetrics(f).getStringBounds(str, g).getWidth();
|
|
|
80 |
str += "a";
|
|
|
81 |
} while (strW < maxW);
|
|
|
82 |
|
|
|
83 |
maxWidth = Math.max(1, str.length() - 1);
|
|
|
84 |
System.out.println(w + " " + getLeftMargin() + " " + maxWidth);
|
|
|
85 |
|
|
|
86 |
}
|
|
|
87 |
super.paint(g);
|
174 |
ilm |
88 |
|
|
|
89 |
g.setColor(Color.LIGHT_GRAY);
|
|
|
90 |
g.drawLine(0, 0, 0, this.getHeight());
|
|
|
91 |
|
83 |
ilm |
92 |
}
|
|
|
93 |
|
|
|
94 |
private int getLeftMargin() {
|
|
|
95 |
|
|
|
96 |
return 10;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public void paintCell(Graphics g, Object object, int index, boolean selected, int posY) {
|
|
|
100 |
|
|
|
101 |
g.setColor(Color.WHITE);
|
|
|
102 |
|
|
|
103 |
g.fillRect(0, posY, this.getWidth(), this.getCellHeight());
|
|
|
104 |
g.setColor(Color.GRAY);
|
|
|
105 |
|
|
|
106 |
g.drawLine(0, posY + this.getCellHeight() - 1, this.getWidth(), posY + this.getCellHeight() - 1);
|
|
|
107 |
|
|
|
108 |
if (selected) {
|
|
|
109 |
g.setColor(Color.BLACK);
|
|
|
110 |
} else {
|
|
|
111 |
g.setColor(Color.GRAY);
|
|
|
112 |
}
|
|
|
113 |
String label = object.toString();
|
|
|
114 |
|
|
|
115 |
if (label.length() > maxWidth * 2) {
|
|
|
116 |
label = label.substring(0, maxWidth * 2) + "...";
|
|
|
117 |
}
|
|
|
118 |
String label2 = null;
|
|
|
119 |
if (label.length() > maxWidth) {
|
|
|
120 |
String t = label.substring(0, maxWidth).trim();
|
|
|
121 |
int lastSpace = t.lastIndexOf(' ');
|
|
|
122 |
if (lastSpace <= 0) {
|
|
|
123 |
lastSpace = maxWidth;
|
|
|
124 |
}
|
|
|
125 |
label2 = label.substring(lastSpace).trim();
|
|
|
126 |
label = label.substring(0, lastSpace).trim();
|
|
|
127 |
if (label2.length() > maxWidth) {
|
|
|
128 |
label2 = label2.substring(0, maxWidth) + "...";
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if (label2 == null) {
|
|
|
133 |
g.setFont(f);
|
|
|
134 |
g.drawString(label, getLeftMargin(), posY + 44);
|
|
|
135 |
} else {
|
|
|
136 |
g.setFont(smallFont);
|
|
|
137 |
g.drawString(label, getLeftMargin(), posY + 26);
|
|
|
138 |
g.drawString(label2, getLeftMargin(), posY + 52);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
};
|
18 |
ilm |
144 |
this.list.setFixedCellHeight(64);
|
83 |
ilm |
145 |
this.list.setOpaque(true);
|
|
|
146 |
this.list.setBackground(Color.WHITE);
|
18 |
ilm |
147 |
this.add(this.list, c);
|
83 |
ilm |
148 |
this.list.addListSelectionListener(this);
|
18 |
ilm |
149 |
|
|
|
150 |
this.comp.addMouseListener(new MouseAdapter() {
|
|
|
151 |
|
|
|
152 |
@Override
|
|
|
153 |
public void mousePressed(MouseEvent e) {
|
65 |
ilm |
154 |
// User pressed "Previous" button on category
|
|
|
155 |
final Categorie newCategory = CategorieSelector.this.previous;
|
|
|
156 |
CategorieSelector.this.model.setRoot(newCategory);
|
18 |
ilm |
157 |
CategorieSelector.this.list.clearSelection();
|
65 |
ilm |
158 |
if (newCategory == null) {
|
18 |
ilm |
159 |
CategorieSelector.this.comp.setTitle("Catégories");
|
65 |
ilm |
160 |
CategorieSelector.this.previous = null;
|
18 |
ilm |
161 |
} else {
|
65 |
ilm |
162 |
CategorieSelector.this.comp.setTitle(newCategory.getName());
|
|
|
163 |
CategorieSelector.this.previous = newCategory.getParent();
|
18 |
ilm |
164 |
}
|
65 |
ilm |
165 |
articleModel.setCategorie(newCategory);
|
132 |
ilm |
166 |
controller.setArticleSelected(null);
|
18 |
ilm |
167 |
}
|
|
|
168 |
|
|
|
169 |
});
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
@Override
|
|
|
173 |
public void valueChanged(ListSelectionEvent e) {
|
65 |
ilm |
174 |
final Object sel = this.list.getSelectedValue();
|
18 |
ilm |
175 |
if (sel != null && !e.getValueIsAdjusting()) {
|
65 |
ilm |
176 |
final Categorie c = (Categorie) sel;
|
18 |
ilm |
177 |
if (!c.getSubCategories().isEmpty()) {
|
|
|
178 |
// Descend la hierarchie
|
|
|
179 |
this.previous = this.model.getRoot();
|
|
|
180 |
this.model.setRoot(c);
|
|
|
181 |
this.comp.setTitle(c.getName());
|
|
|
182 |
this.list.clearSelection();
|
|
|
183 |
}
|
|
|
184 |
this.articleModel.setCategorie(c);
|
|
|
185 |
this.controller.setArticleSelected(null);
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
@Override
|
|
|
190 |
public void caisseStateChanged() {
|
|
|
191 |
final Article articleSelected = this.controller.getArticleSelected();
|
|
|
192 |
if (articleSelected != null) {
|
65 |
ilm |
193 |
final Categorie c = articleSelected.getCategorie();
|
18 |
ilm |
194 |
if (c.getParent() != null) {
|
|
|
195 |
this.previous = c.getParent().getParent();
|
|
|
196 |
this.model.setRoot(c.getParent());
|
|
|
197 |
this.comp.setTitle(c.getParent().getName());
|
|
|
198 |
}
|
|
|
199 |
this.list.setSelectedValue(c, true);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
}
|
|
|
203 |
}
|