From 6157ce04b624f50cac5a9e7038e23e3a3eb065e8 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 17 Feb 2022 01:26:56 +0100 Subject: [PATCH] [fixes #358] Implement multi-component selection in RocketPanel --- .../gui/scalefigure/RocketPanel.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java b/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java index c585eb6c8..b9dddc747 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java @@ -8,6 +8,7 @@ import java.awt.Point; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.util.ArrayList; +import java.util.Arrays; import java.util.EventListener; import java.util.EventObject; import java.util.List; @@ -15,6 +16,7 @@ import java.util.LinkedList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; +import java.util.stream.Collectors; import javax.swing.*; import javax.swing.event.ChangeEvent; @@ -527,35 +529,33 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change return; } - // Check whether the currently selected component is in the clicked components. - TreePath path = selectionModel.getSelectionPath(); - if (path != null) { - RocketComponent current = (RocketComponent) path.getLastPathComponent(); - path = null; - for (int i = 0; i < clicked.length; i++) { - if (clicked[i] == current) { - if (event.isShiftDown() && (event.getClickCount() == 1)) { - path = ComponentTreeModel.makeTreePath(clicked[(i + 1) % clicked.length]); - } else { - path = ComponentTreeModel.makeTreePath(clicked[i]); - } + List selectedComponents = Arrays.stream(selectionModel.getSelectionPaths()) + .map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList()); + + // If the shift-button is held, add a newly clicked component to the selection path + if ((event.isShiftDown() || event.isMetaDown()) && event.getClickCount() == 1) { + List paths = new ArrayList<>(Arrays.asList(selectionModel.getSelectionPaths())); + for (RocketComponent component : clicked) { + if (!selectedComponents.contains(component)) { + TreePath path = ComponentTreeModel.makeTreePath(component); + paths.add(path); break; } } + selectionModel.setSelectionPaths(paths.toArray(new TreePath[0])); } - - // Currently selected component not clicked - if (path == null) { - if (event.isShiftDown() && event.getClickCount() == 1 && clicked.length > 1) { - path = ComponentTreeModel.makeTreePath(clicked[1]); - } else { - path = ComponentTreeModel.makeTreePath(clicked[0]); + // Single click, so set the selection to the first clicked component + else { + if (!selectedComponents.contains(clicked[0])) { + TreePath path = ComponentTreeModel.makeTreePath(clicked[0]); + selectionModel.setSelectionPath(path); } } - // Set selection and check for double-click - selectionModel.setSelectionPath(path); + // Check for double-click if (event.getClickCount() == 2) { + TreePath path = ComponentTreeModel.makeTreePath(clicked[0]); + selectionModel.setSelectionPath(path); // Revert to single selection RocketComponent component = (RocketComponent) path.getLastPathComponent(); ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),