home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIPHEFT062001.ISO / browser / nc32lyc / comm.z / java40.jar / sun / awt / motif / PSPrintControl.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-08-15  |  7.2 KB  |  298 lines

  1. package sun.awt.motif;
  2.  
  3. import java.awt.Dialog;
  4. import java.awt.Dimension;
  5. import java.awt.Frame;
  6. import java.awt.PrintJob;
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.OutputStream;
  12. import java.io.PrintStream;
  13. import java.util.Hashtable;
  14. import java.util.MissingResourceException;
  15. import java.util.Properties;
  16. import java.util.ResourceBundle;
  17.  
  18. public class PSPrintControl implements PrintControl {
  19.    public static final String DEST_PROP = "awt.print.destination";
  20.    public static final String PRINTER = "printer";
  21.    public static final String FILE = "file";
  22.    public static final String PRINTER_PROP = "awt.print.printer";
  23.    public static final String FILENAME_PROP = "awt.print.fileName";
  24.    public static final String NUMCOPIES_PROP = "awt.print.numCopies";
  25.    public static final String OPTIONS_PROP = "awt.print.options";
  26.    public static final String ORIENT_PROP = "awt.print.orientation";
  27.    public static final String PORTRAIT = "portrait";
  28.    public static final String LANDSCAPE = "landscape";
  29.    public static final String PAPERSIZE_PROP = "awt.print.paperSize";
  30.    public static final String LETTER = "letter";
  31.    public static final String LEGAL = "legal";
  32.    public static final String EXECUTIVE = "executive";
  33.    // $FF: renamed from: A4 java.lang.String
  34.    public static final String field_0 = "a4";
  35.    private static ResourceBundle messageRB;
  36.    private UPrintDialog printDialog;
  37.    private Frame parentFrame;
  38.    private String destStr;
  39.    private String copiesStr;
  40.    private String bannerStr;
  41.    private String orientStr;
  42.    private String paperStr;
  43.    private String printerName;
  44.    private String fileName;
  45.    private String lpOptions;
  46.    private int destType = 0;
  47.    private int orient = 1;
  48.    private int paperSize = 0;
  49.    private int numCopies = 1;
  50.  
  51.    static void initResource() {
  52.       try {
  53.          messageRB = ResourceBundle.getBundle("sun.awt.motif.resources.printcontrol");
  54.       } catch (MissingResourceException var0) {
  55.          throw new Error("Fatal: Resource for PSPrintControl is missing.");
  56.       }
  57.    }
  58.  
  59.    static String getMsg(String var0) {
  60.       if (messageRB == null) {
  61.          initResource();
  62.       }
  63.  
  64.       try {
  65.          return messageRB.getString(var0);
  66.       } catch (MissingResourceException var1) {
  67.          throw new Error("Fatal: Resource for PSPrintControl is broken, There is no " + var0 + " key in resource");
  68.       }
  69.    }
  70.  
  71.    public PSPrintControl(Frame var1) {
  72.       this.printDialog = new UPrintDialog(var1, "Print Dialog");
  73.       this.parentFrame = var1;
  74.    }
  75.  
  76.    public PSPrintJob initJob(String var1, Properties var2, boolean var3) {
  77.       Object var4 = null;
  78.       this.readProps(var2);
  79.       this.printDialog.setDocumentTitle(var1);
  80.       this.printDialog.setNumCopies(this.numCopies);
  81.       this.printDialog.setDestType(this.destType);
  82.       this.printDialog.setDestString(0, this.printerName != null ? this.printerName : "lp");
  83.       this.printDialog.setDestString(1, this.fileName != null ? this.fileName : "");
  84.       this.printDialog.setOptionsString(this.lpOptions != null ? this.lpOptions : "");
  85.       this.printDialog.setBannerString(var1);
  86.       this.printDialog.setOrientation(this.orient);
  87.       this.printDialog.setPaperSize(this.paperSize);
  88.       PSPrintJob var5 = (PSPrintJob)this.printDialog.getPrintJob(this);
  89.       if (var5 != null && var3) {
  90.          this.writeProps(var2);
  91.       }
  92.  
  93.       return var5;
  94.    }
  95.  
  96.    public PrintJob createPrintJob(int var1, String var2, String var3, String var4, int var5, int var6, int var7) {
  97.       Object var8 = null;
  98.       String[] var10 = new String[1];
  99.       String var11 = null;
  100.       this.destType = var1;
  101.       this.bannerStr = var4;
  102.       this.numCopies = var7;
  103.       this.orient = var6;
  104.       switch (var1) {
  105.          case 0:
  106.             this.printerName = var2;
  107.             this.lpOptions = var3;
  108.  
  109.             try {
  110.                var10 = this.printExecCmd(var2, var3, var4, var7);
  111.                SecurityManager.enablePrivilege("UniversalExecAccess");
  112.                Process var9 = Runtime.getRuntime().exec(var10);
  113.                SecurityManager.revertPrivilege();
  114.                var8 = var9.getOutputStream();
  115.             } catch (IOException var18) {
  116.                var11 = getMsg("error_msg.exec_print") + var10;
  117.             }
  118.             break;
  119.          case 1:
  120.             this.fileName = var2;
  121.             File var12 = new File(this.fileName);
  122.             String var13 = "";
  123.  
  124.             try {
  125.                SecurityManager.enablePrivilege("UniversalFileAccess");
  126.                var13 = var12.getCanonicalPath();
  127.                if (var12.exists()) {
  128.                   String var14 = getMsg("warning_button.cancel");
  129.                   String var15 = getMsg("warning_button.overwrite");
  130.                   PrintStatusDialog var16 = new PrintStatusDialog(this.parentFrame, getMsg("warning_dialog.title"), getMsg("warning_msg.file_exists") + this.fileName, var15, var14);
  131.                   ((Dialog)var16).show();
  132.                   if (var16.getCommand().equals(var14)) {
  133.                      return null;
  134.                   }
  135.                }
  136.  
  137.                var8 = new FileOutputStream(var12);
  138.             } catch (IOException var17) {
  139.                var11 = getMsg("error_msg.create_file") + var13;
  140.             }
  141.  
  142.             SecurityManager.revertPrivilege();
  143.             break;
  144.          default:
  145.             return null;
  146.       }
  147.  
  148.       if (var11 != null) {
  149.          PrintStatusDialog var21 = new PrintStatusDialog(this.parentFrame, getMsg("error_dialog.title"), var11, getMsg("error_button.ok"));
  150.          ((Dialog)var21).show();
  151.          return null;
  152.       } else {
  153.          Dimension var20;
  154.          switch (var5) {
  155.             case 0:
  156.             default:
  157.                var20 = PSGraphics.Letter;
  158.                break;
  159.             case 1:
  160.                var20 = PSGraphics.Legal;
  161.                break;
  162.             case 2:
  163.                var20 = PSGraphics.Executive;
  164.                break;
  165.             case 3:
  166.                var20 = PSGraphics.A4;
  167.          }
  168.  
  169.          if (var6 == 0) {
  170.             var20 = new Dimension(var20.height, var20.width);
  171.          }
  172.  
  173.          return new PSPrintJob(new PrintStream(new BufferedOutputStream((OutputStream)var8), false), var4, var20, 72);
  174.       }
  175.    }
  176.  
  177.    private void readProps(Properties var1) {
  178.       if (var1 != null) {
  179.          if ((this.destStr = var1.getProperty("awt.print.destination")) != null) {
  180.             this.destType = this.destStr.equals("file") ? 1 : 0;
  181.          }
  182.  
  183.          this.printerName = var1.getProperty("awt.print.printer");
  184.          this.fileName = var1.getProperty("awt.print.fileName");
  185.          this.lpOptions = var1.getProperty("awt.print.options");
  186.          if ((this.orientStr = var1.getProperty("awt.print.orientation")) != null) {
  187.             this.orient = this.orientStr.equals("landscape") ? 0 : 1;
  188.          }
  189.  
  190.          if ((this.paperStr = var1.getProperty("awt.print.paperSize")) != null) {
  191.             if (this.paperStr.equals("letter")) {
  192.                this.paperSize = 0;
  193.             } else if (this.paperStr.equals("legal")) {
  194.                this.paperSize = 1;
  195.             } else if (this.paperStr.equals("executive")) {
  196.                this.paperSize = 2;
  197.             } else if (this.paperStr.equals("a4")) {
  198.                this.paperSize = 3;
  199.             }
  200.          }
  201.  
  202.          if ((this.copiesStr = var1.getProperty("awt.print.numCopies")) != null) {
  203.             Integer var2 = new Integer(this.copiesStr);
  204.             this.numCopies = var2;
  205.          }
  206.  
  207.       }
  208.    }
  209.  
  210.    private void writeProps(Properties var1) {
  211.       if (var1 != null) {
  212.          ((Hashtable)var1).put("awt.print.destination", this.destType == 0 ? "printer" : "file");
  213.          if (this.printerName != null && !this.printerName.equals("") && !this.printerName.equals("lp")) {
  214.             ((Hashtable)var1).put("awt.print.printer", this.printerName);
  215.          }
  216.  
  217.          if (this.fileName != null && !this.fileName.equals("")) {
  218.             ((Hashtable)var1).put("awt.print.fileName", this.fileName);
  219.          }
  220.  
  221.          if (this.lpOptions != null && !this.lpOptions.equals("")) {
  222.             ((Hashtable)var1).put("awt.print.options", this.lpOptions);
  223.          }
  224.  
  225.          ((Hashtable)var1).put("awt.print.orientation", this.orient == 1 ? "portrait" : "landscape");
  226.          switch (this.paperSize) {
  227.             case 0:
  228.             default:
  229.                this.paperStr = "letter";
  230.                break;
  231.             case 1:
  232.                this.paperStr = "legal";
  233.                break;
  234.             case 2:
  235.                this.paperStr = "executive";
  236.                break;
  237.             case 3:
  238.                this.paperStr = "a4";
  239.          }
  240.  
  241.          ((Hashtable)var1).put("awt.print.paperSize", this.paperStr);
  242.          if (this.numCopies > 0) {
  243.             ((Hashtable)var1).put("awt.print.numCopies", (new Integer(this.numCopies)).toString());
  244.          }
  245.  
  246.       }
  247.    }
  248.  
  249.    private String[] printExecCmd(String var1, String var2, String var3, int var4) {
  250.       boolean var5 = true;
  251.       boolean var6 = true;
  252.       boolean var7 = true;
  253.       boolean var8 = true;
  254.       boolean var9 = false;
  255.       int var11 = 1;
  256.       int var12 = 0;
  257.       if (var1 != null && !var1.equals("") && !var1.equals("lp")) {
  258.          var9 |= var5;
  259.          ++var11;
  260.       }
  261.  
  262.       if (var2 != null && !var2.equals("")) {
  263.          var9 |= var6;
  264.          ++var11;
  265.       }
  266.  
  267.       if (var3 != null && !var3.equals("")) {
  268.          var9 |= var7;
  269.          ++var11;
  270.       }
  271.  
  272.       if (var4 > 1) {
  273.          var9 |= var8;
  274.          ++var11;
  275.       }
  276.  
  277.       String[] var10 = new String[var11];
  278.       var10[var12++] = "/usr/bin/lp";
  279.       if (var9 & var5) {
  280.          var10[var12++] = new String("-d" + var1);
  281.       }
  282.  
  283.       if (var9 & var7) {
  284.          var10[var12++] = new String("-t" + var3);
  285.       }
  286.  
  287.       if (var9 & var8) {
  288.          var10[var12++] = new String("-n" + (new Integer(var4)).toString());
  289.       }
  290.  
  291.       if (var9 & var6) {
  292.          var10[var12++] = new String("-o" + var2);
  293.       }
  294.  
  295.       return var10;
  296.    }
  297. }
  298.