[fixes #244] Implement multi-selection for motor mounts

This does not have a direct effect yet. Once multi-component selection is implemented from #358, this will work
This commit is contained in:
SiboVG 2022-02-15 00:11:21 +01:00
parent 08eeac5236
commit 4ba92905b4
5 changed files with 55 additions and 12 deletions

View File

@ -1694,6 +1694,11 @@ public class BasicFrame extends JFrame {
this.selectionModel.setSelectedComponent(component); this.selectionModel.setSelectedComponent(component);
} }
public void setSelectedComponents(List<RocketComponent> components) {
this.selectionModel.setSelectedComponents(components);
}
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
JTabbedPane tabSource = (JTabbedPane) e.getSource(); JTabbedPane tabSource = (JTabbedPane) e.getSource();
int tab = tabSource.getSelectedIndex(); int tab = tabSource.getSelectedIndex();

View File

@ -2,6 +2,7 @@ package net.sf.openrocket.gui.main;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
@ -29,7 +30,7 @@ public class DocumentSelectionModel {
private final OpenRocketDocument document; private final OpenRocketDocument document;
private RocketComponent componentSelection = null; private final List<RocketComponent> componentSelection = new LinkedList<>();
private Simulation[] simulationSelection = NO_SIMULATION; private Simulation[] simulationSelection = NO_SIMULATION;
private TreeSelectionModel componentTreeSelectionModel = null; private TreeSelectionModel componentTreeSelectionModel = null;
@ -72,23 +73,33 @@ public class DocumentSelectionModel {
/** /**
* Return the currently selected rocket component. Returns <code>null</code> * Return the currently selected rocket component. Returns <code>null</code>
* if no rocket component is selected. * if no rocket component is selected. If there is more than one component selected,
* the first selected component is returned.
* *
* @return the currently selected rocket component, or <code>null</code>. * @return the currently selected rocket component, or <code>null</code>.
*/ */
public RocketComponent getSelectedComponent() { public RocketComponent getSelectedComponent() {
return componentSelection; if (componentSelection == null || componentSelection.size() == 0) return null;
return componentSelection.get(0);
} }
public void setSelectedComponent(RocketComponent component) { public void setSelectedComponent(RocketComponent component) {
componentSelection = component; componentSelection.clear();
componentSelection.add(component);
clearSimulationSelection(); clearSimulationSelection();
TreePath path = ComponentTreeModel.makeTreePath(component); TreePath path = ComponentTreeModel.makeTreePath(component);
componentTreeSelectionModel.setSelectionPath(path); componentTreeSelectionModel.setSelectionPath(path);
} }
public void setSelectedComponents(List<RocketComponent> components) {
componentSelection.clear();
componentSelection.addAll(components);
clearSimulationSelection();
List<TreePath> paths = ComponentTreeModel.makeTreePaths(components);
componentTreeSelectionModel.setSelectionPaths(paths.toArray(new TreePath[0]));
}
@ -131,10 +142,10 @@ public class DocumentSelectionModel {
public void clearComponentSelection() { public void clearComponentSelection() {
if (componentSelection == null) if (componentSelection == null || componentSelection.size() == 0)
return; return;
componentSelection = null; componentSelection.clear();
if (componentTreeSelectionModel != null) if (componentTreeSelectionModel != null)
componentTreeSelectionModel.clearSelection(); componentTreeSelectionModel.clearSelection();
@ -168,12 +179,13 @@ public class DocumentSelectionModel {
public void valueChanged(TreeSelectionEvent e) { public void valueChanged(TreeSelectionEvent e) {
TreePath path = componentTreeSelectionModel.getSelectionPath(); TreePath path = componentTreeSelectionModel.getSelectionPath();
if (path == null) { if (path == null) {
componentSelection = null; componentSelection.clear();
fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE); fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);
return; return;
} }
componentSelection = (RocketComponent)path.getLastPathComponent(); componentSelection.clear();
componentSelection.add((RocketComponent)path.getLastPathComponent());
clearSimulationSelection(); clearSimulationSelection();
fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE); fireDocumentSelection(DocumentSelectionListener.COMPONENT_SELECTION_CHANGE);

View File

@ -200,6 +200,21 @@ public class ComponentTreeModel implements TreeModel, ComponentChangeListener {
return new TreePath(list.toArray()); return new TreePath(list.toArray());
} }
/**
* Return TreePaths corresponding to the specified rocket components.
*
* @param components the rocket components
* @return a list of TreePaths corresponding to the different RocketComponents (one path for each component)
*/
public static List<TreePath> makeTreePaths(List<RocketComponent> components) {
List<TreePath> result = new LinkedList<>();
for (RocketComponent component : components) {
result.add(makeTreePath(component));
}
return result;
}
/** /**
* Return a string describing the path, using component normal names. * Return a string describing the path, using component normal names.

View File

@ -297,6 +297,10 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
this.basicFrame.setSelectedComponent(component); this.basicFrame.setSelectedComponent(component);
} }
public void setSelectedComponents(List<RocketComponent> components) {
this.basicFrame.setSelectedComponents(components);
}
@Override @Override
public void stateChanged(EventObject e) { public void stateChanged(EventObject e) {
updateButtonState(); updateButtonState();

View File

@ -45,6 +45,7 @@ import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.unit.UnitGroup; import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.Chars; import net.sf.openrocket.util.Chars;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -237,12 +238,18 @@ public class MotorConfigurationPanel extends FlightConfigurablePanel<MotorMount>
if (e.getValueIsAdjusting()) { if (e.getValueIsAdjusting()) {
return; return;
} }
MotorMount mount = getSelectedComponent(); List<MotorMount> mounts = getSelectedComponents();
if (mounts == null || mounts.size() == 0) return;
List<RocketComponent> components = new ArrayList<>();
for (MotorMount mount : mounts) {
if (mount instanceof RocketComponent) { if (mount instanceof RocketComponent) {
flightConfigurationPanel.setSelectedComponent((RocketComponent) mount); components.add((RocketComponent) mount);
} }
} }
flightConfigurationPanel.setSelectedComponents(components);
}
protected void updateButtonState() { protected void updateButtonState() {
if( configurationTableModel.getColumnCount() > 1 ) { if( configurationTableModel.getColumnCount() > 1 ) {
showContent(); showContent();