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.BorderLayout;
24  import java.awt.Cursor;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.KeyEvent;
28  import java.io.StringReader;
29  
30  import javax.swing.JButton;
31  import javax.swing.JDialog;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.border.EmptyBorder;
35  
36  import no.sintef.xml.XmlHelper;
37  
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  import org.xml.sax.InputSource;
41  
42  import coras.client.ui.ConsistencyDialog;
43  import coras.client.ui.CorasFrame;
44  import coras.riskanalysis.RiskAnalysisResult;
45  
46  /***
47   * @author fvr
48   *
49   * TODO To change the template for this generated type comment go to
50   * Window - Preferences - Java - Code Style - Code Templates
51   */
52  public class CheckConsistencyAction extends CorasAction{
53      
54      public CheckConsistencyAction(CorasClient client) {
55          super(client, "Check Consistency", KeyEvent.VK_C, "Check Consistency", "Check Risk Analysis Result Consistency");
56      }
57      
58      public void actionPerformed(ActionEvent e) {
59      	final RiskAnalysisResult result = getClient().getSelectedResult();
60      	if (result != null) {
61      		final CorasFrame mainFrame = getClient().getMainFrame();
62  			mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
63      		new SwingWorker() {
64      			public Object construct() {
65      				try {
66      					return getClient().getCorasServices().checkConsistency(result.getId());
67      				} catch (Exception e) {
68      					System.err.println("Error while checking consistency: " + e);
69      					return "";
70      				}
71      			}
72      			public void finished() {
73      				mainFrame.setCursor(Cursor.getDefaultCursor());
74      				String s = (String) get();
75      				System.out.println("consistency result: " + s);
76      				try {
77  						Document doc = XmlHelper.parse(new InputSource(new StringReader(s)));
78  						Element elem = doc.getDocumentElement();
79  						if (elem != null && elem.getLocalName().equals("consistencyResults")) {
80  							ConsistencyDialog dialog = new ConsistencyDialog(mainFrame);
81  							dialog.setTitle("Consistency check result for " + result.getName());
82  							dialog.setConsistencyResult(doc);
83  							dialog.setVisible(true);
84  							return;
85  						}
86  					} catch (Exception e) {
87  					}
88  					// fallback option
89  					s = "<HTML>" + s + "</HTML>";
90  					final JDialog dialog = new JDialog(mainFrame);
91  					dialog.setTitle(result.getName() + " consistency results");
92  					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
93  					JLabel resultLabel = new JLabel(s);
94  					resultLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
95  					JPanel buttonPanel = new JPanel();
96  					JButton closeButton = new JButton("Close");
97  					closeButton.addActionListener(new ActionListener() {
98  						public void actionPerformed(ActionEvent e) {
99  							dialog.dispose();
100 						}
101 					});
102 					buttonPanel.add(closeButton);
103 					dialog.getContentPane().setLayout(new BorderLayout());
104 					dialog.getContentPane().add(resultLabel, BorderLayout.CENTER);
105 					dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
106 					dialog.pack();
107 					dialog.setSize(dialog.getPreferredSize());
108 					dialog.setLocationRelativeTo(mainFrame);
109 					dialog.setVisible(true);
110     			}
111     		}.start();
112     	}
113     }
114     
115 }