From 6493fae9fbe67c69502a3c6fc1282bc2041c26f8 Mon Sep 17 00:00:00 2001 From: apvrille <ludovic.apvrille@eurecom.fr> Date: Thu, 11 Jan 2018 13:40:59 +0100 Subject: [PATCH] Update on model loading management --- .idea/misc.xml | 2 +- .../JDialogLoadingNetworkModel.java | 178 +++++++++--------- .../networkmodelloader/NetworkModelPanel.java | 85 +++++---- 3 files changed, 138 insertions(+), 127 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 51e5191ed4..b755d8ca46 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -38,7 +38,7 @@ <property name="caretWidth" class="java.lang.Integer" /> </properties> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="false" project-jdk-name="1.7" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build" /> </component> </project> \ No newline at end of file diff --git a/src/main/java/ui/networkmodelloader/JDialogLoadingNetworkModel.java b/src/main/java/ui/networkmodelloader/JDialogLoadingNetworkModel.java index 6820f27f18..6fd8a78df4 100644 --- a/src/main/java/ui/networkmodelloader/JDialogLoadingNetworkModel.java +++ b/src/main/java/ui/networkmodelloader/JDialogLoadingNetworkModel.java @@ -37,36 +37,39 @@ */ - - package ui.networkmodelloader; -import java.awt.*; -import java.awt.event.*; -import java.util.*; -import javax.swing.*; -import java.io.*; - import common.ConfigurationTTool; import common.SpecConfigTTool; -import ui.*; -import ui.file.TFileFilter; import myutil.*; +import ui.JTextAreaWriter; +import ui.MainGUI; +import ui.file.TFileFilter; import ui.util.IconManager; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + /** * 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 + * @version 1.1 28/05/2017 */ -public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements ActionListener, Runnable, LoaderFacilityInterface, CallbackLoaderInterface { +public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements ActionListener, Runnable, LoaderFacilityInterface, CallbackLoaderInterface { - public final static String [] FEATURES = {"all", "diplodocus", "avatar", "sysml-sec", "assumptions", "requirements", "attacktrees", "properties", "partitioning", "analysis", "design", "prototyping", "securityprotocol", "codegeneration"}; + public final static String[] FEATURES = {"all", "diplodocus", "avatar", "sysml-sec", "assumptions", "requirements", "attacktrees", "properties", "partitioning", "analysis", "design", "prototyping", "securityprotocol", "codegeneration"}; - public final static String [] PROPS = {"safety", "security", "performance"}; + public final static String[] PROPS = {"safety", "security", "performance"}; private ArrayList<NetworkModel> listOfModels; @@ -85,7 +88,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac protected JButton start; protected JButton stop; protected JComboBox<String> featureList; - protected JCheckBox [] props; + protected JCheckBox[] props; protected JScrollPane jsp; @@ -100,8 +103,10 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac private JFileChooser jfc; - /** Creates new form */ - public JDialogLoadingNetworkModel(Frame _f, MainGUI _mgui, String title, String _url) { + /** + * Creates new form + */ + public JDialogLoadingNetworkModel(Frame _f, MainGUI _mgui, String title, String _url) { super(title); f = _f; @@ -125,13 +130,13 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac protected void myInitComponents() { - - if (ConfigurationTTool.DownloadedFILEPath.length() > 0) { + + if (ConfigurationTTool.DownloadedFILEPath.length() > 0) { jfc = new JFileChooser(ConfigurationTTool.DownloadedFILEPath); } else { jfc = new JFileChooser(); } - + mode = NOT_LISTED; setButtons(); } @@ -144,7 +149,6 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - JPanel lowPart = new JPanel(new BorderLayout()); jta = new ScrolledJTextArea(); @@ -155,7 +159,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac 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 ); + textAreaWriter = new JTextAreaWriter(jta); jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jsp.setPreferredSize(new Dimension(400, 200)); @@ -170,7 +174,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac options.add(featureList); props = new JCheckBox[PROPS.length]; - for (int i=0; i<props.length; i++) { + for (int i = 0; i < props.length; i++) { props[i] = new JCheckBox(PROPS[i]); props[i].addActionListener(this); props[i].setSelected(true); @@ -206,19 +210,19 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac c.add(jsp, BorderLayout.CENTER); } - public void actionPerformed(ActionEvent evt) { + public void actionPerformed(ActionEvent evt) { //String command = evt.getActionCommand(); // Compare the action command to the known actions. if (evt.getSource() == stop) { cancel(); - return ; + return; } else if (evt.getSource() == featureList) { featureSelectionMade(); return; } - for (int i = 0; i<props.length; i++) { + for (int i = 0; i < props.length; i++) { if (evt.getSource() == props[i]) { panel.setProperty(i, props[i].isSelected()); return; @@ -231,13 +235,13 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac } public void cancel() { - dispose(); + if (panel != null) panel.stopLoading(); dispose(); } public void run() { // Loading main file describing models, giving information on this, and filling the array of models - // Accsing the main file + // Accessing the main file try { /*HttpURLConnection connection; TraceManager.addDev("URL: going to create it to: " + url); @@ -256,7 +260,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac BufferedReader in = URLManager.getBufferedReader(url); if (in == null) { jta.append("Could not establish a connection to the TTool server\n"); - jta.append("Error when retreiving file: " + url + "\n No internet connection?\n No right for the Java Virtual Machine to use http connections?\n\n"); + jta.append("Error when retreiving file: " + url + "\n No internet connection?\n No right for the Java Virtual Machine to use http connections?\n\n"); } else { jta.append("Connection established...\n"); String inputLine = null; @@ -270,7 +274,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac if (inputLine.startsWith("-FEATURES")) { if (nm != null) { String tmp = inputLine.substring(9, inputLine.length()).trim().toLowerCase(); - for (int i=1; i<FEATURES.length; i++) { + 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()); @@ -280,7 +284,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac if (inputLine.startsWith("-PROPS")) { if (nm != null) { String tmp = inputLine.substring(6, inputLine.length()).trim().toLowerCase(); - for (int i=0; i<PROPS.length; i++) { + 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()); @@ -335,25 +339,25 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac } protected void setButtons() { - switch(mode) { - case NOT_LISTED: - start.setEnabled(false); - stop.setEnabled(true); - //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: - start.setEnabled(true); - stop.setEnabled(true); - getGlassPane().setVisible(false); - break; + switch (mode) { + case NOT_LISTED: + start.setEnabled(false); + stop.setEnabled(true); + //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: + start.setEnabled(true); + stop.setEnabled(true); + getGlassPane().setVisible(false); + break; } } @@ -361,55 +365,55 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac // LoaderFacilityInterface public void load(int index) { String fileName = listOfModels.get(index).fileName; + if (panel != null) panel.stopLoading(); jta.append("Loading model: " + fileName); String urlToLoad = URLManager.getBaseURL(url) + fileName; URLManager urlm = new URLManager(); jfc.setSelectedFile(new File(FileUtils.removeFileExtension(fileName))); int returnVal = jfc.showSaveDialog(f); - if(returnVal == JFileChooser.APPROVE_OPTION) { - filePath = jfc.getSelectedFile().getAbsolutePath(); - filePath = FileUtils.addFileExtensionIfMissing(filePath, "xml"); - boolean ok = urlm.downloadFile(filePath, urlToLoad,this); - if (!ok) { - loadFailed(); - } - } - else { - panel.reactivateSelection(); + if (returnVal == JFileChooser.APPROVE_OPTION) { + filePath = jfc.getSelectedFile().getAbsolutePath(); + filePath = FileUtils.addFileExtensionIfMissing(filePath, "xml"); + boolean ok = urlm.downloadFile(filePath, urlToLoad, this); + if (!ok) { + loadFailed(); + } + } else { + panel.reactivateSelection(); } } // CallbackLoaderInterface public void loadDone() { - jta.append("Model transfered, opening it in TTool\n"); + jta.append("Model transferred, opening it in TTool\n"); this.dispose(); - SwingUtilities.invokeLater(new Runnable() { - public void run() { - File dir = new File(filePath.replace(".xml", "")); - dir = FileUtils.addFileExtensionIfMissing(dir, "ttool"); - dir.mkdir(); - SpecConfigTTool.setDirConfig(dir); - File config = SpecConfigTTool.createProjectConfig(dir); - try { - SpecConfigTTool.loadConfigFile(config); - } catch (MalformedConfigurationException e) { - System.err.println(e.getMessage() + " : Can't load config file."); - } - File file = new File(filePath); - file = FileUtils.addFileExtensionIfMissing(file, TFileFilter.getExtension()); - try { - FileUtils.moveFileToDirectory(file, dir, false); - } catch (IOException e) { - System.err.println(e.getMessage() + " : Network loading failed"); - } - mgui.openProjectFromFile(dir); - // Here, we can safely update the GUI - // because we'll be called from the - // event dispatch thread - //statusLabel.setText("Query: " + queryNo); - } - }); + SwingUtilities.invokeLater(new Runnable() { + public void run() { + File dir = new File(filePath.replace(".xml", "")); + dir = FileUtils.addFileExtensionIfMissing(dir, "ttool"); + dir.mkdir(); + SpecConfigTTool.setDirConfig(dir); + File config = SpecConfigTTool.createProjectConfig(dir); + try { + SpecConfigTTool.loadConfigFile(config); + } catch (MalformedConfigurationException e) { + System.err.println(e.getMessage() + " : Can't load config file."); + } + File file = new File(filePath); + file = FileUtils.addFileExtensionIfMissing(file, TFileFilter.getExtension()); + try { + FileUtils.moveFileToDirectory(file, dir, false); + } catch (IOException e) { + System.err.println(e.getMessage() + " : Network loading failed"); + } + mgui.openProjectFromFile(dir); + // 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)); } diff --git a/src/main/java/ui/networkmodelloader/NetworkModelPanel.java b/src/main/java/ui/networkmodelloader/NetworkModelPanel.java index 2d23ea9385..2bd68880a5 100644 --- a/src/main/java/ui/networkmodelloader/NetworkModelPanel.java +++ b/src/main/java/ui/networkmodelloader/NetworkModelPanel.java @@ -37,29 +37,32 @@ */ - - package ui.networkmodelloader; -import java.awt.*; -import java.awt.event.*; -import java.awt.image.*; -import javax.swing.*; -import javax.swing.border.*; -import java.util.*; - -import myutil.*; -import ui.*; +import myutil.GraphicLib; +import myutil.ImageManager; +import myutil.LoaderFacilityInterface; +import myutil.URLManager; +import ui.ColorManager; +import javax.swing.*; +import javax.swing.border.BevelBorder; +import java.awt.*; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.util.ArrayList; /** * Class NetworkModel * Dialog for managing the loading of network models * Creation: 30/05/2017 - * @version 1.1 30/05/2017 + * * @author Ludovic APVRILLE * @author Ludovic APVRILLE + * @version 1.1 30/05/2017 */ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener, MouseMotionListener { @@ -92,6 +95,7 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener private String url; + private boolean go = true; public NetworkModelPanel(LoaderFacilityInterface _loader, ArrayList<NetworkModel> _listOfModels, ActionListener _listener, JTextArea _jta, JLabel _infoModels) { @@ -99,7 +103,7 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener listOfModels = _listOfModels; listener = _listener; jta = _jta; - infoModels = _infoModels; + infoModels = _infoModels; //Dimension pSize = new Dimension(500, 400); Dimension mSize = new Dimension(400, 300); @@ -111,7 +115,7 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener // properties props = new boolean[JDialogLoadingNetworkModel.PROPS.length]; - for (int i=0; i<props.length; i++) { + for (int i = 0; i < props.length; i++) { props[i] = true; } @@ -137,8 +141,13 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener repaint(); } + public void stopLoading() { + go = false; + } + public void run() { - for(NetworkModel button: listOfModels) { + for (NetworkModel button : listOfModels) { + if (go == false) return; //Dimension d = new Dimension(buttonSizeX, buttonSizeY); //button.setPreferredSize(d); //int tmpX = cptColumn * (buttonSizeX + spaceBetweenButtons); @@ -167,14 +176,14 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener public void preparePanel(String _url) { - url = _url; + url = _url; Thread t = new Thread(this); t.start(); } private boolean hasAtLeastOneSelectedProperty(NetworkModel _nm) { - for (int i=0; i<props.length; i++) { + for (int i = 0; i < props.length; i++) { if (props[i]) { if (_nm.props[i]) { return true; @@ -192,10 +201,10 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener int cptRow = 0; int index = 0; - int modelsDrawn = 0; - - for(NetworkModel button: listOfModels) { - if ((button.features[featureSelectedIndex]) && hasAtLeastOneSelectedProperty(button)){ + int modelsDrawn = 0; + + for (NetworkModel button : listOfModels) { + if ((button.features[featureSelectedIndex]) && hasAtLeastOneSelectedProperty(button)) { Color c = g.getColor(); int tmpX = cptColumn * (buttonSizeX + spaceBetweenButtons) + marginX; int tmpY = cptRow * (buttonSizeY + spaceBetweenButtons) + marginY; @@ -205,17 +214,16 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener g.setColor(ColorManager.AVATAR_BLOCK); g.fillRect(tmpX, tmpY, buttonSizeX, buttonSizeY); g.setColor(c); - GraphicLib.centerString(g, "No picture", tmpX, tmpY + buttonSizeY/2, buttonSizeX); + GraphicLib.centerString(g, "No picture", tmpX, tmpY + buttonSizeY / 2, buttonSizeX); } - modelsDrawn ++; + modelsDrawn++; GraphicLib.centerString(g, button.fileName, tmpX, tmpY + buttonSizeY + 15, buttonSizeX); - - cptColumn ++; + cptColumn++; if (cptColumn == nbOfButtonsPerColumn) { - cptRow ++; + cptRow++; cptColumn = 0; } @@ -231,10 +239,10 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener } else { g.setColor(ColorManager.POINTER_ON_ME_0); } - Graphics2D g2 = (Graphics2D)g; + Graphics2D g2 = (Graphics2D) g; Stroke oldStroke = g2.getStroke(); g2.setStroke(new BasicStroke(5)); - g2.drawRect(button.x-10, button.y-10, button.width+20, button.height+20); + g2.drawRect(button.x - 10, button.y - 10, button.width + 20, button.height + 20); g2.setStroke(oldStroke); g.setColor(c); } @@ -245,11 +253,11 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener button.height = -1; } - index ++; + index++; } Dimension currentPSize = getPreferredSize(); - Dimension pSize = new Dimension(400, Math.max(300, (cptRow+1)*(buttonSizeY +spaceBetweenButtons) + 2* marginY)); + Dimension pSize = new Dimension(400, Math.max(300, (cptRow + 1) * (buttonSizeY + spaceBetweenButtons) + 2 * marginY)); setPreferredSize(pSize); if (!((currentPSize.getWidth() == pSize.getWidth()) && (currentPSize.getHeight() == pSize.getHeight()))) { @@ -261,10 +269,10 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener } } - if ((modelsDrawn != nbOfModels) && (infoModels != null)) { - nbOfModels = modelsDrawn; - infoModels.setText(nbOfModels + " model(s)"); - } + if ((modelsDrawn != nbOfModels) && (infoModels != null)) { + nbOfModels = modelsDrawn; + infoModels.setText(nbOfModels + " model(s)"); + } //g.drawString(listOfModels.size() + " model(s) available", 20, 20); @@ -277,19 +285,19 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener } public void mouseMoved(MouseEvent e) { - if (!selectedModel) { + if (!selectedModel) { int previousIndex = indexOfSelected; boolean found = false; int index = 0; - for(NetworkModel button: listOfModels) { + for (NetworkModel button : listOfModels) { if ((button.features[featureSelectedIndex]) && hasAtLeastOneSelectedProperty(button)) { - if ((e.getX() > button.x) && (e.getX() < button.x + button.width) && (e.getY() > button.y) && (e.getY() < button.y + button.height)) { + if ((e.getX() > button.x) && (e.getX() < button.x + button.width) && (e.getY() > button.y) && (e.getY() < button.y + button.height)) { indexOfSelected = index; found = true; break; } } - index ++; + index++; } if (!found) { indexOfSelected = -1; @@ -333,5 +341,4 @@ public class NetworkModelPanel extends JPanel implements Runnable, MouseListener } - } -- GitLab