001: package org.netbeans.modules.reportgenerator.api;
002:
003: import org.netbeans.modules.reportgenerator.generator.ReportGeneratorFactory;
004: import org.netbeans.modules.reportgenerator.spi.ReportCookie;
005: import java.awt.event.ActionEvent;
006: import java.io.File;
007:
008: import javax.swing.AbstractAction;
009: import javax.swing.Icon;
010: import javax.swing.ImageIcon;
011: import javax.swing.KeyStroke;
012: import org.netbeans.modules.reportgenerator.generator.DataObjectSettings;
013: import org.openide.ErrorManager;
014: import org.openide.filesystems.FileUtil;
015: import org.openide.loaders.DataObject;
016:
017: import org.openide.util.NbBundle;
018: import org.openide.util.Utilities;
019:
020: /**
021: * Action which provides report generation facility.
022: * An editor can add this action to its tool bar to get report
023: * generation functionality.
024: *
025: * @author radval
026: *
027: */
028: public class GenerateReportAction extends AbstractAction {
029:
030: public static final String ACCELERATOR = "alt shift F10"; // NOI18N
031:
032: private static final Icon icon = new ImageIcon(
033: Utilities
034: .loadImage("org/netbeans/modules/reportgenerator/api/impl/resources/images/generateReport.png"));
035:
036: private static final String label = NbBundle.getMessage(
037: GenerateReportAction.class, "NAME_Generate_Report");
038:
039: private ReportGenerator mGenerator;
040: private DataObject mDataObject;
041: private ReportCookie myCookie;
042: private File mReportFile;
043:
044: /**
045: * Pass data object which should have ReportCookie added to its CookieSet.
046: * The report will be generated in the same directory as this DataObject.
047: * The name of the report file will be same as this DataObject but with
048: * different file extension like pdf, html etc
049: * @param dataObject
050: * @param reportFile
051: */
052: public GenerateReportAction(DataObject dataObject,
053: ReportCookie cookie) {
054: super (label, icon);
055: putValue(NAME, label);
056: putValue(SHORT_DESCRIPTION, label);
057: putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ACCELERATOR));
058: this .mDataObject = dataObject;
059: myCookie = cookie;
060:
061: //pdf file
062: String iepFilePath = FileUtil.toFile(
063: dataObject.getPrimaryFile()).getAbsolutePath();
064: int k = iepFilePath.lastIndexOf("."); // NOI18N
065: String pdfFilePath = iepFilePath;
066:
067: if (k != -1) {
068: pdfFilePath = iepFilePath.substring(0, k);
069: }
070: File pdfFile = new File(pdfFilePath + ".pdf");
071:
072: this .mReportFile = pdfFile;
073:
074: ReportCustomizationOptions option = DataObjectSettings
075: .getOrStoreOptions(dataObject);
076: try {
077: this .mGenerator = ReportGeneratorFactory.getDefault()
078: .newReportGenerator(ReportType.REPORT_PDF,
079: this .mReportFile, option);
080: } catch (ReportException ex) {
081: ErrorManager.getDefault().notify(ex);
082: }
083: }
084:
085: public GenerateReportAction(DataObject dataObject) {
086: this (dataObject, null);
087: }
088:
089: public void actionPerformed(ActionEvent e) {
090: try {
091: ReportCookie rCookie = myCookie;
092:
093: if (myCookie == null) {
094: rCookie = mDataObject.getCookie(ReportCookie.class);
095: } else {
096: rCookie = myCookie;
097: }
098: if (rCookie != null) {
099: Report report = rCookie.generateReport();
100:
101: if (report != null) {
102: mGenerator.generateReport(report);
103: }
104: }
105: } catch (Exception ex) {
106: ErrorManager.getDefault().notify(ex);
107: }
108: }
109: }
|