OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Rev 28 | Go to most recent revision | 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
 
25 ilm 16
import org.openconcerto.erp.generationDoc.DocumentLocalStorageManager;
18 ilm 17
import org.openconcerto.erp.generationDoc.SheetXml;
18
import org.openconcerto.erp.utils.FileUtility;
19
import org.openconcerto.ui.DefaultGridBagConstraints;
20
import org.openconcerto.ui.preferences.DefaultPreferencePanel;
21
import org.openconcerto.utils.Tuple2;
22
import org.openconcerto.utils.text.SimpleDocumentListener;
23
 
24
import java.awt.Color;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.io.File;
30
import java.io.IOException;
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.Comparator;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Map.Entry;
38
 
39
import javax.swing.BorderFactory;
40
import javax.swing.JButton;
41
import javax.swing.JFileChooser;
42
import javax.swing.JLabel;
43
import javax.swing.JPanel;
44
import javax.swing.JScrollPane;
45
import javax.swing.JTextField;
46
import javax.swing.SwingUtilities;
47
import javax.swing.UIManager;
48
import javax.swing.event.DocumentEvent;
49
 
50
public abstract class AbstractGenerationDocumentPreferencePanel extends DefaultPreferencePanel {
51
 
52
    protected Map<String, String> mapKeyLabel = new HashMap<String, String>();
53
    private final Map<String, JTextField> mapKeyTextOO = new HashMap<String, JTextField>();
54
    private final Map<String, JTextField> mapKeyTextPDF = new HashMap<String, JTextField>();
55
 
56
    private JFileChooser fileChooser;
57
 
58
    private static final String formatOO = "Format Open Office : ";
59
    private static final String formatPDF = "Format PDF : ";
60
    private static final String parcourir = "...";
61
    private static final String defaut = "Définir l'emplacement suivant pour tous";
62
 
63
    public AbstractGenerationDocumentPreferencePanel() {
64
        super();
65
    }
66
 
67
    public void uiInit() {
68
        setLayout(new GridBagLayout());
69
        final GridBagConstraints c = new DefaultGridBagConstraints();
70
 
71
        // Emplacement par défaut
72
        final GridBagConstraints cDefault = new DefaultGridBagConstraints();
73
        final JLabel labelDefaut = new JLabel(defaut);
74
        final JTextField textDefault = new JTextField();
75
        textDefault.setEditable(false);
76
        final JButton buttonDefault = new JButton(parcourir);
77
        buttonDefault.addActionListener(new ActionListener() {
78
            @Override
79
            public void actionPerformed(final ActionEvent e) {
80
                directoryChoose(textDefault);
81
            }
82
        });
83
 
84
        final JButton buttonSetDefaut = new JButton("Appliquer");
85
        buttonSetDefaut.addActionListener(new ActionListener() {
86
            @Override
87
            public void actionPerformed(final ActionEvent e) {
88
 
89
                final String folder = textDefault.getText();
90
 
91
                for (final Entry<String, JTextField> entry : AbstractGenerationDocumentPreferencePanel.this.mapKeyTextOO.entrySet()) {
92
                    entry.getValue().setText(folder + File.separator + AbstractGenerationDocumentPreferencePanel.this.mapKeyLabel.get(entry.getKey()));
93
                }
94
 
95
                for (final Entry<String, JTextField> entry : AbstractGenerationDocumentPreferencePanel.this.mapKeyTextPDF.entrySet()) {
96
                    entry.getValue().setText(folder + File.separator + AbstractGenerationDocumentPreferencePanel.this.mapKeyLabel.get(entry.getKey()));
97
                }
98
            }
99
        });
100
 
101
        textDefault.getDocument().addDocumentListener(new SimpleDocumentListener() {
102
            @Override
103
            public void update(final DocumentEvent e) {
104
                buttonSetDefaut.setEnabled(textDefault.getText().trim().length() > 0);
105
            }
106
        });
107
        buttonSetDefaut.setEnabled(false);
108
        cDefault.gridx = GridBagConstraints.RELATIVE;
109
        final JPanel panelDefaut = new JPanel(new GridBagLayout());
110
        panelDefaut.add(labelDefaut, cDefault);
111
        cDefault.weightx = 1;
112
        panelDefaut.add(textDefault, cDefault);
113
        cDefault.weightx = 0;
114
        panelDefaut.add(buttonDefault, cDefault);
115
        panelDefaut.add(buttonSetDefaut, cDefault);
116
        c.gridwidth = GridBagConstraints.REMAINDER;
117
        c.weightx = 1;
118
 
119
        this.add(panelDefaut, c);
120
 
121
        final List<String> list = new ArrayList<String>(this.mapKeyLabel.keySet());
122
        Collections.sort(list, new Comparator<String>() {
123
            @Override
124
            public int compare(final String o1, final String o2) {
125
                return o1.compareTo(o2);
126
            }
127
        });
128
 
129
        final JPanel panelGlobal = new JPanel(new GridBagLayout());
130
        final GridBagConstraints cGlobal = new DefaultGridBagConstraints();
131
 
132
        final int size = list.size();
133
        for (int i = 0; i < size; i++) {
134
            final String key = list.get(i);
135
            cGlobal.gridwidth = 1;
136
            cGlobal.weightx = 1;
137
            cGlobal.gridy++;
138
            cGlobal.gridx = 0;
139
            cGlobal.anchor = GridBagConstraints.NORTHWEST;
140
 
141
            if (i == size - 1) {
142
                cGlobal.weighty = 1;
143
            }
144
 
145
            final JPanel panel = new JPanel();
146
            panel.setOpaque(false);
147
            panel.setBorder(BorderFactory.createTitledBorder(this.mapKeyLabel.get(key)));
148
            panel.setLayout(new GridBagLayout());
149
            final GridBagConstraints cPanel = new DefaultGridBagConstraints();
150
 
151
            // Emplacement SXC
152
 
153
            panel.add(new JLabel(formatOO), cPanel);
154
            cPanel.gridx++;
155
            cPanel.weightx = 1;
156
            final JTextField text = new JTextField();
157
            text.setEditable(false);
158
            this.mapKeyTextOO.put(key, text);
159
            panel.add(text, cPanel);
160
 
161
            final JButton button = new JButton(parcourir);
162
            cPanel.gridx++;
163
            cPanel.weightx = 0;
164
            cPanel.fill = GridBagConstraints.NONE;
165
            panel.add(button, cPanel);
166
 
167
            // Emplacement PDF
168
            cPanel.fill = GridBagConstraints.HORIZONTAL;
169
            cPanel.weightx = 0;
170
            cPanel.gridy++;
171
            cPanel.gridx = 0;
172
            cPanel.gridwidth = 1;
173
            panel.add(new JLabel(formatPDF), cPanel);
174
            cPanel.gridx++;
175
            cPanel.weightx = 1;
176
            final JTextField textPDF = new JTextField();
177
            textPDF.setEditable(false);
178
            this.mapKeyTextPDF.put(key, textPDF);
179
            panel.add(textPDF, cPanel);
180
 
181
            final JButton buttonPDF = new JButton(parcourir);
182
            cPanel.gridx++;
183
            cPanel.weightx = 0;
184
            cPanel.fill = GridBagConstraints.NONE;
185
            panel.add(buttonPDF, cPanel);
186
 
187
            panelGlobal.add(panel, cGlobal);
188
 
189
            button.addActionListener(new ActionListener() {
190
                @Override
191
                public void actionPerformed(final ActionEvent e) {
192
                    directoryChoose(AbstractGenerationDocumentPreferencePanel.this.mapKeyTextOO.get(key));
193
                }
194
            });
195
            buttonPDF.addActionListener(new ActionListener() {
196
                @Override
197
                public void actionPerformed(final ActionEvent e) {
198
                    directoryChoose(AbstractGenerationDocumentPreferencePanel.this.mapKeyTextPDF.get(key));
199
                }
200
            });
201
 
202
        }
203
 
204
        c.gridwidth = GridBagConstraints.REMAINDER;
205
        c.weightx = 1;
206
        c.weighty = 1;
207
        c.gridy++;
208
        c.gridx = 0;
209
        c.fill = GridBagConstraints.BOTH;
210
        this.add(new JScrollPane(panelGlobal), c);
211
 
212
        setValues();
213
    }
214
 
215
    @Override
216
    public void storeValues() {
217
 
218
        try {
219
            final File z = new File(".");
220
            for (final Entry<String, JTextField> entry : this.mapKeyTextOO.entrySet()) {
221
                final File f = new File(entry.getValue().getText());
222
                TemplateNXProps.getInstance().setProperty(entry.getKey() + "OO", FileUtility.getPrimaryPath(z.getCanonicalFile(), f));
223
            }
224
 
225
            for (final Entry<String, JTextField> entry : this.mapKeyTextPDF.entrySet()) {
226
                final File f = new File(entry.getValue().getText());
227
                TemplateNXProps.getInstance().setProperty(entry.getKey() + "PDF", FileUtility.getPrimaryPath(z.getCanonicalFile(), f));
228
            }
229
 
230
        } catch (final IOException e) {
231
            e.printStackTrace();
232
        }
233
 
234
        TemplateNXProps.getInstance().store();
235
    }
236
 
237
    @Override
238
    public void restoreToDefaults() {
239
        final Color foregroundColor = UIManager.getColor("TextField.foreground");
240
 
241
        for (final Entry<String, JTextField> entry : this.mapKeyTextOO.entrySet()) {
25 ilm 242
            final File f = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory("Default");
18 ilm 243
            final JTextField textField = entry.getValue();
244
            if (f.exists()) {
245
                textField.setForeground(foregroundColor);
246
            } else {
247
                textField.setForeground(Color.RED);
248
            }
249
            try {
250
                textField.setText(f.getCanonicalPath());
251
            } catch (IOException e) {
252
                // TODO Auto-generated catch block
253
                e.printStackTrace();
254
            }
255
        }
256
 
257
        for (final Entry<String, JTextField> entry : this.mapKeyTextPDF.entrySet()) {
25 ilm 258
            final File f = DocumentLocalStorageManager.getInstance().getDocumentOutputDirectory("Default");
18 ilm 259
            final JTextField textField = entry.getValue();
260
            if (f.exists()) {
261
                textField.setForeground(foregroundColor);
262
            } else {
263
                textField.setForeground(Color.RED);
264
            }
265
            try {
266
                textField.setText(f.getCanonicalPath());
267
            } catch (IOException e) {
268
                e.printStackTrace();
269
            }
270
        }
271
    }
272
 
273
    private void setValues() {
274
        try {
275
            updateTextFields(this.mapKeyTextOO, "OO");
276
            updateTextFields(this.mapKeyTextPDF, "PDF");
277
        } catch (final IOException e) {
278
            e.printStackTrace();
279
        }
280
    }
281
 
282
    private void updateTextFields(final Map<String, JTextField> map, final String format) throws IOException {
283
        final Color foregroundColor = UIManager.getColor("TextField.foreground");
25 ilm 284
        final DocumentLocalStorageManager storage = DocumentLocalStorageManager.getInstance();
18 ilm 285
        for (final Entry<String, JTextField> entry : map.entrySet()) {
25 ilm 286
            final File f;
287
            if (format.equalsIgnoreCase("PDF")) {
288
                f = storage.getPDFOutputDirectory(entry.getKey());
289
            } else {
290
                f = storage.getDocumentOutputDirectory(entry.getKey());
291
            }
18 ilm 292
            final JTextField textField = entry.getValue();
293
            if (f.exists()) {
294
                textField.setForeground(foregroundColor);
295
            } else {
296
                textField.setForeground(Color.RED);
297
            }
298
            textField.setText(f.getCanonicalPath());
299
        }
300
    }
301
 
302
    private void directoryChoose(final JTextField field) {
303
 
304
        if (this.fileChooser == null) {
305
            this.fileChooser = new JFileChooser();
306
            this.fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
307
        }
308
        this.fileChooser.setCurrentDirectory(new File(field.getText()));
309
        SwingUtilities.invokeLater(new Runnable() {
310
            public void run() {
311
 
312
                if (AbstractGenerationDocumentPreferencePanel.this.fileChooser.showDialog(AbstractGenerationDocumentPreferencePanel.this, "Sélectionner") == JFileChooser.APPROVE_OPTION) {
313
                    final File selectedFile = AbstractGenerationDocumentPreferencePanel.this.fileChooser.getSelectedFile();
314
                    if (selectedFile.exists()) {
315
                        field.setForeground(UIManager.getColor("TextField.foreground"));
316
                    } else {
317
                        field.setForeground(Color.RED);
318
                    }
319
 
320
                    field.setText(selectedFile.getAbsolutePath());
321
                }
322
            }
323
        });
324
    }
325
}