OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 140 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
140 ilm 1
package org.openconcerto.modules.label;
2
 
3
import java.awt.Color;
4
import java.awt.Graphics;
5
import java.awt.event.ActionEvent;
6
import java.awt.geom.Rectangle2D;
181 ilm 7
import java.io.File;
140 ilm 8
import java.io.IOException;
181 ilm 9
import java.nio.charset.StandardCharsets;
10
import java.util.ArrayList;
11
import java.util.LinkedHashMap;
140 ilm 12
import java.util.List;
181 ilm 13
import java.util.Map.Entry;
14
import java.util.TreeMap;
140 ilm 15
 
16
import javax.swing.AbstractAction;
181 ilm 17
import javax.swing.JFrame;
140 ilm 18
import javax.swing.SwingWorker;
19
 
20
import org.openconcerto.erp.generationDoc.provider.AdresseFullClientValueProvider;
21
import org.openconcerto.erp.modules.AbstractModule;
22
import org.openconcerto.erp.modules.ComponentsContext;
23
import org.openconcerto.erp.modules.ModuleFactory;
181 ilm 24
import org.openconcerto.sql.model.SQLRow;
140 ilm 25
import org.openconcerto.sql.model.SQLRowAccessor;
26
import org.openconcerto.sql.model.SQLRowValues;
27
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
28
import org.openconcerto.sql.model.SQLTable;
29
import org.openconcerto.sql.model.Where;
30
import org.openconcerto.sql.view.list.IListe;
31
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
32
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
33
import org.openconcerto.utils.ExceptionHandler;
181 ilm 34
import org.openconcerto.utils.FileUtils;
140 ilm 35
import org.openconcerto.utils.GestionDevise;
36
import org.openconcerto.utils.StringUtils;
37
 
38
public final class ModuleLabel extends AbstractModule {
181 ilm 39
    final LinkedHashMap<String, String> zplTemplates = new LinkedHashMap<String, String>();
140 ilm 40
 
41
    public ModuleLabel(ModuleFactory f) throws IOException {
42
        super(f);
43
    }
44
 
45
    @Override
46
    protected void setupComponents(ComponentsContext ctxt) {
181 ilm 47
        readTemplates(new File("Template/Labels"));
48
        readTemplates(new File("Configuration/Template/Labels"));
49
 
140 ilm 50
        final String actionName = "Imprimer les étiquettes";
51
        final PredicateRowAction aArticle = new PredicateRowAction(new AbstractAction(actionName) {
52
 
53
            @Override
54
            public void actionPerformed(ActionEvent arg0) {
55
                final IListe list = IListe.get(arg0);
56
                final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
181 ilm 57
                final SQLTable tArticle = list.getSelectedRows().get(0).getTable();
58
                final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() {
140 ilm 59
 
60
                    @Override
181 ilm 61
                    protected List<RowValuesLabel> doInBackground() throws Exception {
140 ilm 62
                        final SQLRowValues graph = new SQLRowValues(tArticle);
63
                        graph.putNulls("NOM", "PV_TTC");
64
                        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
181 ilm 65
                        final List<SQLRowValues> rows = fetcher.fetch(new Where(tArticle.getKey(), selectedIDs));
66
                        final List<RowValuesLabel> list = new ArrayList<>(rows.size());
67
                        for (SQLRowValues row : rows) {
68
                            list.add(new RowValuesLabel(row));
69
                        }
70
                        return list;
140 ilm 71
                    }
72
 
181 ilm 73
                    @Override
140 ilm 74
                    protected void done() {
75
                        try {
181 ilm 76
                            final List<RowValuesLabel> values = get();
140 ilm 77
 
78
                            final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
79
 
80
                                @Override
181 ilm 81
                                public void paintLabel(Graphics g, Label label, int x, int y, int gridWith, int gridHeight, float fontSize) {
140 ilm 82
                                    g.setColor(Color.BLACK);
83
                                    g.setFont(g.getFont().deriveFont(fontSize));
84
                                    // Labels borders
85
                                    final int hBorder = 12;
86
                                    final int vBorder = 8;
87
                                    // Product name
181 ilm 88
                                    SQLRowValues row = ((RowValuesLabel) label).getSQLRowValues();
140 ilm 89
                                    final String text = row.getString("NOM");
90
                                    final List<String> l = StringUtils.wrap(text, g.getFontMetrics(), gridWith - 2 * hBorder);
91
                                    final int lineHeight = g.getFontMetrics().getHeight();
92
                                    int lineY = y;
93
                                    final int margin = gridHeight - l.size() * lineHeight;
94
                                    if (margin > 0) {
181 ilm 95
                                        lineY += margin / 2;
140 ilm 96
                                    }
97
                                    for (String line : l) {
98
                                        g.drawString(line, x + hBorder, lineY);
99
                                        lineY += lineHeight;
100
                                    }
101
                                    // Price
102
                                    g.setFont(g.getFont().deriveFont(fontSize + 2));
103
                                    final String price = GestionDevise.currencyToString(row.getBigDecimal("PV_TTC")) + " € TTC";
104
                                    final Rectangle2D r2 = g.getFont().getStringBounds(price, g.getFontMetrics().getFontRenderContext());
105
                                    g.drawString(price, x + (int) (gridWith - hBorder - r2.getWidth()), y + gridHeight - vBorder);
106
 
107
                                }
108
                            });
109
                            f.setTitle(actionName);
110
                            f.setLocationRelativeTo(null);
111
                            f.pack();
112
                            f.setResizable(false);
113
                            f.setVisible(true);
114
                        } catch (Exception e) {
115
                            ExceptionHandler.handle("Erreur d'impression", e);
116
                        }
181 ilm 117
                    }
140 ilm 118
                };
119
                wworker.execute();
120
 
121
            }
122
        }, true, false);
123
        final PredicateRowAction aClient = new PredicateRowAction(new AbstractAction(actionName) {
124
 
125
            @Override
126
            public void actionPerformed(ActionEvent arg0) {
127
                final IListe list = IListe.get(arg0);
128
                final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
181 ilm 129
                final SQLTable tClient = list.getSelectedRows().get(0).getTable();
130
                final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() {
140 ilm 131
 
132
                    @Override
181 ilm 133
                    protected List<RowValuesLabel> doInBackground() throws Exception {
140 ilm 134
                        final SQLRowValues graph = new SQLRowValues(tClient);
135
                        graph.putNulls("NOM");
136
                        final SQLRowValues a1 = graph.putRowValues("ID_ADRESSE");
137
                        a1.putNulls(a1.getTable().getFieldsName());
138
                        final SQLRowValues a2 = graph.putRowValues("ID_ADRESSE_L");
139
                        a2.putNulls(a2.getTable().getFieldsName());
140
                        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
181 ilm 141
                        final List<SQLRowValues> rows = fetcher.fetch(new Where(tClient.getKey(), selectedIDs));
142
 
143
                        final List<RowValuesLabel> list = new ArrayList<>(rows.size());
144
                        for (SQLRowValues row : rows) {
145
                            list.add(new RowValuesLabel(row));
146
                        }
147
                        return list;
140 ilm 148
                    }
149
 
181 ilm 150
                    @Override
140 ilm 151
                    protected void done() {
152
                        try {
181 ilm 153
                            final List<RowValuesLabel> values = get();
140 ilm 154
                            final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
155
 
156
                                @Override
181 ilm 157
                                public void paintLabel(Graphics g, Label label, int x, int y, int gridWith, int gridHeight, float fontSize) {
158
                                    SQLRowValues row = ((RowValuesLabel) label).getSQLRowValues();
140 ilm 159
                                    SQLRowAccessor rAddr = row.getForeign("ID_ADRESSE_L");
160
                                    if (rAddr == null || rAddr.isUndefined()) {
161
                                        rAddr = row.getForeign("ID_ADRESSE");
162
                                    }
163
                                    if (rAddr == null || rAddr.isUndefined()) {
164
                                        return;
165
                                    }
166
 
167
                                    String text = AdresseFullClientValueProvider.getFormattedAddress(rAddr, row.getString("NOM") + "\n");
168
 
169
                                    // Default font at 10pt black
170
                                    g.setColor(Color.BLACK);
171
                                    g.setFont(g.getFont().deriveFont(fontSize));
172
                                    // Labels borders
173
                                    final int hBorder = 12;
174
                                    // Product name
175
                                    final List<String> l = StringUtils.wrap(text, g.getFontMetrics(), gridWith - 2 * hBorder);
176
                                    final int lineHeight = g.getFontMetrics().getHeight();
177
                                    int lineY = y + lineHeight + 4;
178
                                    for (String line : l) {
179
                                        g.drawString(line, x + hBorder, lineY);
180
                                        lineY += lineHeight;
181
                                    }
182
 
183
                                }
184
                            });
185
                            f.setTitle(actionName);
186
                            f.setLocationRelativeTo(null);
187
                            f.pack();
188
                            f.setResizable(false);
189
                            f.setVisible(true);
190
 
191
                        } catch (Exception e) {
192
                            ExceptionHandler.handle("Erreur d'impression", e);
193
                        }
181 ilm 194
                    }
140 ilm 195
                };
196
                wworker.execute();
197
 
198
            }
199
        }, true, false);
200
 
201
        aArticle.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
202
        aClient.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
203
        ctxt.getElement("ARTICLE").getRowActions().add(aArticle);
204
        ctxt.getElement("CLIENT").getRowActions().add(aClient);
181 ilm 205
 
206
        if (!this.zplTemplates.isEmpty()) {
207
            for (final Entry<String, String> entry : this.zplTemplates.entrySet()) {
208
                final String zpl = entry.getValue();
209
                final PredicateRowAction action = new PredicateRowAction(new AbstractAction("Imprimer l'étiquette " + entry.getKey()) {
210
 
211
                    @Override
212
                    public void actionPerformed(ActionEvent arg0) {
213
                        final ZPLPrinterPanel p = new ZPLPrinterPanel(zpl);
214
                        final JFrame f = new JFrame();
215
                        final IListe list = IListe.get(arg0);
216
                        final int idProduct = list.getSelection().getSelectedID();
217
                        final SQLTable tArticle = list.getSelectedRows().get(0).getTable();
218
 
219
                        final SwingWorker<SQLRowValues, String> wworker = new SwingWorker<SQLRowValues, String>() {
220
 
221
                            @Override
222
                            protected SQLRowValues doInBackground() throws Exception {
223
                                final SQLRow row = tArticle.getRow(idProduct);
224
                                row.fetchValues();
225
                                return row.asRowValues();
226
                            }
227
 
228
                            @Override
229
                            protected void done() {
230
                                try {
231
                                    final SQLRowValues values = get();
232
                                    p.initUI(values);
233
                                    f.setTitle(entry.getKey());
234
                                    f.setContentPane(p);
235
                                    f.pack();
236
                                    f.setLocationRelativeTo(null);
237
                                    f.setVisible(true);
238
 
239
                                } catch (Exception e) {
240
                                    ExceptionHandler.handle("Erreur d'impression", e);
241
                                }
242
                            }
243
                        };
244
                        wworker.execute();
245
 
246
                    }
247
                }, true, false);
248
 
249
                action.setPredicate(IListeEvent.createSelectionCountPredicate(1, 1));
250
                ctxt.getElement("ARTICLE").getRowActions().add(action);
251
            }
252
        }
253
 
140 ilm 254
    }
255
 
256
    @Override
257
    protected void start() {
181 ilm 258
 
140 ilm 259
    }
260
 
181 ilm 261
    private void readTemplates(File templatesDir) {
262
        System.out.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath());
263
        if (templatesDir.exists() && templatesDir.isDirectory()) {
264
            System.err.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath());
265
            File[] files = templatesDir.listFiles();
266
            if (files != null) {
267
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
268
                for (File f : files) {
269
                    if (f.getName().endsWith(".zpl")) {
270
                        try {
271
                            String zpl = FileUtils.read(f, StandardCharsets.UTF_8);
272
                            String name = f.getName().substring(0, f.getName().length() - 4).trim();
273
                            map.put(name, zpl);
274
                            System.err.println("ModuleLabel.readTemplates() add " + name);
275
                        } catch (Exception e) {
276
                            System.err.println(this.getClass().getCanonicalName() + "start() cannot read zpl template : " + f.getAbsolutePath() + " : " + e.getMessage());
277
                        }
278
                    }
279
                }
280
                // Tri de la map par clef
281
                final TreeMap<String, String> copy = new TreeMap<>(map);
282
                this.zplTemplates.clear();
283
                this.zplTemplates.putAll(copy);
284
            }
285
 
286
        } else {
287
            System.err.println("ModuleLabel.readTemplates() " + templatesDir.getAbsolutePath() + " missing");
288
        }
289
    }
290
 
140 ilm 291
    @Override
292
    protected void stop() {
181 ilm 293
        // nothing
140 ilm 294
    }
295
}