reportconvertergui.java
来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 637 行 · 第 1/2 页
JAVA
637 行
contentPane.add(selectSourceButton, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(3, 1, 1, 1);
contentPane.add(new JLabel(getResources().getString("convertdialog.targetFile")), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(3, 1, 1, 1);
gbc.ipadx = 120;
contentPane.add(targetField, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(3, 1, 1, 1);
gbc.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(selectTargetButton, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(3, 1, 1, 1);
contentPane.add(new JLabel(getResources().getString("convertdialog.encoding")), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(3, 1, 1, 1);
gbc.ipadx = 120;
contentPane.add(new JComboBox(encodingModel), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(3, 1, 1, 1);
gbc.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(convertFilesButton, gbc);
return contentPane;
}
/**
* Starting point for the utility application.
*
* @param args ignored.
*/
public static void main(final String[] args)
{
final ReportConverterGUI gui = new ReportConverterGUI();
gui.addWindowListener(new WindowAdapter()
{
public void windowClosing(final WindowEvent e)
{
System.exit(0);
}
});
gui.pack();
gui.setVisible(true);
}
/**
* Validates the contents of the dialogs input fields. If the selected file exists, it is also
* checked for validity.
*
* @param filename the file name.
*
* @return true, if the input is valid, false otherwise
*/
public boolean performTargetValidate(final String filename)
{
if (filename.trim().length() == 0)
{
JOptionPane.showMessageDialog(this,
getResources().getString("convertdialog.targetIsEmpty"),
getResources().getString("convertdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final File f = new File(filename);
if (f.exists())
{
if (f.isFile() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("convertdialog.targetIsNoFile"),
getResources().getString("convertdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
if (f.canWrite() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("convertdialog.targetIsNotWritable"),
getResources().getString("convertdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final String key1 = "convertdialog.targetOverwriteConfirmation";
final String key2 = "convertdialog.targetOverwriteTitle";
if (JOptionPane.showConfirmDialog(this,
MessageFormat.format(getResources().getString(key1),
new Object[]{filename}
),
getResources().getString(key2),
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
== JOptionPane.NO_OPTION)
{
return false;
}
}
return true;
}
/**
* Validates the contents of the dialogs input fields. If the selected file exists, it is also
* checked for validity.
*
* @param filename the file name.
*
* @return true, if the input is valid, false otherwise
*/
public boolean performSourceValidate(final String filename)
{
if (filename.trim().length() == 0)
{
JOptionPane.showMessageDialog(this,
getResources().getString("convertdialog.sourceIsEmpty"),
getResources().getString("convertdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
final File f = new File(filename);
if (f.exists() == false)
{
return false;
}
if (f.isFile() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("convertdialog.sourceIsNoFile"),
getResources().getString("convertdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
if (f.canRead() == false)
{
JOptionPane.showMessageDialog(this,
getResources().getString("convertdialog.sourceIsNotReadable"),
getResources().getString("convertdialog.errorTitle"),
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
/**
* Retrieves the resources for the frame. If the resources are not initialized,
* they get loaded on the first call to this method.
*
* @return The resources.
*/
protected ResourceBundle getResources()
{
if (resources == null)
{
resources = ResourceBundle.getBundle(BASE_RESOURCE_CLASS);
}
return resources;
}
/**
* Sets the source file.
*
* @param file the file name.
*/
protected void setSourceFile(final String file)
{
sourceField.setText(file);
}
/**
* Sets the target file.
*
* @param file the file name.
*/
protected void setTargetFile(final String file)
{
targetField.setText(file);
}
/**
* Returns the source file name.
*
* @return The name.
*/
protected String getSourceFile()
{
return sourceField.getText();
}
/**
* Returns the name of the target file.
*
* @return The name.
*/
protected String getTargetFile()
{
return targetField.getText();
}
/**
* Performs the conversion, returning <code>true</code> if the conversion is successful, and
* <code>false</code> otherwise.
*
* @return A boolean.
*/
public boolean convert()
{
if (performSourceValidate(getSourceFile()) == false)
{
setStatusText("Validating the source file failed. Please check your inputs.");
return false;
}
if (performTargetValidate(getTargetFile()) == false)
{
setStatusText("Validating the target file failed. Please check your inputs.");
return false;
}
try
{
final ConverterParserFrontend frontend = new ConverterParserFrontend();
final File sourceFile = new File (getSourceFile());
final File targetFile = new File (getTargetFile());
final String encoding = encodingModel.getSelectedEncoding();
final JFreeReport report = (JFreeReport)
frontend.parse(sourceFile.toURL(), sourceFile.toURL());
final DefaultConfiguration config = new DefaultConfiguration();
config.setProperty(Parser.CONTENTBASE_KEY, targetFile.toURL().toExternalForm());
// adding all factories will make sure that all stylekeys are found,
// even if the report was parsed from a simple report definition
final ReportWriter writer = new ReportWriter(report, encoding, config);
writer.addClassFactoryFactory(new URLClassFactory());
writer.addClassFactoryFactory(new DefaultClassFactory());
writer.addClassFactoryFactory(new BandLayoutClassFactory());
writer.addClassFactoryFactory(new ArrayClassFactory());
writer.addStyleKeyFactory(new DefaultStyleKeyFactory());
writer.addStyleKeyFactory(new PageableLayoutStyleKeyFactory());
writer.addTemplateCollection(new DefaultTemplateCollection());
writer.addElementFactory(new DefaultElementFactory());
writer.addDataSourceFactory(new DefaultDataSourceFactory());
final OutputStream base = new FileOutputStream(targetFile);
final Writer w = new BufferedWriter(new OutputStreamWriter(base, encoding));
writer.write(w);
w.close();
setStatusText("Conversion done.");
return true;
}
catch (Exception e)
{
Log.warn ("Failed to convert the report. ", e);
setStatusText("Failed to convert the report:" + e.getMessage());
return false;
}
}
/**
* Selects a file to use.
*
* @param filename the current selection.
* @param appendExt append an extension?
*
* @return The file name.
*/
protected String performSelectFile(final String filename, final boolean appendExt)
{
final File file = new File(filename);
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();
if (appendExt)
{
if ((StringUtil.endsWithIgnoreCase(selFileName, ".xml") == false))
{
selFileName = selFileName + ".xml";
}
}
return selFileName;
}
return filename;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?