From 694147da22c3bd38c5aa3e6f228181c597cd51a1 Mon Sep 17 00:00:00 2001
From: apvrille <ludovic.apvrille@eurecom.fr>
Date: Wed, 31 May 2017 15:36:33 +0200
Subject: [PATCH] Update on network model loading

---
 src/myutil/ImageManager.java                  |  85 ++++++++++++++
 src/myutil/URLManager.java                    | 105 ++++++++++++++++++
 .../JDialogLoadingNetworkModel.java           |  27 +++--
 src/ui/networkmodelloader/NetworkModel.java   |   2 +
 .../networkmodelloader/NetworkModelPanel.java |  38 +++++--
 5 files changed, 241 insertions(+), 16 deletions(-)
 create mode 100755 src/myutil/ImageManager.java
 create mode 100755 src/myutil/URLManager.java

diff --git a/src/myutil/ImageManager.java b/src/myutil/ImageManager.java
new file mode 100755
index 0000000000..d5dab72f00
--- /dev/null
+++ b/src/myutil/ImageManager.java
@@ -0,0 +1,85 @@
+/**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 ImageManager
+   * Creation: 31/05/2017
+   * @version 1.1 31/05/2017
+   * @author Ludovic APVRILLE
+   * @see
+   */
+
+package myutil;
+
+import java.net.*;
+import java.io.*;
+import java.awt.image.*;
+import java.awt.*;
+import javax.imageio.*;
+
+
+public final class ImageManager {
+
+    /**
+     * Resizes an image using a Graphics2D object backed by a BufferedImage.
+     * @param srcImg - source image to scale
+     * @param w - desired width
+     * @param h - desired height
+     * @return - the new resized image
+     */
+    public static BufferedImage getScaledImage(BufferedImage src, int w, int h){
+        int finalw = w;
+        int finalh = h;
+        double factor = 1.0d;
+        if(src.getWidth() > src.getHeight()){
+            factor = ((double)src.getHeight()/(double)src.getWidth());
+            finalh = (int)(finalw * factor);
+        }else{
+            factor = ((double)src.getWidth()/(double)src.getHeight());
+            finalw = (int)(finalh * factor);
+        }
+
+        BufferedImage resizedImg = new BufferedImage(finalw, finalh, BufferedImage.TRANSLUCENT);
+        Graphics2D g2 = resizedImg.createGraphics();
+        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+        g2.drawImage(src, 0, 0, finalw, finalh, null);
+        g2.dispose();
+        return resizedImg;
+    }
+
+
+}
diff --git a/src/myutil/URLManager.java b/src/myutil/URLManager.java
new file mode 100755
index 0000000000..6087cad75f
--- /dev/null
+++ b/src/myutil/URLManager.java
@@ -0,0 +1,105 @@
+/**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 URLManager
+   * Creation: 31/05/2017
+   * @version 1.1 31/05/2017
+   * @author Ludovic APVRILLE
+   * @see
+   */
+
+package myutil;
+
+import java.net.*;
+import java.io.*;
+import java.awt.image.*;
+import javax.imageio.*;
+
+
+public final class URLManager {
+
+    public static String getRealURL(String url) {
+	try {
+	    HttpURLConnection connection;
+            URL file = new URL(url);
+            connection = (HttpURLConnection)(file.openConnection());
+            String redirect = connection.getHeaderField("Location");
+            if (redirect != null){
+		return redirect;
+            }
+	} catch (Exception e) {
+	     TraceManager.addDev("Exception in getRealURL =" + e.getMessage());
+	}
+	return url;
+	
+    }
+
+    public static String getBaseURL(String url) {
+	int index = url.lastIndexOf("/");
+	if (index == -1) {
+	    return url;
+	}
+	return url.substring(0, index+1);
+    }
+
+    public static BufferedReader getBufferedReader(String url) {
+	try {
+	    String urlR = getRealURL(url);
+	    HttpURLConnection connection;
+	    URL file = new URL(urlR);
+	    connection = (HttpURLConnection)(file.openConnection());
+	    return new BufferedReader(new InputStreamReader(connection.getInputStream()));
+	} catch (Exception e) {
+	     TraceManager.addDev("Exception in getBufferedReader =" + e.getMessage());
+	}
+	return null;
+    }
+
+    public static BufferedImage getBufferedImageFromURL(String url) {
+	TraceManager.addDev("getBufferedImageFromURL with url=" + url);
+        try {
+	    return (BufferedImage)(ImageIO.read(new URL(getRealURL(url))));
+        } catch (Exception e) {
+	    TraceManager.addDev("Exception in getBufferedImageFromURL =" + e.getMessage());
+            return null;
+        }
+
+    }
+
+
+}
diff --git a/src/ui/networkmodelloader/JDialogLoadingNetworkModel.java b/src/ui/networkmodelloader/JDialogLoadingNetworkModel.java
index f979c6454a..87c68e807d 100644
--- a/src/ui/networkmodelloader/JDialogLoadingNetworkModel.java
+++ b/src/ui/networkmodelloader/JDialogLoadingNetworkModel.java
@@ -125,10 +125,11 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac
         //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 
         panel = new NetworkModelPanel(listOfModels, this);
-        jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
+        jsp = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 
-        c.add(jsp, BorderLayout.NORTH);
+        c.add(jsp, BorderLayout.CENTER);
 
+	JPanel lowPart = new JPanel(new BorderLayout());
 
         jta = new ScrolledJTextArea();
         jta.setEditable(false);
@@ -141,7 +142,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac
 
         jsp = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 
-        c.add(jsp, BorderLayout.CENTER);
+        lowPart.add(jsp, BorderLayout.CENTER);
 
         start = new JButton("Load", IconManager.imgic23);
         stop = new JButton("Cancel", IconManager.imgic55);
@@ -156,8 +157,9 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac
         jp2.add(stop);
         jp2.add(start);
 
-        c.add(jp2, BorderLayout.SOUTH);
+        lowPart.add(jp2, BorderLayout.SOUTH);
 
+	c.add(lowPart, BorderLayout.SOUTH);
     }
 
     public void actionPerformed(ActionEvent evt)  {
@@ -185,7 +187,7 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac
         // Loading main file describing models, giving information on this, and filling the array of models
         // Accsing the main file
         try {
-	    HttpURLConnection connection;
+	    /*HttpURLConnection connection;
 	    TraceManager.addDev("URL: going to create it to: " + url);
             URL mainFile = new URL(url);
 	    TraceManager.addDev("URL creation");
@@ -198,8 +200,8 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac
 	    }
 	    //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 = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+	    TraceManager.addDev("Connection setup 1");*/
+	    BufferedReader in = URLManager.getBufferedReader(url);
             jta.append("Connection established...\n");
             String inputLine;
 	    NetworkModel nm = null;
@@ -223,17 +225,24 @@ public class JDialogLoadingNetworkModel extends javax.swing.JFrame implements Ac
 		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);
 		    }
 		}
 		
                 //System.out.println(inputLine);
 		
 	    }
-		jta.append("\n" + listOfModels.size() + " loaded, you can now select a model to be loaded\n");
+		jta.append("\n" + listOfModels.size() + " remote models have been detected.\nSelect one to download it locally and open it.\n");
 	    mode = LISTED;
-	    panel.repaint();
 	    panel.addPanelWithButtons();
+	    panel.repaint();
             in.close();
+
+	    // Wait 5seconds before refreshing panel
+	    Thread.sleep(5000);
+	    panel.repaint();
+	    
         } catch (Exception e) {
             jta.append("Error: " + e.getMessage() + " when retreiving file " + url );
         }
diff --git a/src/ui/networkmodelloader/NetworkModel.java b/src/ui/networkmodelloader/NetworkModel.java
index cf1ab102d9..474fc192fe 100644
--- a/src/ui/networkmodelloader/NetworkModel.java
+++ b/src/ui/networkmodelloader/NetworkModel.java
@@ -49,6 +49,7 @@ package ui.networkmodelloader;
 
 import javax.swing.*;
 import java.io.File;
+import java.awt.image.*;
 
 
 public class NetworkModel extends JButton  {
@@ -57,6 +58,7 @@ public class NetworkModel extends JButton  {
     public NetworkModelType type;
     public String description;
     public String image;
+    public BufferedImage bi;
 
     public NetworkModel(String _fileName) {
 	super(_fileName);
diff --git a/src/ui/networkmodelloader/NetworkModelPanel.java b/src/ui/networkmodelloader/NetworkModelPanel.java
index b03b6a4c16..9dab8d06ec 100644
--- a/src/ui/networkmodelloader/NetworkModelPanel.java
+++ b/src/ui/networkmodelloader/NetworkModelPanel.java
@@ -49,19 +49,24 @@ 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.*;
+
 
 
 public class NetworkModelPanel extends JPanel  {
 
+    private static int ImgSizeX = 220;
+    private static int ImgSizeY = 120;
+    
     private static int buttonSizeX = 250;
     private static int buttonSizeY = 150;
     private static int spaceBetweenButtons = 50;
-    private static int nbOfButtonsPerColumn = 3;
+    private static int nbOfButtonsPerColumn = 2;
 
     private ArrayList<NetworkModel> listOfModels;
     private ActionListener listener;
@@ -70,10 +75,10 @@ public class NetworkModelPanel extends JPanel  {
 	listOfModels = _listOfModels;
 	listener = _listener;
 	
-	Dimension pSize = new Dimension(500, 400);
-        Dimension mSize = new Dimension(200, 100);
+	//Dimension pSize = new Dimension(500, 400);
+        Dimension mSize = new Dimension(400, 300);
 
-        setPreferredSize(pSize);
+        //setPreferredSize(pSize);
 	setMinimumSize(mSize);
 	setBackground(new java.awt.Color(250, 250, 250));
 	setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
@@ -82,10 +87,29 @@ public class NetworkModelPanel extends JPanel  {
     public void addPanelWithButtons() {
 	int cptColumn = 0;
 	int cptRow = 0;
-	for(NetworkModel  button: listOfModels) {
-	    button.setBounds(cptColumn * (buttonSizeX + spaceBetweenButtons), cptRow * (buttonSizeY + spaceBetweenButtons), buttonSizeX, buttonSizeY);
+	for(NetworkModel button: listOfModels) {
+	    int tmpX = cptColumn * (buttonSizeX + spaceBetweenButtons);
+	    int tmpY = cptRow * (buttonSizeY + spaceBetweenButtons);
+	    TraceManager.addDev("Adding button at x=" + tmpX + "& y=" + tmpY);
+	    button.setBounds(tmpX, tmpY, buttonSizeX, buttonSizeY);
+	    if (button.description != null) {
+		button.setToolTipText(button.description);
+	    }
+
+	    if (button.bi != null) {
+		TraceManager.addDev("Adding image");
+		
+		/*BufferedImage newImage = new BufferedImage(ImgSizeX, ImgSizeY, button.bi.getType());
+		Graphics g = newImage.createGraphics();
+		g.drawImage(button.bi, 0, 0, ImgSizeX, ImgSizeY, null);
+		g.dispose();*/
+		button.setIcon(new ImageIcon(ImageManager.getScaledImage(button.bi, ImgSizeX, ImgSizeY)));
+	    }
+	    
 	    Dimension d = new Dimension(buttonSizeX, buttonSizeY);
 	    button.setPreferredSize(d);
+	    //button.setBorder(BorderFactory.createEmptyBorder());
+	    //button.setContentAreaFilled(false);
 	    add(button);
 	    cptColumn ++;
 	    if (cptColumn == nbOfButtonsPerColumn) {
-- 
GitLab