📄 pdfsavedialog.java
字号:
txConfOwnerPassword.setText("");
txConfUserPassword.setText("");
txFilename.setText("");
txOwnerPassword.setText("");
txTitle.setText("");
txUserPassword.setText("");
cxAllowAssembly.setSelected(false);
cxAllowCopy.setSelected(false);
cbAllowPrinting.setSelectedIndex(CBMODEL_NOPRINTING);
cxAllowFillIn.setSelected(false);
cxAllowModifyAnnotations.setSelected(false);
cxAllowModifyContents.setSelected(false);
cxAllowScreenReaders.setSelected(false);
rbSecurityNone.setSelected(true);
getActionSecuritySelection().actionPerformed(null);
cbEncoding.setSelectedIndex(
encodingModel.indexOf(System.getProperty("file.encoding", "Cp1251")));
}
/**
* selects a file to use as target for the report processing.
*/
protected void performSelectFile()
{
// lazy initialize ... the file chooser is one of the hot spots here ...
if (fileChooser == null)
{
fileChooser = new JFileChooser();
final FilesystemFilter filter = new FilesystemFilter(".pdf", "PDF Documents");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setMultiSelectionEnabled(false);
}
final File file = new File(getFilename());
fileChooser.setCurrentDirectory(file);
fileChooser.setSelectedFile(file);
final int option = fileChooser.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION)
{
final File selFile = fileChooser.getSelectedFile();
String selFileName = selFile.getAbsolutePath();
// Test if ends of pdf
if (selFileName.toUpperCase().endsWith(".PDF") == false)
{
selFileName = selFileName + ".pdf";
}
setFilename(selFileName);
}
}
/**
* Validates the contents of the dialogs 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()
{
if (getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_128BIT)
|| getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_40BIT))
{
if (txUserPassword.getText().equals(txConfUserPassword.getText()) == false)
{
JOptionPane.showMessageDialog(this, getResources().getString(
"pdfsavedialog.userpasswordNoMatch"));
return false;
}
if (txOwnerPassword.getText().equals(txConfOwnerPassword.getText()) == false)
{
JOptionPane.showMessageDialog(this, getResources().getString(
"pdfsavedialog.ownerpasswordNoMatch"));
return false;
}
}
final String filename = getFilename();
if (filename.trim().length() == 0)
{
JOptionPane.showMessageDialog(this,
getResources().getString("pdfsavedialog.targetIsEmpty"),
getResources().getString("pdfsavedialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final File f = new File(filename);
if (f.exists())
{
if (f.isFile() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("pdfsavedialog.targetIsNoFile"),
getResources().getString("pdfsavedialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
if (f.canWrite() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("pdfsavedialog.targetIsNotWritable"),
getResources().getString("pdfsavedialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final String key1 = "pdfsavedialog.targetOverwriteConfirmation";
final String key2 = "pdfsavedialog.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;
}
}
if (getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_128BIT)
|| getEncryptionValue().equals(PDFOutputTarget.SECURITY_ENCRYPTION_40BIT))
{
if (txOwnerPassword.getText().trim().length() == 0)
{
if (JOptionPane.showConfirmDialog(this,
getResources().getString(
"pdfsavedialog.ownerpasswordEmpty"),
getResources().getString("pdfsavedialog.warningTitle"),
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)
== JOptionPane.NO_OPTION)
{
return false;
}
}
}
return true;
}
public boolean performQueryForExport (final JFreeReport report)
{
initFromConfiguration(report.getReportConfiguration());
setModal(true);
setVisible(true);
if (isConfirmed() == false)
{
return false;
}
storeToConfiguration(report.getReportConfiguration());
return true;
}
/**
* Shows this dialog and (if the dialog is confirmed) saves the complete report into a PDF-File.
*
* @param report the report being processed.
* @return true, if the export was performed without errors or the user canceled the dialog,
* or false on errors during the export.
* @deprecated this dialog should only be used to configure the export process
*/
public boolean performExport(final JFreeReport report)
{
initFromConfiguration(report.getReportConfiguration());
setModal(true);
setVisible(true);
if (isConfirmed() == false)
{
return true;
}
return writePDF(report, report.getDefaultPageFormat());
}
/**
* Saves a report to PDF format.
*
* @param report the report.
* @param pf the page format.
*
* @return true or false.
* @deprecated this dialog should only be used to configure the export process
*/
public boolean writePDF(final JFreeReport report, final PageFormat pf)
{
OutputStream out = null;
try
{
out = new BufferedOutputStream(new FileOutputStream(new File(getFilename())));
final PDFOutputTarget target = new PDFOutputTarget(out, pf, true);
target.configure(report.getReportConfiguration());
target.setProperty(PDFOutputTarget.AUTHOR, getAuthor());
target.setProperty(PDFOutputTarget.TITLE, getPDFTitle());
target.setProperty(PDFOutputTarget.SECURITY_ENCRYPTION, getEncryptionValue());
target.setProperty(PDFOutputTarget.SECURITY_OWNERPASSWORD, getOwnerPassword());
target.setProperty(PDFOutputTarget.SECURITY_USERPASSWORD, getUserPassword());
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_ASSEMBLY,
String.valueOf(isAllowAssembly()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_COPY,
String.valueOf(isAllowCopy()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_DEGRADED_PRINTING,
String.valueOf(isAllowDegradedPrinting()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_FILLIN,
String.valueOf(isAllowFillIn()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_MODIFY_ANNOTATIONS,
String.valueOf(isAllowModifyAnnotations()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_MODIFY_CONTENTS,
String.valueOf(isAllowModifyContents()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_PRINTING,
String.valueOf(isAllowPrinting()));
target.setProperty(PDFOutputTarget.SECURITY_ALLOW_SCREENREADERS,
String.valueOf(isAllowScreenreaders()));
target.setProperty(PDFOutputTarget.ENCODING, getEncoding());
target.open();
final PageableReportProcessor proc = new PageableReportProcessor(report);
proc.setOutputTarget(target);
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);
}
/**
* Stores the settings from the dialog in the report configuration.
*
* @param config the report configuration.
*/
public void storeToConfiguration (final ReportConfiguration config)
{
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.AUTHOR,
getAuthor());
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.TITLE,
getPDFTitle());
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ENCRYPTION,
getEncryptionValue());
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_OWNERPASSWORD,
getOwnerPassword());
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_USERPASSWORD,
getUserPassword());
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_ASSEMBLY,
String.valueOf(isAllowAssembly()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_COPY,
String.valueOf(isAllowCopy()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_DEGRADED_PRINTING,
String.valueOf(isAllowDegradedPrinting()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_FILLIN,
String.valueOf(isAllowFillIn()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_MODIFY_ANNOTATIONS,
String.valueOf(isAllowModifyAnnotations()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_MODIFY_CONTENTS,
String.valueOf(isAllowModifyContents()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_PRINTING,
String.valueOf(isAllowPrinting()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.SECURITY_ALLOW_SCREENREADERS,
String.valueOf(isAllowScreenreaders()));
config.setConfigProperty
(PDFOutputTarget.CONFIGURATION_PREFIX + PDFOutputTarget.ENCODING,
getEncoding());
}
/**
* Initialises the PDF save dialog from the settings in the report configuration.
*
* @param config the report configuration.
*/
public void initFromConfiguration(final ReportConfiguration config)
{
setAllowAssembly(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_ASSEMBLY, config,
isAllowAssembly()));
setAllowCopy(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_COPY, config, isAllowCopy()));
setAllowFillIn(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_FILLIN, config, isAllowFillIn()));
setAllowModifyAnnotations(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_MODIFY_ANNOTATIONS,
config, isAllowModifyAnnotations()));
setAllowModifyContents(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_MODIFY_CONTENTS, config,
isAllowModifyContents()));
setAllowScreenreaders(parseBoolean(PDFOutputTarget.SECURITY_ALLOW_SCREENREADERS, config,
isAllowScreenreaders()));
final boolean printing = parseBoolean(PDFOutputTarget.SECURITY_ALLOW_PRINTING, config,
isAllowPrinting());
final boolean degraded = parseBoolean(PDFOutputTarget.SECURITY_ALLOW_DEGRADED_PRINTING, config,
isAllowDegradedPrinting());
setPrintLevel(printing, degraded);
setEncryptionValue(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
+ PDFOutputTarget.SECURITY_ENCRYPTION, getEncryptionValue()));
setAuthor(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
+ PDFOutputTarget.AUTHOR, getAuthor()));
setUserPassword(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
+ PDFOutputTarget.SECURITY_USERPASSWORD, getUserPassword()));
setOwnerPassword(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
+ PDFOutputTarget.SECURITY_OWNERPASSWORD, getOwnerPassword()));
encodingModel.ensureEncodingAvailable(config.getPdfTargetEncoding());
setEncoding(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX
+ PDFOutputTarget.ENCODING, getEncoding()));
setPDFTitle(config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX +
PDFOutputTarget.TITLE, getPDFTitle()));
}
/**
* Parses a boolean property from a report configuration.
*
* @param key the property key.
* @param config the report configuration.
* @param orgVal the default value.
*
* @return true or false.
*/
private boolean parseBoolean(final String key, final ReportConfiguration config, final boolean orgVal)
{
final String val = config.getConfigProperty(PDFOutputTarget.CONFIGURATION_PREFIX + key,
String.valueOf(orgVal));
return (val.equalsIgnoreCase("true"));
}
/**
* For debugging.
*
* @param args ignored.
*/
public static void main(final String[] args)
{
final JDialog d = new PDFSaveDialog();
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -