PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms 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
  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 */
 17package mpv5.utils.print;
 18
 19import java.awt.Desktop;
 20import java.io.File;
 21import java.io.FileInputStream;
 22import java.io.FileNotFoundException;
 23import java.io.IOException;
 24import java.util.ArrayList;
 25import java.util.Arrays;
 26import java.util.List;
 27import javax.print.Doc;
 28import javax.print.DocFlavor;
 29import javax.print.DocPrintJob;
 30import javax.print.PrintException;
 31import javax.print.PrintService;
 32import javax.print.PrintServiceLookup;
 33import javax.print.ServiceUI;
 34import javax.print.SimpleDoc;
 35import javax.print.attribute.Attribute;
 36import javax.print.attribute.HashPrintRequestAttributeSet;
 37import mpv5.db.common.DatabaseObject;
 38import mpv5.globals.LocalSettings;
 39import mpv5.globals.Messages;
 40import mpv5.logging.Log;
 41import mpv5.ui.dialogs.Popup;
 42import mpv5.utils.export.Export;
 43import mpv5.utils.files.FileDirectoryHandler;
 44import mpv5.utils.files.FileReaderWriter;
 45import mpv5.utils.jobs.Waiter;
 46import mpv5.utils.text.TypeConversion;
 47
 48/**
 49 *
 50 * 
 51 */
 52public class PrintJob implements Waiter {
 53
 54    private PrintService prservDflt;
 55    private PrintService[] prservices;
 56    int idxPrintService = -1;
 57    private HashPrintRequestAttributeSet aset;
 58    private DocFlavor flavor;
 59
 60//    public PrintJob() {
 61//        aset = new HashPrintRequestAttributeSet();
 62//        aset.add(MediaSizeName.ISO_A4);
 63//        this.flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
 64//        prservDflt = PrintServiceLookup.lookupDefaultPrintService();
 65////        prservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
 66//        prservices = PrintServiceLookup.lookupPrintServices(null, aset);
 67//    }
 68//
 69//    public PrintJob(DocFlavor flavor) {
 70//        aset = new HashPrintRequestAttributeSet();
 71//        aset.add(MediaSizeName.ISO_A4);
 72//        this.flavor = flavor;
 73//        prservDflt = PrintServiceLookup.lookupDefaultPrintService();
 74//        prservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
 75//
 76//    }
 77    /**
 78     * Prints files and determines the type by the file extension.
 79     * May print through the default application for the given filetype or by
 80     * printer lookup, depending on the user settings.
 81     * @param filelist
 82     */
 83    public void print(ArrayList<File> filelist) {
 84
 85        if (TypeConversion.stringToBoolean(LocalSettings.getProperty(LocalSettings.PRINT_DEVAPP))
 86                && Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.PRINT)
 87                && filelist.size() == 1) {
 88            try {
 89                for (File f : filelist) {
 90                    Desktop.getDesktop().print(f);
 91                }
 92            } catch (IOException ex) {
 93                Log.Debug(this, ex.getMessage());
 94//                printOldStyle(filelist);
 95                for (File f : filelist) {
 96                    String filename = f.getName();
 97                    try {
 98                        new PrintJob2(f, (filename.lastIndexOf(".") == -1) ? "" : filename.substring(filename.lastIndexOf(".") + 1, filename.length()));
 99                    } catch (Exception ex1) {
100                        Log.Debug(ex1);
101                        Popup.error(ex1);
102                    }
103                }
104            }
105        } else {
106//            printOldStyle(filelist);
107            for (File f : filelist) {
108                String filename = f.getName();
109                try {
110                    new PrintJob2(f, (filename.lastIndexOf(".") == -1) ? "" : filename.substring(filename.lastIndexOf(".") + 1, filename.length()));
111                } catch (Exception ex1) {
112                    Log.Debug(ex1);
113                    Popup.error(ex1);
114                }
115            }
116        }
117    }
118
119    /**
120     * Prints textfiles containing the given {@link databaseObject}s information
121     * @param dbobjarr
122     */
123    public void printl(List<DatabaseObject> dbobjarr) {
124
125        File file = FileDirectoryHandler.getTempFile("txt");
126        if (dbobjarr != null && dbobjarr.size() > 0) {
127            FileReaderWriter rw = new FileReaderWriter(file);
128
129            rw.writeOnce("" + dbobjarr.get(0).getDbIdentity().toUpperCase());
130            rw.write("");
131            rw.write(dbobjarr.get(0).__getCname());
132            rw.write("");
133
134            for (int i = 0; i < dbobjarr.size(); i++) {
135                DatabaseObject databaseObject = dbobjarr.get(i);
136                List<String[]> data = databaseObject.getValues();
137
138                for (int h = 0; h < data.size(); h++) {
139                    rw.write(data.get(h)[0] + ": " + data.get(h)[1]);
140                }
141            }
142        }
143
144        Log.Debug(file);
145        try {
146            print(file);
147        } catch (FileNotFoundException fileNotFoundException) {
148            Log.Debug(this, fileNotFoundException);
149        } catch (PrintException printException) {
150            Log.Debug(this, printException);
151        }
152    }
153
154    @SuppressWarnings("unchecked")
155    public void print(DatabaseObject dbobj) {
156        ArrayList list = new ArrayList();
157        list.add(dbobj);
158        printl(list);
159    }
160
161    /*
162     * Prints a File
163     */
164    public void print(File file) throws FileNotFoundException, PrintException {
165        ArrayList<File> list = new ArrayList<File>();
166        list.add(file);
167        print(list);
168    }
169
170    /*
171     * Prints a mp4.interfaces.Printable Object
172     */
173    public void print(Printable printable) {
174        this.flavor = printable.getFlavor();
175        try {
176            print(printable.getFile());
177        } catch (FileNotFoundException fileNotFoundException) {
178            Log.Debug(this, fileNotFoundException);
179        } catch (PrintException printException) {
180            Log.Debug(this, printException);
181        }
182
183    }
184
185    /**
186     * @deprecated Doesnt work mostly, use {@link PrintJob2} instead.
187     * @param filelist
188     */
189    private void printOldStyle(ArrayList<File> filelist) {
190        if (null == prservices || 0 >= prservices.length) {
191            if (null != prservDflt) {
192                Log.Print("Nur Default-Printer, da lookupPrintServices fehlgeschlagen.");
193                prservices = new PrintService[]{prservDflt};
194            }
195        }
196        Log.Debug(this, "Print-Services:");
197        int i;
198        for (i = 0; i < prservices.length; i++) {
199            Log.Debug(this, "  " + i + ":  " + prservices[i] + ((prservDflt != prservices[i]) ? "" : " (Default)"));
200        }
201        PrintService prserv = null;
202        if (0 <= idxPrintService && idxPrintService < prservices.length) {
203            prserv = prservices[idxPrintService];
204        } else {
205            if (!Arrays.asList(prservices).contains(prservDflt)) {
206                prservDflt = null;
207            }
208            if (prservices == null) {
209                prservices = new PrintService[]{PrintServiceLookup.lookupDefaultPrintService()};
210            }
211            if (prservices != null && prservices.length > 0) {
212                prserv = ServiceUI.printDialog(null, 50, 50, prservices, prservDflt, null, aset);
213            } else {
214                mpv5.ui.dialogs.Popup.notice(Messages.NO_PRINTER_FOUND);
215            }
216        }
217        if (null != prserv) {
218            Log.Debug(this, "Ausgewaehlter Print-Service:");
219            Log.Debug(this, "      " + prserv);
220            printPrintServiceAttributesAndDocFlavors(prserv);
221            DocPrintJob pj = prserv.createPrintJob();
222
223
224            for (int j = 0; j < filelist.size(); j++) {
225                FileInputStream fis = null;
226                try {
227                    File file = filelist.get(j);
228                    fis = new FileInputStream(file);
229                    Doc doc = new SimpleDoc(fis, flavor, null);
230                    try {
231                        pj.print(doc, aset);
232                    } catch (PrintException ex) {
233                        Log.Debug(ex);
234                    }
235                } catch (FileNotFoundException ex) {
236                    Log.Debug(ex);
237                } finally {
238                    try {
239                        fis.close();
240                    } catch (IOException ex) {
241                        Log.Debug(ex);
242                    }
243                }
244            }
245        }
246    }
247
248    private void printPrintServiceAttributesAndDocFlavors(PrintService prserv) {
249        String s1 = null, s2;
250        Attribute[] prattr = prserv.getAttributes().toArray();
251        DocFlavor[] prdfl = prserv.getSupportedDocFlavors();
252        if (null != prattr && 0 < prattr.length) {
253            for (int i = 0; i < prattr.length; i++) {
254                Log.Debug(this, "      PrintService-Attribute[" + i + "]: " + prattr[i].getName() + " = " + prattr[i]);
255            }
256        }
257        if (null != prdfl && 0 < prdfl.length) {
258            for (int i = 0; i < prdfl.length; i++) {
259                s2 = prdfl[i].getMimeType();
260                if (null != s2 && !s2.equals(s1)) {
261                    Log.Debug(this, "      PrintService-DocFlavor-Mime[" + i + "]: " + s2);
262                }
263                s1 = s2;
264            }
265        }
266    }
267
268
269    /*
270     * Prints a File or mp4.interfaces.Printable Object
271     */
272    @Override
273    public void set(Object object, Exception e) {
274        try {
275            try {
276                print((Printable) object);
277            } catch (ClassCastException ex) {
278                try {
279                    print(((Export) object).getTargetFile());
280                } catch (ClassCastException fileNotFoundException) {
281                    print((File) object);
282                }
283            }
284        } catch (FileNotFoundException fileNotFoundException) {
285            Log.Debug(this, fileNotFoundException);
286        } catch (PrintException printException) {
287            Log.Debug(this, printException);
288        }
289    }
290}