(no commit message)
[jalview.git] / forester / java / src / org / forester / archaeopteryx / Printer.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2009-2010 Christian M. Zmasek
6 // Copyright (C) 2009-2010 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx;
27
28 import java.awt.print.PrinterException;
29 import java.awt.print.PrinterJob;
30
31 import org.forester.util.ForesterUtil;
32
33 final class Printer {
34
35     private Printer() {
36         // Hidden constructor.
37     }
38
39     /**
40      * Returns null if printing has been aborted by the user,
41      * a String otherwise -- if a printer name was obtained this String is
42      * the printer name, an empty String otherwise.
43      *
44      * @param tree_panel
45      * @param job_name
46      * @return
47      * @throws PrinterException
48      */
49     static String print( final TreePanel tree_panel, final String job_name ) throws PrinterException {
50         if ( ( tree_panel == null ) || ( tree_panel.getPhylogeny() == null ) ) {
51             throw new IllegalArgumentException( "attempt to print null" );
52         }
53         if ( ForesterUtil.isEmpty( job_name ) ) {
54             throw new IllegalArgumentException( "attempt use null or empty print job name" );
55         }
56         final PrinterJob printer_job = PrinterJob.getPrinterJob();
57         if ( printer_job != null ) {
58             printer_job.setJobName( job_name );
59             printer_job.setPrintable( tree_panel );
60             final boolean ok = printer_job.printDialog();
61             if ( ok ) {
62                 printer_job.print();
63                 final String print_service_name = printer_job.getPrintService().getName();
64                 if ( !ForesterUtil.isEmpty( print_service_name ) ) {
65                     return print_service_name;
66                 }
67                 return "";
68             }
69             else {
70                 return null;
71             }
72         }
73         else {
74             throw new PrinterException( "failed to access printer job" );
75         }
76     }
77 }