📄 plaintextexportdialog.java
字号:
// return the first selected encoding, unless specified otherwise.
return selectedEncodingModel.getEncoding(0);
}
return ReportConfiguration.getGlobalConfig().getTextTargetEncoding();
}
else
{
return selectedEncodingModel.getEncoding(cbEncoding.getSelectedIndex());
}
}
/**
* Sets the encoding.
*
* @param encoding the encoding.
*/
public void setEncoding(final String encoding)
{
if (encoding == null)
{
throw new NullPointerException("Encoding must not be null");
}
if (cbEncoding.getModel() != selectedEncodingModel)
{
throw new IllegalStateException("Model for the combobox is not set up correctly.");
}
ensureEncodingAvailable(plainTextCommandSet, plainTextEncodingModel, encoding);
ensureEncodingAvailable(ibmPrinterCommandSet, ibmPrinterEncodingModel, encoding);
ensureEncodingAvailable(epsonPrinterCommandSet, epsonPrinterEncodingModel, encoding);
if (selectedEncodingModel.indexOf(encoding) == -1)
{
throw new IllegalStateException("This encoding is not known.");
}
final int ibmIndex = ibmPrinterEncodingModel.indexOf(encoding);
final int epsonIndex = epsonPrinterEncodingModel.indexOf(encoding);
final int plainIndex = plainTextEncodingModel.indexOf(encoding);
if (ibmIndex >= 0)
{
ibmPrinterEncodingModel.setSelectedIndex(ibmIndex);
if (ibmPrinterEncodingModel.getSelectedIndex() == -1)
{
throw new IllegalStateException("Unable to select encoding for IBM model.");
}
}
if (plainIndex >= 0)
{
plainTextEncodingModel.setSelectedIndex(plainIndex);
if (plainTextEncodingModel.getSelectedIndex() == -1)
{
throw new IllegalStateException("Unable to select encoding for plain text model.");
}
}
if (epsonIndex >= 0)
{
epsonPrinterEncodingModel.setSelectedIndex(epsonIndex);
if (epsonPrinterEncodingModel.getSelectedIndex() == -1)
{
throw new IllegalStateException("Unable to select encoding for epson model.");
}
}
final int selectedIndex = selectedEncodingModel.indexOf(encoding);
final Object o = cbEncoding.getModel().getElementAt(selectedIndex);
cbEncoding.setSelectedItem(o);
}
/**
* Initialises the CSV export dialog from the settings in the report configuration.
*
* @param config the report configuration.
*/
public void initFromConfiguration(final ReportConfiguration config)
{
setEncoding(config.getConfigProperty
(ReportConfiguration.TEXT_OUTPUT_ENCODING, ReportConfiguration.TEXT_OUTPUT_ENCODING_DEFAULT));
try
{
setLinesPerInch(parseInt(config.getConfigProperty
(PlainTextOutputTarget.CONFIGURATION_PREFIX + PlainTextOutputTarget.LINES_PER_INCH, "6"),
getLinesPerInch()));
}
catch (IllegalArgumentException e)
{
// ignore
}
try
{
setCharsPerInch(parseInt(config.getConfigProperty
(PlainTextOutputTarget.CONFIGURATION_PREFIX + PlainTextOutputTarget.CHARS_PER_INCH, "10"),
getCharsPerInch()));
}
catch (IllegalArgumentException e)
{
// ignore
}
}
private int parseInt (String configProperty, int defaultValue)
{
try
{
return Integer.parseInt(configProperty);
}
catch (Exception e)
{
return defaultValue;
}
}
public void storeToConfiguration(final ReportConfiguration config)
{
config.setConfigProperty(ReportConfiguration.TEXT_OUTPUT_ENCODING, getEncoding());
config.setConfigProperty(PlainTextOutputTarget.CONFIGURATION_PREFIX +
PlainTextOutputTarget.CHARS_PER_INCH, String.valueOf(getCharsPerInch()));
config.setConfigProperty(PlainTextOutputTarget.CONFIGURATION_PREFIX +
PlainTextOutputTarget.LINES_PER_INCH, String.valueOf(getLinesPerInch()));
}
public boolean performQueryForExport (final JFreeReport report)
{
initFromConfiguration(report.getReportConfiguration());
setModal(true);
setVisible(true);
if (isConfirmed() == false)
{
return false;
}
storeToConfiguration(report.getReportConfiguration());
return true;
}
/**
* Exports a report to a text file.
*
* @param report the report.
* @deprecated this dialog should only be used to configure the export process
* @return A boolean.
*/
public boolean performExport(final JFreeReport report)
{
initFromConfiguration(report.getReportConfiguration());
setModal(true);
setVisible(true);
if (isConfirmed() == false)
{
return true;
}
return writeReport(report);
}
/**
* Returns the printer command set.
*
* @param out the output stream.
* @param report the report.
*
* @return The printer command set.
*/
public PrinterCommandSet getPrinterCommandSet(final OutputStream out, final JFreeReport report)
{
switch (getSelectedPrinter())
{
case TYPE_PLAIN_OUTPUT:
{
return new PrinterCommandSet(out, report.getDefaultPageFormat(), getCharsPerInch(),
getLinesPerInch());
}
case TYPE_IBM_OUTPUT:
{
return new IBMPrinterCommandSet(out, report.getDefaultPageFormat(), getCharsPerInch(),
getLinesPerInch());
}
case TYPE_EPSON_OUTPUT:
{
return new EpsonPrinterCommandSet(out, report.getDefaultPageFormat(), getCharsPerInch(),
getLinesPerInch());
}
}
throw new IllegalArgumentException();
}
/**
* Writes a report.
*
* @param report the report.
*
* @return true, if the report was successfully written, false otherwise.
* @deprecated this dialog should only be used to configure the export process
*/
public boolean writeReport(final JFreeReport report)
{
OutputStream out = null;
try
{
out = new BufferedOutputStream(
new FileOutputStream(
new File(getFilename())));
final PrinterCommandSet pc = getPrinterCommandSet(out, report);
final PlainTextOutputTarget target = new PlainTextOutputTarget(report.getDefaultPageFormat(), pc);
target.configure(report.getReportConfiguration());
target.setProperty(PlainTextOutputTarget.ENCODING_PROPERTY, getEncoding());
final PageableReportProcessor proc = new PageableReportProcessor(report);
proc.setHandleInterruptedState(false);
proc.setOutputTarget(target);
target.open();
proc.processReport();
target.close();
return true;
}
catch (Exception re)
{
showExceptionDialog("error.processingfailed", re);
return false;
}
finally
{
try
{
if (out != null)
{
out.close();
}
}
catch (Exception e)
{
showExceptionDialog("error.savefailed", e);
}
}
}
/**
* 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);
}
/**
* 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;
}
/**
* Checks, whether the printer supports the given encoding and adds it if possible.
*
* @param cmd the printer command set that should used for printing.
* @param model the encoding combobox model that should contain the encoding.
* @param encoding the specified encoding.
*/
private void ensureEncodingAvailable
(final PrinterCommandSet cmd, final EncodingComboBoxModel model, final String encoding)
{
if (cmd.isEncodingSupported(encoding))
{
model.ensureEncodingAvailable(encoding);
}
}
/**
* Creates an encoding model.
*
* @param cmd the printer command set.
*
* @return The encoding model.
*/
private EncodingComboBoxModel createEncodingModel(final PrinterCommandSet cmd)
{
final EncodingComboBoxModel defaultEncodingModel = EncodingComboBoxModel.createDefaultModel();
final EncodingComboBoxModel retval = new EncodingComboBoxModel();
for (int i = 0; i < defaultEncodingModel.getSize(); i++)
{
final String encoding = defaultEncodingModel.getEncoding(i);
if (cmd.isEncodingSupported(encoding))
{
final String description = defaultEncodingModel.getDescription(i);
retval.addEncoding(encoding, description);
}
}
retval.sort();
return retval;
}
/**
* Selects a file to use as target for the report processing.
*/
protected void performSelectFile()
{
if (fileChooser == null)
{
fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new ExtensionFileFilter("Plain text files", ".txt"));
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 xls
if (StringUtil.endsWithIgnoreCase(selFileName, ".txt") == false)
{
selFileName = selFileName + ".txt";
}
setFilename(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 performValidate()
{
final String filename = getFilename();
if (filename.trim().length() == 0)
{
JOptionPane.showMessageDialog(this,
getResources().getString(
"plain-text-exportdialog.targetIsEmpty"),
getResources().getString("plain-text-exportdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final File f = new File(filename);
if (f.exists())
{
if (f.isFile() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("plain-text-exportdialog.targetIsNoFile"),
getResources().getString("plain-text-exportdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
if (f.canWrite() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("plain-text-exportdialog.targetIsNotWritable"),
getResources().getString("plain-text-exportdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final String key1 = "plain-text-exportdialog.targetOverwriteConfirmation";
final String key2 = "plain-text-exportdialog.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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -