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  
27  import javax.swing.AbstractAction;
28  import javax.swing.Icon;
29  import javax.swing.JOptionPane;
30  import javax.swing.SwingUtilities;
31  
32  import no.sintef.lock.AlreadyLockedException;
33  import no.sintef.lock.LockException;
34  import no.sintef.lock.LockedByOtherUserException;
35  
36  import org.w3c.dom.Node;
37  
38  import coras.client.ui.CorasFrame;
39  import coras.client.ui.NewElementDialog;
40  import coras.representations.RepresentationFactory;
41  import coras.representations.RepresentationTypeEnum;
42  import coras.representations.TableRepresentation;
43  import coras.riskanalysis.RiskAnalysisProject;
44  import coras.riskanalysis.RiskAnalysisResult;
45  import coras.structure.ConcernEnum;
46  import coras.structure.SubProcessEnum;
47  import coras.structure.ViewpointEnum;
48  import coras.table.RiskMatrixGenerator;
49  import coras.types.ElementTypeEnum;
50  import coras.types.ITable;
51  import coras.types.SubtypeEnum;
52  
53  /***
54   * @author fvr
55   *
56   * TODO To change the template for this generated type comment go to
57   * Window - Preferences - Java - Code Style - Code Templates
58   */
59  public class NewResultAction extends CorasAction {
60  
61      private ElementTypeEnum type = null;
62      
63      public NewResultAction(CorasClient client, ElementTypeEnum type, Icon icon) {
64          super(client, "Result", KeyEvent.VK_R, "New Result", "Create new risk analysis result");
65          setType(type);
66          putValue(AbstractAction.SMALL_ICON, icon);
67      }
68      
69      public void actionPerformed(ActionEvent e) {
70      	final RiskAnalysisProject project = getClient().getProject();
71      	if (project == null) {
72      	    // FIXME user feedback
73      	    return;
74      	}
75      	
76      	final CorasFrame mainFrame = getClient().getMainFrame();
77      	mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
78      	new SwingWorker() {
79              public Object construct() {
80      	        try {
81                      project.checkOut();
82      
83                      SwingUtilities.invokeAndWait(new Runnable() {
84                          public void run() {
85                              mainFrame.setCursor(Cursor.getDefaultCursor());
86                          }
87                      });
88                      
89                      Object selectedProjectItem = getClient().getSelectedProjectItem();
90                      SubProcessEnum subprocessFilter = selectedProjectItem instanceof SubProcessEnum ? (SubProcessEnum) selectedProjectItem : null;
91                      
92                      NewElementDialog dialog = new NewElementDialog(mainFrame);
93                      dialog.setTitle("New " + 
94                      		(type == ElementTypeEnum.UML_MODEL 
95                      				? RepresentationTypeEnum.DGM_MODEL.toString() 
96                      						: type.toString()));
97                      dialog.setCategoryEnabled(false);
98                      dialog.setDomainEnabled(false);
99                      dialog.setAllowMultipleViewpoints(false);
100                     dialog.setType(type, subprocessFilter, RepresentationTypeEnum.DGM_MODEL);
101                     dialog.setLocationRelativeTo(mainFrame);
102                     dialog.setVisible(true);
103                     
104                     if (dialog.getResult() != JOptionPane.OK_OPTION) {
105                         return new Boolean(false);
106                     }
107         				
108                     SwingUtilities.invokeAndWait(new Runnable() {
109                         public void run() {
110                             mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
111                             mainFrame.showProjectPanel();
112                         }
113                     });
114                     String name = dialog.getElementName();
115                     String shortDescription = dialog.getShortDescription();
116                     String fullDescription = dialog.getFullDescription();
117                     ConcernEnum concern = dialog.getConcern();
118                     ViewpointEnum viewpoint = dialog.getViewpoint();
119                     ElementTypeEnum type = dialog.getType();
120                     SubtypeEnum subtype = dialog.getSubtype();
121                     RiskAnalysisResult raResult = null;
122                     
123                     if (type == ElementTypeEnum.UML_MODEL) {
124                         raResult = project.createUMLModel(subtype, name, shortDescription, fullDescription, concern, viewpoint, null, null);
125                     } else if (type == ElementTypeEnum.TABLE) {
126                     	if (subtype == SubtypeEnum.RISK_MATRIX) {
127                     		ITable valueDefinitionsTable = 
128                     			(ITable) project.getResult(ElementTypeEnum.TABLE, SubtypeEnum.VALUE_DEFINITION_TABLE);
129                     		if (valueDefinitionsTable == null) {
130                     			throw new Exception("Need to define frequency and consequence values in Value Definition table before creating Risk Matrix.");
131                     		}
132                     		Node n = RiskMatrixGenerator.generateRiskMatrix(valueDefinitionsTable);
133                     		TableRepresentation riskMatrix = RepresentationFactory.createTable(n, "Risk_Matrix.xml"); 
134                     		raResult = project.createTable(subtype, name, shortDescription, fullDescription, concern, viewpoint, riskMatrix);
135                     	} else {
136                     		raResult = project.createTable(subtype, name, shortDescription, fullDescription, concern, viewpoint, null);
137                     	}
138                     } else {
139                         // FIXME error
140                     }
141     
142                     return raResult;
143                 } catch (Exception e) {
144                     return e;
145                 }
146             }
147             public void finished() {
148                 mainFrame.setCursor(Cursor.getDefaultCursor());
149                 Object o = get();
150                 if (o instanceof RiskAnalysisResult) {
151                 	getClient().openProjectItem((RiskAnalysisResult)o);
152                 } else if (o instanceof LockedByOtherUserException) {
153                     LockedByOtherUserException e = (LockedByOtherUserException) o;
154                     JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create risk analysis result.</P><P>The project 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);
155                 } else if (o instanceof AlreadyLockedException) {
156                     JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create risk analysis result.</P><P>The project details are being edited.</P></HTML>", "Unable to create risk analysis result", JOptionPane.WARNING_MESSAGE);
157                 } else if (o instanceof Boolean || o instanceof Exception) {
158                     new Thread() {
159                         public void run() {
160                             try {
161                                 project.unCheckOut();
162                             } catch (LockException e) {
163                                 e.printStackTrace();
164                             }                            
165                         }
166                     }.start();
167                     if (o instanceof Exception) {
168                         Exception e = (Exception) o;
169                         e.printStackTrace();
170                         JOptionPane.showMessageDialog(mainFrame, "<HTML><P>Unable to create risk analysis result.</P><P>Error message:</P><P>" + e.getMessage() + "</P></HTML>", "Unable to create risk analysis result", JOptionPane.ERROR_MESSAGE);
171                     }
172                 } else {
173                     System.err.println("Unexpected result: " + o);
174                 }
175             }
176     	}.start();
177     }
178 
179     private void setType(ElementTypeEnum newType) {
180     	type = newType;
181     	String name;
182     	int key;
183     	if (type == null) {
184     		name = "Result";
185     		key = 'r';
186     	} else if (type == ElementTypeEnum.UML_MODEL) {
187     		name = "CORAS UML Model";
188     		key = 'u';
189     	} else {
190     		name = type.toString();
191     		key = name.charAt(0);
192     	}
193     	putValue(NAME, name);
194     	putValue(MNEMONIC_KEY, new Integer(key));
195     	putValue(SHORT_DESCRIPTION, "New " + name);
196     	putValue(LONG_DESCRIPTION, "Create new risk analysis " + name);
197     }
198     
199 }
200