PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/print/PrintJob.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 290 lines | 207 code | 21 blank | 62 comment | 47 complexity | c205dce404e013fd9fa362abeffd8c7f MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.print;
  18. import java.awt.Desktop;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileNotFoundException;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import javax.print.Doc;
  27. import javax.print.DocFlavor;
  28. import javax.print.DocPrintJob;
  29. import javax.print.PrintException;
  30. import javax.print.PrintService;
  31. import javax.print.PrintServiceLookup;
  32. import javax.print.ServiceUI;
  33. import javax.print.SimpleDoc;
  34. import javax.print.attribute.Attribute;
  35. import javax.print.attribute.HashPrintRequestAttributeSet;
  36. import mpv5.db.common.DatabaseObject;
  37. import mpv5.globals.LocalSettings;
  38. import mpv5.globals.Messages;
  39. import mpv5.logging.Log;
  40. import mpv5.ui.dialogs.Popup;
  41. import mpv5.utils.export.Export;
  42. import mpv5.utils.files.FileDirectoryHandler;
  43. import mpv5.utils.files.FileReaderWriter;
  44. import mpv5.utils.jobs.Waiter;
  45. import mpv5.utils.text.TypeConversion;
  46. /**
  47. *
  48. *
  49. */
  50. public class PrintJob implements Waiter {
  51. private PrintService prservDflt;
  52. private PrintService[] prservices;
  53. int idxPrintService = -1;
  54. private HashPrintRequestAttributeSet aset;
  55. private DocFlavor flavor;
  56. // public PrintJob() {
  57. // aset = new HashPrintRequestAttributeSet();
  58. // aset.add(MediaSizeName.ISO_A4);
  59. // this.flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
  60. // prservDflt = PrintServiceLookup.lookupDefaultPrintService();
  61. //// prservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
  62. // prservices = PrintServiceLookup.lookupPrintServices(null, aset);
  63. // }
  64. //
  65. // public PrintJob(DocFlavor flavor) {
  66. // aset = new HashPrintRequestAttributeSet();
  67. // aset.add(MediaSizeName.ISO_A4);
  68. // this.flavor = flavor;
  69. // prservDflt = PrintServiceLookup.lookupDefaultPrintService();
  70. // prservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
  71. //
  72. // }
  73. /**
  74. * Prints files and determines the type by the file extension.
  75. * May print through the default application for the given filetype or by
  76. * printer lookup, depending on the user settings.
  77. * @param filelist
  78. */
  79. public void print(ArrayList<File> filelist) {
  80. if (TypeConversion.stringToBoolean(LocalSettings.getProperty(LocalSettings.PRINT_DEVAPP))
  81. && Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.PRINT)
  82. && filelist.size() == 1) {
  83. try {
  84. for (File f : filelist) {
  85. Desktop.getDesktop().print(f);
  86. }
  87. } catch (IOException ex) {
  88. Log.Debug(this, ex.getMessage());
  89. // printOldStyle(filelist);
  90. for (File f : filelist) {
  91. String filename = f.getName();
  92. try {
  93. new PrintJob2(f, (filename.lastIndexOf(".") == -1) ? "" : filename.substring(filename.lastIndexOf(".") + 1, filename.length()));
  94. } catch (Exception ex1) {
  95. Log.Debug(ex1);
  96. Popup.error(ex1);
  97. }
  98. }
  99. }
  100. } else {
  101. // printOldStyle(filelist);
  102. for (File f : filelist) {
  103. String filename = f.getName();
  104. try {
  105. new PrintJob2(f, (filename.lastIndexOf(".") == -1) ? "" : filename.substring(filename.lastIndexOf(".") + 1, filename.length()));
  106. } catch (Exception ex1) {
  107. Log.Debug(ex1);
  108. Popup.error(ex1);
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Prints textfiles containing the given {@link databaseObject}s information
  115. * @param dbobjarr
  116. */
  117. public void printl(List<DatabaseObject> dbobjarr) {
  118. File file = FileDirectoryHandler.getTempFile("txt");
  119. if (dbobjarr != null && dbobjarr.size() > 0) {
  120. FileReaderWriter rw = new FileReaderWriter(file);
  121. rw.writeOnce("" + dbobjarr.get(0).getDbIdentity().toUpperCase());
  122. rw.write("");
  123. rw.write(dbobjarr.get(0).__getCname());
  124. rw.write("");
  125. for (int i = 0; i < dbobjarr.size(); i++) {
  126. DatabaseObject databaseObject = dbobjarr.get(i);
  127. List<String[]> data = databaseObject.getValues();
  128. for (int h = 0; h < data.size(); h++) {
  129. rw.write(data.get(h)[0] + ": " + data.get(h)[1]);
  130. }
  131. }
  132. }
  133. Log.Debug(file);
  134. try {
  135. print(file);
  136. } catch (FileNotFoundException fileNotFoundException) {
  137. Log.Debug(this, fileNotFoundException);
  138. } catch (PrintException printException) {
  139. Log.Debug(this, printException);
  140. }
  141. }
  142. @SuppressWarnings("unchecked")
  143. public void print(DatabaseObject dbobj) {
  144. ArrayList list = new ArrayList();
  145. list.add(dbobj);
  146. printl(list);
  147. }
  148. /*
  149. * Prints a File
  150. */
  151. public void print(File file) throws FileNotFoundException, PrintException {
  152. ArrayList<File> list = new ArrayList<File>();
  153. list.add(file);
  154. print(list);
  155. }
  156. /*
  157. * Prints a mp4.interfaces.Printable Object
  158. */
  159. public void print(Printable printable) {
  160. this.flavor = printable.getFlavor();
  161. try {
  162. print(printable.getFile());
  163. } catch (FileNotFoundException fileNotFoundException) {
  164. Log.Debug(this, fileNotFoundException);
  165. } catch (PrintException printException) {
  166. Log.Debug(this, printException);
  167. }
  168. }
  169. /**
  170. * @deprecated Doesnt work mostly, use {@link PrintJob2} instead.
  171. * @param filelist
  172. */
  173. private void printOldStyle(ArrayList<File> filelist) {
  174. if (null == prservices || 0 >= prservices.length) {
  175. if (null != prservDflt) {
  176. Log.Print("Nur Default-Printer, da lookupPrintServices fehlgeschlagen.");
  177. prservices = new PrintService[]{prservDflt};
  178. }
  179. }
  180. Log.Debug(this, "Print-Services:");
  181. int i;
  182. for (i = 0; i < prservices.length; i++) {
  183. Log.Debug(this, " " + i + ": " + prservices[i] + ((prservDflt != prservices[i]) ? "" : " (Default)"));
  184. }
  185. PrintService prserv = null;
  186. if (0 <= idxPrintService && idxPrintService < prservices.length) {
  187. prserv = prservices[idxPrintService];
  188. } else {
  189. if (!Arrays.asList(prservices).contains(prservDflt)) {
  190. prservDflt = null;
  191. }
  192. if (prservices == null) {
  193. prservices = new PrintService[]{PrintServiceLookup.lookupDefaultPrintService()};
  194. }
  195. if (prservices != null && prservices.length > 0) {
  196. prserv = ServiceUI.printDialog(null, 50, 50, prservices, prservDflt, null, aset);
  197. } else {
  198. mpv5.ui.dialogs.Popup.notice(Messages.NO_PRINTER_FOUND);
  199. }
  200. }
  201. if (null != prserv) {
  202. Log.Debug(this, "Ausgewaehlter Print-Service:");
  203. Log.Debug(this, " " + prserv);
  204. printPrintServiceAttributesAndDocFlavors(prserv);
  205. DocPrintJob pj = prserv.createPrintJob();
  206. for (int j = 0; j < filelist.size(); j++) {
  207. FileInputStream fis = null;
  208. try {
  209. File file = filelist.get(j);
  210. fis = new FileInputStream(file);
  211. Doc doc = new SimpleDoc(fis, flavor, null);
  212. try {
  213. pj.print(doc, aset);
  214. } catch (PrintException ex) {
  215. Log.Debug(ex);
  216. }
  217. } catch (FileNotFoundException ex) {
  218. Log.Debug(ex);
  219. } finally {
  220. try {
  221. fis.close();
  222. } catch (IOException ex) {
  223. Log.Debug(ex);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. private void printPrintServiceAttributesAndDocFlavors(PrintService prserv) {
  230. String s1 = null, s2;
  231. Attribute[] prattr = prserv.getAttributes().toArray();
  232. DocFlavor[] prdfl = prserv.getSupportedDocFlavors();
  233. if (null != prattr && 0 < prattr.length) {
  234. for (int i = 0; i < prattr.length; i++) {
  235. Log.Debug(this, " PrintService-Attribute[" + i + "]: " + prattr[i].getName() + " = " + prattr[i]);
  236. }
  237. }
  238. if (null != prdfl && 0 < prdfl.length) {
  239. for (int i = 0; i < prdfl.length; i++) {
  240. s2 = prdfl[i].getMimeType();
  241. if (null != s2 && !s2.equals(s1)) {
  242. Log.Debug(this, " PrintService-DocFlavor-Mime[" + i + "]: " + s2);
  243. }
  244. s1 = s2;
  245. }
  246. }
  247. }
  248. /*
  249. * Prints a File or mp4.interfaces.Printable Object
  250. */
  251. @Override
  252. public void set(Object object, Exception e) {
  253. try {
  254. try {
  255. print((Printable) object);
  256. } catch (ClassCastException ex) {
  257. try {
  258. print(((Export) object).getTargetFile());
  259. } catch (ClassCastException fileNotFoundException) {
  260. print((File) object);
  261. }
  262. }
  263. } catch (FileNotFoundException fileNotFoundException) {
  264. Log.Debug(this, fileNotFoundException);
  265. } catch (PrintException printException) {
  266. Log.Debug(this, printException);
  267. }
  268. }
  269. }