plaintextexportdialog.java
来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 1,075 行 · 第 1/3 页
JAVA
1,075 行
* Returns the lines-per-inch setting.
*
* @return The lines-per-inch setting.
*/
public int getLinesPerInch()
{
final Integer i = (Integer) cbLinesPerInch.getSelectedItem();
if (i == null)
{
return LPI_6.intValue();
}
return i.intValue();
}
/**
* Sets the lines per inch.
*
* @param i the lines per inch.
*/
public void setLinesPerInch(final int i)
{
if (i == LPI_10.intValue() || i == LPI_6.intValue())
{
cbLinesPerInch.setSelectedItem(new Integer(i));
}
else
{
throw new IllegalArgumentException();
}
}
/**
* Returns the characters-per-inch setting.
*
* @return The characters-per-inch setting.
*/
public int getCharsPerInch()
{
final Integer i = (Integer) cbCharsPerInch.getSelectedItem();
if (i == null)
{
return CPI_10.intValue();
}
return i.intValue();
}
/**
* Sets the characters per inch.
*
* @param i the characters per inch.
*/
public void setCharsPerInch(final int i)
{
if (i == CPI_10.intValue() || i == CPI_12.intValue()
|| i == CPI_15.intValue()
|| i == CPI_17.intValue()
|| i == CPI_20.intValue())
{
cbCharsPerInch.setSelectedItem(new Integer(i));
}
else
{
throw new IllegalArgumentException();
}
}
/**
* Returns the encoding.
*
* @return The encoding.
*/
public String getEncoding()
{
if (cbEncoding.getSelectedIndex() == -1)
{
if (selectedEncodingModel.getSize() > 0)
{
// return the first selected encoding, unless specified otherwise.
return selectedEncodingModel.getEncoding(0);
}
return PlainTextOutputTarget.getTextTargetEncoding(null);
}
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 PDF save dialog from the settings in the report configuration.
*
* @param config the report configuration.
*/
public void initFromConfiguration(final ReportConfiguration config)
{
setEncoding(config.getConfigProperty
(PlainTextOutputTarget.TEXT_OUTPUT_ENCODING,
PlainTextOutputTarget.TEXT_OUTPUT_ENCODING_DEFAULT));
try
{
setLinesPerInch(StringUtil.parseInt(config.getConfigProperty
(PlainTextOutputTarget.CONFIGURATION_PREFIX + PlainTextOutputTarget.LINES_PER_INCH, "6"),
getLinesPerInch()));
}
catch (IllegalArgumentException e)
{
// ignore
}
try
{
setCharsPerInch(StringUtil.parseInt(config.getConfigProperty
(PlainTextOutputTarget.CONFIGURATION_PREFIX + PlainTextOutputTarget.CHARS_PER_INCH, "10"),
getCharsPerInch()));
}
catch (IllegalArgumentException e)
{
// ignore
}
}
/**
* 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(PlainTextOutputTarget.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()));
}
/**
* 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;
}
/**
* 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 + =
减小字号Ctrl + -
显示快捷键?