OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 140 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 140 Rev 181
Line 2... Line 2...
2
 
2
 
3
import java.awt.Color;
3
import java.awt.Color;
4
import java.awt.Graphics;
4
import java.awt.Graphics;
5
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionEvent;
6
import java.awt.geom.Rectangle2D;
6
import java.awt.geom.Rectangle2D;
-
 
7
import java.io.File;
7
import java.io.IOException;
8
import java.io.IOException;
-
 
9
import java.nio.charset.StandardCharsets;
-
 
10
import java.util.ArrayList;
-
 
11
import java.util.LinkedHashMap;
8
import java.util.List;
12
import java.util.List;
-
 
13
import java.util.Map.Entry;
-
 
14
import java.util.TreeMap;
9
 
15
 
10
import javax.swing.AbstractAction;
16
import javax.swing.AbstractAction;
-
 
17
import javax.swing.JFrame;
11
import javax.swing.SwingWorker;
18
import javax.swing.SwingWorker;
12
 
19
 
13
import org.openconcerto.erp.generationDoc.provider.AdresseFullClientValueProvider;
20
import org.openconcerto.erp.generationDoc.provider.AdresseFullClientValueProvider;
14
import org.openconcerto.erp.modules.AbstractModule;
21
import org.openconcerto.erp.modules.AbstractModule;
15
import org.openconcerto.erp.modules.ComponentsContext;
22
import org.openconcerto.erp.modules.ComponentsContext;
16
import org.openconcerto.erp.modules.ModuleFactory;
23
import org.openconcerto.erp.modules.ModuleFactory;
-
 
24
import org.openconcerto.sql.model.SQLRow;
17
import org.openconcerto.sql.model.SQLRowAccessor;
25
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
26
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
27
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
20
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.sql.model.Where;
22
import org.openconcerto.sql.view.list.IListe;
30
import org.openconcerto.sql.view.list.IListe;
23
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
31
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
24
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
32
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
25
import org.openconcerto.utils.ExceptionHandler;
33
import org.openconcerto.utils.ExceptionHandler;
-
 
34
import org.openconcerto.utils.FileUtils;
26
import org.openconcerto.utils.GestionDevise;
35
import org.openconcerto.utils.GestionDevise;
27
import org.openconcerto.utils.StringUtils;
36
import org.openconcerto.utils.StringUtils;
28
 
37
 
29
public final class ModuleLabel extends AbstractModule {
38
public final class ModuleLabel extends AbstractModule {
-
 
39
    final LinkedHashMap<String, String> zplTemplates = new LinkedHashMap<String, String>();
30
 
40
 
31
    public ModuleLabel(ModuleFactory f) throws IOException {
41
    public ModuleLabel(ModuleFactory f) throws IOException {
32
        super(f);
42
        super(f);
33
    }
43
    }
34
 
44
 
35
    @Override
45
    @Override
36
    protected void setupComponents(ComponentsContext ctxt) {
46
    protected void setupComponents(ComponentsContext ctxt) {
-
 
47
        readTemplates(new File("Template/Labels"));
-
 
48
        readTemplates(new File("Configuration/Template/Labels"));
-
 
49
 
37
        final String actionName = "Imprimer les étiquettes";
50
        final String actionName = "Imprimer les étiquettes";
38
        final PredicateRowAction aArticle = new PredicateRowAction(new AbstractAction(actionName) {
51
        final PredicateRowAction aArticle = new PredicateRowAction(new AbstractAction(actionName) {
39
 
52
 
40
            @Override
53
            @Override
41
            public void actionPerformed(ActionEvent arg0) {
54
            public void actionPerformed(ActionEvent arg0) {
42
                final IListe list = IListe.get(arg0);
55
                final IListe list = IListe.get(arg0);
43
                final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
56
                final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
-
 
57
                final SQLTable tArticle = list.getSelectedRows().get(0).getTable();
44
                final SwingWorker<List<SQLRowValues>, String> wworker = new SwingWorker<List<SQLRowValues>, String>() {
58
                final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() {
45
 
59
 
46
                    @Override
60
                    @Override
47
                    protected List<SQLRowValues> doInBackground() throws Exception {
61
                    protected List<RowValuesLabel> doInBackground() throws Exception {
48
                        final SQLTable tArticle = list.getSelectedRows().get(0).getTable();
-
 
49
                        final SQLRowValues graph = new SQLRowValues(tArticle);
62
                        final SQLRowValues graph = new SQLRowValues(tArticle);
50
                        graph.putNulls("NOM", "PV_TTC");
63
                        graph.putNulls("NOM", "PV_TTC");
51
                        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
64
                        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
52
                        final List<SQLRowValues> values = fetcher.fetch(new Where(tArticle.getKey(), selectedIDs));
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
                        }
53
                        return values;
70
                        return list;
54
                    }
71
                    }
55
 
72
 
-
 
73
                    @Override
56
                    protected void done() {
74
                    protected void done() {
57
                        try {
75
                        try {
58
                            final List<SQLRowValues> values = get();
76
                            final List<RowValuesLabel> values = get();
59
 
77
 
60
                            final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
78
                            final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
61
 
79
 
62
                                @Override
80
                                @Override
63
                                public void paintLabel(Graphics g, SQLRowAccessor row, int x, int y, int gridWith, int gridHeight, float fontSize) {
81
                                public void paintLabel(Graphics g, Label label, int x, int y, int gridWith, int gridHeight, float fontSize) {
64
                                    g.setColor(Color.BLACK);
82
                                    g.setColor(Color.BLACK);
65
                                    g.setFont(g.getFont().deriveFont(fontSize));
83
                                    g.setFont(g.getFont().deriveFont(fontSize));
66
                                    // Labels borders
84
                                    // Labels borders
67
                                    final int hBorder = 12;
85
                                    final int hBorder = 12;
68
                                    final int vBorder = 8;
86
                                    final int vBorder = 8;
69
                                    // Product name
87
                                    // Product name
-
 
88
                                    SQLRowValues row = ((RowValuesLabel) label).getSQLRowValues();
70
                                    final String text = row.getString("NOM");
89
                                    final String text = row.getString("NOM");
71
                                    final List<String> l = StringUtils.wrap(text, g.getFontMetrics(), gridWith - 2 * hBorder);
90
                                    final List<String> l = StringUtils.wrap(text, g.getFontMetrics(), gridWith - 2 * hBorder);
72
                                    final int lineHeight = g.getFontMetrics().getHeight();
91
                                    final int lineHeight = g.getFontMetrics().getHeight();
73
                                    int lineY = y;
92
                                    int lineY = y;
74
                                    final int margin = gridHeight - l.size() * lineHeight;
93
                                    final int margin = gridHeight - l.size() * lineHeight;
75
                                    if (margin > 0) {
94
                                    if (margin > 0) {
76
                                        lineY += (int) (margin / 2);
95
                                        lineY += margin / 2;
77
                                    }
96
                                    }
78
                                    for (String line : l) {
97
                                    for (String line : l) {
79
                                        g.drawString(line, x + hBorder, lineY);
98
                                        g.drawString(line, x + hBorder, lineY);
80
                                        lineY += lineHeight;
99
                                        lineY += lineHeight;
81
                                    }
100
                                    }
Line 93... Line 112...
93
                            f.setResizable(false);
112
                            f.setResizable(false);
94
                            f.setVisible(true);
113
                            f.setVisible(true);
95
                        } catch (Exception e) {
114
                        } catch (Exception e) {
96
                            ExceptionHandler.handle("Erreur d'impression", e);
115
                            ExceptionHandler.handle("Erreur d'impression", e);
97
                        }
116
                        }
98
                    };
117
                    }
99
                };
118
                };
100
                wworker.execute();
119
                wworker.execute();
101
 
120
 
102
            }
121
            }
103
        }, true, false);
122
        }, true, false);
Line 105... Line 124...
105
 
124
 
106
            @Override
125
            @Override
107
            public void actionPerformed(ActionEvent arg0) {
126
            public void actionPerformed(ActionEvent arg0) {
108
                final IListe list = IListe.get(arg0);
127
                final IListe list = IListe.get(arg0);
109
                final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
128
                final List<Integer> selectedIDs = list.getSelection().getSelectedIDs();
-
 
129
                final SQLTable tClient = list.getSelectedRows().get(0).getTable();
110
                final SwingWorker<List<SQLRowValues>, String> wworker = new SwingWorker<List<SQLRowValues>, String>() {
130
                final SwingWorker<List<RowValuesLabel>, String> wworker = new SwingWorker<List<RowValuesLabel>, String>() {
111
 
131
 
112
                    @Override
132
                    @Override
113
                    protected List<SQLRowValues> doInBackground() throws Exception {
133
                    protected List<RowValuesLabel> doInBackground() throws Exception {
114
                        final SQLTable tClient = list.getSelectedRows().get(0).getTable();
-
 
115
                        final SQLRowValues graph = new SQLRowValues(tClient);
134
                        final SQLRowValues graph = new SQLRowValues(tClient);
116
                        graph.putNulls("NOM");
135
                        graph.putNulls("NOM");
117
                        final SQLRowValues a1 = graph.putRowValues("ID_ADRESSE");
136
                        final SQLRowValues a1 = graph.putRowValues("ID_ADRESSE");
118
                        a1.putNulls(a1.getTable().getFieldsName());
137
                        a1.putNulls(a1.getTable().getFieldsName());
119
                        final SQLRowValues a2 = graph.putRowValues("ID_ADRESSE_L");
138
                        final SQLRowValues a2 = graph.putRowValues("ID_ADRESSE_L");
120
                        a2.putNulls(a2.getTable().getFieldsName());
139
                        a2.putNulls(a2.getTable().getFieldsName());
121
                        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
140
                        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(graph);
122
                        final List<SQLRowValues> values = fetcher.fetch(new Where(tClient.getKey(), selectedIDs));
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
                        }
123
                        return values;
147
                        return list;
124
                    }
148
                    }
125
 
149
 
-
 
150
                    @Override
126
                    protected void done() {
151
                    protected void done() {
127
                        try {
152
                        try {
128
                            final List<SQLRowValues> values = get();
153
                            final List<RowValuesLabel> values = get();
129
                            final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
154
                            final LabelFrame f = new LabelFrame(values, new LabelRenderer() {
130
 
155
 
131
                                @Override
156
                                @Override
132
                                public void paintLabel(Graphics g, SQLRowAccessor row, int x, int y, int gridWith, int gridHeight, float fontSize) {
157
                                public void paintLabel(Graphics g, Label label, int x, int y, int gridWith, int gridHeight, float fontSize) {
-
 
158
                                    SQLRowValues row = ((RowValuesLabel) label).getSQLRowValues();
133
                                    SQLRowAccessor rAddr = row.getForeign("ID_ADRESSE_L");
159
                                    SQLRowAccessor rAddr = row.getForeign("ID_ADRESSE_L");
134
                                    if (rAddr == null || rAddr.isUndefined()) {
160
                                    if (rAddr == null || rAddr.isUndefined()) {
135
                                        rAddr = row.getForeign("ID_ADRESSE");
161
                                        rAddr = row.getForeign("ID_ADRESSE");
136
                                    }
162
                                    }
137
                                    if (rAddr == null || rAddr.isUndefined()) {
163
                                    if (rAddr == null || rAddr.isUndefined()) {
Line 163... Line 189...
163
                            f.setVisible(true);
189
                            f.setVisible(true);
164
 
190
 
165
                        } catch (Exception e) {
191
                        } catch (Exception e) {
166
                            ExceptionHandler.handle("Erreur d'impression", e);
192
                            ExceptionHandler.handle("Erreur d'impression", e);
167
                        }
193
                        }
168
                    };
194
                    }
169
                };
195
                };
170
                wworker.execute();
196
                wworker.execute();
171
 
197
 
172
            }
198
            }
173
        }, true, false);
199
        }, true, false);
174
 
200
 
175
        aArticle.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
201
        aArticle.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
176
        aClient.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
202
        aClient.setPredicate(IListeEvent.createSelectionCountPredicate(1, Integer.MAX_VALUE));
177
        ctxt.getElement("ARTICLE").getRowActions().add(aArticle);
203
        ctxt.getElement("ARTICLE").getRowActions().add(aArticle);
178
        ctxt.getElement("CLIENT").getRowActions().add(aClient);
204
        ctxt.getElement("CLIENT").getRowActions().add(aClient);
-
 
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
 
179
    }
254
    }
180
 
255
 
181
    @Override
256
    @Override
182
    protected void start() {
257
    protected void start() {
-
 
258
 
-
 
259
    }
-
 
260
 
-
 
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
        }
183
    }
289
    }
184
 
290
 
185
    @Override
291
    @Override
186
    protected void stop() {
292
    protected void stop() {
-
 
293
        // nothing
187
    }
294
    }
188
}
295
}