View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client;
22  
23  import java.awt.Cursor;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.KeyEvent;
26  import java.io.File;
27  
28  import javax.swing.JFrame;
29  import javax.swing.JOptionPane;
30  
31  import no.sintef.file.IOUtils;
32  
33  import org.jdesktop.jdic.desktop.Desktop;
34  
35  import coras.riskanalysis.RiskAnalysisProject;
36  
37  /***
38   * @author fvr
39   *
40   * TODO To change the template for this generated type comment go to
41   * Window - Preferences - Java - Code Style - Code Templates
42   */
43  public class GenerateReportAction extends CorasAction {
44  
45      public GenerateReportAction(CorasClient client) {
46          super(client, "Generate Report", KeyEvent.VK_R, "Generate Report", "Generate Risk Analysis Report");
47      }
48  
49      public void actionPerformed(ActionEvent e) {
50          final JFrame mainFrame = getClient().getMainFrame();
51  		mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
52          new SwingWorker() {
53              public Object construct() {
54                  try {
55                  	RiskAnalysisProject project = getClient().getProject();
56                      return getClient().getCorasServices().generateReport(project.getId());
57                  } catch (Exception e) {
58                      System.err.println("Error while generating report: " + e);
59                      return null;
60                  }
61              }
62              public void finished() {
63                  final byte[] bytes = (byte[]) get();
64                  mainFrame.setCursor(Cursor.getDefaultCursor());
65                  if (bytes == null) {
66                      JOptionPane.showMessageDialog(mainFrame, "Error while generating report", "Error", JOptionPane.ERROR_MESSAGE);
67                      return;
68                  }
69                  final File reportFile = IOUtils.showSaveDialog("report.rtf", mainFrame);
70                  if (reportFile != null) {
71                      new SwingWorker() {
72                          public Object construct() {
73                              try {
74                                  IOUtils.saveFile(bytes, reportFile);
75                                  Desktop.open(reportFile);
76                              } catch (Exception e) {
77                                  System.err.println("Error saving report: " + e);
78                              }
79                              return null;
80                          }
81                      }.start();
82                  }
83              }
84          }.start();
85      }
86  
87  }