001/* 002This file is part of [PROGRAM NAME] - [What it does in brief] 003Copyright (c) 2001 [Author] 004 005This program is free software; you can redistribute it and/or 006modify it under the terms of the GNU General Public License 007as published by the Free Software Foundation; either version 2 008of the License, or any later version. 009 010This program is distributed in the hope that it will be useful, 011but WITHOUT ANY WARRANTY; without even the implied warranty of 012MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013GNU General Public License for more details. 014 015You should have received a copy of the GNU General Public License 016along with this program; if not, write to the Free Software 017Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 018 019*/ 020 021package ca.bc.webarts.widgets; 022 023import ca.bc.webarts.widgets.ColouredLabel; 024import ca.bc.webarts.widgets.Util; 025import ca.bc.webarts.tools.NativeAppLauncher; 026 027import java.awt.BorderLayout; 028import java.awt.FlowLayout; 029import java.awt.GridLayout; 030import java.awt.Color; 031import java.awt.Dimension; 032import java.awt.Font; 033import java.awt.Toolkit; 034import java.awt.event.ActionEvent; 035import java.awt.event.ActionListener; 036import java.awt.event.KeyEvent; 037import java.awt.event.WindowAdapter; 038import java.awt.event.WindowEvent; 039 040import java.io.File; 041 042//import javax.swing.Border; 043import javax.swing.BorderFactory; 044import javax.swing.ButtonGroup; 045import javax.swing.JButton; 046import javax.swing.JRadioButton; 047import javax.swing.JLabel; 048import javax.swing.JPanel; 049import javax.swing.JTabbedPane; 050 051import jargs.gnu.CmdLineParser; 052 053 054public class PrintDialog extends javax.swing.JDialog 055 implements ActionListener 056{ 057 public final static String LINE_SEPARATOR = 058 System.getProperty("line.separator"); 059 public static final int NORMAL_PRINTER = 0; 060 public static final int DRAFT_PRINTER = 1; 061 public static final int QUALITY_PRINTER = 2; 062 public static final int FASTNORMAL_PRINTER = 3; 063 public static final int PDF_PRINTER = 4; 064 public static final int LAST_PRINTER = PDF_PRINTER; 065 public static final int DEFAULT_PRINTER = 0; 066 private int printer_ = DEFAULT_PRINTER; 067 private boolean twoUp_ = false; 068 private boolean duplex_ = false; 069 private String printerName_ = "deskjet"; 070 private String printFilename_ = ""; 071 private String printPDFCmd_ = "/usr/bin/ps2pdf"; 072 private String printPDF2UpCmd_ = "/home/tgutwin/bin/ps2pdf2Up"; 073 private String printPDFoutDir_ = "/home/tgutwin/docs/pdfOutput"; 074 private String printCmd_ = "/home/tgutwin/bin/acroPrint"; 075 private String printCmd2Up_ = "/home/tgutwin/bin/acroPrint2Up"; 076 /** A generic Class accessable Label that is used throughout for Debug 077 * Stmnts to the screen. */ 078 protected ColouredLabel myDebugLabel_ = 079 new ColouredLabel(new Color(5,5,100), 080 Color.white, 081 new Font("SanSerif", Font.PLAIN, 11), 082 "Debug Label."); 083 private String normalPrinterAlias_ = "deskjet"; 084 private String fastNormalPrinterAlias_ = "deskjet/fastNormal"; 085 private String draftPrinterAlias_ = "deskjet/draft"; 086 private String qualityPrinterAlias_ = "deskjet/quality"; 087 JRadioButton bestQuality = null; 088 JRadioButton normalQuality = null; 089 JRadioButton fastNormalQuality = null; 090 JRadioButton draftQuality = null; 091 JRadioButton pdfQuality = null; 092 JRadioButton singleSided = null; 093 JRadioButton doubleSided = null; 094 JRadioButton oneUp = null; 095 JRadioButton twoUp = null; 096 097 098 /** 099 * Default Constructor for the PrintDialog object that does NO processing. 100 */ 101 PrintDialog() 102 { 103 104 } 105 106 107 /** 108 * Constructor for the PrintDialog object 109 * 110 * @param printFilename the filename to print. 111 */ 112 PrintDialog(String printFilename) 113 { 114 115 } 116 117 118 /** 119 * shows the dialog. 120 */ 121 public int doDialog(String printFilename) 122 { 123 int retVal = 0; 124 125 initGui(printFilename); 126 127 return retVal; 128 } 129 130 /** 131 * Sets up the layout of the gui dialog. 132 **/ 133 public void initGui(String printFilename) 134 { 135 this.addWindowListener( 136 new WindowAdapter() 137 { 138 public void windowClosing(WindowEvent e) 139 { 140 dispose(); 141 System.exit(0); 142 } 143 } 144 ); 145 146 this.setTitle("Printer Options for "+printFilename); 147 this.setResizable(true); 148 JPanel mainAreaPanel = initMainArea(); 149 this.getContentPane().add(mainAreaPanel, "Center"); 150 151 this.pack(); 152 this.toFront(); 153 this.setVisible(true); 154 Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize(); 155 int dialogWidth = this.getWidth(), 156 dialogHeight = this.getHeight(); 157 158 this.setLocation(scrnSize.width/2 - (dialogWidth/2), 159 scrnSize.height/2 - ((dialogHeight)/2)); 160 // window.setSize(dialogWidth,dialogHeight); 161 this.repaint(); 162 } 163 164 165 /** 166 *the button action listener. 167 **/ 168 public void actionPerformed(ActionEvent e) 169 { 170 System.out.println("Action... "+e.getActionCommand()); 171 if ("print".equals(e.getActionCommand())) 172 { 173 if (normalQuality.isSelected()) 174 this.setPrinter_(NORMAL_PRINTER); 175 else if (bestQuality.isSelected()) 176 this.setPrinter_(QUALITY_PRINTER); 177 else if (fastNormalQuality.isSelected()) 178 this.setPrinter_(FASTNORMAL_PRINTER); 179 else if (draftQuality.isSelected()) 180 this.setPrinter_(DRAFT_PRINTER); 181 else if (pdfQuality.isSelected()) 182 this.setPrinter_(PDF_PRINTER); 183 184 if (oneUp.isSelected()) 185 this.setTwoUp_(false); 186 else 187 this.setTwoUp_(true); 188 189 if (doubleSided.isSelected()) 190 this.setDuplex_(true); 191 else 192 this.setDuplex_(false); 193 194 this.doPrint(); 195 dispose(); 196 System.exit(0); 197 } 198 else if ("cancel".equals(e.getActionCommand())) 199 { 200 dispose(); 201 System.exit(2); 202 } 203 } 204 205 206 /** 207 * Initializes the GUI Main Area Panel (the tabbed pane, the 208 * inner panels etc.) 209 * 210 * @return JPanel the panel to go into the Main center area of jAckup 211 */ 212 private JPanel initMainArea() 213 { 214 215 JPanel retVal = new JPanel(new BorderLayout(5,5)); 216 JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0,0)); 217 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5,5)); 218 JPanel innerPanel = new JPanel(new GridLayout(1, 3, 2, 5)); 219 JPanel outQualityPanel = new JPanel(new GridLayout(6, 1, 1, 5)); 220 JPanel duplexPanel = new JPanel(new GridLayout(6, 1, 1, 5)); 221 JPanel pagesPerPanel = new JPanel(new GridLayout(6, 1, 1, 5)); 222 223 //Group the radio buttons. 224 ButtonGroup outQualityGroup = new ButtonGroup(); 225 ButtonGroup duplexGroup = new ButtonGroup(); 226 ButtonGroup pagesPerGroup = new ButtonGroup(); 227 228 bestQuality = new JRadioButton("Best"); 229 normalQuality = new JRadioButton("Normal"); 230 fastNormalQuality = new JRadioButton("Fast Normal"); 231 draftQuality = new JRadioButton("Draft"); 232 pdfQuality = new JRadioButton("PDF"); 233 singleSided = new JRadioButton("Single Sided"); 234 doubleSided = new JRadioButton("Double Sided"); 235 oneUp = new JRadioButton("1 per page"); 236 twoUp = new JRadioButton("2 per page"); 237 238 outQualityGroup.add(bestQuality); 239 outQualityGroup.add(normalQuality); 240 outQualityGroup.add(fastNormalQuality); 241 outQualityGroup.add(draftQuality); 242 outQualityGroup.add(pdfQuality); 243 duplexGroup.add(singleSided); 244 duplexGroup.add(doubleSided); 245 pagesPerGroup.add(oneUp); 246 pagesPerGroup.add(twoUp); 247 248 int grayColor = 200; 249 250 outQualityPanel.add( 251 (new JPanel(new FlowLayout(FlowLayout.CENTER, 0,0))).add( 252 new ColouredLabel(new Color(grayColor,grayColor,grayColor), 253 Color.blue, 254 new Font("SanSerif", Font.BOLD, 11), 255 "Print Quality"))); 256 outQualityPanel.add(bestQuality); 257 outQualityPanel.add(normalQuality); 258 outQualityPanel.add(fastNormalQuality); 259 outQualityPanel.add(draftQuality); 260 outQualityPanel.add(pdfQuality); 261 outQualityPanel.setBorder(BorderFactory.createEtchedBorder() ); 262 263 duplexPanel.add( 264 (new JPanel(new FlowLayout(FlowLayout.CENTER, 0,0))).add( 265 new ColouredLabel(new Color(grayColor,grayColor,grayColor), 266 Color.blue, 267 new Font("SanSerif", Font.BOLD, 11), 268 "Duplex"))); 269 duplexPanel.add(singleSided); 270 duplexPanel.add(doubleSided); 271 duplexPanel.setBorder(BorderFactory.createEtchedBorder() ); 272 273 pagesPerPanel.add( 274 (new JPanel(new FlowLayout(FlowLayout.CENTER, 0,0))).add( 275 new ColouredLabel(new Color(grayColor,grayColor,grayColor), 276 Color.blue, 277 new Font("SanSerif", Font.BOLD, 11), 278 "Pages per Side"))); 279 pagesPerPanel.add(oneUp); 280 pagesPerPanel.add(twoUp); 281 pagesPerPanel.setBorder(BorderFactory.createEtchedBorder() ); 282 283 innerPanel.add(outQualityPanel); 284 innerPanel.add(pagesPerPanel); 285 innerPanel.add(duplexPanel); 286 287 normalQuality.setSelected(true); 288 singleSided.setSelected(true); 289 oneUp.setSelected(true); 290 291 JButton printButton = new JButton("Print"); 292 printButton.setActionCommand("print"); 293 printButton.addActionListener(this); 294 295 JButton cancelButton = new JButton("Cancel"); 296 cancelButton.setActionCommand("cancel"); 297 cancelButton.addActionListener(this); 298 299 buttonPanel.add(printButton); 300 buttonPanel.add(cancelButton); 301 buttonPanel.setBackground(new Color(grayColor,grayColor,grayColor)); 302 buttonPanel.setBorder(BorderFactory.createRaisedBevelBorder()); 303 retVal.setBorder(BorderFactory.createRaisedBevelBorder()); 304 305 titlePanel.setBackground(new Color(200,200,255)); 306 titlePanel.add(new ColouredLabel( new Color(200,200,255), 307 new Color(0,0,15), 308 new Font("SanSerif", Font.BOLD, 12), 309 printerName_)); 310 retVal.add(titlePanel, BorderLayout.NORTH); 311 retVal.add(innerPanel, BorderLayout.CENTER); 312 retVal.add(buttonPanel, BorderLayout.SOUTH); 313 return retVal; 314 } 315 316 317 /** 318 * Prints to the printer. this methods needs all the instance vars 319 * set to the output format wanted. 320 */ 321 public void doPrint() 322 { 323 String printCmd = ""; 324 String printerName = ""; 325 switch ( printer_ ) 326 { 327 case NORMAL_PRINTER: 328 if (twoUp_ == true) 329 { 330 if (duplex_ == true) 331 { 332 //normal, 2up, doubleSided 333 printCmd = printCmd2Up_; 334 printerName = "duplex"; 335 } 336 else 337 { 338 //normal, 2up, singleSided 339 printCmd = printCmd2Up_; 340 printerName = normalPrinterAlias_; 341 } 342 } 343 else 344 { 345 if (duplex_ == true) 346 { 347 //normal, 1up, doubleSided 348 printCmd = printCmd_; 349 printerName = "duplex"; 350 } 351 else 352 { 353 //normal, 1up, singleSided 354 printCmd = printCmd_; 355 printerName = normalPrinterAlias_; 356 } 357 } 358 break; 359 360 case DRAFT_PRINTER: 361 if (twoUp_ == true) 362 { 363 if (duplex_ == true) 364 { 365 //draft, 2up, doubleSided 366 printCmd = printCmd2Up_; 367 printerName = "duplexDraft"; 368 } 369 else 370 { 371 //draft, 2up, singleSided 372 printCmd = printCmd2Up_; 373 printerName = draftPrinterAlias_; 374 } 375 } 376 else 377 { 378 if (duplex_ == true) 379 { 380 //draft, 1up, doubleSided 381 printCmd = printCmd_; 382 printerName = "duplexDraft"; 383 } 384 else 385 { 386 //nordraftmal, 1up, singleSided 387 printCmd = printCmd_; 388 printerName = draftPrinterAlias_; 389 } 390 } 391 break; 392 393 case QUALITY_PRINTER: 394 if (twoUp_ == true) 395 { 396 if (duplex_ == true) 397 { 398 //quality, 2up, doubleSided 399 printCmd = printCmd2Up_; 400 printerName = qualityPrinterAlias_; 401 } 402 else 403 { 404 //quality, 2up, singleSided 405 printCmd = printCmd2Up_; 406 printerName = qualityPrinterAlias_; 407 } 408 } 409 else 410 { 411 if (duplex_ == true) 412 { 413 //quality, 1up, doubleSided 414 printCmd = printCmd_; 415 printerName = qualityPrinterAlias_; 416 } 417 else 418 { 419 //quality, 1up, singleSided 420 printCmd = printCmd_; 421 printerName = qualityPrinterAlias_; 422 } 423 } 424 break; 425 426 case FASTNORMAL_PRINTER: 427 if (twoUp_ == true) 428 { 429 if (duplex_ == true) 430 { 431 //fastNormal, 2up, doubleSided 432 printCmd = printCmd2Up_; 433 printerName = "duplexFast"; 434 } 435 else 436 { 437 //fastNormal, 2up, singleSided 438 printCmd = printCmd2Up_; 439 printerName = fastNormalPrinterAlias_; 440 } 441 } 442 else 443 { 444 if (duplex_ == true) 445 { 446 //fastNormal, 1up, doubleSided 447 printCmd = printCmd_; 448 printerName = "duplexFast"; 449 } 450 else 451 { 452 //fastNormal, 1up, singleSided 453 printCmd = printCmd_; 454 printerName = fastNormalPrinterAlias_; 455 } 456 } 457 break; 458 459 case PDF_PRINTER: 460 461 int endIndex = 0; 462 if (printFilename_ != null && !printFilename_.equals("")) 463 { 464 if(printFilename_.lastIndexOf(File.separator) == -1 ) 465 printFilename_ = System.getProperty("user.dir")+File.separator+printFilename_; 466 if(!printFilename_.trim().endsWith(".ps") || 467 printFilename_.lastIndexOf(".ps") < 468 printFilename_.lastIndexOf(File.separator)) 469 endIndex = printFilename_.length(); 470 else 471 endIndex = printFilename_.lastIndexOf(".ps"); 472 } 473 String outFile = printPDFoutDir_+ 474 printFilename_.substring( 475 printFilename_.lastIndexOf(File.separator),endIndex) + 476 ".pdf"; 477 if (twoUp_ == true) 478 { 479 if (duplex_ == true) // PDF DOES NOT HAVE a DUPLEX 480 { 481 //fastNormal, 2up, doubleSided 482 printCmd = printPDF2UpCmd_; 483 printerName = outFile; 484 } 485 else 486 { 487 //fastNormal, 2up, singleSided 488 printCmd = printPDF2UpCmd_; 489 printerName = outFile; 490 } 491 } 492 else 493 { 494 if (duplex_ == true) 495 { 496 //fastNormal, 1up, doubleSided 497 printCmd = printPDFCmd_; 498 printerName = outFile; 499 } 500 else 501 { 502 //fastNormal, 1up, singleSided 503 printCmd = printPDFCmd_; 504 printerName = outFile; 505 } 506 } 507 break; 508 509 default: 510 printCmd = printCmd_; 511 printerName = "normal"; 512 } 513 String[] printArgs = {printFilename_, printerName}; 514 System.out.println("Execing "+ printCmd +" " + printFilename_ +" " + 515 printerName); 516 NativeAppLauncher printApp = new NativeAppLauncher(printCmd, printArgs); 517 } 518 519 520 public static void main(String[] args) 521 { 522 523 PrintDialog instance = new PrintDialog(); 524 525 if (args.length == 1) 526 { 527 // get parameters from dialog 528 instance.setPrintFilename_(args[0]); 529 instance.doDialog(args[0]); 530 } 531 else 532 { 533 // process without dialog 534 parseCmdArgs(args, instance); 535 // print direct 536 instance.doPrint(); 537 } 538 } 539 540 541 public int getPrinter_(){ return printer_;} 542 543 544 public void setPrinter_(int printer) 545 { 546 if (printer<=LAST_PRINTER) 547 printer_ = printer; 548 } 549 550 551 public boolean getDuplex_(){ return duplex_;} 552 553 554 public void setDuplex_(boolean dupe) 555 { 556 duplex_ = dupe; 557 } 558 559 560 public boolean getTwoUp_(){ return twoUp_;} 561 562 563 public void setTwoUp_(boolean pgsPer) 564 { 565 twoUp_ = pgsPer; 566 } 567 568 569 public String getPrintFilename_(){ return printFilename_;} 570 571 572 public void setPrintFilename_(String name) 573 { 574 printFilename_ = name; 575 } 576 577 578 /** Parses the cmd line args into usefule class vars. */ 579 private static void printUsage() 580 { 581 StringBuffer usage = new StringBuffer(); 582 usage.append(LINE_SEPARATOR); 583 usage.append("Java PrintDialog - Copyright 2002 - Tom B. Gutwin, WebARTS Design"); 584 usage.append(LINE_SEPARATOR); 585 usage.append(LINE_SEPARATOR); 586 usage.append("Usage details:"); 587 usage.append(LINE_SEPARATOR); 588 usage.append(" java ca.bc.webarts.tools.PrintDialog [options] "); 589 usage.append("file/dir(s)"); 590 usage.append(LINE_SEPARATOR); 591 usage.append(" options:"); 592 usage.append(LINE_SEPARATOR); 593 usage.append(" -h {--help}"); 594 usage.append(LINE_SEPARATOR); 595 usage.append(" -p {--printer} normal,draft,quality,fastNormal DEFAULT=normal"); 596 usage.append(LINE_SEPARATOR); 597 usage.append(" -2 {--2up} DEFAULT=false"); 598 usage.append(LINE_SEPARATOR); 599 usage.append(" -d {--duplex} DEFAULT=false"); 600 usage.append(LINE_SEPARATOR); 601 usage.append(LINE_SEPARATOR); 602 usage.append(" EXAMPLE:"); 603 usage.append(LINE_SEPARATOR); 604 usage.append(" java ca.bc.webarts.tools.PrintDialog -2 dir -d -p quality"); 605 usage.append(" /home/joeBlo/staging /home/joBlo/src/file.ps"); 606 System.out.println(usage.toString()); 607 } 608 609 610 /** 611 * Parses the cmd line args into usefule class vars. 612 * 613 * @param args Description of the Parameter 614 * @param multiZipper Description of the Parameter 615 */ 616 private static void parseCmdArgs(String args[], PrintDialog instance) 617 { 618 CmdLineParser parser = new CmdLineParser(); 619 CmdLineParser.Option help = parser.addBooleanOption('h', "help"); 620 CmdLineParser.Option duplex = parser.addBooleanOption('d', "duplex"); 621 CmdLineParser.Option twoup = parser.addBooleanOption('2', "2up"); 622 CmdLineParser.Option printer = parser.addStringOption('p', "printer"); 623 624 try 625 { 626 parser.parse(args); 627 } 628 catch (CmdLineParser.OptionException e) 629 { 630 System.out.println(e.getMessage()); 631 printUsage(); 632 System.exit(2); 633 } 634 635 // Extract the values entered for the various options -- if the 636 // options were not specified, the corresponding values will be 637 // null. 638 if (parser.getOptionValue(help) != null) 639 { 640 printUsage(); 641 } 642 if (parser.getOptionValue(duplex) != null) 643 { 644 instance.setDuplex_( 645 ((Boolean) parser.getOptionValue(duplex)).booleanValue()); 646 } 647 if (parser.getOptionValue(twoup) != null) 648 { 649 instance.setTwoUp_( 650 ((Boolean) parser.getOptionValue(twoup)).booleanValue()); 651 } 652 String printerArg = (String) parser.getOptionValue(printer); 653 if (printerArg != null) 654 { 655 if (printerArg.toLowerCase().equals("normal")) 656 instance.setPrinter_(PrintDialog.NORMAL_PRINTER); 657 else if (printerArg.toLowerCase().equals("draft")) 658 instance.setPrinter_(PrintDialog.DRAFT_PRINTER); 659 else if (printerArg.toLowerCase().equals("quality")) 660 instance.setPrinter_(PrintDialog.QUALITY_PRINTER); 661 else if (printerArg.toLowerCase().equals("fastnormal")) 662 instance.setPrinter_(PrintDialog.FASTNORMAL_PRINTER); 663 else 664 instance.setPrinter_(PrintDialog.DEFAULT_PRINTER); 665 } 666 // For testing purposes, we just print out the option values 667 System.out.println("printer: " + instance.getPrinter_()); 668 System.out.println("2up: " + instance.getTwoUp_()); 669 System.out.println("duplex: " + instance.getDuplex_()); 670 671 // Extract the trailing command-line arguments in the 672 String[] otherArgs = parser.getRemainingArgs(); 673 674 if (otherArgs.length == 0) 675 { 676 // ERROR 677 System.out.println("ERROR:"); 678 System.out.println(" No input files specified."); 679 printUsage(); 680 System.exit(2); 681 } 682 else if (otherArgs.length == 1) 683 { 684 //Input files 685 instance.setPrintFilename_(otherArgs[0].trim()); 686 } 687 else 688 { 689 // ERROR 690 System.out.println("ERROR:"); 691 System.out.println(" Please specify only 1 filename to print."); 692 printUsage(); 693 System.exit(2); 694 } 695 696 } 697}