1 package net.sf.openrocket.gui.dialogs.optimization;
4 import java.awt.Component;
6 import java.util.Enumeration;
10 import javax.swing.JTree;
11 import javax.swing.tree.DefaultMutableTreeNode;
12 import javax.swing.tree.DefaultTreeCellRenderer;
13 import javax.swing.tree.TreePath;
15 import net.sf.openrocket.gui.components.BasicTree;
16 import net.sf.openrocket.gui.main.ComponentIcons;
17 import net.sf.openrocket.optimization.rocketoptimization.SimulationModifier;
18 import net.sf.openrocket.rocketcomponent.Rocket;
19 import net.sf.openrocket.rocketcomponent.RocketComponent;
22 * A tree that displays the simulation modifiers in a tree structure.
24 * All nodes in the model are instances of DefaultMutableTreeNode. The user objects
25 * within are either of type RocketComponent, String or SimulationModifier.
27 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
29 public class SimulationModifierTree extends BasicTree {
34 * @param rocket the rocket.
35 * @param simulationModifiers the simulation modifiers, ordered and mapped by components
37 public SimulationModifierTree(Rocket rocket, Map<Object, List<SimulationModifier>> simulationModifiers) {
38 super(createModifierTree(rocket, simulationModifiers));
40 this.setCellRenderer(new ComponentModifierTreeRenderer());
46 private static DefaultMutableTreeNode createModifierTree(Rocket rocket,
47 Map<Object, List<SimulationModifier>> simulationModifiers) {
49 DefaultMutableTreeNode baseNode = new DefaultMutableTreeNode(rocket);
50 populateTree(baseNode, rocket, simulationModifiers);
56 private static void populateTree(DefaultMutableTreeNode node, RocketComponent component,
57 Map<Object, List<SimulationModifier>> simulationModifiers) {
59 // Add modifiers (if any)
60 List<SimulationModifier> modifiers = simulationModifiers.get(component);
61 if (modifiers != null) {
62 DefaultMutableTreeNode modifierNode;
64 if (component.getChildCount() > 0) {
65 modifierNode = new DefaultMutableTreeNode("Optimization parameters");
66 node.add(modifierNode);
71 for (SimulationModifier m : modifiers) {
72 modifierNode.add(new DefaultMutableTreeNode(m));
76 // Add child components
77 for (RocketComponent c : component.getChildren()) {
78 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(c);
80 populateTree(newNode, c, simulationModifiers);
86 @SuppressWarnings("rawtypes")
87 private void expandComponents() {
88 DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) this.getModel().getRoot();
90 Enumeration enumeration = baseNode.breadthFirstEnumeration();
92 while (enumeration.hasMoreElements()) {
93 DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
94 Object object = node.getUserObject();
95 if (object instanceof RocketComponent) {
96 this.makeVisible(new TreePath(node.getPath()));
104 public class ComponentModifierTreeRenderer extends DefaultTreeCellRenderer {
105 private Font componentFont;
106 private Font stringFont;
107 private Font modifierFont;
110 public Component getTreeCellRendererComponent(
119 super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus2);
121 if (componentFont == null) {
126 // Customize based on line type
128 Object object = ((DefaultMutableTreeNode) value).getUserObject();
130 // Set icon (for rocket components, null for others)
131 setIcon(ComponentIcons.getSmallIcon(object.getClass()));
134 // Set text color/style
135 if (object instanceof RocketComponent) {
136 setForeground(Color.GRAY);
137 setFont(componentFont);
138 } else if (object instanceof String) {
139 setForeground(Color.GRAY);
141 } else if (object instanceof SimulationModifier) {
142 setForeground(Color.BLACK);
143 setFont(modifierFont);
144 setText(((SimulationModifier) object).getName());
150 private void makeFonts() {
151 Font font = getFont();
152 componentFont = font.deriveFont(Font.ITALIC);
154 modifierFont = font.deriveFont(Font.BOLD);