31,20 → 31,20 |
|
public class CalendarItemPrinter implements Printable, Pageable { |
|
private static final int RIGHT_MARGIN = 50; |
public static final Font FONT_NORMAL = new Font("Arial", Font.PLAIN, 11); |
public static final Font FONT_BOLD = FONT_NORMAL.deriveFont(Font.BOLD); |
|
private List<JCalendarItem> items; |
|
private List<CalendarItemPage> pages = null; |
|
private String title; |
|
private boolean showDuration = true; |
private PageFormat calendarPageFormat; |
|
private PageFormat pf; |
|
public CalendarItemPrinter(String title, List<JCalendarItem> items, PageFormat pf) { |
this.title = title; |
this.items = items; |
this.pf = pf; |
this.calendarPageFormat = pf; |
} |
|
public String getTitle() { |
51,11 → 51,8 |
return title; |
} |
|
public static final Font FONT_NORMAL = new Font("Arial", Font.PLAIN, 11); |
public static final Font FONT_BOLD = FONT_NORMAL.deriveFont(Font.BOLD); |
|
@Override |
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { |
|
if (pages == null) { |
computeLayout(g, pf); |
} |
68,13 → 65,16 |
g2d.translate(pf.getImageableX(), pf.getImageableY()); |
final CalendarItemPage page = this.pages.get(pageIndex); |
|
printPage(g, pf, page); |
printPage(g, page); |
|
/* tell the caller that this page is part of the printed document */ |
return PAGE_EXISTS; |
} |
|
public void printPage(Graphics g, PageFormat pf, final CalendarItemPage page) { |
public void printPage(Graphics g, final CalendarItemPage page) { |
// We use our own page format to avoid issue between formatting and printing |
PageFormat pf = this.calendarPageFormat; |
|
final Graphics2D g2d = (Graphics2D) g; |
// Title |
g.setFont(getTitleFont()); |
92,6 → 92,7 |
g.drawString(strPages, xStrPages, g2d.getFontMetrics().getHeight()); |
|
final double hourColumnWidth = getHourColumnWidth(); |
final double maxWidth = pf.getImageableWidth() - hourColumnWidth - getMarginRight(); |
for (int i = 0; i < size; i++) { |
final JCalendarItem item = page.getItems().get(i); |
final double lineHeight = page.getHeights().get(i); |
124,7 → 125,6 |
g2d.drawString(formatDuration(dtStart, dtEnd), 3, durationY); |
} |
|
final double maxWidth = pf.getImageableWidth() - hourColumnWidth; |
// Summary |
g.setFont(getLine1Font(item)); |
|
131,7 → 131,7 |
final List<String> l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), (int) maxWidth); |
if (item.hasFlag(Flag.getFlag("warning"))) { |
g2d.setColor(new Color(255, 249, 144)); |
g2d.fillRect((int) hourColumnWidth - 1, lY + 3, (int) maxWidth, (int) g2d.getFontMetrics().getHeight() * l1.size()); |
g2d.fillRect((int) hourColumnWidth - 1, lY + 3, (int) maxWidth, g2d.getFontMetrics().getHeight() * l1.size()); |
} |
g.setColor(getLine1Color(item)); |
for (String string : l1) { |
151,6 → 151,10 |
} |
} |
|
private int getMarginRight() { |
return RIGHT_MARGIN; |
} |
|
public String getDate(final DateFormat df, final JCalendarItem item) { |
return LayoutUtils.firstUp(df.format(item.getDtStart().getTime())); |
} |
255,9 → 259,12 |
} |
|
private void computeLayout(Graphics g, PageFormat pf) { |
int pageIndex = 0; |
pages = new ArrayList<CalendarItemPage>(); |
CalendarItemPage page = new CalendarItemPage(); |
page.setPageIndex(pageIndex); |
this.pages.add(page); |
pageIndex++; |
if (items.isEmpty()) { |
return; |
} |
275,9 → 282,12 |
double secureMargin = 20D; |
if (remainingHeight < h + secureMargin) { |
page = new CalendarItemPage(); |
page.setPageIndex(pageIndex); |
showDate = true; |
remainingHeight = pf.getImageableHeight() - getTitleHeight(); |
this.pages.add(page); |
pageIndex++; |
|
} |
if (showDate) { |
g.setFont(getHourFont(item)); |
301,12 → 311,17 |
} |
|
private double getPrintHeight(Graphics g, PageFormat pf, JCalendarItem item) { |
double maxWidth = pf.getImageableWidth() - getHourColumnWidth(); |
double heigth = 0; |
int maxWidth = (int) pf.getImageableWidth() - getHourColumnWidth() - getMarginRight(); |
// Line 1 |
g.setFont(this.getLine1Font(item)); |
int l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), (int) maxWidth).size(); |
int l1 = LayoutUtils.wrap(getLine1Text(item), g.getFontMetrics(), maxWidth).size(); |
heigth += l1 * g.getFontMetrics().getHeight(); |
// Line 2 |
g.setFont(this.getLine2Font(item)); |
int l2 = LayoutUtils.wrap(getLine2Text(item), g.getFontMetrics(), (int) maxWidth).size(); |
return (l1 + l2) * g.getFontMetrics().getHeight(); |
int l2 = LayoutUtils.wrap(getLine2Text(item), g.getFontMetrics(), maxWidth).size(); |
heigth += l2 * g.getFontMetrics().getHeight(); |
return heigth; |
} |
|
public List<CalendarItemPage> getPages() { |
313,7 → 328,7 |
return pages; |
} |
|
public static void main(String args[]) { |
public static void main(String[] args) { |
SwingUtilities.invokeLater(new Runnable() { |
|
@Override |
331,7 → 346,7 |
@Override |
public void actionPerformed(ActionEvent e) { |
PrinterJob job = PrinterJob.getPrinterJob(); |
List<JCalendarItem> items = new ArrayList<JCalendarItem>(); |
List<JCalendarItem> cItems = new ArrayList<JCalendarItem>(); |
Calendar c = Calendar.getInstance(); |
Flag.register(new Flag("warning", null, "Warning", "A default warning")); |
for (int i = 0; i < 50; i++) { |
343,22 → 358,27 |
if (i % 2 == 0) |
d.append(" Hello"); |
else |
d.append(" World"); |
d.append(" Wo"); |
} |
d.append("END"); |
if (i % 6 == 0) { |
item.addFlag(Flag.getFlag("warning")); |
} |
item.setDescription(d.toString()); |
if (i != 5) { |
item.setDescription(d.toString()); |
} else { |
item.setDescription( |
"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."); |
} |
item.setDtStart(c); |
c.add(Calendar.HOUR_OF_DAY, 1); |
item.setDtEnd(c); |
c.add(Calendar.HOUR_OF_DAY, 1); |
items.add(item); |
cItems.add(item); |
} |
final PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet(); |
printAttributes.add(PrintQuality.HIGH); |
job.setPrintable(new CalendarItemPrinter("OpenConcerto", items, job.getPageFormat(printAttributes))); |
job.setPrintable(new CalendarItemPrinter("OpenConcerto", cItems, job.getPageFormat(printAttributes))); |
boolean ok = job.printDialog(); |
if (ok) { |
try { |
386,20 → 406,20 |
@Override |
public int getNumberOfPages() { |
if (this.pages == null) { |
BufferedImage off_Image = new BufferedImage((int) pf.getHeight(), (int) pf.getWidth(), BufferedImage.TYPE_INT_ARGB); |
Graphics2D g2 = off_Image.createGraphics(); |
computeLayout(g2, pf); |
BufferedImage offImage = new BufferedImage((int) calendarPageFormat.getHeight(), (int) calendarPageFormat.getWidth(), BufferedImage.TYPE_INT_ARGB); |
Graphics2D g2 = offImage.createGraphics(); |
computeLayout(g2, calendarPageFormat); |
} |
return this.pages.size(); |
} |
|
@Override |
public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException { |
return this.pf; |
public PageFormat getPageFormat(int pageIndex) { |
return this.calendarPageFormat; |
} |
|
@Override |
public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException { |
public Printable getPrintable(int pageIndex) { |
final CalendarItemPage page = this.pages.get(pageIndex); |
return new Printable() { |
|
406,8 → 426,8 |
@Override |
public int print(Graphics graphics, PageFormat pageFormat, int i) throws PrinterException { |
final Graphics2D g2d = (Graphics2D) graphics; |
g2d.translate(pf.getImageableX(), pf.getImageableY()); |
printPage(graphics, pageFormat, page); |
g2d.translate(calendarPageFormat.getImageableX(), calendarPageFormat.getImageableY()); |
printPage(graphics, page); |
return PAGE_EXISTS; |
} |
}; |