| 1 | |
package org.jtheque.utils.print; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public final class PrintUtils { |
| 47 | |
|
| 48 | 0 | private static final Paper A4_PAPER = new A4(); |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private PrintUtils() { |
| 54 | 0 | super(); |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 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 | |
|
| 78 | |
|
| 79 | |
|
| 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 | |
|
| 102 | |
|
| 103 | |
|
| 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 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 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 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 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 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 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 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public static void printLineFiles(InputStream stream) { |
| 187 | 0 | printArrayString(FileUtils.getLinesOf(stream)); |
| 188 | 0 | } |
| 189 | |
} |