OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
140 ilm 1
package org.openconcerto.modules.label;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.Insets;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.awt.print.PrinterException;
9
import java.awt.print.PrinterJob;
10
import java.util.List;
11
 
12
import javax.swing.JButton;
13
import javax.swing.JCheckBox;
14
import javax.swing.JFrame;
15
import javax.swing.JLabel;
16
import javax.swing.JPanel;
17
import javax.swing.JSeparator;
18
import javax.swing.JSpinner;
19
import javax.swing.SpinnerNumberModel;
20
import javax.swing.event.ChangeEvent;
21
import javax.swing.event.ChangeListener;
22
 
23
import org.openconcerto.ui.DefaultGridBagConstraints;
24
import org.openconcerto.utils.ExceptionHandler;
25
 
26
public class LabelFrame extends JFrame {
27
    private static final boolean IGNORE_MARGINS = true;
28
    private static final int DEFAULT_LINES = 10;
29
    private static final int DEFAULT_COLS = 4;
30
    final LabelPanel labelPanel;
31
 
181 ilm 32
    public LabelFrame(List<? extends Label> list, LabelRenderer labelRenderer) {
140 ilm 33
        final JPanel p = new JPanel();
34
        p.setLayout(new GridBagLayout());
35
        final GridBagConstraints c = new DefaultGridBagConstraints();
36
        p.add(createToolBar(), c);
37
        c.gridy++;
38
        p.add(createToolBar2(), c);
39
        c.gridy++;
40
        c.insets = new Insets(0, 0, 0, 0);
41
        p.add(new JSeparator(JSeparator.HORIZONTAL), c);
42
        c.gridy++;
43
        c.weightx = 1;
44
        c.weighty = 1;
45
        c.fill = GridBagConstraints.BOTH;
46
        labelPanel = new LabelPanel(list, DEFAULT_LINES, DEFAULT_COLS, labelRenderer);
47
        labelPanel.setIgnoreMargins(IGNORE_MARGINS);
48
        p.add(labelPanel, c);
49
        this.setContentPane(p);
50
    }
51
 
52
    public JPanel createToolBar() {
53
        final JPanel toolbar = new JPanel();
54
        toolbar.setLayout(new GridBagLayout());
55
        final GridBagConstraints c = new DefaultGridBagConstraints();
56
        toolbar.add(new JLabel("Lignes"), c);
57
        c.gridx++;
58
        final JSpinner sLines = new JSpinner(new SpinnerNumberModel(DEFAULT_LINES, 1, 100, 1));
59
        toolbar.add(sLines, c);
60
        c.gridx++;
61
        toolbar.add(new JLabel("Colonnes"), c);
62
        c.gridx++;
63
        final JSpinner sColums = new JSpinner(new SpinnerNumberModel(DEFAULT_COLS, 1, 50, 1));
64
        toolbar.add(sColums, c);
65
        c.gridx++;
66
        final JCheckBox ck = new JCheckBox("Ignorer les marges de l'imprimante");
67
        ck.setSelected(IGNORE_MARGINS);
68
 
69
        toolbar.add(ck, c);
70
 
71
        c.gridx++;
72
        c.weightx = 1;
73
        c.fill = GridBagConstraints.NONE;
74
        c.anchor = GridBagConstraints.EAST;
75
        final JButton printButton = new JButton("Imprimer");
76
        toolbar.add(printButton, c);
77
        c.gridx++;
78
 
79
        sLines.addChangeListener(new ChangeListener() {
80
 
81
            @Override
82
            public void stateChanged(ChangeEvent e) {
83
                Number n = (Number) sLines.getValue();
84
                if (n != null) {
85
                    labelPanel.setLineCount(n.intValue());
86
                }
87
            }
88
        });
89
        sColums.addChangeListener(new ChangeListener() {
90
 
91
            @Override
92
            public void stateChanged(ChangeEvent e) {
93
                Number n = (Number) sColums.getValue();
94
                if (n != null) {
95
                    labelPanel.setColumnCount(n.intValue());
96
                }
97
            }
98
        });
99
        printButton.addActionListener(new ActionListener() {
100
 
101
            @Override
102
            public void actionPerformed(ActionEvent e) {
103
                final PrinterJob job = PrinterJob.getPrinterJob();
104
                job.setPrintable(labelPanel);
105
                boolean ok = job.printDialog();
106
                if (ok) {
107
                    try {
108
                        job.print();
109
                    } catch (PrinterException ex) {
110
                        ExceptionHandler.handle("Print error", ex);
111
                    }
112
                }
113
 
114
            }
115
        });
116
        ck.addActionListener(new ActionListener() {
117
 
118
            @Override
119
            public void actionPerformed(ActionEvent e) {
120
                labelPanel.setIgnoreMargins(ck.isSelected());
121
 
122
            }
123
        });
124
        return toolbar;
125
    }
126
 
127
    public JPanel createToolBar2() {
128
        final JPanel toolbar = new JPanel();
129
        toolbar.setLayout(new GridBagLayout());
130
        final GridBagConstraints c = new DefaultGridBagConstraints();
131
        toolbar.add(new JLabel("Marge en haut (mm)"), c);
132
        c.gridx++;
133
        final JSpinner sLines = new JSpinner(new SpinnerNumberModel(0, 0, 200, 1));
134
        toolbar.add(sLines, c);
135
        c.gridx++;
136
        toolbar.add(new JLabel("Marge à gauche (mm)"), c);
137
        c.gridx++;
138
        final JSpinner sColums = new JSpinner(new SpinnerNumberModel(0, 0, 200, 1));
139
        toolbar.add(sColums, c);
140
        c.gridx++;
141
 
142
        sLines.addChangeListener(new ChangeListener() {
143
 
144
            @Override
145
            public void stateChanged(ChangeEvent e) {
146
                Number n = (Number) sLines.getValue();
147
                if (n != null) {
148
                    labelPanel.setTopMargin(n.intValue());
149
                }
150
            }
151
        });
152
        sColums.addChangeListener(new ChangeListener() {
153
 
154
            @Override
155
            public void stateChanged(ChangeEvent e) {
156
                Number n = (Number) sColums.getValue();
157
                if (n != null) {
158
                    labelPanel.setLeftMargin(n.intValue());
159
                }
160
            }
161
        });
162
 
163
        return toolbar;
164
    }
165
}