Jelajahi Sumber

Merge branch 'Ohne_Drag_and_Drop' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons.git into Ohne_Drag_and_Drop

Kevin Trometer 7 tahun lalu
induk
melakukan
b532cf3ad9

+ 5 - 0
src/ui/controller/Control.java

@@ -11,6 +11,7 @@ import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
 import ui.model.Model;
+import ui.view.MyCanvas;
 
 public class Control {
 
@@ -159,6 +160,10 @@ public class Control {
 		simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(x);
 	}
+	
+	public void setCanvas(MyCanvas can){
+		simulationManager.setCanvas(can);
+	}
 
 	/**
 	 * Getter for Model

+ 8 - 1
src/ui/controller/SimulationManager.java

@@ -8,13 +8,16 @@ import classes.CpsObject;
 import classes.HolonObject;
 import classes.subNet;
 import ui.model.Model;
+import ui.view.MyCanvas;
 
 public class SimulationManager {
 	private Model model;
 	private ArrayList<CpsObject> objectsToHandle;
 	private ArrayList<subNet> subNets;
+	private MyCanvas canvas;
 	
 	public SimulationManager(Model m){
+		canvas = null;
 		model = m;
 		subNets = new ArrayList<subNet>();
 	}
@@ -49,7 +52,7 @@ public class SimulationManager {
 				System.out.println(hl.getName() + "is supplied:" + hl.getSupplied());
 			}
 		}
-		
+		canvas.repaint();
 	}
 	
 	/**
@@ -180,6 +183,10 @@ public class SimulationManager {
 		}
 	}
 	
+	public void setCanvas(MyCanvas can){
+		canvas = can;
+	}
+	
 	public void reset(){
 		copyObjects(model.getObjectsOnCanvas());
 	}

+ 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");
 		}

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

@@ -188,10 +188,10 @@ public class GUI<E> implements CategoryListener {
 		this.model = control.getModel();
 		this.canvas = new MyCanvas(model, control);
 		this.unitGraph = new UnitGraph(model, control);
-
 		control.initListener(this);
 		initialize();
 		updateCategories(model.getCategories());
+		controller.setCanvas(canvas);
 	}
 
 	/**
@@ -1150,6 +1150,7 @@ public class GUI<E> implements CategoryListener {
 			private void showMenu(MouseEvent e) {
 				popup.show(e.getComponent(), e.getX(), e.getY());
 			}
+			
 		});
 	}
 

+ 1 - 1
src/ui/view/MyCanvas.java

@@ -35,7 +35,7 @@ import ui.controller.Control;
 import ui.model.Model;
 import ui.model.idCounter;
 
-class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
+public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	/**
 	 * 
 	 */