📄 csvexportdialog.java
字号:
/**
* ========================================
* 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.
*
* --------------------
* CSVExportDialog.java
* --------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: CSVExportDialog.java,v 1.15.2.2 2003/08/24 14:18:11 taqua Exp $
*
* Changes
* --------
* 25-Feb-2003 : Added standard header and Javadocs (DG);
*
*/
package com.jrefinery.report.preview;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
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.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.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.TitledBorder;
import com.jrefinery.report.JFreeReport;
import com.jrefinery.report.targets.csv.CSVProcessor;
import com.jrefinery.report.targets.table.csv.CSVTableProcessor;
import com.jrefinery.report.util.ActionButton;
import com.jrefinery.report.util.ExceptionDialog;
import com.jrefinery.report.util.LengthLimitingDocument;
import com.jrefinery.report.util.ReportConfiguration;
import com.jrefinery.report.util.StringUtil;
import org.jfree.ui.ExtensionFileFilter;
/**
* A dialog for exporting a report to CSV format.
*
* @author Thomas Morgner.
*/
public class CSVExportDialog extends JDialog
{
/**
* Internal action class to confirm the dialog and to validate the input.
*/
private class ActionConfirm extends AbstractAction
{
/**
* Default constructor.
*/
public ActionConfirm()
{
putValue(Action.NAME, getResources().getString("csvexportdialog.confirm"));
}
/**
* Receives notification that the action has occurred.
*
* @param e the action event.
*/
public void actionPerformed(final ActionEvent e)
{
if (performValidate())
{
setConfirmed(true);
setVisible(false);
}
}
}
/**
* Internal action class to confirm the dialog and to validate the input.
*/
private class ActionSelectSeparator extends AbstractAction
{
/**
* Default constructor.
*/
public ActionSelectSeparator()
{
}
/**
* Receives notification that the action has occurred.
*
* @param e the action event.
*/
public void actionPerformed(final ActionEvent e)
{
performSeparatorSelection();
}
}
/**
* Internal action class to cancel the report processing.
*/
private class ActionCancel extends AbstractAction
{
/**
* Default constructor.
*/
public ActionCancel()
{
putValue(Action.NAME, getResources().getString("csvexportdialog.cancel"));
}
/**
* Receives notification that the action has occurred.
*
* @param e the action event.
*/
public void actionPerformed(final ActionEvent e)
{
setConfirmed(false);
setVisible(false);
}
}
/**
* Internal action class to select a target file.
*/
private class ActionSelectFile extends AbstractAction
{
/**
* Default constructor.
*/
public ActionSelectFile()
{
putValue(Action.NAME, getResources().getString("csvexportdialog.selectFile"));
}
/**
* Receives notification that the action has occurred.
*
* @param e the action event.
*/
public void actionPerformed(final ActionEvent e)
{
performSelectFile();
}
}
/** Filename text field. */
private JTextField txFilename;
/** The encoding combo-box. */
private JComboBox cbEncoding;
/** The encoding model. */
private EncodingComboBoxModel encodingModel;
/** The strict layout check-box. */
private JCheckBox cbxStrictLayout;
/** A radio button for tab separators. */
private JRadioButton rbSeparatorTab;
/** A radio button for colon separators. */
private JRadioButton rbSeparatorColon;
/** A radio button for semi-colon separators. */
private JRadioButton rbSeparatorSemicolon;
/** A radio button for other separators. */
private JRadioButton rbSeparatorOther;
/** A text field for the 'other' separator. */
private JTextField txSeparatorOther;
/** A radio button for exporting data only. */
private JRadioButton rbExportData;
/** A radio button for exporting all printed elements. */
private JRadioButton rbExportPrintedElements;
/** A file chooser. */
private JFileChooser fileChooser;
/** Confirmed flag. */
private boolean confirmed;
/** Localised resources. */
private ResourceBundle resources;
/** The base resource class. */
public static final String BASE_RESOURCE_CLASS =
"com.jrefinery.report.resources.JFreeReportResources";
/**
* Creates a new CSV export dialog.
*
* @param owner the dialog owner.
*/
public CSVExportDialog(final Frame owner)
{
super(owner);
initConstructor();
}
/**
* Creates a new CSV export dialog.
*
* @param owner the dialog owner.
*/
public CSVExportDialog(final Dialog owner)
{
super(owner);
initConstructor();
}
/**
* Creates a new CSV export dialog. The created dialog is modal.
*/
public CSVExportDialog()
{
initConstructor();
}
/**
* Initialisation.
*/
private void initConstructor()
{
setModal(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setTitle(getResources().getString("csvexportdialog.dialogtitle"));
initialize();
clear();
addWindowListener(new WindowAdapter()
{
public void windowClosing(final WindowEvent e)
{
new ActionCancel().actionPerformed(null);
}
}
);
}
/**
* Retrieves the resources for this dialog. If the resources are not initialized,
* they get loaded on the first call to this method.
*
* @return this frames ResourceBundle.
*/
protected ResourceBundle getResources()
{
if (resources == null)
{
resources = ResourceBundle.getBundle(BASE_RESOURCE_CLASS);
}
return resources;
}
/**
* Initializes the Swing components of this dialog.
*/
private void initialize()
{
final JPanel contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
final JLabel lblFileName = new JLabel(getResources().getString("csvexportdialog.filename"));
final JLabel lblEncoding = new JLabel(getResources().getString("csvexportdialog.encoding"));
final JButton btnSelect = new ActionButton(new ActionSelectFile());
cbxStrictLayout = new JCheckBox(getResources().getString("csvexportdialog.strict-layout"));
txFilename = new JTextField();
encodingModel = EncodingComboBoxModel.createDefaultModel();
encodingModel.sort();
cbEncoding = new JComboBox(encodingModel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(3, 1, 1, 1);
contentPane.add(lblFileName, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(1, 1, 1, 1);
contentPane.add(lblEncoding, gbc);
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.ipadx = 120;
gbc.gridwidth = 2;
gbc.insets = new Insets(3, 1, 1, 1);
contentPane.add(txFilename, gbc);
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.insets = new Insets(1, 1, 1, 1);
contentPane.add(cbEncoding, gbc);
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.insets = new Insets(1, 1, 1, 1);
contentPane.add(cbxStrictLayout, gbc);
final JPanel p = new JPanel(new GridLayout());
p.add(createSeparatorPanel());
p.add(createExportTypePanel());
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 1, 1, 1);
contentPane.add(p, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridheight = 2;
contentPane.add(btnSelect, gbc);
// button panel
final JButton btnCancel = new ActionButton(new ActionCancel());
final JButton btnConfirm = new ActionButton(new ActionConfirm());
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout());
buttonPanel.add(btnConfirm);
buttonPanel.add(btnCancel);
btnConfirm.setDefaultCapable(true);
buttonPanel.registerKeyboardAction(new ActionConfirm(),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridwidth = 4;
gbc.gridy = 6;
gbc.insets = new Insets(10, 0, 0, 0);
contentPane.add(buttonPanel, gbc);
setContentPane(contentPane);
}
/**
* Creates a panel for the export type.
*
* @return The panel.
*/
private JPanel createExportTypePanel()
{
// separator panel
final JPanel exportTypePanel = new JPanel();
exportTypePanel.setLayout(new GridBagLayout());
final TitledBorder tb = new TitledBorder(getResources().getString("csvexportdialog.exporttype"));
exportTypePanel.setBorder(tb);
rbExportData = new JRadioButton(getResources().getString("csvexportdialog.export.data"));
rbExportPrintedElements = new JRadioButton(
getResources().getString("csvexportdialog.export.printed_elements"));
final ButtonGroup btg = new ButtonGroup();
btg.add(rbExportData);
btg.add(rbExportPrintedElements);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
exportTypePanel.add(rbExportData, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weighty = 1;
gbc.insets = new Insets(1, 1, 1, 1);
exportTypePanel.add(rbExportPrintedElements, gbc);
return exportTypePanel;
}
/**
* Creates a separator panel.
*
* @return The panel.
*/
private JPanel createSeparatorPanel()
{
// separator panel
final JPanel separatorPanel = new JPanel();
separatorPanel.setLayout(new GridBagLayout());
final TitledBorder tb = new TitledBorder(getResources().getString("csvexportdialog.separatorchar"));
separatorPanel.setBorder(tb);
rbSeparatorTab = new JRadioButton(getResources().getString("csvexportdialog.separator.tab"));
rbSeparatorColon = new JRadioButton(
getResources().getString("csvexportdialog.separator.colon"));
rbSeparatorSemicolon = new JRadioButton(
getResources().getString("csvexportdialog.separator.semicolon"));
rbSeparatorOther = new JRadioButton(
getResources().getString("csvexportdialog.separator.other"));
final ButtonGroup btg = new ButtonGroup();
btg.add(rbSeparatorTab);
btg.add(rbSeparatorColon);
btg.add(rbSeparatorSemicolon);
btg.add(rbSeparatorOther);
final Action selectAction = new ActionSelectSeparator();
rbSeparatorTab.addActionListener(selectAction);
rbSeparatorColon.addActionListener(selectAction);
rbSeparatorSemicolon.addActionListener(selectAction);
rbSeparatorOther.addActionListener(selectAction);
final LengthLimitingDocument ldoc = new LengthLimitingDocument(1);
txSeparatorOther = new JTextField();
txSeparatorOther.setDocument(ldoc);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
separatorPanel.add(rbSeparatorTab, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(1, 1, 1, 1);
separatorPanel.add(rbSeparatorColon, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(1, 1, 1, 1);
separatorPanel.add(rbSeparatorSemicolon, gbc);
final JPanel otherPanel = new JPanel();
otherPanel.setLayout(new GridBagLayout());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -