[fixes #358] Implement multi-component selection in RocketPanel

This commit is contained in:
SiboVG 2022-02-17 01:26:56 +01:00
parent 8ce90d2bb5
commit 6157ce04b6

View File

@ -8,6 +8,7 @@ import java.awt.Point;
import java.awt.event.InputEvent; import java.awt.event.InputEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.EventListener; import java.util.EventListener;
import java.util.EventObject; import java.util.EventObject;
import java.util.List; import java.util.List;
@ -15,6 +16,7 @@ import java.util.LinkedList;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.stream.Collectors;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
@ -527,35 +529,33 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change
return; return;
} }
// Check whether the currently selected component is in the clicked components. List<RocketComponent> selectedComponents = Arrays.stream(selectionModel.getSelectionPaths())
TreePath path = selectionModel.getSelectionPath(); .map(c -> (RocketComponent) c.getLastPathComponent()).collect(Collectors.toList());
if (path != null) {
RocketComponent current = (RocketComponent) path.getLastPathComponent(); // If the shift-button is held, add a newly clicked component to the selection path
path = null; if ((event.isShiftDown() || event.isMetaDown()) && event.getClickCount() == 1) {
for (int i = 0; i < clicked.length; i++) { List<TreePath> paths = new ArrayList<>(Arrays.asList(selectionModel.getSelectionPaths()));
if (clicked[i] == current) { for (RocketComponent component : clicked) {
if (event.isShiftDown() && (event.getClickCount() == 1)) { if (!selectedComponents.contains(component)) {
path = ComponentTreeModel.makeTreePath(clicked[(i + 1) % clicked.length]); TreePath path = ComponentTreeModel.makeTreePath(component);
} else { paths.add(path);
path = ComponentTreeModel.makeTreePath(clicked[i]);
}
break; break;
} }
} }
selectionModel.setSelectionPaths(paths.toArray(new TreePath[0]));
} }
// Single click, so set the selection to the first clicked component
// Currently selected component not clicked else {
if (path == null) { if (!selectedComponents.contains(clicked[0])) {
if (event.isShiftDown() && event.getClickCount() == 1 && clicked.length > 1) { TreePath path = ComponentTreeModel.makeTreePath(clicked[0]);
path = ComponentTreeModel.makeTreePath(clicked[1]);
} else {
path = ComponentTreeModel.makeTreePath(clicked[0]);
}
}
// Set selection and check for double-click
selectionModel.setSelectionPath(path); selectionModel.setSelectionPath(path);
}
}
// Check for double-click
if (event.getClickCount() == 2) { if (event.getClickCount() == 2) {
TreePath path = ComponentTreeModel.makeTreePath(clicked[0]);
selectionModel.setSelectionPath(path); // Revert to single selection
RocketComponent component = (RocketComponent) path.getLastPathComponent(); RocketComponent component = (RocketComponent) path.getLastPathComponent();
ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this), ComponentConfigDialog.showDialog(SwingUtilities.getWindowAncestor(this),