📄 datapanel.java
字号:
private void onDBConnections ()
{
final DBConnectionsDialog dialog =
new DBConnectionsDialog(reportDefinitionManager.getDatabaseManager());
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);
}
private void onReportQuery ()
{
final DatabaseManager databaseManager =
reportDefinitionManager.getDatabaseManager();
final QueryDialog dialog = new QueryDialog(databaseManager);
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);
if (databaseManager.getLastQueryResult() != null)
{
reportDefinitionManager.setReportData(databaseManager.getLastQueryResult());
reportDataTable.setModel(reportDefinitionManager.getReportData());
}
}
private void onLoadQueryResult ()
{
final int returnVal = fileChooser.showOpenDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION)
{
return;
}
final File file = fileChooser.getSelectedFile();
// read the file
try
{
final FileInputStream in = new FileInputStream(file);
final ObjectInputStream s = new ObjectInputStream(in);
reportDefinitionManager.setReportData(loadTableModel(s));
s.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(this,
"Cannot find the specifed File.",
"File not found",
JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this,
"Could not load file",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
}
}
private void onSaveQueryResult ()
{
if (reportDefinitionManager.getReportData() == null)
{
JOptionPane.showMessageDialog(this,
"Please execute a query or load a previous result first",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
return;
}
final int returnVal = fileChooser.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION)
{
return;
}
final File file = fileChooser.getSelectedFile();
// now write the file
try
{
final FileOutputStream out;
out = new FileOutputStream(file);
final ObjectOutputStream s = new ObjectOutputStream(out);
saveTableModel(reportDefinitionManager.getReportData(), s);
s.flush();
s.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(this,
"Could not save file",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this,
"Could not save file",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
}
}
private void onPreviewReport ()
{
if (reportDefinitionManager.getReportData() == null)
{
JOptionPane.showMessageDialog(this,
"Please execute a query or load a previous result first",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (reportDefinitionManager.hasReportDefinitionError())
{
JOptionPane.showMessageDialog(this,
"Please correct the error in the report definition first",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
return;
}
final JFreeReport report = reportDefinitionManager.getReport();
if (report == null)
{
JOptionPane.showMessageDialog(this,
"Please load or create a report definition first",
"Sorry",
JOptionPane.INFORMATION_MESSAGE);
return;
}
// look for named properties and fill them with dummy data
final ReportProperties reportProperties = report.getProperties();
final Iterator enum = reportProperties.keys();
while (enum.hasNext())
{
final String name = (String) enum.next();
if (reportProperties.isMarked(name))
{
// dummy data: fill it with its name
// hey, this failes if the property is an image type !
System.out.println("Property " + name + " is marked");
report.setProperty(name, name);
}
}
try
{
final PreviewDialog frame1 = new PreviewDialog(report);
frame1.pack();
frame1.setModal(true);
RefineryUtilities.positionFrameRandomly(frame1);
frame1.setVisible(true);
frame1.close();
frame1.dispose();
}
catch (ReportProcessingException e)
{
Log.error("Failed to process the report", e);
}
}
private void saveTableModel (final TableModel model, final ObjectOutputStream os)
throws IOException
{
os.writeUTF("ReportEditorTableModel");
os.writeInt(model.getColumnCount());
for (int i = 0; i < model.getColumnCount(); i++)
{
os.writeObject(model.getColumnName(i));
os.writeObject(model.getColumnClass(i));
}
os.writeInt(model.getRowCount());
for (int row = 0; row < model.getRowCount(); row++)
{
for (int col = 0; col < model.getColumnCount(); col++)
{
final Object o = model.getValueAt(row, col);
os.writeObject(o);
}
}
}
private TableModel loadTableModel (final ObjectInputStream is)
throws IOException
{
try
{
final String magic = is.readUTF();
if (magic.equals("ReportEditorTableModel") == false)
{
throw new IOException("This stream is no serialized editor table stream.");
}
final int colCount = is.readInt();
final String[] names = new String[colCount];
final Class[] types = new Class[colCount];
for (int i = 0; i < colCount; i++)
{
names[i] = (String) is.readObject();
types[i] = (Class) is.readObject();
}
final int rowCount = is.readInt();
final Object[][] data = new Object[rowCount][colCount];
for (int row = 0; row < rowCount; row++)
{
for (int col = 0; col < colCount; col++)
{
data[row][col] = is.readObject();
}
}
return new SerializedTableModel(data, names, types);
}
catch (ClassNotFoundException cnfe)
{
throw new IOException("Class not found during deserialization" + cnfe.getMessage());
}
}
public final JComponent getEditorComponent ()
{
return this;
}
public final JMenu[] getEditorMenus ()
{
return editorMenus;
}
public final boolean isActive ()
{
return active;
}
public final void setActive (final boolean active)
{
Log.debug("old Active: " + this.active + " -> " + active);
final boolean oldActive = this.active;
this.active = active;
if (active != oldActive)
{
if (active == true)
{
onActivate();
}
else
{
onDeactivate();
}
}
}
protected void onActivate ()
{
}
protected void onDeactivate ()
{
}
public final String getEditorName ()
{
return "Data";
}
public final JToolBar getToolbar ()
{
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -