csvexportdialog.java
来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 949 行 · 第 1/2 页
JAVA
949 行
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());
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
otherPanel.add(rbSeparatorOther, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
otherPanel.add(txSeparatorOther, gbc);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 3;
gbc.insets = new Insets(1, 1, 1, 1);
separatorPanel.add(otherPanel, gbc);
return separatorPanel;
}
/**
* Returns the export file name.
*
* @return The file name.
*/
public String getFilename()
{
return txFilename.getText();
}
/**
* Sets the export file name.
*
* @param filename the file name.
*/
public void setFilename(final String filename)
{
this.txFilename.setText(filename);
}
/**
* Returns <code>true</code> if the user confirmed the selection, and <code>false</code>
* otherwise. The file should only be saved if the result is <code>true</code>.
*
* @return A boolean.
*/
public boolean isConfirmed()
{
return confirmed;
}
/**
* Defines whether this dialog has been finished using the 'OK' or the 'Cancel' option.
*
* @param confirmed set to <code>true</code>, if OK was pressed, <code>false</code> otherwise
*/
protected void setConfirmed(final boolean confirmed)
{
this.confirmed = confirmed;
}
/**
* Clears all selections, input fields and sets the selected encryption level to none.
*/
public void clear()
{
txFilename.setText("");
cbEncoding.setSelectedIndex(
encodingModel.indexOf(System.getProperty("file.encoding", "Cp1251")));
rbExportPrintedElements.setSelected(true);
rbSeparatorColon.setSelected(true);
cbxStrictLayout.setSelected(false);
performSeparatorSelection();
}
/**
* Returns the separator string, which is controlled by the selection of radio buttons.
*
* @return The separator string.
*/
public String getSeparatorString()
{
if (rbSeparatorColon.isSelected())
{
return ",";
}
if (rbSeparatorSemicolon.isSelected())
{
return ";";
}
if (rbSeparatorTab.isSelected())
{
return "\t";
}
if (rbSeparatorOther.isSelected())
{
return txSeparatorOther.getText();
}
return "";
}
/**
* Sets the separator string.
*
* @param s the separator.
*/
public void setSeparatorString(final String s)
{
if (s == null)
{
rbSeparatorOther.setSelected(true);
txSeparatorOther.setText("");
}
else if (s.equals(","))
{
rbSeparatorColon.setSelected(true);
}
else if (s.equals(";"))
{
rbSeparatorSemicolon.setSelected(true);
}
else if (s.equals("\t"))
{
rbSeparatorTab.setSelected(true);
}
else
{
rbSeparatorOther.setSelected(true);
txSeparatorOther.setText(s);
}
performSeparatorSelection();
}
/**
* Returns the encoding.
*
* @return The encoding.
*/
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.
*/
public void setEncoding(final String encoding)
{
cbEncoding.setSelectedIndex(encodingModel.indexOf(encoding));
}
/**
* Returns <code>true</code> if the user selected to export raw data only, and <code>false</code>
* otherwise.
*
* @return A boolean.
*/
public boolean isExportRawData()
{
return rbExportData.isSelected();
}
/**
* Sets a flag that controls whether raw data is exported.
*
* @param b the new flag value.
*/
public void setExportRawData(final boolean b)
{
if (b == true)
{
rbExportData.setSelected(true);
}
else
{
rbExportPrintedElements.setSelected(true);
}
}
/**
* Selects a file to use as target for the report processing.
*/
protected void performSelectFile()
{
if (fileChooser == null)
{
fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(
new ExtensionFileFilter
(getResources().getString("csvexportdialog.csv-file-description"), ".csv"));
fileChooser.setMultiSelectionEnabled(false);
}
fileChooser.setSelectedFile(new File(getFilename()));
final int option = fileChooser.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION)
{
final File selFile = fileChooser.getSelectedFile();
String selFileName = selFile.getAbsolutePath();
// Test if ends on csv
if (StringUtil.endsWithIgnoreCase(selFileName, ".csv") == false)
{
selFileName = selFileName + ".csv";
}
setFilename(selFileName);
}
}
/**
* Validates the contents of the dialog's input fields. If the selected file exists, it is also
* checked for validity.
*
* @return <code>true</code> if the input is valid, <code>false</code> otherwise
*/
public boolean performValidate()
{
final String filename = getFilename();
if (filename.trim().length() == 0)
{
JOptionPane.showMessageDialog(this,
getResources().getString("csvexportdialog.targetIsEmpty"),
getResources().getString("csvexportdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final File f = new File(filename);
if (f.exists())
{
if (f.isFile() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("csvexportdialog.targetIsNoFile"),
getResources().getString("csvexportdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
if (f.canWrite() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString(
"csvexportdialog.targetIsNotWritable"),
getResources().getString("csvexportdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final String key1 = "csvexportdialog.targetOverwriteConfirmation";
final String key2 = "csvexportdialog.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;
}
}
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;
}
/**
* Initialises the CSV export dialog from the settings in the report configuration.
*
* @param config the report configuration.
*/
public void initFromConfiguration(final ReportConfiguration config)
{
setSeparatorString(config.getConfigProperty(CSVProcessor.CSV_SEPARATOR, ","));
final String strict = config.getConfigProperty
(CSVTableProcessor.CONFIGURATION_PREFIX +
CSVTableProcessor.STRICT_LAYOUT,
config.getConfigProperty(TableProcessor.STRICT_TABLE_LAYOUT,
TableProcessor.STRICT_TABLE_LAYOUT_DEFAULT));
setStrictLayout(strict.equals("true"));
final String encoding = getCSVTargetEncoding(config);
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(CSVProcessor.CSV_SEPARATOR, getSeparatorString());
config.setConfigProperty(CSVTableProcessor.CONFIGURATION_PREFIX +
CSVTableProcessor.SEPARATOR_KEY, getSeparatorString());
config.setConfigProperty(CSVTableProcessor.STRICT_LAYOUT, String.valueOf(isStrictLayout()));
}
/**
* Enables or disables the 'other' separator text field.
*/
protected void performSeparatorSelection()
{
if (rbSeparatorOther.isSelected())
{
txSeparatorOther.setEnabled(true);
}
else
{
txSeparatorOther.setEnabled(false);
}
}
/**
* Returns the current setting of the 'strict layout' combo-box.
*
* @return A boolean.
*/
public boolean isStrictLayout()
{
return cbxStrictLayout.isSelected();
}
/**
* Sets the 'strict layout' combo-box setting.
*
* @param strictLayout the new setting.
*/
public void setStrictLayout(final boolean strictLayout)
{
cbxStrictLayout.setSelected(strictLayout);
}
/**
* This method exists for debugging purposes.
*
* @param args ignored.
*/
public static void main(final String[] args)
{
final JDialog d = new CSVExportDialog();
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);
}
/**
* Returns the CSV encoding property value.
*
* @param config the report configuration from where to read the values.
* @return the CSV encoding property value.
*/
public String getCSVTargetEncoding(final ReportConfiguration config)
{
return config.getConfigProperty(CSV_OUTPUT_ENCODING, CSV_OUTPUT_ENCODING_DEFAULT);
}
/**
* Sets the CSV encoding property value.
*
* @param config the report configuration from where to read the values.
* @param targetEncoding the new encoding.
*/
public void setCSVTargetEncoding(final ReportConfiguration config, final String targetEncoding)
{
config.setConfigProperty(CSV_OUTPUT_ENCODING, targetEncoding);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?