OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder;
2
 
3
import java.awt.Color;
4
import java.awt.Dimension;
5
import java.awt.FileDialog;
6
import java.awt.FlowLayout;
7
import java.awt.GridBagConstraints;
8
import java.awt.GridBagLayout;
9
import java.awt.Insets;
10
import java.awt.event.ActionEvent;
11
import java.awt.event.ActionListener;
12
import java.io.BufferedInputStream;
13
import java.io.BufferedOutputStream;
14
import java.io.ByteArrayOutputStream;
15
import java.io.File;
16
import java.io.FileInputStream;
17
import java.io.FileOutputStream;
18
import java.io.IOException;
19
import java.nio.charset.Charset;
20
import java.sql.SQLException;
21
import java.util.List;
22
import java.util.zip.GZIPInputStream;
23
import java.util.zip.GZIPOutputStream;
24
 
25
import javax.swing.JButton;
26
import javax.swing.JFrame;
27
import javax.swing.JLabel;
28
import javax.swing.JOptionPane;
29
import javax.swing.JPanel;
30
import javax.swing.JSeparator;
31
import javax.swing.SwingUtilities;
32
import javax.swing.event.ChangeEvent;
33
import javax.swing.event.ChangeListener;
34
 
35
import org.openconcerto.erp.config.ComptaPropsConfiguration;
36
import org.openconcerto.modules.extensionbuilder.component.ComponentCreateMainPanel;
37
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
38
import org.openconcerto.modules.extensionbuilder.list.ListCreateMainPanel;
39
import org.openconcerto.modules.extensionbuilder.list.ListDescriptor;
40
import org.openconcerto.modules.extensionbuilder.menu.MenuMainPanel;
41
import org.openconcerto.modules.extensionbuilder.table.TableDescritor;
42
import org.openconcerto.modules.extensionbuilder.table.TableMainPanel;
43
import org.openconcerto.modules.extensionbuilder.translation.TranslationMainPanel;
44
import org.openconcerto.sql.model.DBRoot;
45
import org.openconcerto.ui.DefaultGridBagConstraints;
46
import org.openconcerto.ui.FrameUtil;
47
import org.openconcerto.ui.JLabelBold;
48
import org.openconcerto.ui.ReloadPanel;
49
import org.openconcerto.ui.warning.JLabelWarning;
50
import org.openconcerto.utils.ExceptionHandler;
51
import org.openconcerto.utils.StreamUtils;
52
 
53
public class ExtensionInfoPanel extends JPanel implements ChangeListener {
54
    private Extension extension;
55
 
56
    private JFrame frameTableEditor;
57
 
58
    private JFrame frameListEditor;
59
    private JFrame frameMenuEditor;
60
    private JFrame frameTranslationEditor;
61
 
62
    ExtensionInfoPanel(Extension extension, ExtensionListPanel moduleListPanel) {
63
        this.extension = extension;
64
        this.extension.addChangeListener(this);
65
        reloadUI();
66
    }
67
 
68
    @Override
69
    public void stateChanged(ChangeEvent e) {
70
        SwingUtilities.invokeLater(new Runnable() {
71
 
72
            @Override
73
            public void run() {
74
                reloadUI();
75
 
76
            }
77
        });
78
 
79
    }
80
 
81
    private void reloadUI() {
82
        if (!SwingUtilities.isEventDispatchThread()) {
83
            throw new IllegalStateException("Not in AWT");
84
        }
85
        System.out.println("ExtensionInfoPanel.reloadUI()");
86
        this.setBackground(Color.WHITE);
87
        this.invalidate();
88
        this.removeAll();
89
 
90
        this.setLayout(new GridBagLayout());
91
        GridBagConstraints c = new DefaultGridBagConstraints();
92
        c.insets = new Insets(0, 0, 0, 0);
93
 
94
        this.add(createToolbar(), c);
95
        c.gridy++;
96
 
97
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
98
        c.gridy++;
99
        c.weightx = 1;
100
        c.weighty = 1;
101
        c.fill = GridBagConstraints.BOTH;
102
 
103
        this.add(createInfoPanel(), c);
104
        this.revalidate();
105
    }
106
 
107
    private JPanel createToolbar() {
108
        JPanel panel = new JPanel();
109
        panel.setLayout(new FlowLayout(FlowLayout.LEADING));
110
        panel.add(new JLabelBold(this.extension.getName()));
111
        final ReloadPanel reload = new ReloadPanel();
112
        panel.add(reload);
113
        final JButton startButton = new JButton("Démarrer");
114
 
115
        panel.add(startButton);
181 ilm 116
        startButton.setEnabled(!this.extension.isStarted());
74 ilm 117
        final JButton stopButton = new JButton("Arrêter");
118
        panel.add(stopButton);
181 ilm 119
        stopButton.setEnabled(this.extension.isStarted());
74 ilm 120
        final JButton saveButton = new JButton("Enregister");
121
        saveButton.addActionListener(new ActionListener() {
122
 
123
            @Override
124
            public void actionPerformed(ActionEvent e) {
181 ilm 125
                ExtensionInfoPanel.this.extension.save();
74 ilm 126
 
127
            }
128
        });
129
        panel.add(saveButton);
130
        // TODO: a mettre en clic droit sur la liste gauche
131
        final JButton exportButton = new JButton("Exporter");
132
        panel.add(exportButton);
133
        final JButton importButton = new JButton("Importer");
134
        panel.add(importButton);
135
 
136
        exportButton.addActionListener(new ActionListener() {
137
 
138
            @Override
139
            public void actionPerformed(ActionEvent e) {
181 ilm 140
                String xml = ExtensionInfoPanel.this.extension.toXML();
74 ilm 141
                System.out.println(xml);
142
                FileDialog fDialog = new FileDialog(new JFrame(), "Export", FileDialog.SAVE);
181 ilm 143
                fDialog.setFile(ExtensionInfoPanel.this.extension.getName() + ".ocext");
74 ilm 144
                fDialog.setVisible(true);
145
                String fileName = fDialog.getFile();
146
                if (fileName != null) {
147
                    File file = new File(fDialog.getDirectory(), fDialog.getFile());
148
                    byte[] bytes = xml.getBytes(Charset.forName("UTF-8"));
149
                    byte[] open = "openconcerto".getBytes();
150
                    final int length = bytes.length;
151
                    for (int i = 0; i < length; i++) {
152
                        bytes[i] = (byte) (bytes[i] ^ open[i % 12]);
153
                    }
154
                    BufferedOutputStream out = null;
155
                    try {
156
                        out = new BufferedOutputStream(new FileOutputStream(file));
157
                        GZIPOutputStream gZ = new GZIPOutputStream(out);
158
                        gZ.write(bytes);
159
                        gZ.close();
160
                    } catch (Exception e1) {
161
                        ExceptionHandler.handle("Unable to save extension " + file.getAbsolutePath(), e1);
162
                    } finally {
163
                        if (out != null) {
164
                            try {
165
                                out.close();
166
                            } catch (IOException e1) {
167
                                // Nothing
168
                            }
169
                        }
170
                    }
171
 
172
                }
173
 
174
            }
175
        });
176
        importButton.addActionListener(new ActionListener() {
177
 
178
            @Override
179
            public void actionPerformed(ActionEvent e) {
180
                final FileDialog fDialog = new FileDialog(new JFrame(), "Import", FileDialog.LOAD);
181
                fDialog.setVisible(true);
182
                final String fileName = fDialog.getFile();
183
                String xml = "";
184
                if (fileName != null && fileName.endsWith(".ocext")) {
185
                    File file = new File(fDialog.getDirectory(), fDialog.getFile());
186
                    if (file.exists()) {
187
                        FileInputStream fIn = null;
188
                        GZIPInputStream gIn = null;
189
                        try {
190
                            fIn = new FileInputStream(file);
191
                            BufferedInputStream bIn = new BufferedInputStream(fIn);
192
                            gIn = new GZIPInputStream(bIn);
193
                            final ByteArrayOutputStream out = new ByteArrayOutputStream();
194
                            StreamUtils.copy(gIn, out);
195
                            out.close();
196
                            // decode
197
                            byte[] bytes = out.toByteArray();
198
                            byte[] open = "openconcerto".getBytes();
199
                            final int length = bytes.length;
200
                            for (int i = 0; i < length; i++) {
201
                                bytes[i] = (byte) (bytes[i] ^ open[i % 12]);
202
                            }
203
                            xml = new String(bytes, Charset.forName("UTF-8"));
204
                        } catch (Exception ex) {
205
                            ex.printStackTrace();
206
                            JOptionPane.showMessageDialog(new JFrame(), "Invalid extension");
207
                        } finally {
208
                            if (gIn != null) {
209
                                try {
210
                                    gIn.close();
211
                                } catch (IOException e1) {
212
                                    e1.printStackTrace();
213
                                }
214
                            }
215
                            if (fIn != null) {
216
                                try {
217
                                    fIn.close();
218
                                } catch (IOException e1) {
219
                                    e1.printStackTrace();
220
                                }
221
                            }
222
                        }
223
 
224
                    }
225
 
226
                }
227
                // Create extension from XML
181 ilm 228
                ExtensionInfoPanel.this.extension.stop();
74 ilm 229
                if (!xml.isEmpty()) {
181 ilm 230
                    if (!ExtensionInfoPanel.this.extension.isEmpty()) {
74 ilm 231
                        int result = JOptionPane.showConfirmDialog(ExtensionInfoPanel.this, "Attention l'extension actuelle sera écrasée.\nContinuer l'importation?");
232
                        if (result != JOptionPane.YES_OPTION) {
233
                            return;
234
                        }
235
                    }
181 ilm 236
                    ExtensionInfoPanel.this.extension.clearAll();
237
                    ExtensionInfoPanel.this.extension.importFromXML(xml);
238
                    ExtensionInfoPanel.this.extension.setChanged();
74 ilm 239
                }
240
 
241
            }
242
        });
243
        startButton.addActionListener(new ActionListener() {
244
 
245
            @Override
246
            public void actionPerformed(ActionEvent e) {
247
                reload.setMode(ReloadPanel.MODE_ROTATE);
248
                new Thread(new Runnable() {
249
 
250
                    @Override
251
                    public void run() {
252
                        final DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
253
                        try {
181 ilm 254
                            ExtensionInfoPanel.this.extension.start(root, false);
255
                            SwingUtilities.invokeLater(new Runnable() {
256
 
257
                                @Override
258
                                public void run() {
259
                                    reload.setMode(ReloadPanel.MODE_EMPTY);
260
                                }
261
                            });
262
                        } catch (SQLException e) {
263
                            SwingUtilities.invokeLater(new Runnable() {
264
 
265
                                @Override
266
                                public void run() {
267
                                    reload.setMode(ReloadPanel.MODE_BLINK);
268
                                }
269
                            });
270
                            ExceptionHandler.handle("Start error", e);
74 ilm 271
                        }
272
                    }
273
                }).start();
274
            }
275
        });
276
        stopButton.addActionListener(new ActionListener() {
277
 
278
            @Override
279
            public void actionPerformed(ActionEvent e) {
280
                reload.setMode(ReloadPanel.MODE_ROTATE);
281
                new Thread(new Runnable() {
282
                    @Override
283
                    public void run() {
181 ilm 284
                        try {
285
                            ExtensionInfoPanel.this.extension.stop();
286
                            SwingUtilities.invokeLater(new Runnable() {
287
 
288
                                @Override
289
                                public void run() {
290
                                    reload.setMode(ReloadPanel.MODE_EMPTY);
291
                                }
292
                            });
293
                        } catch (Exception e) {
294
                            SwingUtilities.invokeLater(new Runnable() {
295
 
296
                                @Override
297
                                public void run() {
298
                                    reload.setMode(ReloadPanel.MODE_BLINK);
299
                                }
300
                            });
301
                            ExceptionHandler.handle("Stop error", e);
302
                        }
303
 
74 ilm 304
                    }
305
                }).start();
306
            }
307
        });
308
        return panel;
309
    }
310
 
311
    private JPanel createInfoPanel() {
312
        final JPanel panel = new JPanel();
313
        panel.setOpaque(false);
314
        final GridBagConstraints c = new DefaultGridBagConstraints();
315
        panel.setLayout(new GridBagLayout());
316
        c.fill = GridBagConstraints.NONE;
317
        c.gridy++;
318
        // --------- TABLES
319
        panel.add(new JLabelBold("Tables et champs de la base de données"), c);
320
        c.gridy++;
321
        final List<TableDescritor> createTableList = this.extension.getCreateTableList();
322
        if (createTableList.size() > 0) {
323
 
324
            panel.add(new JLabel("Cette extension crée " + createTableList.size() + " tables :"), c);
325
            c.gridy++;
326
            c.gridwidth = 1;
327
            for (final TableDescritor sqlCreateTable : createTableList) {
328
                c.gridx = 0;
329
                c.weightx = 0;
330
                JPanel line = new JPanel();
331
                line.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
332
                line.setOpaque(false);
333
                line.add(new JLabel("- " + sqlCreateTable.getName()));
334
                if (sqlCreateTable.getFields().size() > 0) {
335
                    line.add(new JLabel(" " + sqlCreateTable.getFields().size() + " champs"));
336
                } else {
337
                    line.add(new JLabelWarning(" " + "aucun champ"));
338
                }
339
                panel.add(line, c);
340
                c.gridy++;
341
            }
342
 
343
        }
344
        final List<TableDescritor> modifyTableList = this.extension.getModifyTableList();
345
        if (modifyTableList.size() > 0) {
346
            String tables = "";
347
            for (int i = 0; i < modifyTableList.size(); i++) {
348
                String tableName = modifyTableList.get(i).getName();
349
                if (i < modifyTableList.size() - 1) {
350
                    tables += tableName + ", ";
351
                } else {
352
                    tables += tableName;
353
                }
354
            }
355
            if (modifyTableList.size() > 1) {
356
                panel.add(new JLabel("Tables modifiées : " + tables), c);
357
            } else {
358
                panel.add(new JLabel("Table modifiée : " + tables), c);
359
            }
360
            c.gridy++;
361
        }
362
        final JButton buttonCreateTable = new JButton("Afficher les tables");
363
        panel.add(buttonCreateTable, c);
364
        c.gridy++;
365
        panel.add(new JLabel(" "), c);
366
        c.gridy++;
367
        // --------- UI
368
        panel.add(new JLabelBold("Interfaces de saisie"), c);
369
        c.gridy++;
181 ilm 370
        List<ComponentDescritor> createComponentList = this.extension.getCreateComponentList();
74 ilm 371
        if (createComponentList.size() > 0) {
372
            String components = "";
373
            for (int i = 0; i < createComponentList.size(); i++) {
374
                String tableName = createComponentList.get(i).getId();
375
                if (i < createComponentList.size() - 1) {
376
                    components += tableName + ", ";
377
                } else {
378
                    components += tableName;
379
                }
380
            }
381
 
382
            if (createComponentList.size() > 1) {
383
                panel.add(new JLabel("Interfaces créées : " + components), c);
384
            } else {
385
                panel.add(new JLabel("Interface créée : " + components), c);
386
            }
387
            c.gridy++;
388
        }
389
 
390
        final JButton buttonCreateComponent = new JButton("Afficher les interfaces");
391
        panel.add(buttonCreateComponent, c);
392
        c.gridy++;
393
        panel.add(new JLabel(" "), c);
394
        c.gridy++;
395
 
396
        // --------- LIST
397
        panel.add(new JLabelBold("Listes"), c);
398
        c.gridy++;
181 ilm 399
        List<ListDescriptor> createListList = this.extension.getCreateListList();
74 ilm 400
        if (createListList.size() > 0) {
401
            String components = "";
402
            for (int i = 0; i < createListList.size(); i++) {
403
                String tableName = createListList.get(i).getId();
404
                if (i < createListList.size() - 1) {
405
                    components += tableName + ", ";
406
                } else {
407
                    components += tableName;
408
                }
409
            }
410
 
411
            if (createListList.size() > 1) {
412
                panel.add(new JLabel("Listes créées : " + components), c);
413
            } else {
414
                panel.add(new JLabel("Liste créée : " + components), c);
415
            }
416
            c.gridy++;
417
        }
418
 
419
        final JButton buttonCreateList = new JButton("Afficher les listes");
420
        panel.add(buttonCreateList, c);
421
        c.gridy++;
422
        panel.add(new JLabel(" "), c);
423
        c.gridy++;
424
        // --------- MENUS
425
        panel.add(new JLabelBold("Menus et actions"), c);
426
        c.gridy++;
181 ilm 427
        final int actionCount = this.extension.getActionDescriptors().size();
428
        final int menuCount = this.extension.getCreateMenuList().size();
429
        final int menuCount2 = this.extension.getRemoveMenuList().size();
74 ilm 430
        if (actionCount > 0) {
431
            if (actionCount > 1)
432
                panel.add(new JLabel(actionCount + " actions"), c);
433
            else
434
                panel.add(new JLabel(actionCount + " action"), c);
435
 
436
            c.gridy++;
437
        }
438
        if (menuCount > 0) {
439
            if (menuCount > 1)
440
                panel.add(new JLabel(menuCount + " ajouts de menu"), c);
441
            else
442
                panel.add(new JLabel(menuCount + " ajout de menu"), c);
443
            c.gridy++;
444
        }
445
        if (menuCount2 > 0) {
446
            if (menuCount2 > 1)
447
                panel.add(new JLabel(menuCount2 + " suppressions de menu"), c);
448
            else
449
                panel.add(new JLabel(menuCount2 + " suppression de menu"), c);
450
            c.gridy++;
451
        }
452
        final JButton buttonCreateMenu = new JButton("Afficher les menus");
453
        panel.add(buttonCreateMenu, c);
454
        c.gridy++;
455
        panel.add(new JLabel(" "), c);
456
 
457
        c.gridy++;
458
 
459
        // --------- TRANSLATIONS
460
        panel.add(new JLabelBold("Traductions et renommage de labels"), c);
461
        c.gridy++;
462
 
181 ilm 463
        int actionTranslationCount = this.extension.getActionTranslations().size();
464
        int menuTranslationCount = this.extension.getMenuTranslations().size();
465
        int fieldTranslationCount = this.extension.getFieldTranslations().size();
74 ilm 466
        if (fieldTranslationCount > 0) {
467
            if (fieldTranslationCount > 1)
468
                panel.add(new JLabel(fieldTranslationCount + " traductions de champs"), c);
469
            else
470
                panel.add(new JLabel(fieldTranslationCount + " traduction de champs"), c);
471
 
472
            c.gridy++;
473
        }
474
        if (menuTranslationCount > 0) {
475
            if (menuTranslationCount > 1)
476
                panel.add(new JLabel(menuTranslationCount + " traductions de menu"), c);
477
            else
478
                panel.add(new JLabel(menuTranslationCount + " traduction de menu"), c);
479
            c.gridy++;
480
        }
481
 
482
        if (actionTranslationCount > 0) {
483
            if (actionTranslationCount > 1)
484
                panel.add(new JLabel(actionTranslationCount + " traductions d'action"), c);
485
            else
486
                panel.add(new JLabel(actionTranslationCount + " traduction d'action"), c);
487
            c.gridy++;
488
        }
489
 
490
        final JButton buttonCreateTranslation = new JButton("Afficher les traductions");
491
        panel.add(buttonCreateTranslation, c);
492
        c.gridy++;
493
        panel.add(new JLabel(" "), c);
494
        c.gridy++;
495
        c.weighty = 1;
496
        c.gridwidth = 2;
497
        c.weightx = 1;
498
        panel.add(new JLabel(" "), c);
499
        c.gridy++;
500
        //
501
        buttonCreateTable.addActionListener(new ActionListener() {
502
 
503
            @Override
504
            public void actionPerformed(ActionEvent e) {
505
                openTableEditor();
506
            }
507
        });
508
        buttonCreateList.addActionListener(new ActionListener() {
509
 
510
            @Override
511
            public void actionPerformed(ActionEvent e) {
512
                openListEditor();
513
            }
514
        });
515
        buttonCreateComponent.addActionListener(new ActionListener() {
516
 
517
            @Override
518
            public void actionPerformed(ActionEvent e) {
519
                openComponentEditor();
520
            }
521
        });
522
        buttonCreateMenu.addActionListener(new ActionListener() {
523
 
524
            @Override
525
            public void actionPerformed(ActionEvent e) {
526
                openMenuEditor();
527
            }
528
        });
529
        buttonCreateTranslation.addActionListener(new ActionListener() {
530
 
531
            @Override
532
            public void actionPerformed(ActionEvent e) {
533
                openTranslationEditor();
534
            }
535
        });
536
        return panel;
537
    }
538
 
539
    private void openTableEditor() {
181 ilm 540
        final TableMainPanel contentPane = new TableMainPanel(this.extension);
541
        openEditor(this.frameTableEditor, contentPane, "Tables dans la base de données");
74 ilm 542
 
543
    }
544
 
545
    private void openListEditor() {
181 ilm 546
        final ListCreateMainPanel contentPane = new ListCreateMainPanel(this.extension);
547
        openEditor(this.frameListEditor, contentPane, "Listes personnalisées");
74 ilm 548
 
549
    }
550
 
551
    protected void openComponentEditor() {
181 ilm 552
        final ComponentCreateMainPanel contentPane = new ComponentCreateMainPanel(this.extension);
553
        openEditor(this.frameListEditor, contentPane, "Interfaces personnalisées");
74 ilm 554
 
555
    }
556
 
557
    protected void openMenuEditor() {
181 ilm 558
        final MenuMainPanel contentPane = new MenuMainPanel(this.extension);
559
        openEditor(this.frameMenuEditor, contentPane, "Menus et actions");
74 ilm 560
 
561
    }
562
 
563
    protected void openTranslationEditor() {
181 ilm 564
        final TranslationMainPanel contentPane = new TranslationMainPanel(this.extension);
565
        openEditor(this.frameTranslationEditor, contentPane, "Traductions");
74 ilm 566
 
567
    }
568
 
569
    private void openEditor(JFrame frame, JPanel mainPanel, String title) {
570
        if (frame == null) {
571
            frame = new JFrame();
181 ilm 572
            frame.setTitle(this.extension.getName() + " - " + title);
74 ilm 573
            frame.setContentPane(mainPanel);
574
            frame.setMinimumSize(new Dimension(796, 560));
575
            frame.pack();
576
            frame.setLocationRelativeTo(ExtensionInfoPanel.this);
577
        }
578
        FrameUtil.show(frame);
579
    }
580
}