From 7bb0363e8bb29229031a631395e2fb215f4a3316 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Sun, 14 Aug 2022 19:34:57 +0200 Subject: [PATCH 1/5] [#1523] Fix copy component order --- swing/src/net/sf/openrocket/gui/main/RocketActions.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index 15ed1d597..1e70b1129 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -659,6 +659,7 @@ public class RocketActions { } List copiedComponents = new LinkedList<>(copyComponentsMaintainParent(components)); + copiedComponents.sort(Comparator.comparing(c -> c.getParent() != null ? -c.getParent().getChildPosition(c) : 0)); OpenRocketClipboard.setClipboard(copiedComponents); delete(components); @@ -717,6 +718,7 @@ public class RocketActions { if (isCopyable(components)) { List copiedComponents = new LinkedList<>(copyComponentsMaintainParent(components)); + copiedComponents.sort(Comparator.comparing(c -> c.getParent() != null ? -c.getParent().getChildPosition(c) : 0)); OpenRocketClipboard.setClipboard(copiedComponents); parentFrame.selectTab(BasicFrame.COMPONENT_TAB); From e1a4816e10b430fbcf1f65cb772eb2bd69392349 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Sun, 14 Aug 2022 20:15:34 +0200 Subject: [PATCH 2/5] Fix duplicate paste position --- .../src/net/sf/openrocket/gui/main/RocketActions.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index 1e70b1129..7c768b1a1 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -851,10 +851,18 @@ public class RocketActions { @Override public void actionPerformed(ActionEvent e) { List components = selectionModel.getSelectedComponents(); + List topComponents = new LinkedList<>(); // Components without a parent component in if (components != null) { components.sort(Comparator.comparing(c -> c.getParent() != null ? c.getParent().getChildPosition(c) : 0)); components = new ArrayList<>(components); fillInMissingSelections(components); + } else { + return; + } + for (RocketComponent c: components) { + if (!RocketComponent.listContainsParent(components, c)) { + topComponents.add(c); + } } Simulation[] sims = selectionModel.getSelectedSimulations(); @@ -877,7 +885,8 @@ public class RocketActions { if (RocketComponent.listContainsParent(duplicateComponents, component)) { pos = getPastePosition(component, component.getParent()); } else { - RocketComponent pasteParent = components.get(duplicateComponents.indexOf(component)).getParent(); + int compIdx = duplicateComponents.indexOf(component); + RocketComponent pasteParent = topComponents.get(compIdx).getParent(); pos = getPastePosition(component, pasteParent); } positions.add(pos); From 3364b29189caa6b476afe23290600f598346d2f2 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 18 Aug 2022 00:57:33 +0200 Subject: [PATCH 3/5] Clean up code --- .../sf/openrocket/gui/main/RocketActions.java | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index 7c768b1a1..987cb0d3c 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -418,35 +418,22 @@ public class RocketActions { * should be pasted. Returns null if the clipboard is empty or if the * clipboard cannot be pasted to the current selection. * - * @param copyComponent the component to be copy-pasted. - * @param pasteComponent the component where copyComponent should be pasted to. + * @param srcComponent the component to be copy-pasted. + * @param destComponent the component where srcComponent should be pasted to. * @return a Pair with both components defined, or null. */ - private Pair getPastePosition(RocketComponent copyComponent, RocketComponent pasteComponent) { - if (pasteComponent == null) + private Pair getPastePosition(RocketComponent srcComponent, RocketComponent destComponent) { + if (destComponent == null) return null; - if (copyComponent == null) + if (srcComponent == null) return null; - if (pasteComponent.isCompatible(copyComponent)) - return new Pair(pasteComponent, pasteComponent.getChildCount()); + if (destComponent.isCompatible(srcComponent)) + return new Pair(destComponent, destComponent.getChildCount()); - RocketComponent parent = pasteComponent.getParent(); - return getPastePositionFromParent(copyComponent, parent); - } - - /** - * Return the component and position to which the current clipboard - * should be pasted. Returns null if the clipboard is empty or if the - * clipboard cannot be pasted to the current selection. - * - * @param copyComponent the component to be copy-pasted. - * @return a Pair with both components defined, or null. - */ - private Pair getPastePosition(RocketComponent copyComponent) { - RocketComponent selected = selectionModel.getSelectedComponent(); - return getPastePosition(copyComponent, selected); + RocketComponent parent = destComponent.getParent(); + return getPastePositionFromParent(srcComponent, parent); } private Pair getPastePositionFromParent(RocketComponent component, RocketComponent parent) { @@ -468,8 +455,9 @@ public class RocketActions { */ private List> getPastePositions(List clipboard) { List> result = new LinkedList<>(); + RocketComponent selected = selectionModel.getSelectedComponent(); for (RocketComponent component : clipboard) { - Pair position = getPastePosition(component); + Pair position = getPastePosition(component, selected); if (position != null) { result.add(position); } @@ -775,10 +763,7 @@ public class RocketActions { pasted.add(component.copy()); } - List> positions = new LinkedList<>(); - for (RocketComponent component : pasted) { - positions.add(getPastePosition(component)); - } + List> positions = getPastePositions(pasted); if (pasted.size() == 1) { document.addUndoPosition("Paste " + pasted.get(0).getComponentName()); From 41cf976bf87842a57af1422910944660691ca9f3 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 18 Aug 2022 21:29:29 +0200 Subject: [PATCH 4/5] Fix paste position from parent --- swing/src/net/sf/openrocket/gui/main/RocketActions.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index 987cb0d3c..b82cb159b 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -433,12 +433,13 @@ public class RocketActions { return new Pair(destComponent, destComponent.getChildCount()); RocketComponent parent = destComponent.getParent(); - return getPastePositionFromParent(srcComponent, parent); + return getPastePositionFromParent(srcComponent, destComponent, parent); } - private Pair getPastePositionFromParent(RocketComponent component, RocketComponent parent) { - if (parent != null && parent.isCompatible(component)) { - int index = parent.getChildPosition(parent) + 1; + private Pair getPastePositionFromParent(RocketComponent srcComponent, RocketComponent destComponent, + RocketComponent parent) { + if (parent != null && parent.isCompatible(srcComponent)) { + int index = parent.getChildPosition(destComponent) + 1; return new Pair<>(parent, index); } From 67cf6a69f2c192545f9be1a82b0e0527e2cb0a2b Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 18 Aug 2022 23:45:19 +0200 Subject: [PATCH 5/5] Fix paste on illegal component (call the cops) --- .../sf/openrocket/gui/main/RocketActions.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index b82cb159b..16a28a1d8 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -424,13 +424,13 @@ public class RocketActions { */ private Pair getPastePosition(RocketComponent srcComponent, RocketComponent destComponent) { if (destComponent == null) - return null; + return new Pair<>(null, null); if (srcComponent == null) - return null; + return new Pair<>(null, null); if (destComponent.isCompatible(srcComponent)) - return new Pair(destComponent, destComponent.getChildCount()); + return new Pair<>(destComponent, destComponent.getChildCount()); RocketComponent parent = destComponent.getParent(); return getPastePositionFromParent(srcComponent, destComponent, parent); @@ -443,7 +443,7 @@ public class RocketActions { return new Pair<>(parent, index); } - return null; + return new Pair<>(null, null); } /** @@ -780,8 +780,14 @@ public class RocketActions { pasted.get(i).getComponentName()), trans.get("RocketActions.PasteAct.invalidPosition.title"), JOptionPane.WARNING_MESSAGE); } else { - positions.get(i).getU().addChild(pasted.get(i), positions.get(i).getV()); - successfullyPasted.add(pasted.get(i)); + RocketComponent parent = positions.get(i).getU(); + RocketComponent child = pasted.get(i); + if (parent != null && parent.isCompatible(child)) { + parent.addChild(child, positions.get(i).getV()); + successfullyPasted.add(pasted.get(i)); + } else { + log.warn("Pasted component {} is not compatible with {}", child, parent); + } } }