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.Frame;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.KeyEvent;
27  
28  import javax.swing.JOptionPane;
29  
30  import no.sintef.lock.AlreadyLockedException;
31  import no.sintef.lock.LockException;
32  import no.sintef.lock.LockedByOtherUserException;
33  import coras.client.ui.CorasFrame;
34  import coras.reuse.Experience;
35  import coras.reuse.ExperiencePackage;
36  import coras.riskanalysis.DocumentResult;
37  import coras.riskanalysis.FaultTreeResult;
38  import coras.riskanalysis.RiskAnalysisResult;
39  import coras.riskanalysis.UMLModelResult;
40  import coras.types.ITable;
41  
42  /***
43   * @author fvr
44   *
45   * TODO To change the template for this generated type comment go to
46   * Window - Preferences - Java - Code Style - Code Templates
47   */
48  public class ExportResultAsExperienceAction extends CorasAction {
49  
50      public ExportResultAsExperienceAction(CorasClient client) {
51          super(client, "Export to current Experience Package", KeyEvent.VK_G, "Export as Experience to current Experience Package", "Export as Experience to current Experience Package");
52      }
53  
54      public void actionPerformed(ActionEvent e) {
55          final RiskAnalysisResult result = getClient().getSelectedResult();
56          if (result == null || result instanceof ITable) {
57              // FIXME user feedback - need to select result
58              return;
59          }
60          
61          final ExperiencePackage thePackage = getClient().getExperiencePackage();
62          final CorasFrame mainFrame = getClient().getMainFrame();
63          if (thePackage == null) {
64              JOptionPane.showMessageDialog(mainFrame, "Please create or open an Experience Package before creating a new Experience", "Cannot create Experience", JOptionPane.INFORMATION_MESSAGE);
65              return;
66          }
67      
68          mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
69          mainFrame.showLibraryPanel();
70          new SwingWorker() {
71              public Object construct() {
72                  try {
73                      thePackage.checkOut();
74                      Experience experience = null;
75                      if (result instanceof UMLModelResult) {
76                          experience = thePackage.createUMLModel((UMLModelResult) result);
77                      } else if (result instanceof FaultTreeResult) {
78                          experience = thePackage.createFaultTree((FaultTreeResult) result);
79                      } else if (result instanceof DocumentResult) {
80                          experience = thePackage.createDocument((DocumentResult) result);
81                      }
82                      return experience;
83                  } catch (Exception e) {
84                      return e;
85                  }
86              }
87              public void finished() {
88      		    mainFrame.setCursor(Cursor.getDefaultCursor());
89                  Object o = get();
90                  if (o instanceof Experience) {
91                  } else if (o instanceof LockedByOtherUserException) {
92                      LockedByOtherUserException e = (LockedByOtherUserException) o;
93      		        JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create experience.</P><P>The experience package is currently being edited by user: " + e.getLockedBy() + "</P><P>Please try again later.</P></HTML>", "Unable to create risk analysis result", JOptionPane.WARNING_MESSAGE);
94                  } else if (o instanceof AlreadyLockedException) {
95      		        JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create experience.</P><P>The experience pakcage details are being edited.</P></HTML>", "Unable to create risk analysis result", JOptionPane.WARNING_MESSAGE);
96                  } else if (o == null || o instanceof Exception ) {
97                      new Thread() {
98                          public void run() {
99                              try {
100                                 thePackage.unCheckOut();
101                             } catch (LockException e) {
102                                 e.printStackTrace();
103                             }                            
104                         }
105                     }.start();
106                     Exception e = (Exception) o;
107                     if (e != null) {
108                         JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create experience.</P><P>Error message:</P><P>" + e.getMessage() + "</P></HTML>", "Unable to create risk analysis result", JOptionPane.ERROR_MESSAGE);
109                     } else {
110                         JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create experience.</P></HTML>", "Unable to create risk analysis result", JOptionPane.ERROR_MESSAGE);
111                     }
112                 } else {
113                     System.err.println("Unexpected result: " + o);
114                 }
115             }
116         }.start();
117     }
118 
119 }