Browse Source

DnD Object creation

Kevin Trometer 8 years ago
parent
commit
9442fb2787

BIN
bin/Interfaces/CategoryListener.class


BIN
bin/Interfaces/ComparableObject.class


BIN
bin/Interfaces/ObjectListener.class


BIN
bin/classes/Category.class


BIN
bin/classes/CpsObject.class


BIN
bin/classes/HolonElement.class


BIN
bin/classes/HolonObject.class


BIN
bin/classes/HolonSwitch.class


BIN
bin/classes/HolonTransformer.class


BIN
bin/classes/Position.class


BIN
bin/exceptions/newException1.class


BIN
bin/tests/Tests1.class


BIN
bin/tests/praktikumHolonsTestMinimal.class


BIN
bin/ui/controller/CategoryController.class


BIN
bin/ui/controller/Control$command.class


BIN
bin/ui/controller/Control.class


BIN
bin/ui/controller/ObjectControl.class


BIN
bin/ui/model/IdCounter.class


BIN
bin/ui/model/Model.class


BIN
bin/ui/model/tests.class


BIN
bin/ui/view/AddElementPopUp.class


BIN
bin/ui/view/AddObjectPopUp.class


BIN
bin/ui/view/GUI$1.class


BIN
bin/ui/view/GUI$2.class


BIN
bin/ui/view/GUI$3.class


BIN
bin/ui/view/GUI$4.class


BIN
bin/ui/view/GUI$5.class


BIN
bin/ui/view/GUI$6.class


BIN
bin/ui/view/GUI.class


BIN
bin/ui/view/Main$1.class


BIN
bin/ui/view/Main.class


BIN
bin/ui/view/MyCanvas.class


+ 4 - 0
src/classes/HolonObject.java

@@ -26,6 +26,10 @@ public class HolonObject extends CpsObject {
 		super(ObjName);
 	}
 	
+	public HolonObject(HolonObject obj) {
+		super(obj.objName);
+	}
+	
 	public void addConsumer(HolonElement consumer){
 		consumers.add(consumer);
 	}

+ 6 - 0
src/classes/HolonSwitch.java

@@ -9,6 +9,12 @@ public class HolonSwitch extends CpsObject {
 		isWorking = false;
 	}
 	
+	public HolonSwitch(HolonObject obj) {
+		super(obj.objName);
+		isWorking = false;
+	}
+	
+	
 	public void switchState() {
 		this.isWorking = !isWorking;
 	}

+ 5 - 0
src/classes/HolonTransformer.java

@@ -12,6 +12,11 @@ public class HolonTransformer extends CpsObject {
 	public HolonTransformer(String ObjName) {
 		super(ObjName);
 	}
+	
+	public HolonTransformer(HolonObject obj) {
+		super(obj.objName);
+	}
+	
 
 	/**
 	 * Set transform ratio

+ 4 - 0
src/ui/controller/CategoryController.java

@@ -36,9 +36,13 @@ public class CategoryController {
 		Category component = new Category("Component");
 
 		HolonObject powerp = new HolonObject("Power Plant");
+		powerp.setImage("/Images/Dummy_House.png");
 		HolonObject house = new HolonObject("House");
+		house.setImage("/Images/Dummy_House.png");
 		HolonTransformer transformer = new HolonTransformer("Transformer");
+		transformer.setImage("/Images/Dummy_House.png");
 		HolonSwitch sw = new HolonSwitch("Switch");
+		sw.setImage("/Images/Dummy_House.png");
 
 		addObject(energy, powerp);
 		addObject(building, house);

+ 25 - 28
src/ui/view/GUI.java

@@ -53,6 +53,8 @@ import javax.swing.ImageIcon;
 import classes.Category;
 import classes.CpsObject;
 import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.HolonTransformer;
 import Interfaces.CategoryListener;
 
 import java.awt.event.ActionListener;
@@ -102,7 +104,12 @@ public class GUI implements CategoryListener {
 	private final JToolBar toolBar = new JToolBar();
 
 	// variables
-	boolean dragging = false;
+	private boolean dragging = false;
+	private Image img = null;
+	private CpsObject tempCps = null;
+	private HolonObject tempHol = null;
+	private HolonSwitch tempSw = null;
+	private HolonTransformer tempTrans = null;
 
 	private final MyCanvas canvas;
 
@@ -190,9 +197,8 @@ public class GUI implements CategoryListener {
 		splitPane_2.setDividerLocation(200);
 
 		splitPane.setLeftComponent(scrollPane_1);
-		final Image img = new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-				.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
-		Icon icon = new ImageIcon(img);
+		
+		Icon icon = new ImageIcon(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage().getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH));
 		DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
 		renderer.setLeafIcon(icon);
 		// renderer.setClosedIcon(icon);
@@ -204,7 +210,7 @@ public class GUI implements CategoryListener {
 			public void mouseReleased(MouseEvent e) {
 				try {
 					if (dragging) {
-						HolonObject h = new HolonObject("House");
+						HolonObject h = new HolonObject((HolonObject)tempCps);
 						h.setPos((int) canvas.getMousePosition().getX(), (int) canvas.getMousePosition().getY());
 						controller.addObject(h);
 						canvas.repaint();
@@ -221,33 +227,24 @@ public class GUI implements CategoryListener {
 				try {
 					String name = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
 					//System.out.println(name);
-					if (name.compareTo("Power Plant") == 0 || name.compareTo("House") == 0
-							|| name.compareTo("Switch") == 0 || name.compareTo("Transformer") == 0) {
-						dragging = true;
-						Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),"Image");
-						frmCyberPhysical.setCursor(cursor);
+					boolean found = false;
+					for (Category cat : model.getCategories()) {
+						System.out.println(cat.getName()+":");
+						for (CpsObject cps : cat.getObjects()) {
+							System.out.println(name+" - "+cps.getCompareName());
+							if(name.compareTo(cps.getCompareName())==0 ){
+								tempCps = cps;
+								//found = true;
+								img = new ImageIcon(this.getClass().getResource(tempCps.getImage())).getImage().getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
+								dragging = true;
+								Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),"Image");
+								frmCyberPhysical.setCursor(cursor);
+							 }
+						}
 					}
 				} catch (Exception e2) {}
 			}
 		});
-
-		/*
-		tree.addTreeSelectionListener(new TreeSelectionListener() {
-			@Override
-			public void valueChanged(TreeSelectionEvent e) {
-				try {
-					String name = tree.getLastSelectedPathComponent().toString();
-					System.out.println(name);
-					if (name.compareTo("Power Plant") == 0 || name.compareTo("House") == 0
-							|| name.compareTo("Switch") == 0 || name.compareTo("Transformer") == 0) {
-						dragging = true;
-						Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
-								"red Cursor");
-						frmCyberPhysical.setCursor(cursor);
-					}
-				} catch (Exception e2) {}
-			}
-		});*/
 	
 		scrollPane_1.setViewportView(tree);
 

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

@@ -27,7 +27,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	
 	boolean dragging = false;
 	boolean dropDelete = false;
-	CpsObject tempCPS = null;
+	CpsObject tempCps = null;
 
 	// PopUpMenu
 	JPopupMenu popmenu = new JPopupMenu();
@@ -40,7 +40,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		itemDelete.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				model.getObjectsOnCanvas().remove(tempCPS);
+				model.getObjectsOnCanvas().remove(tempCps);
 				repaint();
 			}
 		});
@@ -88,7 +88,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			int cy = cps.getPos().y;
 			if (x - img.getWidth(null) <= cx && y - img.getHeight(null) <= cy && x + img.getWidth(null) >= cx
 					&& y + img.getHeight(null) >= cy) {
-				tempCPS = cps;
+				tempCps = cps;
 				if (e.getButton() == e.BUTTON1) {
 					dragging = true;
 				} else {
@@ -104,8 +104,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			x = e.getX();
 			y = e.getY();
 			dragging = false;
-			tempCPS.setPos(e.getX() - 15, e.getY() - 15);
-			tempCPS = null;
+			tempCps.setPos(e.getX() - 15, e.getY() - 15);
+			tempCps = null;
 			repaint();
 		}
 		// Rechtsklick Liste
@@ -120,7 +120,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	public void mouseDragged(MouseEvent e) {
 		// TODO Auto-generated method stub
 		if (dragging) {
-			tempCPS.setPos(e.getX() - 15, e.getY() - 15);
+			tempCps.setPos(e.getX() - 15, e.getY() - 15);
 			repaint();
 			System.out.println("drag: " + e.getX() + " " + e.getY());
 		}