Browse Source

unitgraph when copy pasted

Teh-Hai Julian Zheng 8 years ago
parent
commit
c47c3083da

+ 8 - 8
src/ui/controller/ClipboardController.java

@@ -469,17 +469,17 @@ public class ClipboardController {
 		// TODO Auto-generated method stub
 		int x = temp.getPosition().x - point.x;
 		int y = temp.getPosition().y - point.y;
-		if(x < 0)
-			x = 0;
-		if(x > model.getCanvasX())
+		if (x > model.getCanvasX())
 			x = model.getCanvasX();
-		if(y < 0)
-			y = 0;
-		if(y > model.getCanvasX())
+		if (y > model.getCanvasX())
 			y = model.getCanvasY();
-		
+		if (y < 0)
+			y = 0;
+		if (x < 0)
+			x = 0;
+
 		temp.setPosition(new Position(x, y));
-		
+
 	}
 
 }

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

@@ -341,21 +341,7 @@ public class GUI<E> implements CategoryListener {
 				try {
 					controller.loadAutoSave(controller.getUndoSave());
 					canvas.repaint();
-					ArrayList<HolonElement> tempList = new ArrayList<>();
-					for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-						if (cps instanceof HolonObject) {
-							for (HolonElement h : ((HolonObject) cps).getElements()) {
-								tempList.add(h);
-								unitGraph.repaintWithNewElement(tempList);
-								unitGraph.fillArrayofValue();
-								tempList.remove(0);
-							}
-						} else if (cps instanceof HolonSwitch) {
-							unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
-							unitGraph.fillArrayofBooleans();
-						}
-					}
-					unitGraph.empty();
+					unitGraph.update(model.getObjectsOnCanvas());
 				} catch (IOException eex) {
 					// TODO Auto-generated catch block
 					eex.printStackTrace();
@@ -374,21 +360,7 @@ public class GUI<E> implements CategoryListener {
 				try {
 					controller.loadAutoSave(controller.getRedoSave());
 					canvas.repaint();
-					ArrayList<HolonElement> tempList = new ArrayList<>();
-					for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-						if (cps instanceof HolonObject) {
-							for (HolonElement h : ((HolonObject) cps).getElements()) {
-								tempList.add(h);
-								unitGraph.repaintWithNewElement(tempList);
-								unitGraph.fillArrayofValue();
-								tempList.remove(0);
-							}
-						} else if (cps instanceof HolonSwitch) {
-							unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
-							unitGraph.fillArrayofBooleans();
-						}
-					}
-					unitGraph.empty();
+					unitGraph.update(model.getObjectsOnCanvas());
 				} catch (IOException ex) {
 					// TODO Auto-generated catch block
 					ex.printStackTrace();
@@ -539,6 +511,7 @@ public class GUI<E> implements CategoryListener {
 										.getComponent(0)).getMousePosition());
 					else
 						controller.paste(null, canvas.getMousePosition());
+					unitGraph.update(model.getSelectedCpsObjects());
 				} catch (HeadlessException | JsonParseException | UnsupportedFlavorException | IOException e1) {
 					// TODO Auto-generated catch block
 					JLabel message = new JLabel("The Clipboard information cannot be pasted into Application.");
@@ -1646,21 +1619,7 @@ public class GUI<E> implements CategoryListener {
 					try {
 						controller.loadFile(file.getAbsolutePath());
 						canvas.repaint();
-						ArrayList<HolonElement> tempList = new ArrayList<>();
-						for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-							if (cps instanceof HolonObject) {
-								for (HolonElement h : ((HolonObject) cps).getElements()) {
-									tempList.add(h);
-									unitGraph.repaintWithNewElement(tempList);
-									unitGraph.fillArrayofValue();
-									tempList.remove(0);
-								}
-							} else if (cps instanceof HolonSwitch) {
-								unitGraph.repaintWithNewSwitch((HolonSwitch) cps);
-								unitGraph.fillArrayofBooleans();
-							}
-						}
-						unitGraph.empty();
+						unitGraph.update(model.getObjectsOnCanvas());
 						tree.repaint();
 					} catch (IOException | ArchiveException e) {
 						// TODO Auto-generated catch block
@@ -2147,7 +2106,7 @@ public class GUI<E> implements CategoryListener {
 		} else {
 			tabTemp = tabbedPane2;
 		}
-		if (((JScrollPane) tabTemp.getSelectedComponent()).getViewport().getComponent(0) instanceof MyCanvas) {
+			if (((JScrollPane) tabTemp.getSelectedComponent()).getViewport().getComponent(0) instanceof MyCanvas) {
 			unc = new UpperNodeCanvas(model, controller, (CpsUpperNode) temp, "");
 		} else {
 			unc = new UpperNodeCanvas(model, controller, (CpsUpperNode) temp,

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

@@ -380,6 +380,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			public void actionPerformed(ActionEvent e) {
 				try {
 					controller.paste(null, mousePosition);
+					
 				} catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
 					// TODO Auto-generated catch block
 					JLabel message = new JLabel("The Clipboard information cannot be pastet into Application.");

+ 35 - 0
src/ui/view/UnitGraph.java

@@ -13,13 +13,17 @@ import java.awt.event.MouseMotionListener;
 import java.awt.geom.CubicCurve2D;
 import java.awt.geom.GeneralPath;
 import java.awt.geom.Line2D;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.awt.Point;
 
 import javax.swing.JPanel;
 
+import classes.AbstractCpsObject;
+import classes.CpsUpperNode;
 import classes.HolonElement;
+import classes.HolonObject;
 import ui.controller.Control;
 import ui.model.Model;
 import classes.HolonSwitch;
@@ -755,4 +759,35 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 											// (py + z * ry));
 		}
 	} // end intersection line-line
+
+	public void update(ArrayList<AbstractCpsObject> obj) {
+
+		ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
+		ArrayList<HolonElement> list = new ArrayList<>();
+
+		AbstractCpsObject u = null;
+		queue.addAll(obj);
+
+		while (!queue.isEmpty()) {
+			u = queue.pop();
+			if (u instanceof HolonObject) {
+				for (HolonElement ele : ((HolonObject) u).getElements()) {
+					list.add(ele);
+					repaintWithNewElement(list);
+					fillArrayofValue();
+					list.remove(0);
+				}
+			} else if (u instanceof HolonSwitch) {
+				repaintWithNewSwitch((HolonSwitch) u);
+				fillArrayofBooleans();
+			}
+		}
+		empty();
+
+		if (u instanceof CpsUpperNode)
+			for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
+				queue.add(adjacent);
+			}
+
+	}
 }