Browse Source

Changed resource loading to use Streams.

Also adapted Languages and AddObjectPopUp to use the new system.
I noticed the old implementation of the resource loading in
AddObjectPopUp was inconsistent with the other source files and will
review the code and probably delete unnecessary parts.
Ludwig Tietze 7 years ago
parent
commit
2ed01aa4f4
3 changed files with 32 additions and 11 deletions
  1. 5 2
      src/ui/view/AddObjectPopUp.java
  2. 2 1
      src/ui/view/Languages.java
  3. 25 8
      src/ui/view/Util.java

+ 5 - 2
src/ui/view/AddObjectPopUp.java

@@ -145,12 +145,15 @@ public class AddObjectPopUp extends JDialog {
 				}
 				}
 			});
 			});
 			if (edit) {
 			if (edit) {
-				selectedFile = Util.loadFile(this,obj.getImage());
+				//TODO: check why this needs a file?
+				/*
+				selectedFile = Util.loadStream(this,obj.getImage());
 				filePath = selectedFile.getAbsolutePath();
 				filePath = selectedFile.getAbsolutePath();
 				sourcePath.setText(filePath);
 				sourcePath.setText(filePath);
 				ImageIcon icon = new ImageIcon(
 				ImageIcon icon = new ImageIcon(
 						new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
 						new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
-				lblImagePreview.setIcon(icon);
+						*/
+				lblImagePreview.setIcon(new ImageIcon(Util.loadImage(this, obj.getImage(), 50, 50)));
 			}
 			}
 			sourcePath.setBounds(148, 77, 271, 20);
 			sourcePath.setBounds(148, 77, 271, 20);
 			contentPanel.add(sourcePath);
 			contentPanel.add(sourcePath);

+ 2 - 1
src/ui/view/Languages.java

@@ -2,6 +2,7 @@ package ui.view;
 import java.io.BufferedReader;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.nio.charset.Charset;
 import java.nio.charset.Charset;
 
 
 public class Languages {
 public class Languages {
@@ -114,7 +115,7 @@ public class Languages {
 		String[] langArr;
 		String[] langArr;
 		try {
 		try {
 			//read File from Jar
 			//read File from Jar
-			FileReader reader = new FileReader(Util.loadFile(Languages.class.getClassLoader(),path));
+			InputStreamReader reader = new InputStreamReader(Util.loadStream(Languages.class.getClassLoader(),path));
 			BufferedReader br = new BufferedReader(reader);
 			BufferedReader br = new BufferedReader(reader);
 			
 			
 			//store Lines in Array
 			//store Lines in Array

+ 25 - 8
src/ui/view/Util.java

@@ -3,7 +3,10 @@ package ui.view;
 import java.awt.Image;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.IOException;
+import java.io.InputStream;
 
 
 import javax.imageio.ImageIO;
 import javax.imageio.ImageIO;
 
 
@@ -47,7 +50,7 @@ public class Util {
 	
 	
 	static Image loadImage(Object origin, String url){
 	static Image loadImage(Object origin, String url){
 		try {
 		try {
-			return ImageIO.read(loadFile(origin, url));
+			return ImageIO.read(loadStream(origin, url));
 		} catch (IOException e) {
 		} catch (IOException e) {
 			System.err.println("Warning: '" + url + "' loading failed!");
 			System.err.println("Warning: '" + url + "' loading failed!");
 			e.printStackTrace();
 			e.printStackTrace();
@@ -55,15 +58,29 @@ public class Util {
 		}
 		}
 	}
 	}
 	
 	
-	static File loadFile(Object origin, String url){
-		try{
-			return new File(origin.getClass().getResource(url).getFile());
-		}catch(Exception e){
-			boolean rootSymbol=false;//Whether url starts with a / or \
-			switch(url.charAt(0)){
+	static InputStream loadStream(Object origin, String url){
+
+		InputStream o=origin.getClass().getResourceAsStream(url);
+		if(o!=null)return o;
+		else{
+			System.out.println("Loading of resource \""+url+"\" as stream failed.\nWill attempt to load as File.");
+			boolean rootSymbol=false;	//Whether url starts with a / or \
+			switch(url.charAt(0)){		//So we can make sure to consttruct res/path correctly.
 			case '/':case '\\':rootSymbol=true;
 			case '/':case '\\':rootSymbol=true;
 			}
 			}
-			return new File("res"+(rootSymbol?"":"/")+url);
+			try {
+				return new FileInputStream("res"+(rootSymbol?"":"/")+url);
+			} catch (FileNotFoundException e1) {
+				e1.printStackTrace();
+			}
+			System.out.println("Test");
+			try {
+				Thread.sleep(1000);
+			} catch (InterruptedException e1) {
+				// TODO Auto-generated catch block
+				e1.printStackTrace();
+			}
+			return null;
 		}
 		}
 	}
 	}
 }
 }