Newer
Older

Ludovic Apvrille
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille
*
* ludovic.apvrille AT enst.fr
*
* This software is a computer program whose purpose is to allow the
* edition of TURTLE analysis, design and deployment diagrams, to
* allow the generation of RT-LOTOS or Java code from this diagram,
* and at last to allow the analysis of formal validation traces
* obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
* from INRIA Rhone-Alpes.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
* /**
* Class JDialogNetworkModelPanel
* Dialog for managing the loading of network models
* Creation: 28/05/2017
* @version 1.1 28/05/2017
* @author Ludovic APVRILLE
* @author Ludovic Apvrille
* @see
*/
package ui.networkmodelloader;
import java.awt.*;

Ludovic Apvrille
committed
import java.io.File;
import java.util.*;
import javax.swing.*;
import java.io.*;
import ui.*;
import myutil.*;

Ludovic Apvrille
committed
public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements ActionListener, Runnable, LoaderFacilityInterface, CallbackLoaderInterface {

Ludovic Apvrille
committed
public final static String [] FEATURES = {"all", "diplodocus", "avatar", "sysml-sec", "assumptions", "requirements", "attacktrees", "properties", "partitioning", "analysis", "design", "prototyping", "securityprotocol"};
public final static String [] PROPS = {"safety", "security", "performance"};

Ludovic Apvrille
committed
private ArrayList<NetworkModel> listOfModels;

Ludovic Apvrille
committed
protected Frame f;
protected MainGUI mgui;

Ludovic Apvrille
committed
protected final static int NOT_LISTED = 1;

Ludovic Apvrille
committed
protected final static int LISTED = 2;
protected final static int SELECTED = 3;
private int mode;
//components
protected JTextArea jta;
private JTextAreaWriter textAreaWriter;
protected JButton start;
protected JButton stop;
protected JComboBox<String> featureList;
protected JCheckBox [] props;

Ludovic Apvrille
committed
protected JScrollPane jsp;
private Thread t;
private boolean go = false;
private boolean hasError = false;
protected boolean startProcess = false;
private String url;
private NetworkModelPanel panel;

Ludovic Apvrille
committed

Ludovic Apvrille
committed
/** Creates new form */
public JDialogLoadingNetworkModel(Frame _f, MainGUI _mgui, String title, String _url) {

Ludovic Apvrille
committed
super(title);
f = _f;
mgui = _mgui;

Ludovic Apvrille
committed
url = _url;
listOfModels = new ArrayList<NetworkModel>();

Ludovic Apvrille
committed
initComponents();
myInitComponents();
pack();

Ludovic Apvrille
committed
Thread t = new Thread(this);
t.start();

Ludovic Apvrille
committed
//getGlassPane().addMouseListener( new MouseAdapter() {});
getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
protected void myInitComponents() {
mode = NOT_LISTED;
setButtons();

Ludovic Apvrille
committed
}

Ludovic Apvrille
committed
protected void initComponents() {
Container c = getContentPane();
setFont(new Font("Helvetica", Font.PLAIN, 14));
c.setLayout(new BorderLayout());
//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel lowPart = new JPanel(new BorderLayout());

Ludovic Apvrille
committed
jta = new ScrolledJTextArea();
jta.setEditable(false);

Ludovic Apvrille
committed
jta.setMargin(new Insets(10, 10, 10, 10));
jta.setTabSize(3);

Ludovic Apvrille
committed
jta.append("Connecting to " + url + ".\n Please wait ...\n\n");

Ludovic Apvrille
committed
Font f = new Font("Courrier", Font.BOLD, 12);
jta.setFont(f);
textAreaWriter = new JTextAreaWriter( jta );
jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setPreferredSize(new Dimension(400, 200));
JPanel options = new JPanel();
JLabel infoModels = new JLabel("Not loaded");
options.add(infoModels);
featureList = new JComboBox<String>(FEATURES);
featureList.addActionListener(this);
options.add(featureList);
props = new JCheckBox[PROPS.length];
for (int i=0; i<props.length; i++) {
props[i] = new JCheckBox(PROPS[i]);
props[i].addActionListener(this);
props[i].setSelected(true);
options.add(props[i]);
}
lowPart.add(options, BorderLayout.NORTH);

Ludovic Apvrille
committed

Ludovic Apvrille
committed
start = new JButton("Load", IconManager.imgic23);

Ludovic Apvrille
committed
stop = new JButton("Cancel", IconManager.imgic55);

Ludovic Apvrille
committed
start.setPreferredSize(new Dimension(200, 30));
stop.setPreferredSize(new Dimension(200, 30));

Ludovic Apvrille
committed
start.addActionListener(this);
stop.addActionListener(this);
JPanel jp2 = new JPanel();
jp2.add(stop);

Ludovic Apvrille
committed

Ludovic Apvrille
committed
panel = new NetworkModelPanel(this, listOfModels, this, jta, infoModels);
jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Ludovic Apvrille
committed
}
public void actionPerformed(ActionEvent evt) {
//String command = evt.getActionCommand();
// Compare the action command to the known actions.

Ludovic Apvrille
committed
cancel();
} else if (evt.getSource() == featureList) {
featureSelectionMade();
return;
}
for (int i = 0; i<props.length; i++) {
if (evt.getSource() == props[i]) {
panel.setProperty(i, props[i].isSelected());
return;
}
}

Ludovic Apvrille
committed
public void featureSelectionMade() {
panel.setFeatureSelectedIndex(featureList.getSelectedIndex());

Ludovic Apvrille
committed
}
public void cancel() {

Ludovic Apvrille
committed
dispose();

Ludovic Apvrille
committed
}

Ludovic Apvrille
committed

Ludovic Apvrille
committed
public void run() {

Ludovic Apvrille
committed
// Loading main file describing models, giving information on this, and filling the array of models
// Accsing the main file
try {
/*HttpURLConnection connection;
TraceManager.addDev("URL: going to create it to: " + url);
URL mainFile = new URL(url);
TraceManager.addDev("URL creation");
connection = (HttpURLConnection)(mainFile.openConnection());
TraceManager.addDev("Connection setup 0");
String redirect = connection.getHeaderField("Location");
if (redirect != null){
TraceManager.addDev("Redirection found");
connection = (HttpURLConnection)(new URL(redirect).openConnection());
}
//connection.setRequestMethod("GET");
//connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
TraceManager.addDev("Connection setup 1");*/
BufferedReader in = URLManager.getBufferedReader(url);

Ludovic Apvrille
committed
jta.append("Connection established...\n");
String inputLine;

Ludovic Apvrille
committed
while ((inputLine = in.readLine()) != null) {
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
if (inputLine.startsWith("#FILE")) {
nm = new NetworkModel(inputLine.substring(5, inputLine.length()).trim());
listOfModels.add(nm);
}
if (inputLine.startsWith("-FEATURES")) {
if (nm != null) {
String tmp = inputLine.substring(9, inputLine.length()).trim().toLowerCase();
for (int i=1; i<FEATURES.length; i++) {
nm.features[i] = tmp.indexOf(FEATURES[i]) != -1;
}
//nm.type = NetworkModel.stringToNetworkModelType(inputLine.substring(5, inputLine.length()).trim());
}
}
if (inputLine.startsWith("-PROPS")) {
if (nm != null) {
String tmp = inputLine.substring(6, inputLine.length()).trim().toLowerCase();
for (int i=0; i<PROPS.length; i++) {
nm.props[i] = tmp.indexOf(PROPS[i]) != -1;
}
//nm.type = NetworkModel.stringToNetworkModelType(inputLine.substring(5, inputLine.length()).trim());
}
}
if (inputLine.startsWith("-AUTHOR")) {
if (nm != null) {
nm.author = inputLine.substring(7, inputLine.length()).trim();
}
}
if (inputLine.startsWith("-DESCRIPTION")) {
if (nm != null) {
nm.description = inputLine.substring(12, inputLine.length()).trim();
}
}
if (inputLine.startsWith("-IMG")) {
if (nm != null) {
nm.image = inputLine.substring(4, inputLine.length()).trim();
//TraceManager.addDev("Dealing with image:" + nm.image);
//nm.bi = URLManager.getBufferedImageFromURL(URLManager.getBaseURL(url) + nm.image);
}
}

Ludovic Apvrille
committed
//System.out.println(inputLine);
}
jta.append("\n" + listOfModels.size() + " remote models found.\nSelect a model to download it locally and open it.\n\n");
mode = LISTED;
panel.preparePanel(url);
panel.repaint();

Ludovic Apvrille
committed
in.close();
// Wait 5seconds before refreshing panel
Thread.sleep(5000);
panel.repaint();

Ludovic Apvrille
committed
} catch (Exception e) {
jta.append("Error: " + e.getMessage() + " when retreiving file " + url );
}

Ludovic Apvrille
committed
}
protected void checkMode() {
mode = NOT_LISTED;
}
protected void setButtons() {
switch(mode) {
case NOT_LISTED:

Ludovic Apvrille
committed
start.setEnabled(false);
stop.setEnabled(true);

Ludovic Apvrille
committed
//setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
getGlassPane().setVisible(false);
break;
case LISTED:
start.setEnabled(false);
stop.setEnabled(true);
getGlassPane().setVisible(true);
//setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
break;
case SELECTED:
default:

Ludovic Apvrille
committed
start.setEnabled(true);
stop.setEnabled(true);

Ludovic Apvrille
committed
getGlassPane().setVisible(false);
break;
}
}
// LoaderFacilityInterface
String fileName = listOfModels.get(index).fileName;
jta.append("Loading model: " + fileName);
String urlToLoad = URLManager.getBaseURL(url) + fileName;
URLManager urlm = new URLManager();
filePath = ConfigurationTTool.FILEPath + "/" + fileName;
boolean ok = urlm.downloadFile(ConfigurationTTool.FILEPath + "/" + fileName, urlToLoad,this);
if (!ok) {
jta.append("Model transfer failed\nPlease, select another model, or retry\n");
panel.reactivateSelection();
}
// CallbackLoaderInterface
jta.append("Model transfered, opening it in TTool\n");
this.dispose();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mgui.openProjectFromFile(new File(filePath));
// Here, we can safely update the GUI
// because we'll be called from the
// event dispatch thread
//statusLabel.setText("Query: " + queryNo);
}
});
//mgui.openProjectFromFile(new File(filePath));
jta.append("Model transfer failed\nPlease, select another model, or retry\n");
panel.reactivateSelection();
// JTA manipulation by external objects

Ludovic Apvrille
committed
public void appendOut(String s) {
jta.append(s);
}
}