OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 158 | 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) {
181 ilm 46
 
47
        System.err.println("CalendarPrintPanel.CalendarPrintPanel()" + selectedUsers);
112 ilm 48
        preview.setSelected(true);
49
        //
50
        this.setLayout(new GridBagLayout());
51
        final GridBagConstraints c = new DefaultGridBagConstraints();
52
        final JLabel l = new JLabel("Date de début", SwingConstants.RIGHT);
53
        this.add(l, c);
54
        c.gridx++;
55
        final JDate d1 = new JDate(false, true);
56
        Calendar cal = Calendar.getInstance();
57
        cal.clear();
58
        cal.set(Calendar.YEAR, year);
59
        cal.set(Calendar.WEEK_OF_YEAR, week);
60
        cal.set(Calendar.HOUR_OF_DAY, 0);
61
        cal.set(Calendar.MINUTE, 0);
62
        cal.set(Calendar.SECOND, 0);
63
        cal.set(Calendar.MILLISECOND, 0);
64
        d1.setDate(cal.getTime());
65
        c.weightx = 1;
66
        this.add(d1, c);
67
        c.gridx = 0;
68
        c.gridy++;
69
        c.weightx = 0;
70
        final JLabel l2 = new JLabel("Date de fin", SwingConstants.RIGHT);
71
 
72
        this.add(l2, c);
73
        c.gridx++;
74
        final JDate d2 = new JDate(false, true);
75
        cal.add(Calendar.DAY_OF_YEAR, 7);
76
        d2.setDate(cal.getTime());
77
        c.weightx = 1;
78
        this.add(d2, c);
79
        final JPanel p = new JPanel();
80
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));
81
        p.add(preview);
82
        p.add(bPrint);
83
        c.gridwidth = 2;
84
        c.gridx = 0;
85
        c.gridy++;
86
        c.fill = GridBagConstraints.NONE;
87
        c.anchor = GridBagConstraints.SOUTHEAST;
88
        this.add(p, c);
89
        //
90
 
91
        bPrint.addActionListener(new ActionListener() {
92
 
93
            @Override
94
            public void actionPerformed(ActionEvent e) {
95
                if (d1.getDate().after(d2.getDate())) {
96
                    return;
97
                }
98
 
158 ilm 99
                final List<Pageable> p = new ArrayList<>();
112 ilm 100
 
101
                for (User user : selectedUsers) {
158 ilm 102
                    final List<User> l = new ArrayList<>();
112 ilm 103
                    l.add(user);
104
                    final List<JCalendarItem> itemInWeek = manager.getItemIn(d1.getDate(), d2.getDate(), l, selectedStates);
105
                    if (!itemInWeek.isEmpty()) {
106
                        Collections.sort(itemInWeek, new Comparator<JCalendarItem>() {
107
                            @Override
108
                            public int compare(JCalendarItem o1, JCalendarItem o2) {
109
                                return o1.getDtStart().compareTo(o2.getDtStart());
110
                            }
111
                        });
112
                        final PageFormat pf = new PageFormat();
113
                        pf.setPaper(new A4());
181 ilm 114
                        List<JCalendarItem> itemsToWork = new ArrayList<>();
115
                        for (JCalendarItem item : itemInWeek) {
116
                            if (!item.hasFlag(ModuleOperation.FREE_TIME_FLAG)) {
117
                                itemsToWork.add(item);
118
                            }
119
                        }
112 ilm 120
 
181 ilm 121
                        final CalendarItemPrinter printable = new OperationCalendarItemPrinter(user.getFullName(), itemInWeek, pf, itemsToWork);
122
 
112 ilm 123
                        p.add(printable);
124
                    }
125
                }
126
                if (p.isEmpty()) {
127
                    JOptionPane.showMessageDialog(CalendarPrintPanel.this, "Aucune page à imprimer.\nMerci de vérifier la période.");
128
                    return;
129
                }
130
                if (preview.isSelected()) {
131
 
132
                    final PrintPreviewFrame f = new PrintPreviewFrame(new ListOfPageable(p)) {
133
                        @Override
134
                        public void printAllPages() {
135
                            printPages(p);
136
                            closeFrame();
137
                        }
138
 
139
                    };
140
                    f.pack();
141
                    f.setLocationRelativeTo(CalendarPrintPanel.this);
142
                    f.setVisible(true);
143
                } else {
144
                    printPages(p);
145
                    closeFrame();
146
                }
147
            }
148
        });
149
    }
150
 
151
    protected void closeFrame() {
152
        SwingUtilities.getWindowAncestor(this).dispose();
153
    }
154
 
155
    public void printPages(final List<Pageable> pageables) {
156
        PrinterJob job = PrinterJob.getPrinterJob();
157
        boolean ok = job.printDialog();
158
        if (ok) {
158 ilm 159
            final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
160
            final MediaSizeName media = MediaSizeName.ISO_A4;
161
            attributes.add(media);
162
            final Paper paper = new A4();
163
 
164
            final MediaPrintableArea printableArea = new MediaPrintableArea((float) (paper.getImageableX() / POINTS_PER_INCH), (float) (paper.getImageableY() / POINTS_PER_INCH),
165
                    (float) (paper.getImageableWidth() / POINTS_PER_INCH), (float) (paper.getImageableHeight() / POINTS_PER_INCH), Size2DSyntax.INCH);
166
            attributes.add(printableArea);
167
 
112 ilm 168
            for (Pageable document : pageables) {
169
                PageFormat p = job.getPageFormat(null);
170
                job.getPageFormat(null).getImageableHeight();
171
                job.setPageable(document);
172
                try {
158 ilm 173
                    job.print(attributes);
112 ilm 174
                } catch (PrinterException e) {
175
                    e.printStackTrace();
176
                }
177
            }
178
        }
179
    }
180
}