📄 reportdefinitionmanager.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner (taquera@sherito.org);
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* ------------------------------
* DocumentHandler.java
* ------------------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: ReportDefinitionManager.java,v 1.3 2004/04/20 18:54:41 taqua Exp $
*
* Changes
* -------------------------
* 27.09.2003 : Initial version
*
*/
package org.jfree.designer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.Properties;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jfree.designer.db.DatabaseManager;
import org.jfree.designer.util.DowngradeActionMap;
import org.jfree.report.JFreeReport;
import org.jfree.report.modules.misc.configstore.base.ConfigFactory;
import org.jfree.report.modules.misc.configstore.base.ConfigStorage;
import org.jfree.report.modules.misc.configstore.base.ConfigStoreException;
import org.jfree.report.modules.parser.base.ReportGenerator;
import org.jfree.report.modules.parser.ext.factory.datasource.DefaultDataSourceFactory;
import org.jfree.report.modules.parser.ext.factory.elements.DefaultElementFactory;
import org.jfree.report.modules.parser.ext.factory.objects.BandLayoutClassFactory;
import org.jfree.report.modules.parser.ext.factory.objects.DefaultClassFactory;
import org.jfree.report.modules.parser.ext.factory.stylekey.DefaultStyleKeyFactory;
import org.jfree.report.modules.parser.ext.factory.stylekey.PageableLayoutStyleKeyFactory;
import org.jfree.report.modules.parser.ext.factory.templates.DefaultTemplateCollection;
import org.jfree.report.modules.parser.extwriter.ReportWriter;
import org.jfree.report.util.Log;
import org.jfree.report.util.ReportConfiguration;
import org.jfree.util.DefaultConfiguration;
import org.jfree.xml.Parser;
import org.jfree.xml.factory.objects.ArrayClassFactory;
import org.jfree.xml.factory.objects.URLClassFactory;
import org.xml.sax.InputSource;
/**
* Communication and concurrency component...
*/
public final class ReportDefinitionManager
{
public static final String REPORT_PROPERTY = "report";
public static final String REPORT_DATA_PROPERTY = "reportData";
public static final String REPORT_DEFINITION_ERROR_PROPERTY = "reportDefinitionError";
public static final String REPORT_DEFINITION_TEXT_PROPERTY = "reportDefinitionText";
private final DefaultConfiguration editorConfiguration;
private final PropertyChangeSupport propertyChangeSupport;
private final DatabaseManager databaseManager;
private JFreeReport report;
private String reportDefinitionText;
private TableModel reportData;
private Exception reportDefinitionError;
private URL reportSource;
private final DowngradeActionMap globalActions;
public static final String CONFIG_NAME = "designersettings";
public ReportDefinitionManager (final URL defaultSource,
final DowngradeActionMap globalActions)
{
this.editorConfiguration = new DefaultConfiguration();
this.globalActions = globalActions;
this.propertyChangeSupport = new PropertyChangeSupport(this);
this.databaseManager = new DatabaseManager();
setReportSource(defaultSource);
loadConfiguration();
}
public DowngradeActionMap getGlobalActions ()
{
return globalActions;
}
private void loadConfiguration ()
{
try
{
final ConfigStorage storage = ConfigFactory.getInstance().getUserStorage();
if (storage.existsProperties(CONFIG_NAME))
{
final Properties props = storage.loadProperties(CONFIG_NAME, null);
this.editorConfiguration.putAll(props);
}
}
catch (ConfigStoreException ce)
{
Log.debug("Unable to load default configuration.");
}
}
public void storeConfiguration ()
{
try
{
final ConfigStorage storage = ConfigFactory.getInstance().getUserStorage();
storage.storeProperties(CONFIG_NAME, editorConfiguration);
}
catch (ConfigStoreException ce)
{
Log.debug("Unable to load default configuration.");
}
}
public DatabaseManager getDatabaseManager ()
{
return databaseManager;
}
public JFreeReport getReport ()
{
return report;
}
/**
* Throws oldreport -> null change event and then a null -> report event.
*
* @param report
*/
public void setReport (final JFreeReport report)
{
propertyChangeSupport.firePropertyChange("report", this.report, null);
// as the report definition does not detect internal changes in
// the bands or properties, we force the throwing of the report event to
// the hard way
this.report = report;
if (report != null)
{
internalSetReportDefinitionText(convertReportToText(report));
if (reportData != null)
{
// synchronize with current data ..
this.report.setData(reportData);
}
else
{
// set empty tablemodel to get rid of any possibly set data in the report
this.report.setData(new DefaultTableModel());
}
}
else
{
internalSetReportDefinitionText(null);
}
propertyChangeSupport.firePropertyChange("report", null, this.report);
}
public TableModel getReportData ()
{
return reportData;
}
public void setReportData (final TableModel reportData)
{
final TableModel oldValue = this.reportData;
this.reportData = reportData;
if (this.report != null)
{
if (this.reportData != null)
{
this.report.setData(reportData);
}
else
{
this.report.setData(new DefaultTableModel());
}
}
propertyChangeSupport.firePropertyChange(REPORT_DATA_PROPERTY, oldValue, reportData);
}
public boolean hasReportDefinitionError ()
{
return reportDefinitionError != null;
}
public Exception getReportDefinitionError ()
{
return reportDefinitionError;
}
private void setReportDefinitionError (final Exception reportDefinitionError)
{
final Exception oldValue = this.reportDefinitionError;
this.reportDefinitionError = reportDefinitionError;
propertyChangeSupport.firePropertyChange
(REPORT_DEFINITION_ERROR_PROPERTY, oldValue, reportDefinitionError);
}
public String getReportDefinitionText ()
{
return reportDefinitionText;
}
public void setReportDefinitionText (final String reportDefinitionText)
{
if (reportDefinitionText != null)
{
setReport(parseReport(reportDefinitionText));
}
else
{
setReport(null);
}
internalSetReportDefinitionText(reportDefinitionText);
}
private void internalSetReportDefinitionText (final String reportDefinitionText)
{
final String oldValue = this.reportDefinitionText;
this.reportDefinitionText = reportDefinitionText;
propertyChangeSupport.firePropertyChange
(REPORT_DEFINITION_TEXT_PROPERTY, oldValue, reportDefinitionText);
Log.debug("ReportDefinitionText: " + reportDefinitionText);
}
public URL getReportSource ()
{
return reportSource;
}
public void setReportSource (final URL reportSource)
{
if (reportSource == null)
{
throw new NullPointerException();
}
this.reportSource = reportSource;
}
public void addPropertyChangeListener (final PropertyChangeListener l)
{
propertyChangeSupport.addPropertyChangeListener(l);
}
public void removePropertyChangeListener (final PropertyChangeListener l)
{
propertyChangeSupport.removePropertyChangeListener(l);
}
private String convertReportToText (final JFreeReport report)
{
final StringWriter swriter = new StringWriter();
final URL contentBase = getReportSource();
if (contentBase == null)
{
throw new NullPointerException("ContentBase is null - implementation invaid!");
}
final ReportConfiguration config = new ReportConfiguration(report.getReportConfiguration());
config.setConfigProperty(Parser.CONTENTBASE_KEY, contentBase.toExternalForm());
final ReportWriter writer = new ReportWriter(report, "UTF-8", 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());
try
{
writer.write(swriter);
return swriter.toString();
}
catch (Exception e)
{
setReportDefinitionError(e);
Log.warn("Unable to convert report to text: ", e);
return null;
}
}
private JFreeReport parseReport (final String text)
{
final InputSource in = new InputSource(new StringReader(text));
try
{
return ReportGenerator.getInstance().parseReport
(in, getReportSource());
}
catch (Exception e)
{
Log.debug("Failed to parse the report", e);
setReportDefinitionError(e);
}
return null;
}
public DefaultConfiguration getEditorConfiguration ()
{
return editorConfiguration;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -