xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mathias Kalb <mathias.k...@prodato.de>
Subject PrintTranscoder dialog sequence
Date Thu, 14 Apr 2005 14:10:39 GMT
Hello,

I think the sequence of the two dialogs (printer and page) in the
PrintTranscoder is wrong.

The PrintTranscoder shows at first the page dialog and then the printer
dialog. If you have many printers which supports diffent page formats
then you can only choose some of page formats. The possible page formats
depends on the printer you choose.

I wrote a new class which extends the PrintTranscoder (Batik 1.5.1) and
which changed the print method. It also validates the page format
because sometimes the values are wrong.


   public void print() throws PrinterException {
     //
     // Now, request the transcoder to actually perform the
     // printing job.
     //
     PrinterJob printerJob = PrinterJob.getPrinterJob();

     //
     // If required, pop up a dialog to select the printer
     //
     Boolean showPrinterDialog = (Boolean) 
hints.get(KEY_SHOW_PRINTER_DIALOG);

     if ((showPrinterDialog != null) && showPrinterDialog.booleanValue()) {
       if (!printerJob.printDialog()) {
         // Dialog was cancelled, meaning that the print process
         // should be stopped.
         return;
       }
     }

     PageFormat pageFormat = printerJob.defaultPage();

     //
     // Set the page parameters from the hints
     //
     Paper paper = pageFormat.getPaper();

     Float pageWidth = (Float) hints.get(KEY_PAGE_WIDTH);
     Float pageHeight = (Float) hints.get(KEY_PAGE_HEIGHT);

     if (pageWidth != null) {
       paper.setSize(pageWidth.floatValue(), paper.getHeight());
     }

     if (pageHeight != null) {
       paper.setSize(paper.getWidth(), pageHeight.floatValue());
     }

     float x = 0;
     float y = 0;
     float width = (float) paper.getWidth();
     float height = (float) paper.getHeight();

     Float leftMargin = (Float) hints.get(KEY_MARGIN_LEFT);
     Float topMargin = (Float) hints.get(KEY_MARGIN_TOP);
     Float rightMargin = (Float) hints.get(KEY_MARGIN_RIGHT);
     Float bottomMargin = (Float) hints.get(KEY_MARGIN_BOTTOM);

     if (leftMargin != null) {
       x = leftMargin.floatValue();
       width -= leftMargin.floatValue();
     }

     if (topMargin != null) {
       y = topMargin.floatValue();
       height -= topMargin.floatValue();
     }

     if (rightMargin != null) {
       width -= rightMargin.floatValue();
     }

     if (bottomMargin != null) {
       height -= bottomMargin.floatValue();
     }

     paper.setImageableArea(x, y, width, height);

     String pageOrientation = (String) hints.get(KEY_PAGE_ORIENTATION);

     if 
(VALUE_PAGE_ORIENTATION_PORTRAIT.equalsIgnoreCase(pageOrientation)) {
       pageFormat.setOrientation(pageFormat.PORTRAIT);
     } else if 
(VALUE_PAGE_ORIENTATION_LANDSCAPE.equalsIgnoreCase(pageOrientation)) {
       pageFormat.setOrientation(pageFormat.LANDSCAPE);
     } else if 
(VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE.equalsIgnoreCase(pageOrientation)) 
{
       pageFormat.setOrientation(pageFormat.REVERSE_LANDSCAPE);
     }

     pageFormat.setPaper(paper);
     pageFormat = printerJob.validatePage(pageFormat);


     //
     // If required, pop up a dialog to adjust the page format
     //
     Boolean showPageFormat = (Boolean) hints.get(KEY_SHOW_PAGE_DIALOG);

     if ((showPageFormat != null) && showPageFormat.booleanValue()) {
       do {
         PageFormat tmpPageFormat = printerJob.pageDialog(pageFormat);

         if (tmpPageFormat == pageFormat) {
           // Dialog was cancelled, meaning that the print process should
           // be stopped.
           return;
         }

         // validates the new format
         pageFormat = printerJob.validatePage(tmpPageFormat);


         // check the format
         // sometimes a value (width/height) is <= 0.0
       } while (!isValidPageFormat(pageFormat));
     }

     // Print now
     printerJob.setPrintable(this, pageFormat);

     printerJob.print();
   }

   protected boolean isValidPageFormat(PageFormat pageFormat) {
     return (pageFormat.getWidth() > 0.0) && (pageFormat.getHeight() > 
0.0) &&
            (pageFormat.getImageableWidth() > 0.0) &&
            (pageFormat.getImageableHeight() > 0.0) &&
            (pageFormat.getPaper().getWidth() > 0.0) &&
            (pageFormat.getPaper().getHeight() > 0.0) &&
            (pageFormat.getPaper().getImageableWidth() > 0.0) &&
            (pageFormat.getPaper().getImageableHeight() > 0.0);
   }


Regards
Mathias Kalb

-- 
ProDatO Integration Technology GmbH
Hauptstrasse 60, 91054 Erlangen
http://www.prodato.de

Mathias Kalb, Dipl.-Inf. (FH)
mailto:mathias.kalb@prodato.de
fon: +49-9131-612877
fax: +49-9131-612881

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message