Browse Source

Merge branch 'LWR' into BP_master

# Conflicts:
#	src/ui/view/Languages.java

Also ultimately removed all traces of old resource loading.
Modified Languages.java to use the new loading system,
making it work in Eclipse and gradlew alike.
Ludwig Tietze 7 years ago
parent
commit
b7e102cb9c

+ 2 - 3
src/ui/view/AboutUsPopUp.java

@@ -51,9 +51,8 @@ public class AboutUsPopUp extends JFrame {
         contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
         contentPanel2.setLayout(new BoxLayout(contentPanel2, BoxLayout.Y_AXIS));
         contentPanel3.setLayout(new BoxLayout(contentPanel3, BoxLayout.Y_AXIS));
-
-        this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-                .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
+        
+        this.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         setBounds(100, 100, 500, 800);
         setLocationRelativeTo(parentFrame);

+ 2 - 2
src/ui/view/AbstractCanvas.java

@@ -160,7 +160,7 @@ public abstract class AbstractCanvas extends JPanel {
         // node image
         if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
                 || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
-            img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
+        	img = Util.loadImage(this, "/Images/node_selected.png");
         } else {
             if (cps instanceof HolonSwitch) {
                 if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
@@ -203,7 +203,7 @@ public abstract class AbstractCanvas extends JPanel {
             if (checkPath.exists()) {
                 img = new ImageIcon(cps.getImage()).getImage();
             } else {
-                img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
+            	img = Util.loadImage(this, cps.getImage());
             }
         }
     }

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

@@ -50,8 +50,7 @@ public class AddElementPopUp extends JDialog {
 	 */
 	AddElementPopUp(JFrame parentFrame) {
 		super((java.awt.Frame) null, true);
-		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
+		this.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
 		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
 		setBounds(100, 100, 400, 190);
 		setLocationRelativeTo(parentFrame);

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

@@ -71,8 +71,7 @@ public class AddObjectPopUp extends JDialog {
     AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
         toEdit = obj;
 		editState = edit;
-		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
+		this.setIconImage(Util.loadImage(this, "/Images/Dummy_House.png",30,30));
 		setBounds(100, 100, 450, 342);
         setLocationRelativeTo(parentFrame);
         getContentPane().setLayout(new BorderLayout());
@@ -146,12 +145,7 @@ public class AddObjectPopUp extends JDialog {
 				}
 			});
 			if (edit) {
-				File checkPath = new File(obj.getImage());
-				if (checkPath.exists()) {
-					selectedFile = new File(obj.getImage());
-				} else {
-					selectedFile = new File(this.getClass().getResource(obj.getImage()).getFile());
-				}
+				selectedFile = Util.loadFile(this,obj.getImage());
 				filePath = selectedFile.getAbsolutePath();
 				sourcePath.setText(filePath);
 				ImageIcon icon = new ImageIcon(

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

@@ -64,8 +64,7 @@ public class BackgroundPopUp extends JDialog {
 			imageWidth.setText("" + icon.getIconWidth());
 			imageHeight.setText("" + icon.getIconHeight());
 		}
-		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
+		this.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
 		setBounds(100, 100, 600, 340);
         setLocationRelativeTo(parentFrame);
 

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

@@ -38,8 +38,7 @@ public class CanvasResizePopUp extends JDialog {
 		this.canvas = canvas;
 
 		// properties and stuff
-		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
+		this.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
 		this.setTitle("Set the Size of the View");
 		setBounds(200, 100, 200, 100);
         setLocationRelativeTo(parentFrame);

+ 5 - 14
src/ui/view/GUI.java

@@ -560,9 +560,8 @@ public class GUI implements CategoryListener {
 
         frmCyberPhysical.setJMenuBar(menuBar);
 
-        frmCyberPhysical.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-                .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
-
+        frmCyberPhysical.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
+        		
         menuBar.add(mnNewMenu);
 
         mnNewMenu.add(mntmNew);
@@ -1246,14 +1245,7 @@ public class GUI implements CategoryListener {
                     for (Category cat : model.getCategories()) {
                         for (AbstractCpsObject cps : cat.getObjects()) {
                             if (value.toString().compareTo(cps.getObjName()) == 0) {
-                                File checkPath = new File(cps.getImage());
-                                if (checkPath.exists()) {
-                                    imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
-                                            java.awt.Image.SCALE_SMOOTH);
-                                } else {
-                                    imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
-                                            .getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
-                                }
+                                imgR = Util.loadImage(this,cps.getImage(),50,50);
                                 if (imgR != null) {
                                     label.setIcon(new ImageIcon(imgR));
                                 }
@@ -1270,7 +1262,7 @@ public class GUI implements CategoryListener {
                 if (label.getText().length() == 0) {
                     label.setText(value.toString());
                     if (value.toString().compareTo("Categories") != 0) {
-                        label.setIcon(new ImageIcon(this.getClass().getResource("/Images/folder.png")));
+                    	label.setIcon(new ImageIcon(Util.loadImage(this, "/Images/folder.png")));
                     }
                 }
 
@@ -1394,8 +1386,7 @@ public class GUI implements CategoryListener {
                                         img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(32, 32,
                                                 java.awt.Image.SCALE_SMOOTH);
                                     } else {
-                                        img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
-                                                .getScaledInstance(32, 32, java.awt.Image.SCALE_SMOOTH);
+                                    	img = Util.loadImage(this,cps.getImage(),32,32);
                                     }
                                     tempCps = cps;
                                     dragging = true;

+ 9 - 9
src/ui/view/Languages.java

@@ -1,14 +1,14 @@
 package ui.view;
-
+
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
-
+
 public class Languages {
 
 	static int languageVar = 0;
-
+
 	// Language Array Positions:
 	// ToolBar (0-16)
 	// Tables (17-24)
@@ -31,7 +31,7 @@ public class Languages {
 	 * Number of different Language Array Positions
 	 */
 	private static int languageSize = 104;
-
+
 	static String[] arrayEN = {
 			// ToolBar (0-16)
 			"File", "New", "Open", "Save", "Edit", "Undo", "Redo", "Find/Replace", "Edit showed Information", "Options",
@@ -73,7 +73,7 @@ public class Languages {
 			"Image Size",
 			// Menu - right click on HolonObjt (95-102)
 			"Cut", "Copy", "Paste", "Delete", "Group", "Ungroup", "Track", "Untrack"};
-
+
 	static String[] arrayES = readLanguageFile("Texts"+File.separator+"ES.lang"); 
 
 	static String[] arrayDE = readLanguageFile("Texts"+File.separator+"DE.lang"); 
@@ -81,7 +81,7 @@ public class Languages {
 	static String[] arrayCZ = readLanguageFile("Texts"+File.separator+"CZ.lang"); 
 
 	static String[] arrayZH = readLanguageFile("Texts"+File.separator+"ZH.lang");
-	
+	
 	public static String[] getLanguage() {
 		switch (languageVar) {
 		case 0:
@@ -101,7 +101,7 @@ public class Languages {
 
 	public static void setLanguage(int i) {
 		languageVar = i;
-	}
+	}
 	
 	/**
 	 * Reads language file from jar, each line corresponds to
@@ -115,7 +115,7 @@ public class Languages {
 		String[] langArr;
 		try {
 			//read File from Jar
-			FileReader reader = new FileReader(new File(Languages.class.getClassLoader().getResource(path).getFile()));
+			FileReader reader = new FileReader(Util.loadFile(Languages.class.getClassLoader(),path));
 			BufferedReader br = new BufferedReader(reader);
 			
 			//store Lines in Array
@@ -139,5 +139,5 @@ public class Languages {
 			//default: English
 			return arrayEN;
 		}
-	}
+	}
 }

+ 7 - 12
src/ui/view/TimePanel.java

@@ -138,24 +138,22 @@ public class TimePanel extends JPanel {
 		playBtn.setContentAreaFilled(false);
 		playBtn.setBorderPainted(false);
 		playBtn.setBorder(null);
-		playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/play.png")).getImage()
-				.getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
+		playBtn.setIcon(new ImageIcon(Util.loadImage(this, "/Button_Images/play.png",30,30)));
 		playBtn.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				running = !running;
+				playBtn.setIcon(new ImageIcon(Util.loadImage(this,"/Button_Images/pause.png",30,30)));//relocated this here. as it was duplicate before.
 				if (running) {
 					if (!model.getIsSimRunning()) {
 						controller.setIsSimRunning(true);
 					}
 					timer.start();
 					timer.setDelay(model.getTimerSpeed());
-					playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/pause.png"))
-							.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
+					//playBtn.setIcon(new ImageIcon(Util.loadImage(this,"/Button_Images/pause.png",30,30)));
 				} else {
 					timer.stop();
-					playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/play.png"))
-							.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
+					//playBtn.setIcon(new ImageIcon(Util.loadImage(this,"/Button_Images/pause.png",30,30)));
 				}
 			}
 		});
@@ -163,8 +161,7 @@ public class TimePanel extends JPanel {
 
 		timeResetBtn.setContentAreaFilled(false);
 		timeResetBtn.setBorder(null);
-		timeResetBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/reset.png"))
-				.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
+		timeResetBtn.setIcon(new ImageIcon(Util.loadImage(this, "/Button_Images/reset.png", 30, 30)));
 		timeResetBtn.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent ae) {
 				timeSlider.setValue(timeSlider.getMinimum());
@@ -180,8 +177,7 @@ public class TimePanel extends JPanel {
 
 		timeForwardBtn.setContentAreaFilled(false);
 		timeForwardBtn.setBorder(null);
-		timeForwardBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/forward.png"))
-				.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
+		timeForwardBtn.setIcon(new ImageIcon(Util.loadImage(this,"/Button_Images/forward.png",30,30)));
 		timeForwardBtn.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent ae) {
 				if (!model.getIsSimRunning()) {
@@ -194,8 +190,7 @@ public class TimePanel extends JPanel {
 		timeBackwardBtn.setToolTipText(Languages.getLanguage()[92]);
 
 		timeBackwardBtn.setBorder(null);
-		timeBackwardBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/backward.png"))
-				.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
+		timeBackwardBtn.setIcon(new ImageIcon(Util.loadImage(this, "/Button_Images/backward.png", 30,30)));
 		timeBackwardBtn.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent ae) {
 				timeSlider.setValue(timeSlider.getValue() - 1);

+ 4 - 2
src/ui/view/UpperNodeCanvas.java

@@ -464,7 +464,8 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
             // node image
             if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
                     || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
-                img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
+                //img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();//TODO: ONE MORE
+            	img = Util.loadImage(this,"/Images/node_selected.png");
             } else {
                 if (cps instanceof HolonSwitch) {
                     if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
@@ -492,7 +493,8 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
                 if (checkPath.exists()) {
                     img = new ImageIcon(cps.getImage()).getImage();
                 } else {
-                    img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
+                    //img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();//TODO: and I'm done
+                	img = Util.loadImage(this,cps.getImage());
                 }
             }
             g2.drawImage(img, (upperNode.getLeftBorder() >> 1) - 25,

+ 36 - 0
src/ui/view/Util.java

@@ -0,0 +1,36 @@
+package ui.view;
+
+import java.awt.Image;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+public class Util {
+	
+	static Image loadImage(Object origin, String url, int w, int h, int scale){
+		return loadImage(origin,url).getScaledInstance(w, h, scale);
+	}
+	static Image loadImage(Object origin, String url, int w, int h){
+		return loadImage(origin,url,w,h, Image.SCALE_SMOOTH);
+	}
+	static Image loadImage(Object origin, String url){
+		try {
+			return ImageIO.read(loadFile(origin, url));
+		} catch (IOException e) {
+			e.printStackTrace();
+			return null;
+		}
+	}
+	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)){
+			case '/':case '\\':rootSymbol=true;
+			}
+			return new File("res"+(rootSymbol?"":"/")+url);
+		}
+	}
+}