Coverage Report - org.jtheque.utils.print.PrintUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PrintUtils
0%
0/50
0%
0/8
1.75
 
 1  
 package org.jtheque.utils.print;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.utils.io.FileUtils;
 20  
 import org.slf4j.LoggerFactory;
 21  
 import sun.print.DialogTypeSelection;
 22  
 
 23  
 import javax.print.attribute.HashPrintRequestAttributeSet;
 24  
 import javax.print.attribute.PrintRequestAttributeSet;
 25  
 import javax.print.attribute.standard.OrientationRequested;
 26  
 import javax.swing.JFrame;
 27  
 import javax.swing.JTextArea;
 28  
 import javax.swing.RepaintManager;
 29  
 import javax.swing.text.JTextComponent;
 30  
 import java.awt.Component;
 31  
 import java.awt.FontMetrics;
 32  
 import java.awt.Window;
 33  
 import java.awt.print.PageFormat;
 34  
 import java.awt.print.Paper;
 35  
 import java.awt.print.Printable;
 36  
 import java.awt.print.PrinterException;
 37  
 import java.awt.print.PrinterJob;
 38  
 import java.io.InputStream;
 39  
 import java.util.Collection;
 40  
 
 41  
 /**
 42  
  * This class provide utility to print.
 43  
  *
 44  
  * @author Baptiste Wicht
 45  
  */
 46  
 public final class PrintUtils {
 47  
     /* Variables */
 48  0
     private static final Paper A4_PAPER = new A4();
 49  
 
 50  
     /**
 51  
      * Construct a new <code>PrintUtils</code>. This class is an utils code
 52  
      */
 53  
     private PrintUtils() {
 54  0
         super();
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Print a String.
 59  
      *
 60  
      * @param toPrint The String to print
 61  
      */
 62  
     public static void printString(String toPrint) {
 63  0
         Window frame = new JFrame();
 64  
 
 65  0
         JTextComponent area = new JTextArea();
 66  0
         area.setText(toPrint);
 67  
 
 68  0
         frame.add(area);
 69  0
         frame.pack();
 70  
 
 71  0
         printComponent(area);
 72  
 
 73  0
         frame.dispose();
 74  0
     }
 75  
 
 76  
     /**
 77  
      * Print the component.
 78  
      *
 79  
      * @param component the component to print
 80  
      */
 81  
     private static void printComponent(Component component) {
 82  0
         Printable printer = new ComponentPrinter(component);
 83  
 
 84  0
         PrinterJob printJob = PrinterJob.getPrinterJob();
 85  0
         PageFormat pageFormat = printJob.defaultPage();
 86  
 
 87  0
         pageFormat.setPaper(A4_PAPER);
 88  
 
 89  0
         printJob.setPrintable(printer, pageFormat);
 90  
 
 91  0
         if (printJob.printDialog()) {
 92  
             try {
 93  0
                 printJob.print();
 94  0
             } catch (PrinterException pe) {
 95  0
                 LoggerFactory.getLogger(PrintUtils.class).debug("Unable to print", pe);
 96  0
             }
 97  
         }
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Print a list of <code>String</code>.
 102  
      *
 103  
      * @param list The list of <code>String</code> we want to print
 104  
      */
 105  
     public static void printArrayString(Collection<String> list) {
 106  0
         PrintRequestAttributeSet request = new HashPrintRequestAttributeSet();
 107  
 
 108  0
         request.add(OrientationRequested.LANDSCAPE);
 109  0
         request.add(DialogTypeSelection.NATIVE);
 110  
 
 111  0
         PrinterJob job = PrinterJob.getPrinterJob();
 112  
 
 113  0
         job.setPageable(new ArrayPrinter(list));
 114  
 
 115  0
         if (job.printDialog(request)) {
 116  
             try {
 117  0
                 job.print();
 118  0
             } catch (PrinterException pe) {
 119  0
                 LoggerFactory.getLogger(PrintUtils.class).debug("Unable to print", pe);
 120  0
             }
 121  
         }
 122  0
     }
 123  
 
 124  
     /**
 125  
      * Modify the state of the double buffering on the component.
 126  
      *
 127  
      * @param doubleBuffer The new state of double buffering
 128  
      * @param component    The component for which we want to change the state of double buffering
 129  
      */
 130  
     static void setDoubleBuffered(boolean doubleBuffer, Component component) {
 131  0
         RepaintManager currentManager = RepaintManager.currentManager(component);
 132  0
         currentManager.setDoubleBufferingEnabled(doubleBuffer);
 133  0
     }
 134  
 
 135  
     /**
 136  
      * Return the total number of pages for the component .
 137  
      *
 138  
      * @param pgFormat  The <code>PageFormat</code> in which we want to print
 139  
      * @param component The component to print
 140  
      * @return The number of pages
 141  
      */
 142  
     static int getTotalPageForComponent(PageFormat pgFormat, Component component) {
 143  
         int nbPages;
 144  
 
 145  0
         double heightDoc = component.getHeight();
 146  0
         double heightPg = pgFormat.getImageableHeight();
 147  
 
 148  0
         nbPages = (int) (heightDoc / heightPg);
 149  
 
 150  0
         if (nbPages == 0) {
 151  0
             nbPages = 1;
 152  
         }
 153  
 
 154  0
         return nbPages;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Return the necessary number of pages to print a list of lines.
 159  
      *
 160  
      * @param lines      The number of lines
 161  
      * @param metrics    The <code>FontMetrics</code> of the component
 162  
      * @param pageFormat The PageFormat in which we want to print
 163  
      * @param increment  The space between the lines
 164  
      * @return The necessary number of pages
 165  
      */
 166  
     public static int getTotalPageForList(int lines, FontMetrics metrics, PageFormat pageFormat, int increment) {
 167  
         int nbPages;
 168  
 
 169  0
         double heightDoc = lines * (metrics.getHeight() + increment);
 170  0
         double heightPg = pageFormat.getImageableHeight();
 171  
 
 172  0
         nbPages = (int) (heightDoc / heightPg);
 173  
 
 174  0
         if (nbPages == 0) {
 175  0
             nbPages = 1;
 176  
         }
 177  
 
 178  0
         return nbPages;
 179  
     }
 180  
 
 181  
     /**
 182  
      * Print each line of the stream.
 183  
      *
 184  
      * @param stream The stream.
 185  
      */
 186  
     public static void printLineFiles(InputStream stream) {
 187  0
         printArrayString(FileUtils.getLinesOf(stream));
 188  0
     }
 189  
 }