Browse Source

Merge remote-tracking branch 'origin/LWR' into BP_master

Andreas T. Meyer-Berg 7 years ago
parent
commit
33891701e8
3 changed files with 30 additions and 11 deletions
  1. 5 2
      src/ui/view/AddObjectPopUp.java
  2. 2 1
      src/ui/view/Languages.java
  3. 23 8
      src/ui/view/Util.java

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

@@ -145,12 +145,15 @@ public class AddObjectPopUp extends JDialog {
 				}
 			});
 			if (edit) {
-				selectedFile = Util.loadFile(this,obj.getImage());
+				//TODO: check why this needs a file?
+				/*
+				selectedFile = Util.loadStream(this,obj.getImage());
 				filePath = selectedFile.getAbsolutePath();
 				sourcePath.setText(filePath);
 				ImageIcon icon = new ImageIcon(
 						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);
 			contentPanel.add(sourcePath);

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

@@ -2,6 +2,7 @@ package ui.view;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.nio.charset.Charset;
 
 public class Languages {
@@ -114,7 +115,7 @@ public class Languages {
 		String[] langArr;
 		try {
 			//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);
 			
 			//store Lines in Array

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

@@ -3,11 +3,15 @@ package ui.view;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 
 import javax.imageio.ImageIO;
 
 public class Util {
+	
 	/**
 	 * 30x30 pixel FileNotFound Icon, which can be shown without using I/O operations
 	 */
@@ -47,7 +51,7 @@ public class Util {
 	
 	static Image loadImage(Object origin, String url){
 		try {
-			return ImageIO.read(loadFile(origin, url));
+			return ImageIO.read(loadStream(origin, url));
 		} catch (IOException e) {
 			System.err.println("Warning: '" + url + "' loading failed!");
 			e.printStackTrace();
@@ -55,15 +59,26 @@ 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 \""+url+"\" as local resource 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 construct res/path correctly.
 			case '/':case '\\':rootSymbol=true;
 			}
-			return new File("res"+(rootSymbol?"":"/")+url);
+			try {
+				//I am checking, whether the requested File is an internal resource.
+				//If it is, res/url has to be used, otherwise url.
+				//Mixed separators might cause problems. Will fix later.
+				File f=new File(url);
+				if(!f.exists()/*||!f.getAbsolutePath().equals(url)*/)url="res"+(rootSymbol?"":"/")+url;
+				return new FileInputStream(url);
+			} catch (FileNotFoundException e1) {
+				e1.printStackTrace();
+			}
+			return null;
 		}
 	}
 }