htmlexportdialog.java

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

JAVA
1,476
字号
  }

  /**
   * Returns the setting of the 'strict layout' check-box.
   *
   * @return A boolean.
   */
  public boolean isStrictLayout()
  {
    return cbxStrictLayout.isSelected();
  }

  /**
   * Sets the 'strict layout' check-box.
   *
   * @param s  boolean.
   */
  public void setStrictLayout(final boolean s)
  {
    cbxStrictLayout.setSelected(s);
  }

  /**
   * Returns the selected encoding.
   *
   * @return The encoding name.
   */
  public String getEncoding()
  {
    if (cbEncoding.getSelectedIndex() == -1)
    {
      return System.getProperty("file.encoding");
    }
    else
    {
      return encodingModel.getEncoding(cbEncoding.getSelectedIndex());
    }
  }

  /**
   * Sets the encoding.
   *
   * @param encoding  the encoding name.
   */
  public void setEncoding(final String encoding)
  {
    cbEncoding.setSelectedIndex(encodingModel.indexOf(encoding));
  }

  /**
   * Selects a file to use as target for the report processing.
   */
  protected void performSelectFileStream()
  {
    final File file = new File(getStreamFilename());

    if (fileChooserStream == null)
    {
      fileChooserStream = new JFileChooser();
      fileChooserStream.addChoosableFileFilter
          (new FilesystemFilter(new String[]{".html", ".htm"},
              getResources().getString("htmlexportdialog.html-documents"), true));
      fileChooserStream.setMultiSelectionEnabled(false);
    }

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

      // Test if ends on html
      if ((StringUtil.endsWithIgnoreCase(selFileName, ".html") == false)
          && (StringUtil.endsWithIgnoreCase(selFileName, ".htm") == false))
      {
        selFileName = selFileName + ".html";
      }
      setStreamFilename(selFileName);
    }
  }

  /**
   * Selects a file to use as target for the report processing.
   */
  protected void performSelectFileZip()
  {
    final File file = new File(getZipFilename());

    if (fileChooserZip == null)
    {
      fileChooserZip = new JFileChooser();
      fileChooserZip.addChoosableFileFilter
          (new FilesystemFilter(new String[]{".zip", ".jar"},
              getResources().getString("htmlexportdialog.zip-archives"), true));
      fileChooserZip.setMultiSelectionEnabled(false);
    }

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

      // Test if ends on xls
      if (StringUtil.endsWithIgnoreCase(selFileName, ".zip") == false)
      {
        selFileName = selFileName + ".zip";
      }
      setZipFilename(selFileName);
    }
  }

  /**
   * Selects a file to use as target for the report processing.
   */
  protected void performSelectFileDir()
  {
    if (fileChooserDir == null)
    {
      fileChooserDir = new JFileChooser();
      fileChooserDir.addChoosableFileFilter(new FilesystemFilter(new String[]{".html", ".htm"},
          getResources().getString("htmlexportdialog.html-documents"), true));
      fileChooserDir.setMultiSelectionEnabled(false);
    }

    final File file = new File(getDirFilename());
    fileChooserDir.setCurrentDirectory(file);
    fileChooserDir.setSelectedFile(file);
    final int option = fileChooserDir.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION)
    {
      final File selFile = fileChooserDir.getSelectedFile();
      String selFileName = selFile.getAbsolutePath();
      // Test if ends on html
      if ((StringUtil.endsWithIgnoreCase(selFileName, ".html") == false)
          && (StringUtil.endsWithIgnoreCase(selFileName, ".htm") == false))
      {
        selFileName = selFileName + ".html";
      }
      setDirFilename(selFileName);
    }
  }

  /**
   * Validates the contents of the dialog's input fields. If the selected file exists, it is also
   * checked for validity.
   *
   * @return true, if the input is valid, false otherwise
   */
  public boolean performValidateStream()
  {
    final String filename = getStreamFilename();
    if (filename.trim().length() == 0)
    {
      JOptionPane.showMessageDialog(this,
          getResources().getString("htmlexportdialog.targetIsEmpty"),
          getResources().getString("htmlexportdialog.errorTitle"),
          JOptionPane.ERROR_MESSAGE);
      return false;
    }
    final File f = new File(filename);
    if (f.exists())
    {
      if (f.isFile() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString("htmlexportdialog.targetIsNoFile"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      if (f.canWrite() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString(
                "htmlexportdialog.targetIsNotWritable"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      final String key1 = "htmlexportdialog.targetOverwriteConfirmation";
      final String key2 = "htmlexportdialog.targetOverwriteTitle";
      if (JOptionPane.showConfirmDialog(this,
          MessageFormat.format(getResources().getString(key1),
              new Object[]{getStreamFilename()}
          ),
          getResources().getString(key2),
          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
          == JOptionPane.NO_OPTION)
      {
        return false;
      }
    }

    return true;
  }

  /**
   * Validates the contents of the dialog's input fields. If the selected file exists, it is also
   * checked for validity.
   *
   * @return true, if the input is valid, false otherwise
   */
  public boolean performValidateZip()
  {
    final String filename = getZipFilename();
    if (filename.trim().length() == 0)
    {
      JOptionPane.showMessageDialog(this,
          getResources().getString("htmlexportdialog.targetIsEmpty"),
          getResources().getString("htmlexportdialog.errorTitle"),
          JOptionPane.ERROR_MESSAGE);
      return false;
    }
    final File f = new File(filename);
    if (f.exists())
    {
      if (f.isFile() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString("htmlexportdialog.targetIsNoFile"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      if (f.canWrite() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString(
                "htmlexportdialog.targetIsNotWritable"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      final String key1 = "htmlexportdialog.targetOverwriteConfirmation";
      final String key2 = "htmlexportdialog.targetOverwriteTitle";
      if (JOptionPane.showConfirmDialog(this,
          MessageFormat.format(getResources().getString(key1),
              new Object[]{getZipFilename()}
          ),
          getResources().getString(key2),
          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
          == JOptionPane.NO_OPTION)
      {
        return false;
      }
    }

    try
    {
      final File dataDir = new File(getZipDataFilename());
      final File baseDir = new File("");

      if (IOUtils.getInstance().isSubDirectory(baseDir, dataDir) == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString(
                "htmlexportdialog.targetPathIsAbsolute"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
    }
    catch (Exception e)
    {
      showExceptionDialog("error.validationfailed", e);
      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)
  {
    setModal(true);
    initFromConfiguration(report.getReportConfiguration());
    setVisible(true);
    if (isConfirmed() == false)
    {
      return false;
    }
    storeToConfiguration(report.getReportConfiguration());
    return true;
  }

  /**
   * Returns the selected export method.
   *
   * @return the selected Export method, one of EXPORT_STREAM, EXPORT_ZIP or EXPORT_DIR.
   */
  public int getSelectedExportMethod()
  {
    return exportSelection.getSelectedIndex();
  }

  /**
   * Sets the export method.
   *
   * @param index  the method index.
   */
  public void setSelectedExportMethod(final int index)
  {
    exportSelection.setSelectedIndex(index);
  }

  /**
   * 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 performValidateDir()
  {
    final String filename = getDirFilename();
    if (filename.trim().length() == 0)
    {
      JOptionPane.showMessageDialog(this,
          getResources().getString("htmlexportdialog.targetIsEmpty"),
          getResources().getString("htmlexportdialog.errorTitle"),
          JOptionPane.ERROR_MESSAGE);
      return false;
    }
    final File f = new File(filename);
    if (f.exists())
    {
      if (f.isFile() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString("htmlexportdialog.targetIsNoFile"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      if (f.canWrite() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString(
                "htmlexportdialog.targetIsNotWritable"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;
      }
      final String key1 = "htmlexportdialog.targetOverwriteConfirmation";
      final String key2 = "htmlexportdialog.targetOverwriteTitle";
      if (JOptionPane.showConfirmDialog(this,
          MessageFormat.format(getResources().getString(key1),
              new Object[]{getDirFilename()}
          ),
          getResources().getString(key2),
          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
          == JOptionPane.NO_OPTION)
      {
        return false;
      }
    }

    File dataDir = new File(getDirDataFilename());
    if (dataDir.isAbsolute() == false)
    {
      dataDir = new File(f.getParentFile(), dataDir.getPath());
    }

    if (dataDir.exists())
    {
      // dataDirectory does exist ... if no directory : fail
      if (dataDir.isDirectory() == false)
      {
        JOptionPane.showMessageDialog(this,
            getResources().getString(
                "htmlexportdialog.targetDataDirIsNoDirectory"),
            getResources().getString("htmlexportdialog.errorTitle"),
            JOptionPane.ERROR_MESSAGE);
        return false;

      }
    }
    else
    {
      final String key1 = "htmlexportdialog.targetCreateDataDirConfirmation";
      final String key2 = "htmlexportdialog.targetCreateDataDirTitle";
      if (JOptionPane.showConfirmDialog(this,
          MessageFormat.format(getResources().getString(key1),
              new Object[]{getDirFilename()}
          ),
          getResources().getString(key2),
          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
          == JOptionPane.NO_OPTION)
      {
        return false;
      }
    }
    return true;
  }

  /**
   * Shows the exception dialog by using localized messages. The message base is
   * used to construct the localisation key by appending ".title" and ".message" to the
   * base name.
   *
   * @param localisationBase  the resource key prefix.
   * @param e  the exception.
   */
  private void showExceptionDialog(final String localisationBase, final Exception e)
  {
    ExceptionDialog.showExceptionDialog(
        getResources().getString(localisationBase + ".title"),
        MessageFormat.format(
            getResources().getString(localisationBase + ".message"),
            new Object[]{e.getLocalizedMessage()}
        ),
        e);
  }

  /**
   * Initialises the Html export dialog from the settings in the report configuration.
   *
   * @param config  the report configuration.
   */
  public void initFromConfiguration(final ReportConfiguration config)
  {
    final String strict = config.getConfigProperty
        (HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProcessor.STRICT_LAYOUT,
            config.getConfigProperty(TableProcessor.STRICT_TABLE_LAYOUT,
                TableProcessor.STRICT_TABLE_LAYOUT_DEFAULT));
    setStrictLayout(strict.equals("true"));
    final String encoding = config.getConfigProperty
        (HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProducer.ENCODING, HtmlProducer.ENCODING_DEFAULT);
    setAuthor
        (config.getConfigProperty(HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProducer.AUTHOR, getAuthor()));
    setHTMLTitle
        (config.getConfigProperty(HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProducer.TITLE, getHTMLTitle()));
    encodingModel.ensureEncodingAvailable(encoding);
    setEncoding(encoding);
  }

  /**
   * 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(HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProducer.ENCODING, getEncoding());
    config.setConfigProperty(HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProducer.AUTHOR, getAuthor());
    config.setConfigProperty(HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProducer.TITLE, getHTMLTitle());
    config.setConfigProperty(HtmlProcessor.CONFIGURATION_PREFIX +
        HtmlProcessor.STRICT_LAYOUT, String.valueOf(isStrictLayout()));
  }

  /**
   * For debugging.
   *
   * @param args  ignored.
   */
  public static void main(final String[] args)
  {
    final HtmlExportDialog dialog = new HtmlExportDialog();
    dialog.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);
      }
    });
    dialog.pack();
    dialog.setVisible(true);
  }
}

⌨️ 快捷键说明

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