Jelajahi Sumber

Fixes UpperNodeTabs not being closed on replace and Canvas:ungrouping

Andreas T. Meyer-Berg 6 tahun lalu
induk
melakukan
2356f24dac

+ 6 - 0
src/ui/view/AbstractCanvas.java

@@ -690,4 +690,10 @@ public abstract class AbstractCanvas extends JPanel {
 		    }
 		}
 	}
+
+	/**
+	 * Closes a tab of the UpperNode with ID upperNodeID
+	 * @param upperNodeId
+	 */
+	public abstract void closeUpperNodeTab(int upperNodeId);
 }

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

@@ -1893,7 +1893,14 @@ public class GUI implements CategoryListener {
 								}
 								h.setPosition(x, y);
 
+								/**
+								 * close UpperNodeTabs of replaced UpperNode
+								 */
+								if(unc.mayBeReplaced!=null &&unc.mayBeReplaced instanceof CpsUpperNode){
+									unc.closeUpperNodeTab(unc.mayBeReplaced.getId());
+								}
 								controller.addObjUpperNode(h, unc.upperNode);
+								
 								/**
 								 * object would be replaced
 								 */
@@ -1923,7 +1930,14 @@ public class GUI implements CategoryListener {
 								h = new HolonBattery(tempCps);
 							}
 							h.setPosition(x, y);
-
+							
+							/**
+							 * close UpperNodeTabs of replaced UpperNode
+							 */
+							if(canvas.mayBeReplaced!=null &&canvas.mayBeReplaced instanceof CpsUpperNode){
+								canvas.closeUpperNodeTab(canvas.mayBeReplaced.getId());
+							}
+							
 							controller.addObjectCanvas(h);
 							/**
 							 * no object should get replaced

+ 23 - 12
src/ui/view/MyCanvas.java

@@ -126,16 +126,9 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		itemUngroup
 				.addActionListener(actionEvent -> {
 					// save old Position
-					JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
-							.getParent().getParent();
-					for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
-						if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner
-								.getComponentAt(i)).getViewport().getComponent(
-								0)).upperNode.getId() == tempCps.getId()) {
-							tabbedPaneInner.remove(i);
-							break;
-						}
-					}
+					int upperNodeId = tempCps.getId();
+					
+					closeUpperNodeTab(upperNodeId);
 
 					savePos = new ArrayList<>();
 					animCps = ((CpsUpperNode) tempCps).getNodes();
@@ -376,9 +369,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		String maxCap = null;
 		super.paintComponent(g);
 		// Rendering
-		
 		g2 = (Graphics2D) g;
-		
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
 				RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
@@ -626,6 +617,11 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			if(model.getSelectedCpsObjects().size()==1 
 					&& checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
 				/**
+            	 * if UpperNode would be replaced, close its tabs
+            	 */
+            	if(mayBeReplaced instanceof CpsUpperNode)
+            		closeUpperNodeTab(mayBeReplaced.getId());
+				/**
 				 * replace on canvas (will save)
 				 */
 				controller.replaceCanvasObject(mayBeReplaced, tempCps);
@@ -998,4 +994,19 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			ex.printStackTrace();
 		}
 	}
+	
+	
+	@Override
+	public void closeUpperNodeTab(int upperNodeId) {
+		JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
+				.getParent().getParent().getParent();
+		for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
+			if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner
+					.getComponentAt(i)).getViewport().getComponent(
+							0)).upperNode.getId() == upperNodeId) {
+				tabbedPaneInner.remove(i);
+				break;
+			}
+		}
+	}
 }

+ 18 - 8
src/ui/view/UpperNodeCanvas.java

@@ -139,14 +139,7 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
 
         itemUngroup.addActionListener(actionEvent -> {
             // save old Position
-            JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
-            for (int i = 4; i < tabbedPane.getTabCount(); i++) {
-                if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
-                        .getComponent(0)).upperNode.getId() == tempCps.getId()) {
-                    tabbedPane.remove(i);
-                    break;
-                }
-            }
+        	closeUpperNodeTab(tempCps.getId());
 
             savePos = new ArrayList<>();
             animCps = ((CpsUpperNode) tempCps).getNodes();
@@ -741,6 +734,11 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
              * check if tempCps could replace an Object on the UpperNodeanvas
              */
             if(model.getSelectedCpsObjects().size() == 1 && checkForReplacement(upperNode.getNodes(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
+            	/**
+            	 * if UpperNode would be replaced, close its tabs
+            	 */
+            	if(mayBeReplaced instanceof CpsUpperNode)
+            		closeUpperNodeTab(mayBeReplaced.getId());
             	/**
             	 * replace on canvas
             	 */
@@ -1278,4 +1276,16 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
 		}
 	}
 
+	@Override
+	public void closeUpperNodeTab(int upperNodeId) {
+        JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
+        for (int i = 4; i < tabbedPane.getTabCount(); i++) {
+            if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
+                    .getComponent(0)).upperNode.getId() == upperNodeId) {
+                tabbedPane.remove(i);
+                break;
+            }
+        }
+	}
+
 }