Browse Source

"edit with same image" fix

Jessey Widhalm 7 years ago
parent
commit
126402253f
1 changed files with 31 additions and 11 deletions
  1. 31 11
      src/ui/view/AddObjectPopUp.java

+ 31 - 11
src/ui/view/AddObjectPopUp.java

@@ -57,6 +57,7 @@ public class AddObjectPopUp extends JDialog {
 	private String givenCategory;
 	private JLabel lblImagePreview;
 	private CpsObject toEdit;
+	private boolean imageChanged = false;
 
 	/**
 	 * Launch the application.
@@ -129,6 +130,11 @@ public class AddObjectPopUp extends JDialog {
 			});
 
 		}
+		{
+			lblImagePreview = new JLabel("");
+			lblImagePreview.setBounds(295, 3, 50, 50);
+			contentPanel.add(lblImagePreview);
+		}
 		{
 			sourcePath = new JTextField();
 			sourcePath.addKeyListener(new KeyListener() {
@@ -150,13 +156,27 @@ public class AddObjectPopUp extends JDialog {
 				}
 			});
 			if (edit) {
-				sourcePath.setText(obj.getImage());
-				if (sourcePath.getText().startsWith("/Images/")) {
-					sourcePath
-							.setText(System.getProperty("user.home") + "/praktikum-holons/res/" + sourcePath.getText());
+				File checkPath = new File(obj.getImage());
+				if (checkPath.exists()) {
+					selectedFile = new File(obj.getImage());
+				} else {
+					selectedFile = new File(this.getClass().getResource(obj.getImage()).getFile());
 				}
-				filePath = sourcePath.getText();
-				selectedFile = new File(sourcePath.getText());
+				filePath = selectedFile.getAbsolutePath();
+				sourcePath.setText(filePath);
+				ImageIcon icon = new ImageIcon(
+						new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
+				if(icon != null)
+				lblImagePreview.setIcon(icon);
+			
+				
+//				sourcePath.setText(obj.getImage());
+//				if (sourcePath.getText().startsWith("/Images/")) {
+//					sourcePath
+//							.setText(System.getProperty("user.home") + "/praktikum-holons/res/" + sourcePath.getText());
+//				}
+//				filePath = sourcePath.getText();
+//				selectedFile = new File(sourcePath.getText());
 			}
 			sourcePath.setBounds(148, 77, 271, 20);
 			contentPanel.add(sourcePath);
@@ -212,11 +232,7 @@ public class AddObjectPopUp extends JDialog {
 			btnNewButton.setBounds(270, 182, 142, 27);
 			contentPanel.add(btnNewButton);
 		}
-		{
-			lblImagePreview = new JLabel("");
-			lblImagePreview.setBounds(295, 3, 50, 50);
-			contentPanel.add(lblImagePreview);
-		}
+		
 		{
 			JPanel buttonPane = new JPanel();
 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -228,7 +244,10 @@ public class AddObjectPopUp extends JDialog {
 						Component frame = null;
 						if (objectName.getText().length() > 0) {
 							if (sourcePath.getText().equals(filePath)) {
+								imagePath = filePath;
+								if(imageChanged)
 								copieFile();
+								imageChanged = false;
 								// theObject = new
 								// HolonObject(objectName.getText());
 								// theObject.setElements(hElements);
@@ -293,6 +312,7 @@ public class AddObjectPopUp extends JDialog {
 			ImageIcon icon = new ImageIcon(
 					new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
 			lblImagePreview.setIcon(icon);
+			imageChanged = true;
 		} else {
 			System.out.println("Failed to Load");
 		}