Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.pos.ui;
import org.openconcerto.erp.core.sales.pos.model.Article;
import org.openconcerto.ui.touch.ScrollableList;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ArticleSelectorDialogPanel extends JPanel {
private final ScrollableList list;
private final CaisseControler controller;
private final ArticleSelectionListener listener;
public ArticleSelectorDialogPanel(CaisseFrame caisseFrame, List<Article> articles, ArticleSelectionListener articleSelectionListener) {
this.listener = articleSelectionListener;
this.controller = caisseFrame.getControler();
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
ArticleModel model = new ArticleModel();
model.setArticles(articles);
final Font f;
if (controller.getPOSConf().getScreenWidth() < 1280) {
f = new Font("Arial", Font.PLAIN, 18);
} else {
f = new Font("Arial", Font.PLAIN, 21);
}
this.list = new ScrollableList(model) {
int maxStringWidth = 0;
@Override
public void paint(Graphics g) {
if (this.maxStringWidth == 0) {
g.setFont(f);
int w = this.getWidth();
int priceWidth = (int) g.getFontMetrics(f).getStringBounds("9999 €", g).getWidth();
int maxW = w - priceWidth - getLeftMargin();
String str = "a";
int strW;
do {
strW = (int) g.getFontMetrics(f).getStringBounds(str, g).getWidth();
str += "a";
} while (strW < maxW);
this.maxStringWidth = Math.max(1, str.length() - 1);
}
super.paint(g);
g.setColor(Color.GRAY);
g.drawLine(0, 0, 0, this.getHeight());
}
@Override
public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
Article article = (Article) object;
ArticleSelector.paintArticle(f, g, article, isSelected, posY, this.getWidth(), this.getCellHeight(), this.maxStringWidth, getLeftMargin(), false);
}
};
this.list.setFixedCellHeight(64);
this.list.setOpaque(true);
this.add(this.list, c);
this.list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Object sel = list.getSelectedValue();
if (sel != null && !e.getValueIsAdjusting()) {
Article article = (Article) sel;
controller.setArticleSelected(article);
}
}
});
this.list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Object sel = list.getSelectedValue();
if (sel != null) {
Article article = (Article) sel;
articledSelected(article);
}
}
});
}
public void articledSelected(Article a) {
listener.articleSelected(a);
}
public int getLeftMargin() {
if (this.controller.getPOSConf().getScreenWidth() < 1280) {
return 3;
}
return 10;
}
@Override
public Dimension getPreferredSize() {
int w = Math.min(1000, controller.getPOSConf().getScreenWidth());
w = (int) (w * 0.8f);
int h = (int) (0.8 * controller.getPOSConf().getScreenHeight());
return new Dimension(w, h);
}
}