From 388cbdd385b579be9b81244062001e9b51d70ade Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Thu, 12 Aug 2021 12:36:29 +0200
Subject: [PATCH] read reference files from jar to temp file and open in
 browser

---
 .../edu/mit/blocks/workspace/ContextMenu.java | 112 +++++++++---------
 1 file changed, 58 insertions(+), 54 deletions(-)

diff --git a/src/main/java/edu/mit/blocks/workspace/ContextMenu.java b/src/main/java/edu/mit/blocks/workspace/ContextMenu.java
index ce95f2b..5bbccd8 100644
--- a/src/main/java/edu/mit/blocks/workspace/ContextMenu.java
+++ b/src/main/java/edu/mit/blocks/workspace/ContextMenu.java
@@ -7,9 +7,15 @@ import java.awt.MenuItem;
 import java.awt.PopupMenu;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URLDecoder;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -188,70 +194,68 @@ public class ContextMenu extends PopupMenu implements ActionListener {
      * in standard browser
      * @param blockGenusName = genus name of the active block
      * added by letsgoING 
+     * @throws URISyntaxException 
      */
     private void createReferenceWindow(String blockGenusName) {
-    	//TODO: Test with reference files
-    	String resourcePath = "/com/ardublock/reference/"+blockGenusName+".html";
+    	File htmlFile = null;
+    	File imageFile = null;
     	
-    	System.out.println("TEST | Active Block: "+blockGenusName);
+    	String htmlResource ="/com/ardublock/reference/"+blockGenusName+".html";
+    	String imageResource ="/com/ardublock/reference/"+blockGenusName+".png";
+    	String tempPath = "";
     	
-    	//TODO: TEST on all plattforms (Linux/MAC/Windows)
-		try {
-			Desktop.getDesktop().browse(getClass().getResource(resourcePath).toURI());
-		} catch (IOException e) {
+    	//get current .jar path for temp files
+    	try {
+			tempPath = new File(URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8")).getParentFile().getPath();
+		} catch (UnsupportedEncodingException e1) {
 			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} catch (URISyntaxException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} catch (NullPointerException e) {
-			//e.printStackTrace();
-			try {
-				Desktop.getDesktop().browse(getClass().getResource("/com/ardublock/reference/404.html").toURI());
-			} catch (IOException e1) {
-				// TODO Auto-generated catch block
-				e1.printStackTrace();
-			} catch (URISyntaxException e1) {
-				// TODO Auto-generated catch block
-				e1.printStackTrace();
-			}
+			e1.printStackTrace();
 		}
-		
-		/*	
-		 //get html path
-		String referenceText = "Block not found!";
-    	Path path = null;
-		try {
-			path = Paths.get(getClass().getResource(resourcePath).toURI());
-		} catch (URISyntaxException e1 ) {
-			// TODO Auto-generated catch block
-			//e1.printStackTrace();
-		} catch (NullPointerException ne) {
-			//ne.printStackTrace();
-		}
-			
-		if(path != null) {
-			try {
-				referenceText = new String(Files.readAllBytes(path));
+    	
+    	//copy html to tmp file
+        try {
+            InputStream input = getClass().getResourceAsStream(htmlResource);
+            htmlFile = new File(tempPath +"/"+ blockGenusName + ".html");
+            OutputStream out = new FileOutputStream(htmlFile);
+            int read;
+            byte[] bytes = new byte[1024];
+    	        while ((read = input.read(bytes)) != -1) {
+   	            out.write(bytes, 0, read);
+   	        }
+   	        out.close();
+   	        htmlFile.deleteOnExit();
+   	        
+   	    } catch (IOException ex) {
+   	        ex.printStackTrace();
+   	    }
+        //copy image to tmp file
+   	    try {
+   	        InputStream input = getClass().getResourceAsStream(imageResource);
+   	        imageFile = new File(tempPath +"/"+ blockGenusName+".png");
+   	        OutputStream out = new FileOutputStream(imageFile);
+   	        int read;
+   	        byte[] bytes = new byte[10000];
+    	        while ((read = input.read(bytes)) != -1) {
+   	            out.write(bytes, 0, read);
+   	        }
+   	        out.close();
+   	        imageFile.deleteOnExit();
+   	           
+   	    } catch (IOException ex) {
+   	        ex.printStackTrace();
+   	    }
+   	
+   	    //open file in standard browser
+    	if (htmlFile != null && !htmlFile.exists() && imageFile != null && !imageFile.exists()) {
+    	    throw new RuntimeException("Error: File " + htmlFile + "or " + imageFile + " not found!");
+    	}else {    	
+	    	try {
+				Desktop.getDesktop().browse(htmlFile.toURI());
 			} catch (IOException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
-		}
-		//OPTION JFRAME
-		Frame frame = new JFrame("Block Reference");
-		JLabel label = new JLabel(referenceText);
-		frame.add(label);		
-		frame.setLocationRelativeTo( null );
-	    frame.pack();
-        frame.setVisible(true);
-        
-    	//OPTION DIALOG
-        JOptionPane.showMessageDialog(
-                null,
-                referenceText,
-                "Block-Referenz - "+((RenderableBlock) activeComponent).getBlock().getGenusName(), JOptionPane.INFORMATION_MESSAGE, null);
-       */
+    	}
         return;
     }
 
-- 
GitLab