OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 137 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 137 Rev 168
Line 29... Line 29...
29
import org.jopencalendar.model.Flag;
29
import org.jopencalendar.model.Flag;
30
import org.jopencalendar.model.JCalendarItem;
30
import org.jopencalendar.model.JCalendarItem;
31
 
31
 
32
public class CalendarItemPrinter implements Printable, Pageable {
32
public class CalendarItemPrinter implements Printable, Pageable {
33
 
33
 
34
    private List<JCalendarItem> items;
34
    private static final int RIGHT_MARGIN = 50;
-
 
35
    public static final Font FONT_NORMAL = new Font("Arial", Font.PLAIN, 11);
-
 
36
    public static final Font FONT_BOLD = FONT_NORMAL.deriveFont(Font.BOLD);
35
 
37
 
-
 
38
    private List<JCalendarItem> items;
36
    private List<CalendarItemPage> pages = null;
39
    private List<CalendarItemPage> pages = null;
37
 
-
 
38
    private String title;
40
    private String title;
39
 
-
 
40
    private boolean showDuration = true;
41
    private boolean showDuration = true;
41
 
-
 
42
    private PageFormat pf;
42
    private PageFormat calendarPageFormat;
43
 
43
 
44
    public CalendarItemPrinter(String title, List<JCalendarItem> items, PageFormat pf) {
44
    public CalendarItemPrinter(String title, List<JCalendarItem> items, PageFormat pf) {
45
        this.title = title;
45
        this.title = title;
46
        this.items = items;
46
        this.items = items;
47
        this.pf = pf;
47
        this.calendarPageFormat = pf;
48
    }
48
    }
49
 
49
 
50
    public String getTitle() {
50
    public String getTitle() {
51
        return title;
51
        return title;
52
    }
52
    }
53
 
53
 
54
    public static final Font FONT_NORMAL = new Font("Arial", Font.PLAIN, 11);
-
 
55
    public static final Font FONT_BOLD = FONT_NORMAL.deriveFont(Font.BOLD);
54
    @Override
56
 
-
 
57
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
55
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
58
 
-
 
59
        if (pages == null) {
56
        if (pages == null) {
60
            computeLayout(g, pf);
57
            computeLayout(g, pf);
61
        }
58
        }
62
 
59
 
63
        if (pageIndex >= pages.size()) {
60
        if (pageIndex >= pages.size()) {
Line 66... Line 63...
66
 
63
 
67
        final Graphics2D g2d = (Graphics2D) g;
64
        final Graphics2D g2d = (Graphics2D) g;
68
        g2d.translate(pf.getImageableX(), pf.getImageableY());
65
        g2d.translate(pf.getImageableX(), pf.getImageableY());
69
        final CalendarItemPage page = this.pages.get(pageIndex);
66
        final CalendarItemPage page = this.pages.get(pageIndex);
70
 
67
 
71
        printPage(g, pf, page);
68
        printPage(g, page);
72
 
69
 
73
        /* tell the caller that this page is part of the printed document */
70
        /* tell the caller that this page is part of the printed document */
74
        return PAGE_EXISTS;
71
        return PAGE_EXISTS;
75
    }
72
    }
76
 
73
 
77
    public void printPage(Graphics g, PageFormat pf, final CalendarItemPage page) {
74
    public void printPage(Graphics g, final CalendarItemPage page) {
-
 
75
        // We use our own page format to avoid issue between formatting and printing
-
 
76
        PageFormat pf = this.calendarPageFormat;
-
 
77
 
78
        final Graphics2D g2d = (Graphics2D) g;
78
        final Graphics2D g2d = (Graphics2D) g;
79
        // Title
79
        // Title
80
        g.setFont(getTitleFont());
80
        g.setFont(getTitleFont());
81
        g.setColor(getTitleColor());
81
        g.setColor(getTitleColor());
82
        final int xTitle = ((int) pf.getImageableWidth() - (int) g2d.getFontMetrics().getStringBounds(getTitle(), g2d).getWidth()) / 2;
82
        final int xTitle = ((int) pf.getImageableWidth() - (int) g2d.getFontMetrics().getStringBounds(getTitle(), g2d).getWidth()) / 2;
Line 90... Line 90...
90
        final String strPages = (page.getPageIndex() + 1) + " / " + pages.size();
90
        final String strPages = (page.getPageIndex() + 1) + " / " + pages.size();
91
        int xStrPages = (int) pf.getImageableWidth() - (int) g2d.getFontMetrics().getStringBounds(strPages, g2d).getWidth();
91
        int xStrPages = (int) pf.getImageableWidth() - (int) g2d.getFontMetrics().getStringBounds(strPages, g2d).getWidth();
92
        g.drawString(strPages, xStrPages, g2d.getFontMetrics().getHeight());
92
        g.drawString(strPages, xStrPages, g2d.getFontMetrics().getHeight());
93
 
93
 
94
        final double hourColumnWidth = getHourColumnWidth();
94
        final double hourColumnWidth = getHourColumnWidth();
-
 
95
        final double maxWidth = pf.getImageableWidth() - hourColumnWidth - getMarginRight();
95
        for (int i = 0; i < size; i++) {
96
        for (int i = 0; i < size; i++) {
96
            final JCalendarItem item = page.getItems().get(i);
97
            final JCalendarItem item = page.getItems().get(i);
97
            final double lineHeight = page.getHeights().get(i);
98
            final double lineHeight = page.getHeights().get(i);
98
            int lY = y;
99
            int lY = y;
99
            drawBackground(0, lY, (int) pf.getImageableWidth(), (int) lineHeight);
100
            drawBackground(0, lY, (int) pf.getImageableWidth(), (int) lineHeight);
Line 122... Line 123...
122
                g.setFont(getDurationFont(item));
123
                g.setFont(getDurationFont(item));
123
                final int durationY = hourY + g2d.getFontMetrics().getHeight();
124
                final int durationY = hourY + g2d.getFontMetrics().getHeight();
124
                g2d.drawString(formatDuration(dtStart, dtEnd), 3, durationY);
125
                g2d.drawString(formatDuration(dtStart, dtEnd), 3, durationY);
125
            }
126
            }
126
 
127
 
127
            final double maxWidth = pf.getImageableWidth() - hourColumnWidth;
-
 
128
            // Summary
128
            // Summary
129
            g.setFont(getLine1Font(item));
129
            g.setFont(getLine1Font(item));
130
 
130
 
131
            final List<String> l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), (int) maxWidth);
131
            final List<String> l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), (int) maxWidth);
132
            if (item.hasFlag(Flag.getFlag("warning"))) {
132
            if (item.hasFlag(Flag.getFlag("warning"))) {
133
                g2d.setColor(new Color(255, 249, 144));
133
                g2d.setColor(new Color(255, 249, 144));
134
                g2d.fillRect((int) hourColumnWidth - 1, lY + 3, (int) maxWidth, (int) g2d.getFontMetrics().getHeight() * l1.size());
134
                g2d.fillRect((int) hourColumnWidth - 1, lY + 3, (int) maxWidth, g2d.getFontMetrics().getHeight() * l1.size());
135
            }
135
            }
136
            g.setColor(getLine1Color(item));
136
            g.setColor(getLine1Color(item));
137
            for (String string : l1) {
137
            for (String string : l1) {
138
                lY += g2d.getFontMetrics().getHeight();
138
                lY += g2d.getFontMetrics().getHeight();
139
                g2d.drawString(string, (int) hourColumnWidth, lY);
139
                g2d.drawString(string, (int) hourColumnWidth, lY);
Line 149... Line 149...
149
            //
149
            //
150
            y += lineHeight;
150
            y += lineHeight;
151
        }
151
        }
152
    }
152
    }
153
 
153
 
-
 
154
    private int getMarginRight() {
-
 
155
        return RIGHT_MARGIN;
-
 
156
    }
-
 
157
 
154
    public String getDate(final DateFormat df, final JCalendarItem item) {
158
    public String getDate(final DateFormat df, final JCalendarItem item) {
155
        return LayoutUtils.firstUp(df.format(item.getDtStart().getTime()));
159
        return LayoutUtils.firstUp(df.format(item.getDtStart().getTime()));
156
    }
160
    }
157
 
161
 
158
    public String formatDuration(Calendar dtStart, Calendar dtEnd) {
162
    public String formatDuration(Calendar dtStart, Calendar dtEnd) {
Line 253... Line 257...
253
        }
257
        }
254
        return h + ":" + m;
258
        return h + ":" + m;
255
    }
259
    }
256
 
260
 
257
    private void computeLayout(Graphics g, PageFormat pf) {
261
    private void computeLayout(Graphics g, PageFormat pf) {
-
 
262
        int pageIndex = 0;
258
        pages = new ArrayList<CalendarItemPage>();
263
        pages = new ArrayList<CalendarItemPage>();
259
        CalendarItemPage page = new CalendarItemPage();
264
        CalendarItemPage page = new CalendarItemPage();
-
 
265
        page.setPageIndex(pageIndex);
260
        this.pages.add(page);
266
        this.pages.add(page);
-
 
267
        pageIndex++;
261
        if (items.isEmpty()) {
268
        if (items.isEmpty()) {
262
            return;
269
            return;
263
        }
270
        }
264
        double remainingHeight = pf.getImageableHeight() - getTitleHeight();
271
        double remainingHeight = pf.getImageableHeight() - getTitleHeight();
265
        boolean showDate = true;
272
        boolean showDate = true;
Line 273... Line 280...
273
            c = item.getDtStart();
280
            c = item.getDtStart();
274
            double h = getPrintHeight(g, pf, item);
281
            double h = getPrintHeight(g, pf, item);
275
            double secureMargin = 20D;
282
            double secureMargin = 20D;
276
            if (remainingHeight < h + secureMargin) {
283
            if (remainingHeight < h + secureMargin) {
277
                page = new CalendarItemPage();
284
                page = new CalendarItemPage();
-
 
285
                page.setPageIndex(pageIndex);
278
                showDate = true;
286
                showDate = true;
279
                remainingHeight = pf.getImageableHeight() - getTitleHeight();
287
                remainingHeight = pf.getImageableHeight() - getTitleHeight();
280
                this.pages.add(page);
288
                this.pages.add(page);
-
 
289
                pageIndex++;
-
 
290
 
281
            }
291
            }
282
            if (showDate) {
292
            if (showDate) {
283
                g.setFont(getHourFont(item));
293
                g.setFont(getHourFont(item));
284
                h += g.getFontMetrics().getHeight();
294
                h += g.getFontMetrics().getHeight();
285
            }
295
            }
Line 299... Line 309...
299
    public int getTitleHeight() {
309
    public int getTitleHeight() {
300
        return 40;
310
        return 40;
301
    }
311
    }
302
 
312
 
303
    private double getPrintHeight(Graphics g, PageFormat pf, JCalendarItem item) {
313
    private double getPrintHeight(Graphics g, PageFormat pf, JCalendarItem item) {
-
 
314
        double heigth = 0;
304
        double maxWidth = pf.getImageableWidth() - getHourColumnWidth();
315
        int maxWidth = (int) pf.getImageableWidth() - getHourColumnWidth() - getMarginRight();
-
 
316
        // Line 1
305
        g.setFont(this.getLine1Font(item));
317
        g.setFont(this.getLine1Font(item));
306
        int l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), (int) maxWidth).size();
318
        int l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), maxWidth).size();
-
 
319
        heigth += l1 * g.getFontMetrics().getHeight();
-
 
320
        // Line 2
307
        g.setFont(this.getLine2Font(item));
321
        g.setFont(this.getLine2Font(item));
308
        int l2 = LayoutUtils.wrap(getLine2Text(item), g.getFontMetrics(), (int) maxWidth).size();
322
        int l2 = LayoutUtils.wrap(getLine2Text(item), g.getFontMetrics(), maxWidth).size();
309
        return (l1 + l2) * g.getFontMetrics().getHeight();
323
        heigth += l2 * g.getFontMetrics().getHeight();
-
 
324
        return heigth;
310
    }
325
    }
311
 
326
 
312
    public List<CalendarItemPage> getPages() {
327
    public List<CalendarItemPage> getPages() {
313
        return pages;
328
        return pages;
314
    }
329
    }
315
 
330
 
316
    public static void main(String args[]) {
331
    public static void main(String[] args) {
317
        SwingUtilities.invokeLater(new Runnable() {
332
        SwingUtilities.invokeLater(new Runnable() {
318
 
333
 
319
            @Override
334
            @Override
320
            public void run() {
335
            public void run() {
321
                try {
336
                try {
Line 329... Line 344...
329
                printButton.addActionListener(new ActionListener() {
344
                printButton.addActionListener(new ActionListener() {
330
 
345
 
331
                    @Override
346
                    @Override
332
                    public void actionPerformed(ActionEvent e) {
347
                    public void actionPerformed(ActionEvent e) {
333
                        PrinterJob job = PrinterJob.getPrinterJob();
348
                        PrinterJob job = PrinterJob.getPrinterJob();
334
                        List<JCalendarItem> items = new ArrayList<JCalendarItem>();
349
                        List<JCalendarItem> cItems = new ArrayList<JCalendarItem>();
335
                        Calendar c = Calendar.getInstance();
350
                        Calendar c = Calendar.getInstance();
336
                        Flag.register(new Flag("warning", null, "Warning", "A default warning"));
351
                        Flag.register(new Flag("warning", null, "Warning", "A default warning"));
337
                        for (int i = 0; i < 50; i++) {
352
                        for (int i = 0; i < 50; i++) {
338
                            JCalendarItem item = new JCalendarItem();
353
                            JCalendarItem item = new JCalendarItem();
339
                            item.setSummary("Item " + i);
354
                            item.setSummary("Item " + i);
Line 341... Line 356...
341
                            d.append("Description");
356
                            d.append("Description");
342
                            for (int j = 0; j < i; j++) {
357
                            for (int j = 0; j < i; j++) {
343
                                if (i % 2 == 0)
358
                                if (i % 2 == 0)
344
                                    d.append(" Hello");
359
                                    d.append(" Hello");
345
                                else
360
                                else
346
                                    d.append(" World");
361
                                    d.append(" Wo");
347
                            }
362
                            }
348
                            d.append("END");
363
                            d.append("END");
349
                            if (i % 6 == 0) {
364
                            if (i % 6 == 0) {
350
                                item.addFlag(Flag.getFlag("warning"));
365
                                item.addFlag(Flag.getFlag("warning"));
351
                            }
366
                            }
-
 
367
                            if (i != 5) {
352
                            item.setDescription(d.toString());
368
                                item.setDescription(d.toString());
-
 
369
                            } else {
-
 
370
                                item.setDescription(
-
 
371
                                        "Escalier Ménage Complet - 2 escaliers : Interrupteurs, poignées portes, mains courantes côté route, traces de doigts sur vitres et ascenseur, aspiration moquettes et carrelage, lavage des halls.");
-
 
372
                            }
353
                            item.setDtStart(c);
373
                            item.setDtStart(c);
354
                            c.add(Calendar.HOUR_OF_DAY, 1);
374
                            c.add(Calendar.HOUR_OF_DAY, 1);
355
                            item.setDtEnd(c);
375
                            item.setDtEnd(c);
356
                            c.add(Calendar.HOUR_OF_DAY, 1);
376
                            c.add(Calendar.HOUR_OF_DAY, 1);
357
                            items.add(item);
377
                            cItems.add(item);
358
                        }
378
                        }
359
                        final PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
379
                        final PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
360
                        printAttributes.add(PrintQuality.HIGH);
380
                        printAttributes.add(PrintQuality.HIGH);
361
                        job.setPrintable(new CalendarItemPrinter("OpenConcerto", items, job.getPageFormat(printAttributes)));
381
                        job.setPrintable(new CalendarItemPrinter("OpenConcerto", cItems, job.getPageFormat(printAttributes)));
362
                        boolean ok = job.printDialog();
382
                        boolean ok = job.printDialog();
363
                        if (ok) {
383
                        if (ok) {
364
                            try {
384
                            try {
365
                                job.print();
385
                                job.print();
366
                            } catch (PrinterException ex) {
386
                            } catch (PrinterException ex) {
Line 384... Line 404...
384
    }
404
    }
385
 
405
 
386
    @Override
406
    @Override
387
    public int getNumberOfPages() {
407
    public int getNumberOfPages() {
388
        if (this.pages == null) {
408
        if (this.pages == null) {
389
            BufferedImage off_Image = new BufferedImage((int) pf.getHeight(), (int) pf.getWidth(), BufferedImage.TYPE_INT_ARGB);
409
            BufferedImage offImage = new BufferedImage((int) calendarPageFormat.getHeight(), (int) calendarPageFormat.getWidth(), BufferedImage.TYPE_INT_ARGB);
390
            Graphics2D g2 = off_Image.createGraphics();
410
            Graphics2D g2 = offImage.createGraphics();
391
            computeLayout(g2, pf);
411
            computeLayout(g2, calendarPageFormat);
392
        }
412
        }
393
        return this.pages.size();
413
        return this.pages.size();
394
    }
414
    }
395
 
415
 
396
    @Override
416
    @Override
397
    public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
417
    public PageFormat getPageFormat(int pageIndex) {
398
        return this.pf;
418
        return this.calendarPageFormat;
399
    }
419
    }
400
 
420
 
401
    @Override
421
    @Override
402
    public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
422
    public Printable getPrintable(int pageIndex) {
403
        final CalendarItemPage page = this.pages.get(pageIndex);
423
        final CalendarItemPage page = this.pages.get(pageIndex);
404
        return new Printable() {
424
        return new Printable() {
405
 
425
 
406
            @Override
426
            @Override
407
            public int print(Graphics graphics, PageFormat pageFormat, int i) throws PrinterException {
427
            public int print(Graphics graphics, PageFormat pageFormat, int i) throws PrinterException {
408
                final Graphics2D g2d = (Graphics2D) graphics;
428
                final Graphics2D g2d = (Graphics2D) graphics;
409
                g2d.translate(pf.getImageableX(), pf.getImageableY());
429
                g2d.translate(calendarPageFormat.getImageableX(), calendarPageFormat.getImageableY());
410
                printPage(graphics, pageFormat, page);
430
                printPage(graphics, page);
411
                return PAGE_EXISTS;
431
                return PAGE_EXISTS;
412
            }
432
            }
413
        };
433
        };
414
    }
434
    }
415
}
435
}