OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
112 ilm 1
package org.openconcerto.modules.operation;
2
 
3
import java.awt.FlowLayout;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.awt.print.PageFormat;
9
import java.awt.print.Pageable;
158 ilm 10
import java.awt.print.Paper;
112 ilm 11
import java.awt.print.PrinterException;
12
import java.awt.print.PrinterJob;
13
import java.util.ArrayList;
14
import java.util.Calendar;
15
import java.util.Collections;
16
import java.util.Comparator;
17
import java.util.List;
18
 
158 ilm 19
import javax.print.attribute.HashPrintRequestAttributeSet;
20
import javax.print.attribute.PrintRequestAttributeSet;
21
import javax.print.attribute.Size2DSyntax;
22
import javax.print.attribute.standard.MediaPrintableArea;
23
import javax.print.attribute.standard.MediaSizeName;
112 ilm 24
import javax.swing.JButton;
25
import javax.swing.JCheckBox;
26
import javax.swing.JLabel;
27
import javax.swing.JOptionPane;
28
import javax.swing.JPanel;
29
import javax.swing.SwingConstants;
30
import javax.swing.SwingUtilities;
31
 
32
import org.jopencalendar.model.JCalendarItem;
33
import org.jopencalendar.print.CalendarItemPrinter;
158 ilm 34
import org.openconcerto.erp.generationDoc.A4;
112 ilm 35
import org.openconcerto.sql.users.User;
36
import org.openconcerto.ui.DefaultGridBagConstraints;
37
import org.openconcerto.ui.JDate;
38
import org.openconcerto.ui.PrintPreviewFrame;
39
 
40
public class CalendarPrintPanel extends JPanel {
41
    final JCheckBox preview = new JCheckBox("Aperçu");
42
    final JButton bPrint = new JButton("Imprimer");
158 ilm 43
    private static final double POINTS_PER_INCH = 72.0;
112 ilm 44
 
45
    public CalendarPrintPanel(final OperationCalendarManager manager, final int week, final int year, final List<User> selectedUsers, final List<String> selectedStates) {
46
        preview.setSelected(true);
47
        //
48
        this.setLayout(new GridBagLayout());
49
        final GridBagConstraints c = new DefaultGridBagConstraints();
50
        final JLabel l = new JLabel("Date de début", SwingConstants.RIGHT);
51
        this.add(l, c);
52
        c.gridx++;
53
        final JDate d1 = new JDate(false, true);
54
        Calendar cal = Calendar.getInstance();
55
        cal.clear();
56
        cal.set(Calendar.YEAR, year);
57
        cal.set(Calendar.WEEK_OF_YEAR, week);
58
        cal.set(Calendar.HOUR_OF_DAY, 0);
59
        cal.set(Calendar.MINUTE, 0);
60
        cal.set(Calendar.SECOND, 0);
61
        cal.set(Calendar.MILLISECOND, 0);
62
        d1.setDate(cal.getTime());
63
        c.weightx = 1;
64
        this.add(d1, c);
65
        c.gridx = 0;
66
        c.gridy++;
67
        c.weightx = 0;
68
        final JLabel l2 = new JLabel("Date de fin", SwingConstants.RIGHT);
69
 
70
        this.add(l2, c);
71
        c.gridx++;
72
        final JDate d2 = new JDate(false, true);
73
        cal.add(Calendar.DAY_OF_YEAR, 7);
74
        d2.setDate(cal.getTime());
75
        c.weightx = 1;
76
        this.add(d2, c);
77
        final JPanel p = new JPanel();
78
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));
79
        p.add(preview);
80
        p.add(bPrint);
81
        c.gridwidth = 2;
82
        c.gridx = 0;
83
        c.gridy++;
84
        c.fill = GridBagConstraints.NONE;
85
        c.anchor = GridBagConstraints.SOUTHEAST;
86
        this.add(p, c);
87
        //
88
 
89
        bPrint.addActionListener(new ActionListener() {
90
 
91
            @Override
92
            public void actionPerformed(ActionEvent e) {
93
                if (d1.getDate().after(d2.getDate())) {
94
                    return;
95
                }
96
 
158 ilm 97
                final List<Pageable> p = new ArrayList<>();
112 ilm 98
 
99
                for (User user : selectedUsers) {
158 ilm 100
                    final List<User> l = new ArrayList<>();
112 ilm 101
                    l.add(user);
102
                    final List<JCalendarItem> itemInWeek = manager.getItemIn(d1.getDate(), d2.getDate(), l, selectedStates);
103
                    if (!itemInWeek.isEmpty()) {
104
                        Collections.sort(itemInWeek, new Comparator<JCalendarItem>() {
105
                            @Override
106
                            public int compare(JCalendarItem o1, JCalendarItem o2) {
107
                                return o1.getDtStart().compareTo(o2.getDtStart());
108
                            }
109
                        });
110
                        final PageFormat pf = new PageFormat();
111
                        pf.setPaper(new A4());
112
                        final CalendarItemPrinter printable = new OperationCalendarItemPrinter(user.getFullName(), itemInWeek, pf);
113
 
114
                        p.add(printable);
115
                    }
116
                }
117
                if (p.isEmpty()) {
118
                    JOptionPane.showMessageDialog(CalendarPrintPanel.this, "Aucune page à imprimer.\nMerci de vérifier la période.");
119
                    return;
120
                }
121
                if (preview.isSelected()) {
122
 
123
                    final PrintPreviewFrame f = new PrintPreviewFrame(new ListOfPageable(p)) {
124
                        @Override
125
                        public void printAllPages() {
126
                            printPages(p);
127
                            closeFrame();
128
                        }
129
 
130
                    };
131
                    f.pack();
132
                    f.setLocationRelativeTo(CalendarPrintPanel.this);
133
                    f.setVisible(true);
134
                } else {
135
                    printPages(p);
136
                    closeFrame();
137
                }
138
            }
139
        });
140
    }
141
 
142
    protected void closeFrame() {
143
        SwingUtilities.getWindowAncestor(this).dispose();
144
    }
145
 
146
    public void printPages(final List<Pageable> pageables) {
147
        PrinterJob job = PrinterJob.getPrinterJob();
148
        boolean ok = job.printDialog();
149
        if (ok) {
158 ilm 150
            final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
151
            final MediaSizeName media = MediaSizeName.ISO_A4;
152
            attributes.add(media);
153
            final Paper paper = new A4();
154
 
155
            final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
156
                    (float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
157
            attributes.add(printableArea);
158
 
112 ilm 159
            for (Pageable document : pageables) {
160
                PageFormat p = job.getPageFormat(null);
161
                job.getPageFormat(null).getImageableHeight();
162
                job.setPageable(document);
163
                try {
158 ilm 164
                    job.print(attributes);
112 ilm 165
                } catch (PrinterException e) {
166
                    e.printStackTrace();
167
                }
168
            }
169
        }
170
    }
171
}