1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 import coras.client.ui.CorasFrame;
36 import coras.client.ui.NewElementDialog;
37 import coras.representations.DiagramRepresentation;
38 import coras.representations.DocumentRepresentation;
39 import coras.representations.FaultTreeModel;
40 import coras.representations.UMLModelRepresentation;
41 import coras.riskanalysis.RiskAnalysisProject;
42 import coras.riskanalysis.RiskAnalysisResult;
43 import coras.structure.ConcernEnum;
44 import coras.structure.SubProcessEnum;
45 import coras.structure.ViewpointEnum;
46 import coras.types.ElementTypeEnum;
47 import coras.types.SubtypeEnum;
48
49 /***
50 * @author fvr
51 *
52 * TODO To change the template for this generated type comment go to
53 * Window - Preferences - Java - Code Style - Code Templates
54 */
55 public class ImportResultFromFileAction extends CorasAction {
56
57 private ElementTypeEnum type = null;
58
59 public ImportResultFromFileAction(CorasClient client, ElementTypeEnum type, Icon icon) {
60 super(client, "Import Result", KeyEvent.VK_R, "Import risk analysis result from file", "Import risk analysis result from file");
61 setType(type);
62 putValue(AbstractAction.SMALL_ICON, icon);
63 }
64
65 public void actionPerformed(ActionEvent e) {
66 final RiskAnalysisProject project = getClient().getProject();
67 if (project == null) {
68
69 return;
70 }
71
72 final CorasFrame mainFrame = getClient().getMainFrame();
73 mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
74 new SwingWorker() {
75 public Object construct() {
76 try {
77 project.checkOut();
78
79 SwingUtilities.invokeAndWait(new Runnable() {
80 public void run() {
81 mainFrame.setCursor(Cursor.getDefaultCursor());
82 }
83 });
84
85 Object selectedProjectItem = getClient().getSelectedProjectItem();
86 SubProcessEnum subprocessFilter = selectedProjectItem instanceof SubProcessEnum ? (SubProcessEnum) selectedProjectItem : null;
87
88 NewElementDialog dialog = new NewElementDialog(mainFrame);
89 dialog.setTitle("Import " + type.toString() + " from file");
90 dialog.setCategoryEnabled(false);
91 dialog.setDomainEnabled(false);
92 dialog.setAllowMultipleViewpoints(false);
93 dialog.setType(type, subprocessFilter, null);
94 dialog.setLocationRelativeTo(mainFrame);
95 dialog.setVisible(true);
96
97 if (dialog.getResult() != JOptionPane.OK_OPTION) {
98 return new Boolean(false);
99 }
100
101 SwingUtilities.invokeAndWait(new Runnable() {
102 public void run() {
103 mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
104 mainFrame.showProjectPanel();
105 }
106 });
107 String name = dialog.getElementName();
108 String shortDescription = dialog.getShortDescription();
109 String fullDescription = dialog.getFullDescription();
110 ConcernEnum concern = dialog.getConcern();
111 ViewpointEnum viewpoint = dialog.getViewpoint();
112 ElementTypeEnum type = dialog.getType();
113 SubtypeEnum subtype = dialog.getSubtype();
114 RiskAnalysisResult raResult = null;
115
116 if (type == ElementTypeEnum.UML_MODEL) {
117 UMLModelRepresentation xmiModel = dialog.getUMLModel();
118 DiagramRepresentation diagram = dialog.getDiagram();
119 raResult = project.createUMLModel(subtype, name, shortDescription, fullDescription, concern, viewpoint, xmiModel, diagram);
120 } else if (type == ElementTypeEnum.FAULT_TREE) {
121 FaultTreeModel faultTreeModel = dialog.getFaultTreeModel();
122 DiagramRepresentation diagram = dialog.getDiagram();
123 raResult = project.createFaultTree(name, shortDescription, fullDescription, concern, viewpoint, faultTreeModel, diagram);
124 } else if (type == ElementTypeEnum.DOCUMENT) {
125 DocumentRepresentation document = dialog.getDocument();
126 raResult = project.createDocument(name, shortDescription, fullDescription, concern, viewpoint, document);
127 } else {
128
129 }
130
131 return raResult;
132 } catch (Exception e) {
133 return e;
134 }
135 }
136 public void finished() {
137 mainFrame.setCursor(Cursor.getDefaultCursor());
138 Object o = get();
139 if (o instanceof RiskAnalysisResult) {
140 } else if (o instanceof LockedByOtherUserException) {
141 LockedByOtherUserException e = (LockedByOtherUserException) o;
142 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);
143 } else if (o instanceof AlreadyLockedException) {
144 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);
145 } else if (o instanceof Boolean || o instanceof Exception) {
146 new Thread() {
147 public void run() {
148 try {
149 project.unCheckOut();
150 } catch (LockException e) {
151 e.printStackTrace();
152 }
153 }
154 }.start();
155 if (o instanceof Exception) {
156 Exception e = (Exception) o;
157 e.printStackTrace();
158 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);
159 }
160 } else {
161 System.err.println("Unexpected result: " + o);
162 }
163 }
164 }.start();
165 }
166
167 private void setType(ElementTypeEnum newType) {
168 type = newType;
169 String name = type != null ? type.toString() : "Result";
170 putValue(NAME, name);
171 putValue(MNEMONIC_KEY, new Integer(name.charAt(0)));
172 putValue(SHORT_DESCRIPTION, "Import " + name + " from file");
173 putValue(LONG_DESCRIPTION, "Import " + name + " from file");
174 }
175
176 }
177