pdfsavedialog.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 1,499 行 · 第 1/4 页

JAVA
1,499
字号
   * Queries the currently selected encryption. If an encryption is selected this method returns
   * either Boolean.TRUE or Boolean.FALSE, when no encryption is set, <code>null</code> is
   * returned. If no encryption is set, the security properties have no defined state.
   *
   * @return the selection state for the encryption. If no encryption is set, this method returns
   * null, if 40-bit encryption is set, the method returns Boolean.FALSE and on 128-Bit-encryption,
   * Boolean.TRUE is returned.
   */
  public String getEncryptionValue()
  {
    if (rbSecurity40Bit.isSelected())
    {
      return PDFOutputTarget.SECURITY_ENCRYPTION_40BIT;
    }
    if (rbSecurity128Bit.isSelected())
    {
      return PDFOutputTarget.SECURITY_ENCRYPTION_128BIT;
    }
    return PDFOutputTarget.SECURITY_ENCRYPTION_NONE;
  }

  /**
   * Defines the currently selected encryption.
   *
   * @param b the new encryption state, one of null, Boolean.TRUE or Boolean.FALSE
   */
  public void setEncryptionValue(final String b)
  {
    if (b != null)
    {
      if (b.equals(PDFOutputTarget.SECURITY_ENCRYPTION_128BIT))
      {
        rbSecurity128Bit.setSelected(true);
        updateSecurityPanelEnabled();
        return;
      }
      else if (b.equals(PDFOutputTarget.SECURITY_ENCRYPTION_40BIT))
      {
        rbSecurity40Bit.setSelected(true);
        updateSecurityPanelEnabled();
        return;
      }
      else if (b.equals(PDFOutputTarget.SECURITY_ENCRYPTION_NONE) == false)
      {
        Log.warn("Invalid encryption value entered. " + b);
      }
    }
    rbSecurityNone.setSelected(true);
    updateSecurityPanelEnabled();
  }

  /**
   * Gets the confirmation state of the dialog. A confirmed dialog has no invalid
   * settings and the user confirmed any resource conflicts.
   *
   * @return true, if the dialog has been confirmed and the pdf should be saved, false otherwise.
   */
  public boolean isConfirmed()
  {
    return confirmed;
  }

  /**
   * Defines whether this dialog has been finished using the 'OK' or the 'Cancel' option.
   *
   * @param confirmed set to true, if OK was pressed, false otherwise
   */
  protected void setConfirmed(final boolean confirmed)
  {
    this.confirmed = confirmed;
  }

  /**
   * Clears all selections, input fields and set the selected encryption level to none.
   */
  public void clear()
  {
    txAuthor.setText(System.getProperty("user.name"));
    txConfOwnerPassword.setText("");
    txConfUserPassword.setText("");
    txFilename.setText("");
    txOwnerPassword.setText("");
    txTitle.setText("");
    txUserPassword.setText("");

    cxAllowAssembly.setSelected(false);
    cxAllowCopy.setSelected(false);
    cbAllowPrinting.setSelectedIndex(CBMODEL_NOPRINTING);
    cxAllowFillIn.setSelected(false);
    cxAllowModifyAnnotations.setSelected(false);
    cxAllowModifyContents.setSelected(false);
    cxAllowScreenReaders.setSelected(false);

    rbSecurityNone.setSelected(true);
    getActionSecuritySelection().actionPerformed(null);

    cbEncoding.setSelectedIndex(
        encodingModel.indexOf(System.getProperty("file.encoding", "Cp1251")));
  }

  /**
   * selects a file to use as target for the report processing.
   */
  protected void performSelectFile()
  {
    // lazy initialize ... the file chooser is one of the hot spots here ...
    if (fileChooser == null)
    {
      fileChooser = new JFileChooser();
      final FilesystemFilter filter = new FilesystemFilter(".pdf", "PDF Documents");
      fileChooser.addChoosableFileFilter(filter);
      fileChooser.setMultiSelectionEnabled(false);
    }

    final File file = new File(getFilename());
    fileChooser.setCurrentDirectory(file);
    fileChooser.setSelectedFile(file);
    final int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION)
    {
      final File selFile = fileChooser.getSelectedFile();
      String selFileName = selFile.getAbsolutePath();

      // Test if ends of pdf
      if (selFileName.toUpperCase().endsWith(".PDF") == false)
      {
        selFileName = selFileName + ".pdf";
      }
      setFilename(selFileName);
    }
  }

  /**
   * Validates the contents of the dialogs input fields. If the selected file exists, it is also
   * checked for validity.
   *
   * @return true, if the input is valid, false otherwise
   */
  public boolean performValidate()
  {
    if (getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_128BIT)
        || getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_40BIT))
    {
      if (txUserPassword.getText().equals(txConfUserPassword.getText()) == false)
      {
        JOptionPane.showMessageDialog(this, getResources().getString(
            "pdfsavedialog.userpasswordNoMatch"));
        return false;
      }
      if (txOwnerPassword.getText().equals(txConfOwnerPassword.getText()) == false)
      {
        JOptionPane.showMessageDialog(this, getResources().getString(
            "pdfsavedialog.ownerpasswordNoMatch"));
        return false;
      }
    }

    final String filename = getFilename();
    if (filename.trim().length() == 0)
    {
      JOptionPane.showMessageDialog(this,
          getResources().getString("pdfsavedialog.targetIsEmpty"),
          getResources().getString("pdfsavedialog.errorTitle"),
          JOptionPane.ERROR_MESSAGE);
      return false;
    }
    final File f = new File(filename);
    if (f.exists())
    {
      if (f.isFile() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString("pdfsavedialog.targetIsNoFile"),
            getResources().getString("pdfsavedialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      if (f.canWrite() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString("pdfsavedialog.targetIsNotWritable"),
            getResources().getString("pdfsavedialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      final String key1 = "pdfsavedialog.targetOverwriteConfirmation";
      final String key2 = "pdfsavedialog.targetOverwriteTitle";
      if (JOptionPane.showConfirmDialog(this,
          MessageFormat.format(getResources().getString(key1),
              new Object[]{getFilename()}
          ),
          getResources().getString(key2),
          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
          == JOptionPane.NO_OPTION)
      {
        return false;
      }
    }

    if (getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_128BIT)
        || getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_40BIT))
    {
      if (txOwnerPassword.getText().trim().length() == 0)
      {
        if (JOptionPane.showConfirmDialog(this,
            getResources().getString(
                "pdfsavedialog.ownerpasswordEmpty"),
            getResources().getString("pdfsavedialog.warningTitle"),
            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)
            == JOptionPane.NO_OPTION)
        {
          return false;
        }
      }
    }
    return true;
  }

  /**
   * Opens the dialog to query all necessary input from the user.
   * This will not start the processing, as this is done elsewhere.
   * 
   * @param report the report that should be processed.
   * @return true, if the processing should continue, false otherwise.
   */
  public boolean performQueryForExport(final JFreeReport report)
  {
    initFromConfiguration(report.getReportConfiguration());
    setModal(true);
    setVisible(true);
    if (isConfirmed() == false)
    {
      return false;
    }
    storeToConfiguration(report.getReportConfiguration());
    return true;
  }

  /**
   * Stores the input from the dialog into the report configuration of the 
   * report.
   * 
   * @param config the report configuration that should receive the new
   * settings.
   */
  public void storeToConfiguration(final ReportConfiguration config)
  {
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.AUTHOR,
            getAuthor());
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.TITLE,
            getPDFTitle());
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ENCRYPTION,
            getEncryptionValue());
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_OWNERPASSWORD,
            getOwnerPassword());
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_USERPASSWORD,
            getUserPassword());
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_ASSEMBLY,
            String.valueOf(isAllowAssembly()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_COPY,
            String.valueOf(isAllowCopy()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_DEGRADED_PRINTING,
            String.valueOf(isAllowDegradedPrinting()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_FILLIN,
            String.valueOf(isAllowFillIn()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_MODIFY_ANNOTATIONS,
            String.valueOf(isAllowModifyAnnotations()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_MODIFY_CONTENTS,
            String.valueOf(isAllowModifyContents()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_PRINTING,
            String.valueOf(isAllowPrinting()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_SCREENREADERS,
            String.valueOf(isAllowScreenreaders()));
    config.setConfigProperty
        (PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.ENCODING,
            getEncoding());
  }

  /**
   * Initialises the PDF save dialog from the settings in the report configuration.
   *
   * @param config  the report configuration.
   */
  public void initFromConfiguration(final ReportConfiguration config)
  {
    setAllowAssembly(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_ASSEMBLY, config,
        isAllowAssembly()));
    setAllowCopy(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_COPY, config, isAllowCopy()));
    setAllowFillIn(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_FILLIN, config, isAllowFillIn()));
    setAllowModifyAnnotations(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_MODIFY_ANNOTATIONS,
        config, isAllowModifyAnnotations()));
    setAllowModifyContents(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_MODIFY_CONTENTS, config,
        isAllowModifyContents()));
    setAllowScreenreaders(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_SCREENREADERS, config,
        isAllowScreenreaders()));

    final boolean printing = parseBoolean(PDFOutputTarget.SECURITY_ALLOW_PRINTING, config,
        isAllowPrinting());
    final boolean degraded = parseBoolean(PDFOutputTarget.SECURITY_ALLOW_DEGRADED_PRINTING, config,
        isAllowDegradedPrinting());
    setPrintLevel(printing, degraded);

    setEncryptionValue(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
        + PDFOutputTarget.SECURITY_ENCRYPTION, getEncryptionValue()));
    setAuthor(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
        + PDFOutputTarget.AUTHOR, getAuthor()));
    setUserPassword(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
        + PDFOutputTarget.SECURITY_USERPASSWORD, getUserPassword()));
    setOwnerPassword(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
        + PDFOutputTarget.SECURITY_OWNERPASSWORD, getOwnerPassword()));

    encodingModel.ensureEncodingAvailable(config.getConfigProperty
        (PDFOutputTarget.PDFTARGET_ENCODING));
    setEncoding(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
        + PDFOutputTarget.ENCODING, getEncoding()));
    setPDFTitle(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX +
        PDFOutputTarget.TITLE, getPDFTitle()));
  }

  /**
   * Parses a boolean property from a report configuration.
   *
   * @param key  the property key.
   * @param config  the report configuration.
   * @param orgVal  the default value.
   *
   * @return true or false.
   */
  private boolean parseBoolean(final String key, final ReportConfiguration config,
                               final boolean orgVal)
  {
    final String val = config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX + key,
        String.valueOf(orgVal));
    return (val.equalsIgnoreCase("true"));
  }

  /**
   * For debugging.
   *
   * @param args  ignored.
   */
  public static void main(final String[] args)
  {
    final JDialog d = new PDFSaveDialog();
    d.pack();
    d.addWindowListener(new WindowAdapter()
    {
      /**
       * Invoked when a window is in the process of being closed.
       * The close operation can be overridden at this point.
       */
      public void windowClosing(final WindowEvent e)
      {
        System.exit(0);
      }
    });
    d.setVisible(true);
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?