reportconvertergui.java

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

JAVA
637
字号
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------------------
 * ReportConverterGUI.java
 * -----------------------
 * (C)opyright 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: ReportConverterGUI.java,v 1.11 2003/11/07 18:33:52 taqua Exp $
 *
 * Changes
 * -------
 * 21-Feb-2003 : Added standard header and Javadocs (DG);
 *
 */
package org.jfree.report.modules.gui.converter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.jfree.report.JFreeReport;
import org.jfree.report.modules.gui.base.components.ActionButton;
import org.jfree.report.modules.gui.base.components.EncodingComboBoxModel;
import org.jfree.report.modules.gui.base.components.FilesystemFilter;
import org.jfree.report.modules.gui.converter.components.OperationResultTableModel;
import org.jfree.report.modules.gui.converter.parser.ConverterParserFrontend;
import org.jfree.report.modules.gui.converter.resources.ConverterResources;
import org.jfree.report.modules.parser.ext.factory.datasource.DefaultDataSourceFactory;
import org.jfree.report.modules.parser.ext.factory.elements.DefaultElementFactory;
import org.jfree.report.modules.parser.ext.factory.objects.BandLayoutClassFactory;
import org.jfree.report.modules.parser.ext.factory.objects.DefaultClassFactory;
import org.jfree.report.modules.parser.ext.factory.stylekey.DefaultStyleKeyFactory;
import org.jfree.report.modules.parser.ext.factory.stylekey.PageableLayoutStyleKeyFactory;
import org.jfree.report.modules.parser.ext.factory.templates.DefaultTemplateCollection;
import org.jfree.report.modules.parser.extwriter.ReportWriter;
import org.jfree.report.util.Log;
import org.jfree.report.util.StringUtil;
import org.jfree.util.DefaultConfiguration;
import org.jfree.xml.Parser;
import org.jfree.xml.factory.objects.ArrayClassFactory;
import org.jfree.xml.factory.objects.URLClassFactory;

/**
 * A utility application for converting XML report files from the old format to the
 * new format.
 *
 * @author Thomas Morgner.
 */
public class ReportConverterGUI extends JFrame
{
  /** The base resource class. */
  public static final String BASE_RESOURCE_CLASS =
      ConverterResources.class.getName();

  /**
   * An action for selecting the target.
   */
  private class SelectTargetAction extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default
     * description string and default icon.
     */
    public SelectTargetAction()
    {
      putValue(Action.NAME, getResources().getString("convertdialog.action.selectTarget.name"));
      putValue(Action.SHORT_DESCRIPTION,
          getResources().getString("convertdialog.action.selectTarget.description"));
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e  the action event.
     */
    public void actionPerformed(final ActionEvent e)
    {
      setTargetFile(performSelectFile(getTargetFile(), true));
    }
  }

  /**
   * An action for selecting the source file.
   */
  private class SelectSourceAction extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default
     * description string and default icon.
     */
    public SelectSourceAction()
    {
      putValue(Action.NAME, getResources().getString("convertdialog.action.selectSource.name"));
      putValue(Action.SHORT_DESCRIPTION,
          getResources().getString("convertdialog.action.selectSource.description"));
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e  the action event.
     */
    public void actionPerformed(final ActionEvent e)
    {
      setSourceFile(performSelectFile(getSourceFile(), false));
    }
  }

  /**
   * An action for converting an XML report definition from the old format to the new.
   */
  private class ConvertAction extends AbstractAction
  {
    /**
     * Defines an <code>Action</code> object with a default
     * description string and default icon.
     */
    public ConvertAction()
    {
      putValue(Action.NAME, getResources().getString("convertdialog.action.convert.name"));
      putValue(Action.SHORT_DESCRIPTION,
          getResources().getString("convertdialog.action.convert.description"));
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e  the action event.
     */
    public void actionPerformed(final ActionEvent e)
    {
      convert();
    }
  }

  /** The source field. */
  private final JTextField sourceField;

  /** The target field. */
  private final JTextField targetField;

  /** A file chooser. */
  private final JFileChooser fileChooser;

  /** Localised resources. */
  private ResourceBundle resources;

  /** The encoding combo box model used to select the target file encoding. */
  private final EncodingComboBoxModel encodingModel;

  /** The table model that displays all messages from the conversion. */
  private final OperationResultTableModel resultTableModel;

  /** A primitive status bar. */
  private JLabel statusHolder;

  /**
   * Default constructor.
   */
  public ReportConverterGUI()
  {
    encodingModel = EncodingComboBoxModel.createDefaultModel();
    encodingModel.ensureEncodingAvailable("UTF-16");
    encodingModel.setSelectedIndex(encodingModel.indexOf("UTF-16"));
    encodingModel.sort();

    resultTableModel = new OperationResultTableModel();

    sourceField = new JTextField();
    targetField = new JTextField();

    final JTable table = new JTable(resultTableModel);
    table.setMinimumSize(new Dimension (100, 100));
    final JSplitPane componentPane = new JSplitPane
        (JSplitPane.VERTICAL_SPLIT, createMainPane(), new JScrollPane(table));
    componentPane.setOneTouchExpandable(true);
    componentPane.resetToPreferredSizes();

    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(componentPane, BorderLayout.CENTER);
    contentPane.add(createStatusBar(), BorderLayout.SOUTH);
    setContentPane(contentPane);

    fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new FilesystemFilter(new String[]{".xml"},
        "XML-Report definitions", true));
    fileChooser.setMultiSelectionEnabled(false);

    setTitle(getResources().getString("convertdialog.title"));
  }

  /**
   * Creates the statusbar for this frame. Use setStatus() to display text on the status bar.
   *
   * @return the status bar.
   */
  protected JPanel createStatusBar()
  {
    final JPanel statusPane = new JPanel();
    statusPane.setLayout(new BorderLayout());
    statusPane.setBorder(
        BorderFactory.createLineBorder(UIManager.getDefaults().getColor("controlShadow")));
    statusHolder = new JLabel(" ");
    statusPane.setMinimumSize(statusHolder.getPreferredSize());
    statusPane.add(statusHolder, BorderLayout.WEST);

    return statusPane;
  }

  /**
   * Sets the text of the status line.
   * 
   * @param text the new text that should be displayed in the status bar.
   */
  public void setStatusText (final String text)
  {
    statusHolder.setText(text);
  }

  /**
   * Returns the text from the status bar.
   * 
   * @return the status bar text.
   */
  public String getStatusText ()
  {
    return statusHolder.getText();
  }

  /**
   * Creates the main panel and all child components.
   *  
   * @return the created panel.
   */
  private JPanel createMainPane ()
  {
    final JButton selectSourceButton = new ActionButton(new SelectSourceAction());
    final JButton selectTargetButton = new ActionButton(new SelectTargetAction());
    final JButton convertFilesButton = new ActionButton(new ConvertAction());

    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());
    contentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(3, 1, 1, 1);
    contentPane.add(new JLabel(getResources().getString("convertdialog.sourceFile")), gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);
    gbc.ipadx = 120;
    contentPane.add(sourceField, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(3, 1, 1, 1);

⌨️ 快捷键说明

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