瀏覽代碼

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

Teh-Hai Julian Zheng 8 年之前
父節點
當前提交
81abdf99dc

+ 17 - 0
src/ui/controller/SimulationManager.java

@@ -5,6 +5,7 @@ import java.util.HashMap;
 
 import classes.CpsEdge;
 import classes.CpsNode;
+import classes.CpsUpperNode;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
@@ -448,6 +449,12 @@ public class SimulationManager {
 				if (!visited.contains(b.getID()) && legitState(cps)) {
 					sN = buildSubNet(b, visited, sN);
 				}
+				if(a instanceof CpsUpperNode && a.getID() != cps.getID()){
+					checkForConnectedSates(b, (CpsUpperNode)a);
+				}
+				if(b instanceof CpsUpperNode && b.getID() != cps.getID()){
+					checkForConnectedSates(a, (CpsUpperNode)b);
+				}
 			}
 		}
 		return sN;
@@ -556,5 +563,15 @@ public class SimulationManager {
 	public ArrayList<SubNet> getSubNets() {
 		return subNets;
 	}
+	
+	/**
+	 * checks wether a given object is connected to an object inside the upperNode.
+	 * if yes, the state for the edge is changed in "connected" or "not connected"
+	 * @param cps
+	 * @param cUNode
+	 */
+	public void checkForConnectedSates(AbstractCpsObject cps, CpsUpperNode cUNode){
+		for(CpsEdge edge: cps.getConnections());
+	}
 
 }

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

@@ -1879,6 +1879,7 @@ public class GUI<E> implements CategoryListener {
 					((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
 							.getComponent(0)).path + " -> ");
 		}
+		unc.setShowedInformation(canvas.getShowedInformation());
 
 		// check if tab already open for clicked NodeOfNode
 		boolean dupl = false;

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

@@ -118,7 +118,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 
 		showedInformation[0] = true;
 		showedInformation[1] = true;
-		showedInformation[2] = false;
+		showedInformation[3] = false;
 		control.setMaxCapacity(10000);
 
 		popmenu.add(itemCut);
@@ -396,7 +396,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
 						(int) (Math.random() * 255)));
 			}
-			if(showedInformation[2]){
+			if(showedInformation[3]){
 			for (HolonObject cps : s.getObjects()) {
 				cps.setBorderColor(model.getSubNetColors().get(i));
 			}
@@ -504,13 +504,13 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		// Objects
 		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
 			// Border Highlighting
-			if(showedInformation[2]){
-			g2.setColor(cps.getBorderColor());
-			if (g2.getColor() != Color.WHITE) {
-				g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
-						(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
-						(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
-			}
+			if(showedInformation[3]){
+				g2.setColor(cps.getBorderColor());
+				if (g2.getColor() != Color.WHITE) {
+					g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
+							(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
+							(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
+				}
 			}
 			// node image
 			if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
@@ -1004,7 +1004,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	public void setShowedInformation(boolean connection, boolean object, boolean border) {
 		showedInformation[0] = connection;
 		showedInformation[1] = object;
-		showedInformation[2] = border;
+		showedInformation[3] = border;
 	}
 
 	/**

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

@@ -78,7 +78,7 @@ public class ShowedInformationPopUp extends JDialog {
 		colorizedBorderCheckbox = new JCheckBox("Show colorized Border for Objects");
 		colorizedBorderCheckbox.setBounds(19, 96, 195, 23);
 		contentPanel.add(colorizedBorderCheckbox);
-		colorizedBorderCheckbox.setSelected(canvas.getShowedInformation()[2]);
+		colorizedBorderCheckbox.setSelected(canvas.getShowedInformation()[3]);
 	}
 
 	/**

+ 149 - 17
src/ui/view/UpperNodeCanvas.java

@@ -99,6 +99,19 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 
 	private UpdateController updCon;
 
+	javax.swing.Timer animT; // animation Timer
+	private final int ANIMTIME = 500; // animation Time
+
+	private ArrayList<AbstractCpsObject> animCps = null;
+	private int animFPS = 60;
+	private int animDuration = ANIMTIME; // animation Duration
+	private int animDelay = 1000 / animFPS; // animation Delay
+	private int animSteps = animDuration / animDelay; // animation Steps;
+	private long start = 0; // for time
+	private long elapsedTime = 0; // outprint
+	private Position unPos;
+	private ArrayList<Position> savePos;
+
 	/**
 	 * Constructor.
 	 * 
@@ -151,16 +164,121 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		itemGroup.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				controller.addUpperNode("NodeOfNode", upperNode, model.getSelectedCpsObjects());
-				repaint();
+				// calculate uppernode pos (taken from the controller)
+				unPos = new Position(0, 0);
+				animCps = new ArrayList<>();
+				for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
+					animCps.add(cps); // add to animation Cps ArrayList
+					unPos.x += cps.getPosition().x;
+					unPos.y += cps.getPosition().y;
+				}
+				unPos.x /= animCps.size();
+				unPos.y /= animCps.size();
+
+				// save old Position
+				savePos = new ArrayList<>();
+				for (int i = 0; i < animCps.size(); i++) {
+					savePos.add(new Position(0, 0));
+					savePos.get(i).x = animCps.get(i).getPosition().x;
+					savePos.get(i).y = animCps.get(i).getPosition().y;
+				}
+
+				animT = new javax.swing.Timer(animDelay, new ActionListener() {
+
+					@Override
+					public void actionPerformed(ActionEvent e) {
+						if (animDuration - animDelay > 0 && animCps.size() > 1) {
+							for (int i = 0; i < animCps.size(); i++) {
+								double x1 = animCps.get(i).getPosition().x - unPos.x;
+								double y1 = animCps.get(i).getPosition().y - unPos.y;
+								animCps.get(i).getPosition().x -= x1 / animSteps;
+								animCps.get(i).getPosition().y -= y1 / animSteps;
+							}
+							repaint();
+							animDuration -= animDelay;
+							animSteps--;
+						} else {
+							animDuration = ANIMTIME;
+							animSteps = animDuration / animDelay;
+							animT.stop();
+							for (int i = 0; i < animCps.size(); i++) {
+								animCps.get(i).getPosition().x = savePos.get(i).x;
+								animCps.get(i).getPosition().y = savePos.get(i).y;
+							}
+							controller.addUpperNode("NodeOfNode", upperNode, model.getSelectedCpsObjects());
+							controller.calculateStateForCurrentTimeStep();
+							repaint();
+							elapsedTime = System.currentTimeMillis() - start;
+							controller.addTextToConsole(elapsedTime + "");
+						}
+					}
+				});
+				start = System.currentTimeMillis();
+				animT.start();
 			}
 		});
 
 		itemUngroup.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
+				// save old Position
+				JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
+				for (int i = 3; i < tabbedPane.getTabCount(); i++) {
+					if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
+							.getComponent(0)).upperNode.getID() == ((CpsUpperNode) tempCps).getID()) {
+						tabbedPane.remove(i);
+						break;
+					}
+				}
+				
+				savePos = new ArrayList<>();
+				animCps = ((CpsUpperNode) tempCps).getNodes();
 				controller.delUpperNode((CpsUpperNode) tempCps, upperNode);
-				repaint();
+
+				for (int i = 0; i < animCps.size(); i++) {
+					savePos.add(new Position(0, 0));
+					savePos.get(i).x = animCps.get(i).getPosition().x;
+					savePos.get(i).y = animCps.get(i).getPosition().y;
+				}
+				for (AbstractCpsObject cps : animCps) {
+					int x = ((CpsUpperNode) tempCps).getPosition().x;
+					int y = ((CpsUpperNode) tempCps).getPosition().y;
+
+					cps.setPosition(new Position(x, y));
+				}
+
+				animT = new javax.swing.Timer(animDelay, new ActionListener() {
+
+					@Override
+					public void actionPerformed(ActionEvent e) {
+						if (animDuration - animDelay >= 0) {
+							for (int i = 0; i < animCps.size(); i++) {
+								double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
+								double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
+								animCps.get(i).getPosition().x -= x1 / animSteps;
+								animCps.get(i).getPosition().y -= y1 / animSteps;
+							}
+							repaint();
+							animDuration -= animDelay;
+							animSteps--;
+						} else {
+							animDuration = ANIMTIME;
+							animSteps = animDuration / animDelay;
+							animT.stop();
+							for (int i = 0; i < animCps.size(); i++) {
+								animCps.get(i).getPosition().x = savePos.get(i).x;
+								animCps.get(i).getPosition().y = savePos.get(i).y;
+							}
+
+							controller.calculateStateForCurrentTimeStep();
+							repaint();
+							elapsedTime = System.currentTimeMillis() - start;
+							controller.addTextToConsole(elapsedTime + "");
+						}
+					}
+				});
+				start = System.currentTimeMillis();
+				animT.start();
 			}
 		});
 
@@ -172,10 +290,10 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 						boolean found = false;
 						if (controller.getTrackingObj() != null) {
 							for (AbstractCpsObject obj : controller.getTrackingObj()) {
-								if(obj instanceof HolonObject){
+								if (obj instanceof HolonObject) {
 									if (obj.getID() == o.getID()) {
 										found = true;
-									}	
+									}
 								}
 							}
 						}
@@ -201,7 +319,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 						boolean found = false;
 						if (controller.getTrackingObj() != null) {
 							for (AbstractCpsObject obj : controller.getTrackingObj()) {
-								if(obj instanceof HolonObject){
+								if (obj instanceof HolonObject) {
 									if (obj.getID() == o.getID()) {
 										found = true;
 									}
@@ -229,6 +347,8 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 				for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
 					if (upperNode.getNodes().contains(cps)) {
 						controller.delObjUpperNode(cps, upperNode);
+						// Removes the object from the tracked objects, in case it was tracked
+						controller.removeTrackingObj(cps);
 						// Remove UpperNodeTab if UpperNode deleted
 						if (cps instanceof CpsUpperNode) {
 							JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
@@ -431,11 +551,13 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		// Objects in upper node
 		for (AbstractCpsObject cps : upperNode.getNodes()) {
 			// Border Highlighting
-			g2.setColor(cps.getBorderColor());
-			if (g2.getColor() != Color.WHITE) {
-				g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
-						(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
-						(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
+			if(showedInformation[3]){
+				g2.setColor(cps.getBorderColor());
+				if (g2.getColor() != Color.WHITE) {
+					g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
+							(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
+							(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
+				}
 			}
 
 			// node image
@@ -516,12 +638,14 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 			}
 
 			// Border Highlighting
-			g2.setColor(cps.getBorderColor());
-			if (g2.getColor() != Color.WHITE) {
-				g2.fillRect((int) ((borderPos >> 1) - model.getScaleDiv2() - scalediv20) - 3,
-						(int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count - scalediv20) - 3,
-						(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
-						(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
+			if(showedInformation[3]){
+				g2.setColor(cps.getBorderColor());
+				if (g2.getColor() != Color.WHITE) {
+					g2.fillRect((int) ((borderPos >> 1) - model.getScaleDiv2() - scalediv20) - 3,
+							(int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count - scalediv20) - 3,
+							(int) (controller.getScale() + ((scalediv20 + 3) * 2)),
+							(int) (controller.getScale() + ((scalediv20 + 3) * 2)));
+				}
 			}
 
 			// node image
@@ -1222,6 +1346,14 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		showedInformation[0] = connection;
 		showedInformation[1] = object;
 	}
+	
+	/**
+	 * copies a set of given informations
+	 * @param informations
+	 */
+	public void setShowedInformation(boolean[] informations){
+		showedInformation = informations;
+	}
 
 	/**
 	 * Returns if Information should be shown.