OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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