Beispiel #1
0
  // {{{ getPrintJob() method
  private static PrinterJob getPrintJob(String jobName) {
    job = PrinterJob.getPrinterJob();

    format = new HashPrintRequestAttributeSet();

    String settings = jEdit.getSettingsDirectory();
    if (settings != null) {
      String printSpecPath = MiscUtilities.constructPath(settings, "printspec");
      File filePrintSpec = new File(printSpecPath);

      if (filePrintSpec.exists()) {
        try {
          FileInputStream fileIn = new FileInputStream(filePrintSpec);
          ObjectInputStream obIn = new ObjectInputStream(fileIn);
          format = (HashPrintRequestAttributeSet) obIn.readObject();
        } catch (Exception e) {
          Log.log(Log.ERROR, BufferPrinter1_4.class, e);
        }
        // for backwards compatibility, the color variable is stored also as a property
        if (jEdit.getBooleanProperty("print.color")) format.add(Chromaticity.COLOR);
        else format.add(Chromaticity.MONOCHROME);

        // no need to always keep the same job name for every printout.
        format.add(new JobName(jobName, null));
      }
    }

    return job;
  } // }}}
  private void savePlotAsImage() {
    // get the possible image name.
    String imageName = "ScreePlot.png";

    // Ask the user to specify a file name for saving the histo.
    String pathSep = File.separator;
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    // fc.setCurrentDirectory(new File(workingDirectory + pathSep + imageName + ".png"));
    fc.setAcceptAllFileFilterUsed(false);

    // File f = new File(workingDirectory + pathSep + imageName + ".png");
    // fc.setSelectedFile(f);

    // set the filter.
    ArrayList<ExtensionFileFilter> filters = new ArrayList<ExtensionFileFilter>();
    String[] extensions = ImageIO.getReaderFormatNames(); // {"PNG", "JPEG", "JPG"};
    String filterDescription = "Image Files (" + extensions[0];
    for (int i = 1; i < extensions.length; i++) {
      filterDescription += ", " + extensions[i];
    }
    filterDescription += ")";
    ExtensionFileFilter eff = new ExtensionFileFilter(filterDescription, extensions);
    fc.setFileFilter(eff);

    int result = fc.showSaveDialog(this);
    File file = null;
    if (result == JFileChooser.APPROVE_OPTION) {
      file = fc.getSelectedFile();
      // see if file has an extension.
      if (file.toString().lastIndexOf(".") <= 0) {
        String fileName = file.toString() + ".png";
        file = new File(fileName);
      }

      String fileDirectory = file.getParentFile() + pathSep;
      //            if (!fileDirectory.equals(workingDirectory)) {
      //                workingDirectory = fileDirectory;
      //            }

      // see if the file exists already, and if so, should it be overwritten?
      if (file.exists()) {
        Object[] options = {"Yes", "No"};
        int n =
            JOptionPane.showOptionDialog(
                this,
                "The file already exists.\n" + "Would you like to overwrite it?",
                "Whitebox GAT Message",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null, // do not use a custom Icon
                options, // the titles of buttons
                options[0]); // default button title

        if (n == JOptionPane.YES_OPTION) {
          file.delete();
        } else if (n == JOptionPane.NO_OPTION) {
          return;
        }
      }
      if (!saveToImage(file.toString())) {
        //                showFeedback("An error occurred while saving the map to the image file.");
      }
    }
  }