Analysis
Overview
We should provide print functionality, which gives the user possibility to print on paper the state of the book at desired moment. The product of the printing action will be very similar to the exported pdf, but on paper. When the user clicks on print, after he chooses the desired print options, the state of the book at this moment must be printed. We're going to drop "print to file" option, we have export to html and pdf.
Task requirements
- Make print works again.
- Remove the "print to file" related functionality.
- Use as much as possible of the "export to PDF" logic, so that the visible result will be the same.
Task result
The result of this task must be code.
Implementation idea
See pdf export tasks. The exported pdf is very similar to the contents that must be printed.
Related
BOOK_PRINTING_R1
BOOK_STATIC_PDF_EXPORT_R0
How to demo
- Open book
- Choose print from the file menu or press Ctrl+P
Design
We will use the java.awt.print API. The simplest workflow for printing with this API consists of the following steps:
- Obtain a java.awt.print.PrinterJob instance.
- Pass a java.awt.print.Printable or java.awt.print.Pageable object to the PrinterJob instance (we will use Pageable since it fits our needs better).
- Call PrinterJob.printDialog() and if it returns true, call PrinterJob.print().
The only thing we need to do to make this work in Sophie2 is to create a custom Pageable object at step 2. We will create a java.awt.print.Book (which implements Pageable) by passing it a PagePainter (implements Printable) object which can print each page of a Sophie2 book. PagePainter's constructor will have one parameter of type BookView. The PagePainter.print method will draw a page from the BookView (by given page index) to a given Graphics2D object. The drawing will be done in the exact same way as "export to pdf".
As a summary:
- The class PagePainter will be added
- The PrintLogic.ON_PRINT.handle method will be refactored to work according to the above workflow
- The following classes will be removed:
- DefaultSophiePrintable
- AbstractPrintItem
- PrintExtensionPoint
- PageSophiePrintable
- PrintToFileItem
- SophiePrintable
- HotTextSophiePrintable
- PrintToFileDialog
- PrintToFileDialogInput
Since this task reduces all the print-related model things to 1 class, which just implements one method, writing unit tests has is pointless. Furthermore, the printing uses the same logic as the PDF export. So, no automatic tests are provided.
The branch for this task is here.
Implementation
The task is implemented according to the design, with some differences:
- The logic for printing is moved from PrintLogic to SophiePrinterJob.
- "Page setup.." item is added.
Known issues:
- The print now pops up a native print dialog (according to the JavaDoc), which has "apperance" tab on some platforms (seen in Ubuntu, not persistent in Snow Leopard). Clicking this tab might cause a NullPointerException , which unfortunately cannot be caught (Probably thrown in a separate thread).
The branch for this task is branches/private/kyli/print.
Testing
(Place the testing results here.)
Comments
(Write comments for this or later revisions here.)