diff --git a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java index 685560786..8134f1fb7 100644 --- a/swing/src/net/sf/openrocket/gui/main/BasicFrame.java +++ b/swing/src/net/sf/openrocket/gui/main/BasicFrame.java @@ -279,11 +279,13 @@ public class BasicFrame extends JFrame { if( componentSelectionModel.isSelectionEmpty() ){ final Rocket rocket = document.getRocket(); if( rocket != null ) { - final AxialStage topStage = (AxialStage) rocket.getChild(0); + final RocketComponent topStage = rocket.getChild(0); if (topStage != null) { final TreePath selectionPath = new TreePath(topStage); componentSelectionModel.setSelectionPath(selectionPath); tree.setSelectionRow(1); + // Don't select children components at startup (so override the default behavior with this new selection) + rocketpanel.getFigure().setSelection(new RocketComponent[] { topStage }); log.debug("... Setting Initial Selection: " + tree.getSelectionPath() ); } } diff --git a/swing/src/net/sf/openrocket/gui/main/DesignPanel.java b/swing/src/net/sf/openrocket/gui/main/DesignPanel.java index 9795207e7..dcb300261 100644 --- a/swing/src/net/sf/openrocket/gui/main/DesignPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/DesignPanel.java @@ -69,36 +69,30 @@ public class DesignPanel extends JSplitPane { im.put(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_KEY), null); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_N, SHORTCUT_KEY), null); - // Visually select all child components of a stage/rocket/podset when it is selected + // Highlight all child components of a stage/rocket/podset when it is selected tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { - if (tree == null || tree.getSelectionPaths() == null || tree.getSelectionPaths().length == 0 - || parent.getRocketPanel() == null) return; - - // Get all the components that need to be selected = currently selected components + children of stages/boosters/podsets - List children = new ArrayList<>(Arrays.asList(parent.getRocketPanel().getFigure().getSelection())); - for (TreePath p : tree.getSelectionPaths()) { - if (p != null) { - RocketComponent c = (RocketComponent) p.getLastPathComponent(); - if (c instanceof AxialStage || c instanceof Rocket || c instanceof PodSet) { - Iterator iter = c.iterator(false); - while (iter.hasNext()) { - RocketComponent child = iter.next(); - children.add(child); - } - } - } - } - - // Select all the child components - if (parent.getRocketPanel().getFigure() != null && parent.getRocketPanel().getFigure3d() != null) { - parent.getRocketPanel().getFigure().setSelection(children.toArray(new RocketComponent[0])); - parent.getRocketPanel().getFigure3d().setSelection(children.toArray(new RocketComponent[0])); - } + highlightAssemblyChildren(tree, parent); } }); + // Add a mouse listener for when the sustainer is selected at startup, to ensure that its children are highlighted. + // This is necessary because we force the children to not be highlighted when the tree is first created, and + // re-clicking the sustainer would not fire a change event in the tree (which normally highlights the children). + MouseAdapter mouseAdapter = new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (tree.getSelectionPath() != null && + tree.getSelectionPath().getLastPathComponent() == document.getRocket().getChild(0)) { + highlightAssemblyChildren(tree, parent); + } + // Delete the listener again. We only need it at start-up, i.e. when the first click is registered. + tree.removeMouseListener(this); + } + }; + tree.addMouseListener(mouseAdapter); + // Double-click opens config dialog MouseListener ml = new MouseAdapter() { @Override @@ -234,6 +228,37 @@ public class DesignPanel extends JSplitPane { this.setRightComponent(panel); } + /** + * Highlight all child components of a stage/rocket/podset when it is selected + * @param tree the tree in which the component selection took place + * @param parent the parent frame to highlight the components in + */ + private static void highlightAssemblyChildren(ComponentTree tree, BasicFrame parent) { + if (tree == null || tree.getSelectionPaths() == null || tree.getSelectionPaths().length == 0 + || parent.getRocketPanel() == null) return; + + // Get all the components that need to be selected = currently selected components + children of stages/boosters/podsets + List children = new ArrayList<>(Arrays.asList(parent.getRocketPanel().getFigure().getSelection())); + for (TreePath p : tree.getSelectionPaths()) { + if (p != null) { + RocketComponent c = (RocketComponent) p.getLastPathComponent(); + if (c instanceof AxialStage || c instanceof Rocket || c instanceof PodSet) { + Iterator iter = c.iterator(false); + while (iter.hasNext()) { + RocketComponent child = iter.next(); + children.add(child); + } + } + } + } + + // Select all the child components + if (parent.getRocketPanel().getFigure() != null && parent.getRocketPanel().getFigure3d() != null) { + parent.getRocketPanel().getFigure().setSelection(children.toArray(new RocketComponent[0])); + parent.getRocketPanel().getFigure3d().setSelection(children.toArray(new RocketComponent[0])); + } + } + /** * Focus on the component tree. */