Skip to content
Snippets Groups Projects
JDialogLoadingNetworkModel.java 13.5 KiB
Newer Older
/**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.*;
apvrille's avatar
apvrille committed
import java.awt.event.*;
apvrille's avatar
apvrille committed
import java.util.*;
import javax.swing.*;
import java.io.*;

import ui.*;
import myutil.*;
Florian Lugou's avatar
Florian Lugou committed
import ui.util.IconManager;
apvrille's avatar
apvrille committed
public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements ActionListener, Runnable, LoaderFacilityInterface, CallbackLoaderInterface  {
apvrille's avatar
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"};
    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;

    protected JScrollPane jsp;

    private Thread t;
    private boolean go = false;
    private boolean hasError = false;
    protected boolean startProcess = false;

    private String url;
    private NetworkModelPanel panel;
    private String filePath;
apvrille's avatar
apvrille committed
    public JDialogLoadingNetworkModel(Frame _f, MainGUI _mgui, String title, String _url)  {

        //getGlassPane().addMouseListener( new MouseAdapter() {});
        getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }


    protected void myInitComponents() {
        mode = NOT_LISTED;
        setButtons();

    protected void initComponents() {

        Container c = getContentPane();
        setFont(new Font("Helvetica", Font.PLAIN, 14));
        c.setLayout(new BorderLayout());
        //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


apvrille's avatar
apvrille committed

        JPanel lowPart = new JPanel(new BorderLayout());
apvrille's avatar
apvrille committed
        jta.setLineWrap(true);
        jta.setMargin(new Insets(10, 10, 10, 10));
        jta.setTabSize(3);
        jta.append("Connecting to " + url + ".\n Please wait ...\n\n");
        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);
apvrille's avatar
apvrille committed
        jsp.setPreferredSize(new Dimension(400, 200));

        JPanel options = new JPanel();

	JLabel infoModels = new JLabel("Not loaded");
	options.add(infoModels);
apvrille's avatar
apvrille committed
        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);

        lowPart.add(jsp, BorderLayout.CENTER);
        start = new JButton("Load", IconManager.imgic23);
        start.setPreferredSize(new Dimension(200, 30));
        stop.setPreferredSize(new Dimension(200, 30));

        start.addActionListener(this);
        stop.addActionListener(this);

        JPanel jp2 = new JPanel();
        jp2.add(stop);
apvrille's avatar
apvrille committed
        //jp2.add(start);
        lowPart.add(jp2, BorderLayout.SOUTH);
apvrille's avatar
apvrille committed
        c.add(lowPart, BorderLayout.SOUTH);
apvrille's avatar
apvrille committed
        panel = new NetworkModelPanel(this, listOfModels, this, jta, infoModels);
        jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
apvrille's avatar
apvrille committed
        panel.setJSP(jsp);

        c.add(jsp, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent evt)  {
        //String command = evt.getActionCommand();

        // Compare the action command to the known actions.
apvrille's avatar
apvrille committed
        if (evt.getSource() == stop) {
apvrille's avatar
apvrille committed
            return ;
        } else if (evt.getSource() == featureList) {
apvrille's avatar
apvrille committed
            featureSelectionMade();
            return;
        }

        for (int i = 0; i<props.length; i++) {
            if (evt.getSource() == props[i]) {
                panel.setProperty(i, props[i].isSelected());
                return;
            }
        }
    public void featureSelectionMade() {
apvrille's avatar
apvrille committed
        panel.setFeatureSelectedIndex(featureList.getSelectedIndex());
        // Loading main file describing models, giving information on this, and filling the array of models
        // Accsing the main file
        try {
apvrille's avatar
apvrille committed
            /*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);
            jta.append("Connection established...\n");
            String inputLine;
apvrille's avatar
apvrille committed
            NetworkModel nm = null;
apvrille's avatar
apvrille committed
                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);
                    }
                }

apvrille's avatar
apvrille committed

            }
            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();
apvrille's avatar
apvrille committed
            // Wait 5seconds before refreshing panel
            Thread.sleep(5000);
            panel.repaint();

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

    protected void checkMode() {
        mode = NOT_LISTED;
    }

    protected void setButtons() {
        switch(mode) {
        case NOT_LISTED:
            //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:

    // LoaderFacilityInterface
apvrille's avatar
apvrille committed
    public void load(int index) {
apvrille's avatar
apvrille committed
        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
apvrille's avatar
apvrille committed
    public void loadDone() {
apvrille's avatar
apvrille committed
        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));
apvrille's avatar
apvrille committed
    }

    public void loadFailed() {
apvrille's avatar
apvrille committed
        jta.append("Model transfer failed\nPlease, select another model, or retry\n");
        panel.reactivateSelection();
    // JTA manipulation by external objects