OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.preferences;
15
 
156 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
61 ilm 17
import org.openconcerto.erp.generationDoc.AbstractLocalTemplateProvider;
28 ilm 18
import org.openconcerto.erp.generationDoc.DefaultLocalTemplateProvider;
19
import org.openconcerto.erp.generationDoc.TemplateManager;
61 ilm 20
import org.openconcerto.erp.generationDoc.TemplateProvider;
142 ilm 21
import org.openconcerto.sql.users.rights.UserRightsManager;
18 ilm 22
import org.openconcerto.ui.DefaultGridBagConstraints;
23
import org.openconcerto.ui.preferences.DefaultPreferencePanel;
61 ilm 24
import org.openconcerto.ui.table.TablePopupMouseListener;
25
import org.openconcerto.utils.ExceptionHandler;
26
import org.openconcerto.utils.FileUtils;
27
import org.openconcerto.utils.cc.ITransformer;
18 ilm 28
 
29
import java.awt.Color;
61 ilm 30
import java.awt.Component;
18 ilm 31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionListener;
61 ilm 35
import java.awt.event.MouseEvent;
18 ilm 36
import java.io.File;
37
import java.io.IOException;
38
 
61 ilm 39
import javax.swing.AbstractAction;
18 ilm 40
import javax.swing.JButton;
41
import javax.swing.JFileChooser;
42
import javax.swing.JLabel;
61 ilm 43
import javax.swing.JMenuItem;
44
import javax.swing.JOptionPane;
18 ilm 45
import javax.swing.JPanel;
61 ilm 46
import javax.swing.JPopupMenu;
47
import javax.swing.JScrollPane;
48
import javax.swing.JTable;
18 ilm 49
import javax.swing.JTextField;
61 ilm 50
import javax.swing.ListSelectionModel;
18 ilm 51
import javax.swing.UIManager;
61 ilm 52
import javax.swing.event.ListSelectionEvent;
53
import javax.swing.event.ListSelectionListener;
54
import javax.swing.table.DefaultTableCellRenderer;
18 ilm 55
 
56
public class TemplatePreferencePanel extends DefaultPreferencePanel {
57
 
58
    private JTextField textTemplate;
59
 
61 ilm 60
    private JButton bModify;
61
    private JButton bUndo;
62
    private JButton bSync;
63
 
18 ilm 64
    public TemplatePreferencePanel() {
65
        this.setLayout(new GridBagLayout());
66
        final GridBagConstraints cPanel = new DefaultGridBagConstraints();
67
        cPanel.weighty = 0;
68
        cPanel.anchor = GridBagConstraints.WEST;
69
        /*******************************************************************************************
70
         * Emplacement
71
         ******************************************************************************************/
156 ilm 72
        if (!ComptaPropsConfiguration.getInstanceCompta().isOnCloud()) {
73
            this.add(new JLabel("Modèles des documents"), cPanel);
74
            cPanel.gridx++;
75
            cPanel.weightx = 1;
76
            this.textTemplate = new JTextField();
77
            this.add(this.textTemplate, cPanel);
78
            this.textTemplate.setEditable(false);
79
            final JButton buttonTemplate = new JButton("...");
80
            cPanel.gridx++;
81
            cPanel.weightx = 0;
82
            cPanel.fill = GridBagConstraints.NONE;
83
            this.add(buttonTemplate, cPanel);
84
            buttonTemplate.addActionListener(new ActionListener() {
85
                public void actionPerformed(ActionEvent e) {
86
                    final JFileChooser fileChooser = new JFileChooser();
87
                    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
88
                    fileChooser.setCurrentDirectory(new File(TemplatePreferencePanel.this.textTemplate.getText()));
89
                    if (fileChooser.showDialog(TemplatePreferencePanel.this, "Sélectionner") == JFileChooser.APPROVE_OPTION) {
90
                        File selectedFile = fileChooser.getSelectedFile();
91
                        if (selectedFile.exists()) {
92
                            TemplatePreferencePanel.this.textTemplate.setForeground(UIManager.getColor("TextField.foreground"));
93
                        } else {
94
                            TemplatePreferencePanel.this.textTemplate.setForeground(Color.RED);
95
                        }
96
                        TemplatePreferencePanel.this.textTemplate.setText(selectedFile.getPath());
97
                    }
18 ilm 98
 
156 ilm 99
                }
100
            });
101
        }
61 ilm 102
        final JPanel templates = createTemplateList();
103
        templates.setOpaque(false);
21 ilm 104
        cPanel.gridy++;
61 ilm 105
        cPanel.fill = GridBagConstraints.BOTH;
106
        cPanel.weightx = 1;
107
        cPanel.weighty = 1;
108
        cPanel.gridwidth = 3;
21 ilm 109
        cPanel.gridx = 0;
61 ilm 110
        this.add(templates, cPanel);
21 ilm 111
 
18 ilm 112
        setValues();
113
    }
114
 
61 ilm 115
    private JPanel createTemplateList() {
116
        JPanel p = new JPanel();
117
        p.setLayout(new GridBagLayout());
118
        GridBagConstraints c = new DefaultGridBagConstraints();
119
        c.fill = GridBagConstraints.NONE;
120
        bModify = new JButton("Modifier");
121
        bModify.setEnabled(false);
122
        bModify.setOpaque(false);
123
        p.add(bModify, c);
124
 
125
        bUndo = new JButton("Rétablir");
126
        bUndo.setEnabled(false);
127
        bUndo.setOpaque(false);
128
        c.gridx++;
129
        p.add(bUndo, c);
156 ilm 130
        if (ComptaPropsConfiguration.getInstanceCompta().isOnCloud()) {
131
            bSync = new JButton("Synchroniser vers le cloud");
132
        } else {
133
            bSync = new JButton("Synchroniser");
134
        }
61 ilm 135
        bSync.setEnabled(false);
136
        bSync.setOpaque(false);
142 ilm 137
        if (!UserRightsManager.getCurrentUserRights().haveRight("SYNC_TEMPLATE")) {
132 ilm 138
            bSync.setToolTipText("Vous n'avez pas les droits suffisants pour synchroniser les modèles!");
139
        }
61 ilm 140
        c.gridx++;
141
        p.add(bSync, c);
142
        final TemplateTableModel dm = new TemplateTableModel();
143
        final JTable table = new JTable(dm);
144
        TablePopupMouseListener.add(table, new ITransformer<MouseEvent, JPopupMenu>() {
145
            @Override
146
            public JPopupMenu transformChecked(MouseEvent input) {
147
                final JPopupMenu res = new JPopupMenu();
148
                final int selectedRow = table.getSelectedRow();
149
                if (selectedRow >= 0) {
150
                    final JMenuItem menuItem = new JMenuItem(dm.getTemplateId(selectedRow));
151
                    res.add(menuItem);
152
                    menuItem.setEnabled(false);
153
                    res.addSeparator();
154
 
155
                    res.add(new AbstractAction("Modifier le modèle") {
156
 
157
                        @Override
158
                        public void actionPerformed(ActionEvent e) {
159
                            modifyTemplate(dm, table);
160
 
161
                        }
162
                    });
163
                    res.add(new AbstractAction("Modifier la configuration du modèle") {
164
 
165
                        @Override
166
                        public void actionPerformed(ActionEvent e) {
167
                            modifyTemplateXML(dm, table);
168
 
169
                        }
170
                    });
171
                    res.add(new AbstractAction("Modifier les paramètres d'impression") {
172
 
173
                        @Override
174
                        public void actionPerformed(ActionEvent e) {
175
                            modifyTemplateODSP(dm, table);
176
 
177
                        }
178
                    });
179
                }
180
                return res;
181
            }
182
        });
183
        table.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
184
            @Override
185
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
186
                final Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
187
                TemplateProvider provider = dm.getTemplateProvider(row);
188
                if (provider instanceof AbstractLocalTemplateProvider) {
189
                    final String templateId = dm.getTemplateId(row);
190
                    File f = ((AbstractLocalTemplateProvider) provider).getFileTemplate(templateId, null, null);
156 ilm 191
                    String path = "invalid path";
192
                    if (f != null) {
193
                        try {
194
                            path = f.getCanonicalPath();
195
                        } catch (IOException e) {
196
                            path = f.getAbsolutePath();
197
                        }
198
                    }
61 ilm 199
                    if (f == null || !f.exists()) {
200
                        tableCellRendererComponent.setBackground(Color.ORANGE);
201
                        if (f != null) {
156 ilm 202
                            setToolTipText(path + " not found");
61 ilm 203
                        } else {
204
                            setToolTipText("no file for template " + templateId);
205
                        }
206
                    } else {
207
                        if (isSelected) {
208
                            tableCellRendererComponent.setBackground(table.getSelectionBackground());
209
                        } else {
210
                            tableCellRendererComponent.setBackground(table.getBackground());
211
                        }
156 ilm 212
                        setToolTipText(path);
61 ilm 213
                    }
214
                }
215
                return tableCellRendererComponent;
216
            }
217
        });
218
        c.gridy++;
219
        c.gridx = 0;
220
        c.gridwidth = 3;
221
        c.fill = GridBagConstraints.BOTH;
222
        c.weightx = 1;
223
        c.weighty = 1;
224
        p.add(new JScrollPane(table), c);
225
        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
226
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
227
 
228
            @Override
229
            public void valueChanged(ListSelectionEvent e) {
230
                int row = table.getSelectedRow();
231
                if (row < 0) {
232
                    bModify.setEnabled(false);
233
                    bSync.setEnabled(false);
234
                    bUndo.setEnabled(false);
235
                    return;
236
                }
237
                boolean b = dm.isSynced(row);
238
                if (b) {
239
                    bModify.setEnabled(true);
240
                    bSync.setEnabled(false);
241
                    bUndo.setEnabled(false);
242
                } else {
243
                    bModify.setEnabled(true);
142 ilm 244
                    bSync.setEnabled(UserRightsManager.getCurrentUserRights().haveRight("SYNC_TEMPLATE"));
61 ilm 245
                    bUndo.setEnabled(true);
246
                }
247
 
248
            }
249
        });
250
        bModify.addActionListener(new ActionListener() {
251
 
252
            @Override
253
            public void actionPerformed(ActionEvent e) {
254
                modifyTemplate(dm, table);
255
            }
256
        });
257
        bSync.addActionListener(new ActionListener() {
258
 
259
            @Override
260
            public void actionPerformed(ActionEvent e) {
261
                int row = table.getSelectedRow();
262
                dm.sync(row);
263
            }
264
        });
265
 
266
        bUndo.addActionListener(new ActionListener() {
267
 
268
            @Override
269
            public void actionPerformed(ActionEvent e) {
270
                int row = table.getSelectedRow();
271
                dm.restore(row);
272
 
273
            }
274
        });
275
 
276
        return p;
277
    }
278
 
18 ilm 279
    public void storeValues() {
156 ilm 280
        if (this.textTemplate != null) {
281
            final File z = new File(".");
282
            final File f = new File(this.textTemplate.getText());
283
            try {
284
                TemplateNXProps.getInstance().setProperty("LocationTemplate", FileUtils.relative(z, f));
285
                final DefaultLocalTemplateProvider provider = new DefaultLocalTemplateProvider();
18 ilm 286
 
156 ilm 287
                provider.setBaseDirectory(new File(FileUtils.relative(z, f)));
28 ilm 288
 
156 ilm 289
                TemplateManager.getInstance().setDefaultProvider(provider);
290
            } catch (IOException e) {
291
                e.printStackTrace();
292
            }
28 ilm 293
 
156 ilm 294
            TemplateNXProps.getInstance().store();
18 ilm 295
        }
296
    }
297
 
298
    public void restoreToDefaults() {
61 ilm 299
 
18 ilm 300
    }
301
 
302
    public String getTitleName() {
303
        return "Emplacement des modèles";
304
    }
305
 
306
    private void setValues() {
156 ilm 307
        if (this.textTemplate != null) {
308
            try {
309
                final File f = new File(TemplateNXProps.getInstance().getStringProperty("LocationTemplate"));
310
                if (f.exists()) {
311
                    this.textTemplate.setForeground(UIManager.getColor("TextField.foreground"));
312
                } else {
313
                    this.textTemplate.setForeground(Color.RED);
314
                }
315
                this.textTemplate.setText(f.getCanonicalPath());
316
 
317
            } catch (IOException e) {
318
                e.printStackTrace();
18 ilm 319
            }
320
        }
321
    }
322
 
61 ilm 323
    public void modifyTemplate(final TemplateTableModel dm, final JTable table) {
324
        int row = table.getSelectedRow();
325
        dm.unsync(row);
326
        // modify on OO
327
        final TemplateProvider templateProvider = dm.getTemplateProvider(row);
328
        if (templateProvider instanceof AbstractLocalTemplateProvider) {
329
            final String templateId = dm.getTemplateId(row);
330
 
331
            Thread t = new Thread(new Runnable() {
332
 
333
                @Override
334
                public void run() {
335
                    try {
336
                        final File fileTemplate = ((AbstractLocalTemplateProvider) templateProvider).getFileTemplate(templateId, null, null);
337
                        if (fileTemplate != null && fileTemplate.exists()) {
338
                            FileUtils.openFile(fileTemplate);
339
                        }
340
                    } catch (IOException e1) {
341
                        ExceptionHandler.handle("Impossible d'ouvrir le modèle", e1);
342
                    }
343
                }
344
            });
345
            t.setName("TemplatePreferencePanel: open with OO");
346
            t.setDaemon(true);
347
            t.start();
348
 
349
        }
350
    }
351
 
352
    public void modifyTemplateXML(final TemplateTableModel dm, final JTable table) {
353
        int row = table.getSelectedRow();
354
        dm.unsync(row);
355
        // modify on OO
356
        final TemplateProvider templateProvider = dm.getTemplateProvider(row);
357
        if (templateProvider instanceof AbstractLocalTemplateProvider) {
358
            final String templateId = dm.getTemplateId(row);
359
 
360
            Thread t = new Thread(new Runnable() {
361
 
362
                @Override
363
                public void run() {
364
                    try {
365
                        final File fileTemplateConfiguration = ((AbstractLocalTemplateProvider) templateProvider).getFileTemplateConfiguration(templateId, null, null);
366
                        if (fileTemplateConfiguration != null && fileTemplateConfiguration.exists()) {
367
                            FileUtils.openFile(fileTemplateConfiguration);
368
                        } else {
369
                            JOptionPane.showMessageDialog(TemplatePreferencePanel.this, "Pas de fichier de configuration associé");
370
                        }
371
                    } catch (IOException e1) {
372
                        ExceptionHandler.handle("Impossible d'ouvrir la configuration du modèle", e1);
373
                    }
374
                }
375
            });
376
            t.setName("TemplatePreferencePanel: open with OO");
377
            t.setDaemon(true);
378
            t.start();
379
 
380
        }
381
 
382
    }
383
 
384
    public void modifyTemplateODSP(final TemplateTableModel dm, final JTable table) {
385
        int row = table.getSelectedRow();
386
        dm.unsync(row);
387
        // modify on OO
388
        final TemplateProvider templateProvider = dm.getTemplateProvider(row);
389
        if (templateProvider instanceof AbstractLocalTemplateProvider) {
390
            final String templateId = dm.getTemplateId(row);
391
 
392
            Thread t = new Thread(new Runnable() {
393
 
394
                @Override
395
                public void run() {
396
                    try {
397
                        final File fileTemplatePrintConfiguration = ((AbstractLocalTemplateProvider) templateProvider).getFileTemplatePrintConfiguration(templateId, null, null);
398
                        if (fileTemplatePrintConfiguration != null && fileTemplatePrintConfiguration.exists()) {
399
                            FileUtils.openFile(fileTemplatePrintConfiguration);
400
                        }
401
                    } catch (IOException e1) {
402
                        ExceptionHandler.handle("Impossible d'ouvrir la configuration d'impression", e1);
403
                    }
404
                }
405
            });
406
            t.setName("TemplatePreferencePanel: open with OO");
407
            t.setDaemon(true);
408
            t.start();
409
 
410
        }
411
 
412
    }
18 ilm 413
}