diff --git a/src/main/java/edu/mit/blocks/workspace/ContextMenu.java b/src/main/java/edu/mit/blocks/workspace/ContextMenu.java index 0e3ef73a6558921c185a67a324193ee16cf6ac47..14cb8baee9b87ed8e6d39186b5f0dadc0004a663 100644 --- a/src/main/java/edu/mit/blocks/workspace/ContextMenu.java +++ b/src/main/java/edu/mit/blocks/workspace/ContextMenu.java @@ -178,19 +178,32 @@ public class ContextMenu extends PopupMenu implements ActionListener { * @throws URISyntaxException */ private void createReferenceWindow(String blockGenusName) { + File cssFile = null; + File indexFile = null; File htmlFile = null; File imageFile = null; + File logoFile = null; + File exampleFile = null; + File example2File = null; - String htmlResource ="/com/ardublock/reference/"+blockGenusName+".html"; - String imageResource ="/com/ardublock/reference/"+blockGenusName+".png"; + String cssResource ="/com/ardublock/reference/_seitenformatierung.css"; + String indexResource ="/com/ardublock/reference/_inhaltsverzeichnis.html"; + String htmlResource ="/com/ardublock/reference/"+blockGenusName+".html"; + String logoResource ="/com/ardublock/reference/_Logo_LGI_page.png"; + String imageResource ="/com/ardublock/reference/"+blockGenusName+".png"; + String exampleResource ="/com/ardublock/reference/"+blockGenusName+"-beispiel.png"; + //String example2Resource ="/com/ardublock/reference/"+blockGenusName+"-beispiel-2.png"; + String example2Resource ="/com/ardublock/reference/"+blockGenusName+"-beispiel-"; + String exampleFileType = ".png"; String fileNotFoundResource = "/com/ardublock/reference/404.png"; String tempPath = ""; - - //TODO: REMOVE PRINT AFTER BLOCKREFERENCE IS DONE + /* System.out.println("REMOVE PRINT in ContextMenue.java after BlockRef is done"); System.out.println("Blockname HTML: "+ blockGenusName + ".html"); System.out.println("Blockname Bild: "+ blockGenusName + ".png"); + System.out.println("Blockname Bild: "+ blockGenusName + "-beispiel.png"); + */ //get current .jar path for temp files try { @@ -200,7 +213,30 @@ public class ContextMenu extends PopupMenu implements ActionListener { e1.printStackTrace(); } - //copy html to tmp file + //copy css File to tmp files + try { + InputStream input = getClass().getResourceAsStream(cssResource); + if(input != null) { + cssFile = new File(tempPath +"/"+ "_seitenformatierung.css"); + OutputStream out = new FileOutputStream(cssFile); + int read; + byte[] bytes = new byte[1024]; + while ((read = input.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + out.close(); + cssFile.deleteOnExit(); + } else { + //TODO: use resources + System.out.println("Sorry, css stylesheet was not found in reference."); + //TODO: open 404-page + //!!!!!!!!!!!!!!!!!!!!!!!!!!!! + } + } catch (IOException ex) { + ex.printStackTrace(); + } + + //copy html to tmp files try { InputStream input = getClass().getResourceAsStream(htmlResource); if(input != null) { @@ -215,12 +251,36 @@ public class ContextMenu extends PopupMenu implements ActionListener { htmlFile.deleteOnExit(); } else { //TODO: use resources - System.out.println("Sorry, block was not found in reference."); + System.out.println("Sorry, block "+blockGenusName+" was not found in reference."); + //TODO: open 404-page + //!!!!!!!!!!!!!!!!!!!!!!!!!!!! } } catch (IOException ex) { ex.printStackTrace(); } - //copy image to tmp file + + //copy logo to tmp files + try { + InputStream input = getClass().getResourceAsStream(logoResource); + if(input != null) { + logoFile = new File(tempPath +"/_Logo_LGI_page.png"); + OutputStream out = new FileOutputStream(logoFile); + int read; + byte[] bytes = new byte[10000]; + while ((read = input.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + out.close(); + logoFile.deleteOnExit(); + } else { + //TODO: use resources + System.out.println("Sorry, logo image was not found."); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + + //copy block-image to tmp files try { InputStream input = getClass().getResourceAsStream(imageResource); if(input != null) { @@ -235,21 +295,72 @@ public class ContextMenu extends PopupMenu implements ActionListener { imageFile.deleteOnExit(); } else { //TODO: use resources - System.out.println("Sorry, image was not found in reference."); + System.out.println("Sorry, block image for "+blockGenusName+" was not found in reference."); } } catch (IOException ex) { ex.printStackTrace(); } + + //copy example-image to tmp files + try { + InputStream input = getClass().getResourceAsStream(exampleResource); + if(input != null) { + exampleFile = new File(tempPath +"/"+ blockGenusName+"-beispiel.png"); + OutputStream out = new FileOutputStream(exampleFile); + int read; + byte[] bytes = new byte[10000]; + while ((read = input.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + out.close(); + exampleFile.deleteOnExit(); + } else { + //TODO: use resources + System.out.println("Sorry, example for "+blockGenusName+" was not found in reference."); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + + //copy optional second example-image to tmp files + //TODO: add number to all exampleImages -> starting with 1 + //TODO: or use just one example image + try { + boolean allFilesRead = false; + int exampleImageCounter = 2; + + while(!allFilesRead) { + InputStream input = getClass().getResourceAsStream(example2Resource + exampleImageCounter + exampleFileType); + if(input != null) { + example2File = new File(tempPath +"/"+ blockGenusName+"-beispiel-"+exampleImageCounter+".png"); + OutputStream out = new FileOutputStream(example2File); + int read; + byte[] bytes = new byte[10000]; + while ((read = input.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + out.close(); + example2File.deleteOnExit(); + exampleImageCounter++; + } else { + allFilesRead = true; + //TODO: use resources + //System.out.println("Sorry, block was not found in reference."); + } + } + } 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!"); + if (htmlFile != null && !htmlFile.exists() && imageFile != null && !imageFile.exists() && !logoFile.exists() && !exampleFile.exists()) { + throw new RuntimeException("Error: Resource not found!"); }else { try { Desktop.getDesktop().browse(htmlFile.toURI()); } catch (IOException e) { // TODO Auto-generated catch block - e.printStackTrace(); + //e.printStackTrace(); } catch (NullPointerException e) { // TODO Auto-generated catch block //e.printStackTrace();