From daf6189c8f31f1a0feb3f812494b8d9a8ff6ee02 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 20 Mar 2023 22:44:23 +0100 Subject: [PATCH] [#2102] Add "Select components of same color" option --- core/resources/l10n/messages.properties | 3 + .../sf/openrocket/gui/main/BasicFrame.java | 13 +++ .../sf/openrocket/gui/main/RocketActions.java | 92 +++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/core/resources/l10n/messages.properties b/core/resources/l10n/messages.properties index 6d70535fc..0aaca5e6c 100644 --- a/core/resources/l10n/messages.properties +++ b/core/resources/l10n/messages.properties @@ -39,6 +39,9 @@ RocketActions.DuplicateAct.Duplicate = Duplicate RocketActions.DuplicateAct.ttip.Duplicate = Duplicate this component (and subcomponents). RocketActions.EditAct.Edit = Edit RocketActions.EditAct.ttip.Edit = Edit the selected component. +RocketActions.Select = Select +RocketActions.Select.SelectSameColorAct = Components with same color +RocketActions.Select.SelectSameColorAct.ttip = Select all components with the same color as this component. RocketActions.ScaleAct.Scale = Scale RocketActions.ScaleAct.ttip.Scale = Scale parts of the rocket design RocketActions.NewStageAct.Newstage = New stage diff --git a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java index 260f27d7d..7a19f6a01 100644 --- a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -236,6 +236,12 @@ public class BasicFrame extends JFrame { popupMenu.add(actions.getPasteAction()); popupMenu.add(actions.getDuplicateAction()); popupMenu.add(actions.getDeleteAction()); + + popupMenu.addSeparator(); + JMenu selectMenu = new JMenu(trans.get("RocketActions.Select")); + selectMenu.add(actions.getSelectSameColorAction()); + popupMenu.add(selectMenu); + popupMenu.addSeparator(); popupMenu.add(actions.getScaleAction()); } @@ -656,6 +662,13 @@ public class BasicFrame extends JFrame { menu.addSeparator(); + JMenu subMenu = new JMenu(trans.get("RocketActions.Select")); + item = new JMenuItem(actions.getSelectSameColorAction()); + subMenu.add(item); + menu.add(subMenu); + + menu.addSeparator(); + item = new JMenuItem(actions.getScaleAction()); menu.add(item); diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index 497f30bcb..d4f1dd9b9 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -35,6 +35,7 @@ import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.AxialStage; import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Preferences; +import net.sf.openrocket.util.Color; import net.sf.openrocket.util.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,6 +75,7 @@ public class RocketActions { private final RocketAction pasteAction; private final RocketAction duplicateAction; private final RocketAction editAction; + private final RocketAction selectSameColorAction; private final RocketAction scaleAction; private final RocketAction moveUpAction; private final RocketAction moveDownAction; @@ -98,6 +100,7 @@ public class RocketActions { this.pasteAction = new PasteAction(); this.duplicateAction = new DuplicateAction(); this.editAction = new EditAction(); + this.selectSameColorAction = new SelectSameColor(); this.scaleAction = new ScaleAction(); this.moveUpAction = new MoveUpAction(); this.moveDownAction = new MoveDownAction(); @@ -131,6 +134,7 @@ public class RocketActions { pasteAction.clipboardChanged(); duplicateAction.clipboardChanged(); editAction.clipboardChanged(); + selectSameColorAction.clipboardChanged(); scaleAction.clipboardChanged(); moveUpAction.clipboardChanged(); moveDownAction.clipboardChanged(); @@ -171,6 +175,10 @@ public class RocketActions { return editAction; } + public Action getSelectSameColorAction() { + return selectSameColorAction; + } + public Action getScaleAction() { return scaleAction; } @@ -1006,6 +1014,90 @@ public class RocketActions { } } + /** + * Action to select all components with the same color as the currently selected component. + */ + private class SelectSameColor extends RocketAction { + private static final long serialVersionUID = 1L; + + public SelectSameColor() { + //// Select same color + this.putValue(NAME, trans.get("RocketActions.Select.SelectSameColorAct")); + this.putValue(SHORT_DESCRIPTION, trans.get("RocketActions.Select.SelectSameColorAct.ttip")); + clipboardChanged(); + } + + @Override + public void actionPerformed(ActionEvent e) { + List components = selectionModel.getSelectedComponents(); + if (components.size() == 0) { + return; + } + + RocketComponent component = components.get(0); + List sameColorComponents; + + // Case 1: component has a default appearance (null) + if (component.getAppearance() == null) { + sameColorComponents = getComponentsDefaultColor(component); + } + // Case 2: component has a custom appearance + else { + sameColorComponents = getComponentsCustomColor(component); + } + + selectionModel.setSelectedComponents(sameColorComponents); + } + + private List getComponentsCustomColor(RocketComponent component) { + Color targetColor = component.getAppearance().getPaint(); + List components = new ArrayList<>(); + components.add(component); + + for (RocketComponent c : rocket) { + if (c == component || c.getAppearance() == null || c.getAppearance().getPaint() == null) { + continue; + } + Color color = c.getAppearance().getPaint(); + // Add components with the same RGB values (ignore alpha) + if (color.getRed() == targetColor.getRed() && + color.getGreen() == targetColor.getGreen() && + color.getBlue() == targetColor.getBlue()) { + components.add(c); + } + } + + return components; + } + + private List getComponentsDefaultColor(RocketComponent component) { + List components = new ArrayList<>(); + components.add(component); + + for (RocketComponent c : rocket) { + if (c == component) { + continue; + } + + // Only add same components & components that also have the default color + if (c.getClass().equals(component.getClass()) && c.getAppearance() == null) { + components.add(c); + } + } + + return components; + } + + @Override + public void clipboardChanged() { + List components = selectionModel.getSelectedComponents(); + this.setEnabled(components.size() > 0); + } + } + + + + /** * Action to scale the currently selected component. */