Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/GPLPrinterPanel.java |
---|
New file |
0,0 → 1,454 |
package org.openconcerto.modules.label; |
import java.awt.Desktop; |
import java.awt.FlowLayout; |
import java.awt.GridBagConstraints; |
import java.awt.GridBagLayout; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.awt.image.BufferedImage; |
import java.awt.print.PrinterJob; |
import java.io.BufferedReader; |
import java.io.DataOutputStream; |
import java.io.File; |
import java.io.FileInputStream; |
import java.io.FileOutputStream; |
import java.io.IOException; |
import java.io.StringReader; |
import java.net.Socket; |
import java.nio.charset.StandardCharsets; |
import java.text.DecimalFormat; |
import java.util.ArrayList; |
import java.util.HashMap; |
import java.util.HashSet; |
import java.util.List; |
import java.util.Map; |
import java.util.Properties; |
import java.util.Set; |
import javax.imageio.ImageIO; |
import javax.swing.ButtonGroup; |
import javax.swing.JButton; |
import javax.swing.JFileChooser; |
import javax.swing.JFrame; |
import javax.swing.JLabel; |
import javax.swing.JOptionPane; |
import javax.swing.JPanel; |
import javax.swing.JRadioButton; |
import javax.swing.JSpinner; |
import javax.swing.JTextField; |
import javax.swing.SpinnerNumberModel; |
import javax.swing.SwingConstants; |
import javax.swing.SwingUtilities; |
import javax.swing.UIManager; |
import javax.swing.UnsupportedLookAndFeelException; |
import javax.xml.parsers.ParserConfigurationException; |
import org.openconcerto.modules.label.graphicspl.GraphicsPL; |
import org.openconcerto.sql.model.SQLRowAccessor; |
import org.openconcerto.ui.DefaultGridBagConstraints; |
import org.openconcerto.ui.JLabelBold; |
import org.openconcerto.utils.BaseDirs; |
import org.openconcerto.utils.ExceptionHandler; |
import org.openconcerto.utils.FileUtils; |
import org.openconcerto.utils.ProductInfo; |
import org.xml.sax.SAXException; |
public class GPLPrinterPanel extends JPanel { |
private final HashMap<String, String> mapName = new HashMap<>(); |
private final List<String> variables; |
private final List<String> knownVariables = new ArrayList<>(); |
private Map<String, JTextField> editorMap = new HashMap<>(); |
private String gpl; |
private final Properties properties = new Properties(); |
private File imageDir; |
public static void main(String[] args) throws IOException { |
// final File f = new File("Templates/Labels", "50x50.zpl"); |
final File file = new File("Template/Labels", "rouquette.graphicspl"); |
String gpl = FileUtils.read(file); |
SwingUtilities.invokeLater(new Runnable() { |
@Override |
public void run() { |
try { |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { |
e.printStackTrace(); |
} |
GPLPrinterPanel p = new GPLPrinterPanel(gpl, file.getParentFile()); |
p.initUI(null); |
JFrame f = new JFrame(); |
f.setTitle(file.getName()); |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
f.setContentPane(p); |
f.pack(); |
f.setLocationRelativeTo(null); |
f.setVisible(true); |
} |
}); |
} |
public GPLPrinterPanel(String gpl, File imageDir) { |
this.gpl = gpl; |
this.imageDir = imageDir; |
this.variables = getVariables(gpl); |
knownVariables.add("product.code"); |
knownVariables.add("product.name"); |
knownVariables.add("product.material"); |
knownVariables.add("product.ean13"); |
knownVariables.add("product.price"); |
knownVariables.add("product.pricewithtax"); |
// |
mapName.put("product.name", "Nom"); |
mapName.put("product.code", "Code"); |
mapName.put("product.ean13", "Code à barres"); |
mapName.put("product.price", "Prix HT"); |
mapName.put("product.pricewithtax", "Prix TTC"); |
mapName.put("product.treatment", "Traitement"); |
mapName.put("product.origin", "Origine"); |
mapName.put("product.batch", "Lot"); |
mapName.put("product.size", "Taille"); |
mapName.put("product.color", "Couleur"); |
mapName.put("product.material", "Matière"); |
} |
private final File getPrefFile() { |
final File prefsFolder = BaseDirs.create(ProductInfo.getInstance()).getPreferencesFolder(); |
if (!prefsFolder.exists()) { |
prefsFolder.mkdirs(); |
} |
return new File(prefsFolder, "labels.properties"); |
} |
protected void initUI(SQLRowAccessor row) { |
final File prefsFolder = BaseDirs.create(ProductInfo.getInstance()).getPreferencesFolder(); |
if (!prefsFolder.exists()) { |
prefsFolder.mkdirs(); |
} |
final File file = getPrefFile(); |
System.out.println(file.getAbsolutePath()); |
if (file.exists()) { |
try { |
properties.load(new FileInputStream(file)); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
this.setLayout(new GridBagLayout()); |
GridBagConstraints c = new DefaultGridBagConstraints(); |
c.fill = GridBagConstraints.HORIZONTAL; |
this.removeAll(); |
// Fields |
Set<String> added = new HashSet<>(); |
for (String v : this.knownVariables) { |
if (variables.contains(v)) { |
// Non editable |
String label = getName(v); |
c.gridx = 0; |
c.weightx = 0; |
this.add(new JLabel(label, SwingConstants.RIGHT), c); |
c.gridx++; |
c.weightx = 1; |
String value = getValueAsString(row, v); |
JTextField txt = new JTextField(20); |
if (value != null) { |
txt.setText(value); |
txt.setEditable(false); |
} |
editorMap.put(v, txt); |
this.add(txt, c); |
added.add(v); |
c.gridy++; |
} |
} |
for (String v : this.variables) { |
if (!added.contains(v)) { |
// Editable |
String label = getName(v); |
c.gridx = 0; |
c.weightx = 0; |
this.add(new JLabel(label, SwingConstants.RIGHT), c); |
c.gridx++; |
c.weightx = 1; |
JTextField txt = new JTextField(20); |
editorMap.put(v, txt); |
this.add(txt, c); |
added.add(v); |
c.gridy++; |
} |
} |
c.gridwidth = 2; |
c.gridx = 0; |
this.add(new JLabelBold("Paramètres d'impression"), c); |
// Printer selector |
c.gridx = 0; |
c.gridy++; |
final JPanel l1 = new JPanel(); |
l1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0)); |
l1.add(new JLabel("Nombre d'étiquettes")); |
final JSpinner nbLabels = new JSpinner(new SpinnerNumberModel(1, 1, 1000, 10)); |
l1.add(nbLabels); |
this.add(l1, c); |
// Delay |
l1.add(new JLabel(" Pause entre chaque impression")); |
final JSpinner delayLabels = new JSpinner(new SpinnerNumberModel(800, 100, 10000, 100)); |
l1.add(delayLabels); |
this.add(l1, c); |
l1.add(new JLabel("ms")); |
c.gridy++; |
final JPanel lPrintNetwork = new JPanel(); |
lPrintNetwork.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 0)); |
JRadioButton radioNetworkPrinter = new JRadioButton("imprimante réseau ZPL IP :"); |
lPrintNetwork.add(radioNetworkPrinter); |
JTextField textPrinterIP = new JTextField(16); |
lPrintNetwork.add(textPrinterIP); |
lPrintNetwork.add(new JLabel(" Port :")); |
JSpinner portZPL = new JSpinner(new SpinnerNumberModel(9100, 24, 10000, 1)); |
lPrintNetwork.add(portZPL); |
this.add(lPrintNetwork, c); |
c.gridy++; |
final JPanel lPrintLocal = new JPanel(); |
lPrintLocal.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 0)); |
JRadioButton radioLocalPrinter = new JRadioButton("imprimante locale"); |
lPrintLocal.add(radioLocalPrinter); |
radioLocalPrinter.setSelected(true); |
this.add(lPrintLocal, c); |
final ButtonGroup gr = new ButtonGroup(); |
gr.add(radioLocalPrinter); |
gr.add(radioNetworkPrinter); |
c.gridy++; |
c.weighty = 1; |
c.gridx = 1; |
c.fill = GridBagConstraints.NONE; |
c.anchor = GridBagConstraints.SOUTHEAST; |
// Restore state from properties |
if (properties.getOrDefault("printerType", "local").equals("local")) { |
radioLocalPrinter.setSelected(true); |
} else { |
radioNetworkPrinter.setSelected(true); |
} |
textPrinterIP.setText(properties.getOrDefault("printerIp", "").toString()); |
portZPL.setValue(Long.parseLong(properties.getOrDefault("printerPort", "9100").toString())); |
nbLabels.setValue(Long.parseLong(properties.getOrDefault("nbLabels", "1").toString())); |
delayLabels.setValue(Long.parseLong(properties.getOrDefault("delay", "800").toString())); |
JPanel actions = new JPanel(); |
actions.setLayout(new FlowLayout(FlowLayout.LEFT)); |
this.add(actions, c); |
JButton exportButton = new JButton("Export PNG"); |
actions.add(exportButton); |
JButton printButton = new JButton("Imprimer"); |
actions.add(printButton); |
exportButton.addActionListener(new ActionListener() { |
@Override |
public void actionPerformed(ActionEvent e) { |
final String code = createFilledCode(); |
GraphicsPL g = new GraphicsPL(); |
try { |
g.load(code, GPLPrinterPanel.this.imageDir); |
BufferedImage img = g.createImage(1); |
JFileChooser fileChooser = new JFileChooser(); |
fileChooser.setDialogTitle("Export de l'etiquette"); |
final File dir = new File(System.getProperties().getProperty("user.home")); |
fileChooser.setCurrentDirectory(dir); |
fileChooser.setSelectedFile(new File(dir, "etiquette.png")); |
int userSelection = fileChooser.showSaveDialog(GPLPrinterPanel.this); |
if (userSelection == JFileChooser.APPROVE_OPTION) { |
File fileToSave = fileChooser.getSelectedFile(); |
System.out.println("Save as file: " + fileToSave.getAbsolutePath()); |
ImageIO.write(img, "png", fileToSave); |
Desktop.getDesktop().open(fileToSave); |
} |
} catch (ParserConfigurationException | SAXException | IOException e1) { |
e1.printStackTrace(); |
JOptionPane.showMessageDialog(GPLPrinterPanel.this, "Erreur lors de l'export\n" + e1.getMessage()); |
} |
} |
}); |
printButton.addActionListener(new ActionListener() { |
@Override |
public void actionPerformed(ActionEvent e) { |
try { |
if (radioLocalPrinter.isSelected()) { |
properties.put("printerType", "local"); |
} else { |
properties.put("printerType", "network"); |
properties.put("printerIp", textPrinterIP.getText()); |
properties.put("printerPort", portZPL.getValue().toString()); |
} |
properties.put("nbLabels", nbLabels.getValue().toString()); |
properties.put("delay", delayLabels.getValue().toString()); |
// Save Prefs |
properties.store(new FileOutputStream(getPrefFile()), ""); |
} catch (Exception e1) { |
ExceptionHandler.handle("Erreur de sauvegarde de " + getPrefFile().getAbsolutePath(), e1); |
} |
final String code = createFilledCode(); |
System.out.println("GraphicsPL:"); |
System.out.println(code); |
if (radioNetworkPrinter.isSelected()) { |
Socket socket = null; |
try { |
GraphicsPL g = new GraphicsPL(); |
g.load(code, imageDir); |
String zpl = g.getZPL(); |
byte[] data = zpl.getBytes(StandardCharsets.UTF_8); |
socket = new Socket(textPrinterIP.getText(), ((Number) portZPL.getValue()).intValue()); |
final DataOutputStream out = new DataOutputStream(socket.getOutputStream()); |
final int nb = ((Number) nbLabels.getValue()).intValue(); |
for (int i = 0; i < nb; i++) { |
out.write(data); |
} |
} catch (Exception ex) { |
ex.printStackTrace(); |
JOptionPane.showMessageDialog(printButton, "Erreur d'impression réseau : " + ex.getMessage()); |
} finally { |
if (socket != null) { |
try { |
socket.close(); |
} catch (IOException e1) { |
e1.printStackTrace(); |
} |
} |
} |
} else { |
try { |
final PrinterJob pj1 = PrinterJob.getPrinterJob(); |
GraphicsPL g = new GraphicsPL(); |
g.load(code, imageDir); |
if (pj1.printDialog()) { |
final int nb = ((Number) nbLabels.getValue()).intValue(); |
for (int i = 0; i < nb; i++) { |
pj1.setPrintable(g.createPrintable()); |
pj1.print(); |
Thread.sleep(((Number) delayLabels.getValue()).intValue()); |
} |
} |
} catch (Exception ex) { |
ex.printStackTrace(); |
JOptionPane.showMessageDialog(printButton, "Erreur d'impression locale : " + ex.getMessage()); |
} |
} |
} |
}); |
} |
private String createFilledCode() { |
final BufferedReader reader = new BufferedReader(new StringReader(this.gpl)); |
final StringBuilder builder = new StringBuilder(); |
try { |
String line = reader.readLine(); |
while (line != null) { |
if (line.contains("${")) { |
for (String v : this.editorMap.keySet()) { |
if (line.contains("${" + v + "}")) { |
final String value = this.editorMap.get(v).getText(); |
line = line.replace("${" + v + "}", value); |
} |
} |
} |
builder.append(line); |
builder.append("\n"); |
line = reader.readLine(); |
} |
} catch (Exception e) { |
e.printStackTrace(); |
} |
return builder.toString(); |
} |
public String getValueAsString(SQLRowAccessor row, String variableName) { |
if (row == null) { |
return null; |
} |
if (variableName.equals("product.code")) { |
return row.getString("CODE"); |
} else if (variableName.equals("product.name")) { |
return row.getString("NOM"); |
} else if (variableName.equals("product.ean13")) { |
return row.getString("CODE_BARRE"); |
} else if (variableName.equals("product.price")) { |
return new DecimalFormat("#0.00").format(row.getBigDecimal("PV_HT")); |
} else if (variableName.equals("product.pricewithtax")) { |
return new DecimalFormat("#0.00").format(row.getBigDecimal("PV_TTC")); |
} else if (variableName.equals("product.material")) { |
return row.getString("MATIERE"); |
} |
return ""; |
} |
public String getName(String variableName) { |
String n = mapName.get(variableName); |
if (n == null) { |
return variableName; |
} |
return n; |
} |
public List<String> getVariables(String str) { |
final List<String> result = new ArrayList<>(); |
if (str == null || str.length() < 4) { |
return result; |
} |
final int l = str.length() - 1; |
int start = 0; |
boolean inName = false; |
for (int i = 0; i < l; i++) { |
char c1 = str.charAt(i); |
char c2 = str.charAt(i + 1); |
if (!inName) { |
if (c1 == '$' && c2 == '{') { |
start = i + 2; |
inName = true; |
} |
} else if (c2 == '}') { |
final int stop = i + 1; |
String v = str.substring(start, stop); |
result.add(v); |
inName = false; |
} |
} |
return result; |
} |
} |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/gs1/GS1Util.java |
---|
21,6 → 21,7 |
// character. |
// ]d2 : GS1 DataMatrix : Standard AI element strings |
private static final String GS1_DATAMATRIX_SCANNER_PREFIX = "]d2"; |
private static final String GS1_DATAMATRIX_SCANNER_PREFIX_2 = "]D2"; |
// ]Q3 : GS1 QR Code : Standard AI element strings |
private static final String GS1_QRCODE_SCANNER_PREFIX = "]Q3"; |
// ]J1 : GS1 DotCode : Standard AI element strings |
51,7 → 52,8 |
} |
public GS1AIElements parseFromScanner(String scan) throws GS1ParseException { |
if (scan.startsWith(GS1_DATAMATRIX_SCANNER_PREFIX) || scan.startsWith(GS1_128_SCANNER_PREFIX) || scan.startsWith(GS1_DATABAR_SCANNER_PREFIX) || scan.startsWith(GS1_QRCODE_SCANNER_PREFIX)) { |
if (scan.startsWith(GS1_DATAMATRIX_SCANNER_PREFIX) || scan.startsWith(GS1_DATAMATRIX_SCANNER_PREFIX_2) || scan.startsWith(GS1_128_SCANNER_PREFIX) || scan.startsWith(GS1_DATABAR_SCANNER_PREFIX) |
|| scan.startsWith(GS1_QRCODE_SCANNER_PREFIX)) { |
return parse(scan.substring(3)); |
} |
return parse(scan); |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/graphicspl/GPLDemoFrame.java |
---|
New file |
0,0 → 1,140 |
package org.openconcerto.modules.label.graphicspl; |
import java.awt.Dimension; |
import java.awt.Graphics; |
import java.awt.datatransfer.DataFlavor; |
import java.awt.dnd.DnDConstants; |
import java.awt.dnd.DropTarget; |
import java.awt.dnd.DropTargetDropEvent; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.awt.image.BufferedImage; |
import java.io.File; |
import java.io.IOException; |
import java.util.List; |
import javax.imageio.ImageIO; |
import javax.swing.JFrame; |
import javax.swing.JLabel; |
import javax.swing.JPanel; |
import javax.swing.SwingUtilities; |
import javax.swing.Timer; |
import javax.swing.UIManager; |
import javax.xml.parsers.ParserConfigurationException; |
import org.xml.sax.SAXException; |
public class GPLDemoFrame extends JFrame { |
public GPLDemoFrame() { |
JPanel p = new JPanel(); |
p.add(new JLabel("Drop your graphicspl file here")); |
p.setDropTarget(new DropTarget() { |
public synchronized void drop(DropTargetDropEvent evt) { |
try { |
evt.acceptDrop(DnDConstants.ACTION_COPY); |
List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); |
for (File file : droppedFiles) { |
System.out.println("GPLDemoFrame.GPLDemoFrame().new DropTarget() {...}.drop()" + file.getAbsolutePath()); |
if (file.getName().endsWith(".graphicspl")) { |
open(file); |
break; |
} |
} |
} catch (Exception ex) { |
ex.printStackTrace(); |
} |
} |
}); |
this.setContentPane(p); |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
} |
long last; |
protected void open(File file) throws Exception { |
loadFile(file); |
last = file.lastModified(); |
Timer timer = new Timer(1000, new ActionListener() { |
@Override |
public void actionPerformed(ActionEvent e) { |
System.out.println("check"); |
long l = file.lastModified(); |
if (l != last) { |
last = l; |
// reload |
System.out.println("check reload"); |
try { |
loadFile(file); |
} catch (ParserConfigurationException | SAXException | IOException e1) { |
e1.printStackTrace(); |
} |
} |
} |
}); |
timer.setRepeats(true); |
timer.start(); |
} |
public void loadFile(File file) throws ParserConfigurationException, SAXException, IOException { |
GraphicsPL g = new GraphicsPL(); |
g.load(file); |
BufferedImage img = g.createImage(1); |
ImageIO.write(img, "png", new File(file.getParent(), file.getName() + ".png")); |
System.out.println("GPLDemoFrame.loadFile() " + img.getWidth() + " x " + img.getHeight()); |
final Dimension dimension = new Dimension(img.getWidth(), img.getHeight()); |
JPanel iPanel = new JPanel() { |
@Override |
public Dimension getMinimumSize() { |
return dimension; |
} |
@Override |
public Dimension getPreferredSize() { |
return new Dimension(img.getWidth(), img.getHeight()); |
} |
@Override |
public Dimension getMaximumSize() { |
return new Dimension(img.getWidth(), img.getHeight()); |
} |
@Override |
public void paint(Graphics g) { |
g.drawImage(img, 0, 0, null); |
} |
}; |
this.invalidate(); |
this.setContentPane(iPanel); |
this.setSize(dimension); |
this.revalidate(); |
this.repaint(); |
} |
public static void main(String[] args) { |
SwingUtilities.invokeLater(new Runnable() { |
@Override |
public void run() { |
try { |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
} catch (Exception e) { |
e.printStackTrace(); |
} |
GPLDemoFrame f = new GPLDemoFrame(); |
f.setSize(300, 200); |
f.setVisible(true); |
f.setLocationRelativeTo(null); |
} |
}); |
} |
} |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/graphicspl/GPLRenderer.java |
---|
96,7 → 96,9 |
String fileName = element.getAttribute("file"); |
int x = Math.round(ratio * Integer.parseInt(element.getAttribute("x"))); |
int y = Math.round(ratio * Integer.parseInt(element.getAttribute("y"))); |
BufferedImage img = ImageIO.read(new File(graphicsPL.getImageDir(), fileName)); |
final File input = new File(graphicsPL.getImageDir(), fileName); |
if (input.exists()) { |
BufferedImage img = ImageIO.read(input); |
int w = Math.round(ratio * img.getWidth()); |
int h = Math.round(ratio * img.getHeight()); |
if (element.hasAttribute("width")) { |
106,6 → 108,9 |
h = Math.round(ratio * Integer.parseInt(element.getAttribute("height"))); |
} |
drawImage(x, y, w, h, img); |
} else { |
throw new IllegalStateException(input.getAbsolutePath() + " not found"); |
} |
} else if (name.equals("barcode")) { |
int x = Math.round(ratio * Integer.parseInt(element.getAttribute("x"))); |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/graphicspl/GraphicsPL.java |
---|
24,8 → 24,8 |
import org.xml.sax.SAXException; |
public class GraphicsPL { |
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><graphicspl height=\"400\" width=\"600\"/>"; |
Document doc; |
private String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><graphicspl height=\"400\" width=\"600\"/>"; |
private Document doc; |
private File imgDir; |
public static void main(String[] args) throws Exception { |
75,6 → 75,7 |
final BufferedImage img = createImage(ratio); |
g2d.drawImage(img, (int) Math.round(pf.getImageableX()), (int) Math.round(pf.getImageableY()), width, height, null); |
} catch (ParserConfigurationException | SAXException | IOException e) { |
e.printStackTrace(); |
throw new PrinterException(e.getMessage()); |
} |
return PAGE_EXISTS; |
82,13 → 83,13 |
}; |
} |
private String getZPL() throws IOException { |
public String getZPL() throws IOException { |
final ZPLRenderer renderer = new ZPLRenderer(); |
renderer.render(this); |
return renderer.getZPL(); |
} |
private BufferedImage createImage(float ratio) throws ParserConfigurationException, SAXException, IOException { |
public BufferedImage createImage(float ratio) throws ParserConfigurationException, SAXException, IOException { |
final Element root = this.doc.getDocumentElement(); |
final int width = Math.round(ratio * Integer.parseInt(root.getAttribute("width"))); |
final int height = Math.round(ratio * Integer.parseInt(root.getAttribute("height"))); |
106,7 → 107,7 |
return this.doc; |
} |
private void load(File file) throws ParserConfigurationException, SAXException, IOException { |
public void load(File file) throws ParserConfigurationException, SAXException, IOException { |
this.xml = new String(Files.readAllBytes(file.toPath())); |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
// factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
117,6 → 118,17 |
this.imgDir = file.getParentFile(); |
} |
public void load(String str, File imageDir) throws ParserConfigurationException, SAXException, IOException { |
this.xml = str; |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
// factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
final DocumentBuilder builder = factory.newDocumentBuilder(); |
final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)); |
this.doc = builder.parse(input); |
this.doc.getDocumentElement().normalize(); |
this.imgDir = imageDir; |
} |
public File getImageDir() { |
return this.imgDir; |
} |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/ImagePrintable.java |
---|
New file |
0,0 → 1,46 |
package org.openconcerto.modules.label; |
import java.awt.Graphics; |
import java.awt.image.BufferedImage; |
import java.awt.print.PageFormat; |
import java.awt.print.Printable; |
import java.awt.print.PrinterException; |
import java.awt.print.PrinterJob; |
public class ImagePrintable implements Printable { |
private double x, y, width; |
private int orientation; |
private BufferedImage image; |
public ImagePrintable(PrinterJob printJob, BufferedImage image) { |
PageFormat pageFormat = printJob.defaultPage(); |
this.x = pageFormat.getImageableX(); |
this.y = pageFormat.getImageableY(); |
this.width = pageFormat.getImageableWidth(); |
this.orientation = pageFormat.getOrientation(); |
this.image = image; |
} |
@Override |
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { |
if (pageIndex == 0) { |
int pWidth = 0; |
int pHeight = 0; |
if (orientation == PageFormat.PORTRAIT) { |
pWidth = (int) Math.min(width, (double) image.getWidth()); |
pHeight = pWidth * image.getHeight() / image.getWidth(); |
} else { |
pHeight = (int) Math.min(width, (double) image.getHeight()); |
pWidth = pHeight * image.getWidth() / image.getHeight(); |
} |
g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null); |
return PAGE_EXISTS; |
} else { |
return NO_SUCH_PAGE; |
} |
} |
} |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/ModuleLabel.java |
---|
8,8 → 8,10 |
import java.io.IOException; |
import java.nio.charset.StandardCharsets; |
import java.util.ArrayList; |
import java.util.HashMap; |
import java.util.LinkedHashMap; |
import java.util.List; |
import java.util.Map; |
import java.util.Map.Entry; |
import java.util.TreeMap; |
21,6 → 23,7 |
import org.openconcerto.erp.modules.AbstractModule; |
import org.openconcerto.erp.modules.ComponentsContext; |
import org.openconcerto.erp.modules.ModuleFactory; |
import org.openconcerto.sql.model.SQLField; |
import org.openconcerto.sql.model.SQLRow; |
import org.openconcerto.sql.model.SQLRowAccessor; |
import org.openconcerto.sql.model.SQLRowValues; |
37,6 → 40,8 |
public final class ModuleLabel extends AbstractModule { |
final LinkedHashMap<String, String> zplTemplates = new LinkedHashMap<String, String>(); |
final LinkedHashMap<String, String> gplTemplates = new LinkedHashMap<String, String>(); |
private Map<String, File> dirMap = new HashMap<>(); |
public ModuleLabel(ModuleFactory f) throws IOException { |
super(f); |
54,7 → 59,7 |
public void actionPerformed(ActionEvent arg0) { |
final IListe list = IListe.get(arg0); |
final List<Integer> selectedIDs = list.getSelection().getSelectedIDs(); |
final SQLTable tArticle = list.getSelectedRows().get(0).getTable(); |
final SQLTable tArticle = list.getSelectedRowAccessors().get(0).getTable(); |
final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() { |
@Override |
214,12 → 219,68 |
final JFrame f = new JFrame(); |
final IListe list = IListe.get(arg0); |
final int idProduct = list.getSelection().getSelectedID(); |
final SQLTable tArticle = list.getSelectedRows().get(0).getTable(); |
final SQLTable tArticle = list.getSelectedRowAccessors().get(0).getTable(); |
final SwingWorker<SQLRowValues, String> wworker = new SwingWorker<SQLRowValues, String>() { |
@Override |
protected SQLRowValues doInBackground() throws Exception { |
SQLRowValues rArticle = new SQLRowValues(tArticle); |
rArticle.putNulls(tArticle.getFieldsName()); |
for (SQLField f : tArticle.getFields()) { |
if (f.getName().startsWith("ID_ARTICLE_DECLINAISON_")) { |
SQLRowValues rowValsDecl = rArticle.putRowValues(f.getName()); |
rowValsDecl.putNulls(f.getForeignTable().getFieldsName()); |
} |
} |
final List<SQLRowValues> fetchRow = SQLRowValuesListFetcher.create(rArticle).fetch(new Where(tArticle.getKey(), "=", idProduct)); |
return fetchRow.get(0); |
} |
@Override |
protected void done() { |
try { |
final SQLRowValues values = get(); |
p.initUI(values); |
f.setTitle(entry.getKey()); |
f.setContentPane(p); |
f.pack(); |
f.setLocationRelativeTo(null); |
f.setVisible(true); |
} catch (Exception e) { |
ExceptionHandler.handle("Erreur d'impression", e); |
} |
} |
}; |
wworker.execute(); |
} |
}, true, false); |
action.setPredicate(IListeEvent.createSelectionCountPredicate(1, 1)); |
ctxt.getElement("ARTICLE").getRowActions().add(action); |
} |
} |
if (!gplTemplates.isEmpty()) { |
for (final Entry<String, String> entry : gplTemplates.entrySet()) { |
final String gpl = entry.getValue(); |
final PredicateRowAction action = new PredicateRowAction(new AbstractAction("Imprimer l'étiquette " + entry.getKey()) { |
@Override |
public void actionPerformed(ActionEvent arg0) { |
final GPLPrinterPanel p = new GPLPrinterPanel(gpl, getDir(entry.getKey())); |
final JFrame f = new JFrame(); |
final IListe list = IListe.get(arg0); |
final int idProduct = list.getSelection().getSelectedID(); |
final SQLTable tArticle = list.getSelectedRowAccessors().get(0).getTable(); |
final SwingWorker<SQLRowValues, String> wworker = new SwingWorker<SQLRowValues, String>() { |
@Override |
protected SQLRowValues doInBackground() throws Exception { |
final SQLRow row = tArticle.getRow(idProduct); |
row.fetchValues(); |
return row.asRowValues(); |
253,6 → 314,10 |
} |
protected File getDir(String value) { |
return dirMap.get(value); |
} |
@Override |
protected void start() { |
264,23 → 329,44 |
System.err.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath()); |
File[] files = templatesDir.listFiles(); |
if (files != null) { |
LinkedHashMap<String, String> map = new LinkedHashMap<>(); |
LinkedHashMap<String, String> zmap = new LinkedHashMap<>(); |
LinkedHashMap<String, String> gmap = new LinkedHashMap<>(); |
for (File f : files) { |
if (f.getName().endsWith(".zpl")) { |
try { |
String zpl = FileUtils.read(f, StandardCharsets.UTF_8); |
String name = f.getName().substring(0, f.getName().length() - 4).trim(); |
map.put(name, zpl); |
zmap.put(name, zpl); |
dirMap.put(name, templatesDir); |
System.err.println("ModuleLabel.readTemplates() add " + name); |
} catch (Exception e) { |
System.err.println(this.getClass().getCanonicalName() + "start() cannot read zpl template : " + f.getAbsolutePath() + " : " + e.getMessage()); |
} |
} else if (f.getName().endsWith(".graphicspl")) { |
try { |
String zpl = FileUtils.read(f, StandardCharsets.UTF_8); |
String name = f.getName().substring(0, f.getName().length() - ".graphicspl".length()).trim(); |
gmap.put(name, zpl); |
dirMap.put(name, templatesDir); |
} catch (Exception e) { |
System.err.println(this.getClass().getCanonicalName() + "start() cannot read graphicspl template : " + f.getAbsolutePath() + " : " + e.getMessage()); |
} |
} |
} |
// Tri de la map par clef |
final TreeMap<String, String> copy = new TreeMap<>(map); |
this.zplTemplates.clear(); |
this.zplTemplates.putAll(copy); |
final TreeMap<String, String> copy1 = new TreeMap<>(zmap); |
zplTemplates.clear(); |
zplTemplates.putAll(copy1); |
final TreeMap<String, String> copy2 = new TreeMap<>(gmap); |
gplTemplates.clear(); |
gplTemplates.putAll(copy2); |
} |
} else { |
/trunk/Modules/Module Label/src/org/openconcerto/modules/label/ZPLPrinterPanel.java |
---|
13,6 → 13,7 |
import java.io.FileOutputStream; |
import java.io.IOException; |
import java.io.StringReader; |
import java.math.BigDecimal; |
import java.net.Socket; |
import java.nio.charset.StandardCharsets; |
import java.text.DecimalFormat; |
89,25 → 90,26 |
public ZPLPrinterPanel(String zpl) { |
this.zpl = zpl; |
this.variables = getVariables(zpl); |
knownVariables.add("product.code"); |
knownVariables.add("product.name"); |
knownVariables.add("product.material"); |
knownVariables.add("product.ean13"); |
knownVariables.add("product.price"); |
knownVariables.add("product.pricewithtax"); |
this.knownVariables.add("product.code"); |
this.knownVariables.add("product.name"); |
this.knownVariables.add("product.material"); |
this.knownVariables.add("product.color"); |
this.knownVariables.add("product.size"); |
this.knownVariables.add("product.ean13"); |
this.knownVariables.add("product.price"); |
this.knownVariables.add("product.pricewithtax"); |
// |
mapName.put("product.name", "Nom"); |
mapName.put("product.code", "Code"); |
mapName.put("product.ean13", "Code à barres"); |
mapName.put("product.price", "Prix HT"); |
mapName.put("product.pricewithtax", "Prix TTC"); |
mapName.put("product.treatment", "Traitement"); |
mapName.put("product.origin", "Origine"); |
mapName.put("product.batch", "Lot"); |
mapName.put("product.size", "Taille"); |
mapName.put("product.color", "Couleur"); |
mapName.put("product.material", "Matière"); |
this.mapName.put("product.name", "Nom"); |
this.mapName.put("product.code", "Code"); |
this.mapName.put("product.ean13", "Code à barres"); |
this.mapName.put("product.price", "Prix HT"); |
this.mapName.put("product.pricewithtax", "Prix TTC"); |
this.mapName.put("product.treatment", "Traitement"); |
this.mapName.put("product.origin", "Origine"); |
this.mapName.put("product.batch", "Lot"); |
this.mapName.put("product.size", "Taille"); |
this.mapName.put("product.color", "Couleur"); |
this.mapName.put("product.material", "Matière"); |
} |
private final File getPrefFile() { |
128,7 → 130,7 |
System.out.println(file.getAbsolutePath()); |
if (file.exists()) { |
try { |
properties.load(new FileInputStream(file)); |
this.properties.load(new FileInputStream(file)); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
143,7 → 145,7 |
Set<String> added = new HashSet<>(); |
for (String v : this.knownVariables) { |
if (variables.contains(v)) { |
if (this.variables.contains(v)) { |
// Non editable |
String label = getName(v); |
c.gridx = 0; |
158,7 → 160,7 |
txt.setText(value); |
txt.setEditable(false); |
} |
editorMap.put(v, txt); |
this.editorMap.put(v, txt); |
this.add(txt, c); |
added.add(v); |
c.gridy++; |
174,7 → 176,7 |
c.gridx++; |
c.weightx = 1; |
JTextField txt = new JTextField(20); |
editorMap.put(v, txt); |
this.editorMap.put(v, txt); |
this.add(txt, c); |
added.add(v); |
c.gridy++; |
233,15 → 235,15 |
c.anchor = GridBagConstraints.SOUTHEAST; |
// Restore state from properties |
if (properties.getOrDefault("printerType", "local").equals("local")) { |
if (this.properties.getOrDefault("printerType", "local").equals("local")) { |
radioLocalPrinter.setSelected(true); |
} else { |
radioNetworkPrinter.setSelected(true); |
} |
textPrinterIP.setText(properties.getOrDefault("printerIp", "").toString()); |
portZPL.setValue(Long.parseLong(properties.getOrDefault("printerPort", "9100").toString())); |
nbLabels.setValue(Long.parseLong(properties.getOrDefault("nbLabels", "1").toString())); |
delayLabels.setValue(Long.parseLong(properties.getOrDefault("delay", "800").toString())); |
textPrinterIP.setText(this.properties.getOrDefault("printerIp", "").toString()); |
portZPL.setValue(Long.parseLong(this.properties.getOrDefault("printerPort", "9100").toString())); |
nbLabels.setValue(Long.parseLong(this.properties.getOrDefault("nbLabels", "1").toString())); |
delayLabels.setValue(Long.parseLong(this.properties.getOrDefault("delay", "800").toString())); |
JButton printButton = new JButton("Imprimer"); |
252,16 → 254,16 |
public void actionPerformed(ActionEvent e) { |
try { |
if (radioLocalPrinter.isSelected()) { |
properties.put("printerType", "local"); |
ZPLPrinterPanel.this.properties.put("printerType", "local"); |
} else { |
properties.put("printerType", "network"); |
properties.put("printerIp", textPrinterIP.getText()); |
properties.put("printerPort", portZPL.getValue().toString()); |
ZPLPrinterPanel.this.properties.put("printerType", "network"); |
ZPLPrinterPanel.this.properties.put("printerIp", textPrinterIP.getText()); |
ZPLPrinterPanel.this.properties.put("printerPort", portZPL.getValue().toString()); |
} |
properties.put("nbLabels", nbLabels.getValue().toString()); |
properties.put("delay", delayLabels.getValue().toString()); |
ZPLPrinterPanel.this.properties.put("nbLabels", nbLabels.getValue().toString()); |
ZPLPrinterPanel.this.properties.put("delay", delayLabels.getValue().toString()); |
// Save Prefs |
properties.store(new FileOutputStream(getPrefFile()), ""); |
ZPLPrinterPanel.this.properties.store(new FileOutputStream(getPrefFile()), ""); |
} catch (Exception e1) { |
ExceptionHandler.handle("Erreur de sauvegarde de " + getPrefFile().getAbsolutePath(), e1); |
} |
296,10 → 298,10 |
final PrinterJob pj1 = PrinterJob.getPrinterJob(); |
if (pj1.printDialog()) { |
final PrintService ps = pj1.getPrintService(); |
final int nb = ((Number) nbLabels.getValue()).intValue(); |
for (int i = 0; i < nb; i++) { |
final DocPrintJob pj = ps.createPrintJob(); |
final SimpleDoc doc = new SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, null); |
final int nb = ((Number) nbLabels.getValue()).intValue(); |
for (int i = 0; i < nb; i++) { |
pj.print(doc, null); |
Thread.sleep(((Number) delayLabels.getValue()).intValue()); |
} |
357,6 → 359,7 |
if (row == null) { |
return null; |
} |
System.err.println("ZPLPrinterPanel.getValueAsString()" + variableName + " : " + row); |
if (variableName.equals("product.code")) { |
return row.getString("CODE"); |
} else if (variableName.equals("product.name")) { |
364,17 → 367,31 |
} else if (variableName.equals("product.ean13")) { |
return row.getString("CODE_BARRE"); |
} else if (variableName.equals("product.price")) { |
return new DecimalFormat("#0.00").format(row.getBigDecimal("PV_HT")); |
final BigDecimal bigDecimal = row.getBigDecimal("PV_HT"); |
return new DecimalFormat("#0.00").format(bigDecimal); |
} else if (variableName.equals("product.pricewithtax")) { |
return new DecimalFormat("#0.00").format(row.getBigDecimal("PV_TTC")); |
final BigDecimal bigDecimal = row.getBigDecimal("PV_TTC"); |
return new DecimalFormat("#0.00").format(bigDecimal); |
} else if (variableName.equals("product.material")) { |
return row.getString("MATIERE"); |
} else if (variableName.equals("product.color")) { |
if (row.getTable().contains("ID_ARTICLE_DECLINAISON_COULEUR")) { |
if (row.getObject("ID_ARTICLE_DECLINAISON_COULEUR") != null && !row.isForeignEmpty("ID_ARTICLE_DECLINAISON_COULEUR")) { |
return row.getForeign("ID_ARTICLE_DECLINAISON_COULEUR").getString("NOM"); |
} |
} |
} else if (variableName.equals("product.size")) { |
if (row.getTable().contains("ID_ARTICLE_DECLINAISON_TAILLE")) { |
if (row.getObject("ID_ARTICLE_DECLINAISON_TAILLE") != null && !row.isForeignEmpty("ID_ARTICLE_DECLINAISON_TAILLE")) { |
return row.getForeign("ID_ARTICLE_DECLINAISON_TAILLE").getString("NOM"); |
} |
} |
} |
return ""; |
} |
public String getName(String variableName) { |
String n = mapName.get(variableName); |
String n = this.mapName.get(variableName); |
if (n == null) { |
return variableName; |
} |