OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | 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.panel;
15
 
16
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
17
import org.openconcerto.sql.model.SQLRow;
25 ilm 18
import org.openconcerto.sql.model.SQLRowAccessor;
18 ilm 19
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 20
import org.openconcerto.utils.ExceptionHandler;
18 ilm 21
 
22
import java.awt.FlowLayout;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.lang.reflect.Constructor;
28
import java.lang.reflect.InvocationTargetException;
29
import java.util.List;
30
import java.util.concurrent.ExecutionException;
31
 
32
import javax.swing.BorderFactory;
33
import javax.swing.JButton;
34
import javax.swing.JFrame;
35
import javax.swing.JLabel;
36
import javax.swing.JOptionPane;
37
import javax.swing.JPanel;
38
import javax.swing.JProgressBar;
39
import javax.swing.JSpinner;
40
import javax.swing.SpinnerNumberModel;
41
import javax.swing.SwingUtilities;
42
 
43
public class ListeFastPrintFrame extends JFrame {
44
    /**
45
     *
46
     */
47
    private static final long serialVersionUID = -1653555706074122489L;
48
    private final Class<? extends AbstractSheetXml> clazz;
49
    private final JPanel panel;
25 ilm 50
    private final List<SQLRowAccessor> liste;
18 ilm 51
    private final JLabel operation = new JLabel("");
52
    private final JProgressBar bar = new JProgressBar();
53
    private final JSpinner spin;
54
    Thread thread;
55
    private boolean cancelOp = false;
56
 
57
    private final JButton valid, cancel;
58
 
25 ilm 59
    public ListeFastPrintFrame(final List<SQLRowAccessor> liste, final Class<? extends AbstractSheetXml> clazz) {
18 ilm 60
        this.panel = new JPanel(new GridBagLayout());
61
        this.liste = liste;
62
        this.clazz = clazz;
63
        final GridBagConstraints c = new DefaultGridBagConstraints();
64
        // c.gridwidth = GridBagConstraints.REMAINDER;
65
        // FIXME Add Preferences nombre de copies par defaut
25 ilm 66
        final SQLRowAccessor row = this.liste.get(0);
18 ilm 67
 
25 ilm 68
        final AbstractSheetXml bSheet = this.createAbstractSheet(row.asRow());
18 ilm 69
        if (this.liste.size() <= 1) {
70
            this.panel.add(new JLabel("Lancer l'impression document"), c);
71
        } else {
72
            this.panel.add(new JLabel("Lancer l'impression des " + this.liste.size() + " documents"), c);
73
        }
74
        c.gridx++;
75
        c.gridwidth = 1;
76
        this.panel.add(new JLabel("en "), c);
77
        final SpinnerNumberModel model = new SpinnerNumberModel(1, 1, 15, 1);
78
        this.spin = new JSpinner(model);
79
        c.gridx++;
80
        this.panel.add(this.spin, c);
81
        c.gridx++;
82
        this.panel.add(new JLabel(" exemplaire(s) sur " + bSheet.getPrinter()));
83
 
84
        c.gridy++;
85
        c.gridx = 0;
86
        final JPanel panelOp = new JPanel(new FlowLayout(FlowLayout.LEFT));
87
        panelOp.setBorder(BorderFactory.createEmptyBorder());
88
        c.gridwidth = GridBagConstraints.REMAINDER;
89
        c.weightx = 0;
90
 
91
        panelOp.add(new JLabel("Opération en cours : "));
92
        panelOp.add(this.operation);
93
        this.panel.add(panelOp, c);
94
 
95
        c.gridy++;
96
        c.gridx = 0;
97
        this.panel.add(new JLabel("Progression des impressions"), c);
98
 
99
        c.gridy++;
100
        c.gridx = 0;
101
        this.panel.add(this.bar, c);
102
        this.bar.setMaximum(this.liste.size());
103
 
104
        this.cancel = new JButton("Annuler");
105
        this.valid = new JButton("Valider");
106
        c.gridy++;
107
        c.gridwidth = GridBagConstraints.REMAINDER;
108
        c.fill = GridBagConstraints.NONE;
109
        c.anchor = GridBagConstraints.EAST;
110
        c.gridx = 0;
111
        final JPanel panelButton = new JPanel();
112
        panelButton.add(this.valid);
113
        panelButton.add(this.cancel);
114
        this.cancel.addActionListener(new ActionListener() {
115
            @Override
116
            public void actionPerformed(final ActionEvent e) {
117
 
118
                if (ListeFastPrintFrame.this.thread != null && ListeFastPrintFrame.this.thread.isAlive()) {
119
                    ListeFastPrintFrame.this.cancelOp = true;
120
                } else {
121
                    ListeFastPrintFrame.this.dispose();
122
                }
123
            }
124
        });
125
 
126
        this.valid.addActionListener(new ActionListener() {
127
            @Override
128
            public void actionPerformed(final ActionEvent e) {
129
 
130
                ListeFastPrintFrame.this.printAll();
131
            }
132
        });
133
 
134
        this.panel.add(panelButton, c);
135
 
136
        this.getContentPane().add(this.panel);
137
        this.setTitle("Impressions multiples");
138
        this.setLocationRelativeTo(null);
139
        this.pack();
140
    }
141
 
142
    public void printAll() {
143
 
144
        this.valid.setEnabled(false);
145
        this.spin.setEnabled(false);
146
        this.thread = new Thread(new Runnable() {
147
            public void run() {
148
                final short copies = Short.valueOf(ListeFastPrintFrame.this.spin.getValue().toString());
149
                int i = 0;
150
                SwingUtilities.invokeLater(new Runnable() {
151
                    public void run() {
152
                        ListeFastPrintFrame.this.bar.setStringPainted(true);
153
                        ListeFastPrintFrame.this.bar.setString("0/" + ListeFastPrintFrame.this.liste.size());
154
                    }
155
                });
25 ilm 156
                for (final SQLRowAccessor rowAt : ListeFastPrintFrame.this.liste) {
18 ilm 157
 
25 ilm 158
                    final AbstractSheetXml bSheet = ListeFastPrintFrame.this.createAbstractSheet(rowAt.asRow());
159
                    if (!bSheet.getGeneratedFile().exists()) {
18 ilm 160
 
161
                        try {
162
                            SwingUtilities.invokeLater(new Runnable() {
163
                                public void run() {
25 ilm 164
                                    ListeFastPrintFrame.this.operation.setText("Création du document " + bSheet.getGeneratedFile());
18 ilm 165
                                }
166
                            });
25 ilm 167
                            bSheet.createDocument();
168
                            bSheet.showPrintAndExportAsynchronous(false, false, true);
169
                        } catch (Exception e) {
170
                            ExceptionHandler.handle("Erreur lors de l'impression du document " + bSheet.getGeneratedFile());
18 ilm 171
                        }
172
                    }
173
 
174
                    SwingUtilities.invokeLater(new Runnable() {
175
                        public void run() {
25 ilm 176
                            ListeFastPrintFrame.this.operation.setText("Impression du document " + bSheet.getGeneratedFile());
18 ilm 177
                        }
178
                    });
179
                    bSheet.fastPrintDocument(copies);
180
                    final int progress = ++i;
181
                    SwingUtilities.invokeLater(new Runnable() {
182
                        public void run() {
183
                            ListeFastPrintFrame.this.bar.setValue(progress);
184
                            ListeFastPrintFrame.this.bar.setString(progress + "/" + ListeFastPrintFrame.this.liste.size());
185
                        }
186
                    });
187
                    if (ListeFastPrintFrame.this.cancelOp) {
188
                        break;
189
                    }
190
                }
191
                SwingUtilities.invokeLater(new Runnable() {
192
                    public void run() {
193
                        JOptionPane.showMessageDialog(ListeFastPrintFrame.this, ListeFastPrintFrame.this.cancelOp ? "Impressions annulées!" : "Impressions terminées!");
194
                        ListeFastPrintFrame.this.dispose();
195
                    }
196
                });
197
            }
198
        });
199
        this.thread.start();
200
 
201
    }
202
 
203
    private AbstractSheetXml createAbstractSheet(final SQLRow row) {
204
        try {
205
            final Constructor<? extends AbstractSheetXml> ctor = this.clazz.getConstructor(SQLRow.class);
206
            return ctor.newInstance(row);
207
        } catch (final IllegalArgumentException e) {
208
            e.printStackTrace();
209
        } catch (final InstantiationException e) {
210
            e.printStackTrace();
211
        } catch (final IllegalAccessException e) {
212
            e.printStackTrace();
213
        } catch (final InvocationTargetException e) {
214
            e.printStackTrace();
215
        } catch (final SecurityException e) {
216
            e.printStackTrace();
217
        } catch (final NoSuchMethodException e) {
218
            e.printStackTrace();
219
        }
220
        return null;
221
    }
222
}