merge fixing

This commit is contained in:
Sampo Niskanen 2011-01-09 09:02:58 +00:00
parent d2637f4bbd
commit e8abfbd306

View File

@ -3,10 +3,10 @@
*/ */
package net.sf.openrocket.gui.print.components; package net.sf.openrocket.gui.print.components;
import net.sf.openrocket.gui.print.OpenRocketPrintable; import java.util.ArrayList;
import net.sf.openrocket.gui.print.PrintableContext; import java.util.Iterator;
import net.sf.openrocket.rocketcomponent.RocketComponent; import java.util.List;
import net.sf.openrocket.rocketcomponent.Stage; import java.util.Vector;
import javax.swing.JTree; import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionEvent;
@ -15,206 +15,212 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.ExpandVetoException; import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath; import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel; import javax.swing.tree.TreeSelectionModel;
import java.util.ArrayList;
import java.util.Iterator; import net.sf.openrocket.gui.print.OpenRocketPrintable;
import java.util.List; import net.sf.openrocket.gui.print.PrintableContext;
import java.util.Vector; import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.Stage;
/** /**
* A specialized JTree for displaying various rocket items that can be printed. * A specialized JTree for displaying various rocket items that can be printed.
*/ */
public class RocketPrintTree extends JTree { public class RocketPrintTree extends JTree {
/** /**
* All check boxes are initially set to true (selected). * All check boxes are initially set to true (selected).
*/ */
public static final boolean INITIAL_CHECKBOX_SELECTED = true; public static final boolean INITIAL_CHECKBOX_SELECTED = true;
/** /**
* The selection model that tracks the check box state. * The selection model that tracks the check box state.
*/ */
private TreeSelectionModel theCheckBoxSelectionModel; private TreeSelectionModel theCheckBoxSelectionModel;
/** /**
* Constructor. * Constructor.
* *
* @param root the vector of check box nodes (rows) to place into the tree * @param root the vector of check box nodes (rows) to place into the tree
*/ */
private RocketPrintTree (Vector root) { private RocketPrintTree(Vector root) {
super(root); super(root);
//Remove the little down and sideways arrows. These are not needed because the tree expansion is fixed. //Remove the little down and sideways arrows. These are not needed because the tree expansion is fixed.
((javax.swing.plaf.basic.BasicTreeUI) this.getUI()). ((javax.swing.plaf.basic.BasicTreeUI) this.getUI()).
setExpandedIcon(null); setExpandedIcon(null);
((javax.swing.plaf.basic.BasicTreeUI) this.getUI()). ((javax.swing.plaf.basic.BasicTreeUI) this.getUI()).
setCollapsedIcon(null); setCollapsedIcon(null);
} }
/** /**
* Factory method to create a specialized JTree. This version is for rocket's that have more than one stage. * Factory method to create a specialized JTree. This version is for rocket's that have more than one stage.
* *
* @param rocketName the name of the rocket * @param rocketName the name of the rocket
* @param stages the array of all stages * @param stages the array of all stages
* *
* @return an instance of JTree * @return an instance of JTree
*/ */
public static RocketPrintTree create (String rocketName, RocketComponent[] stages) { public static RocketPrintTree create(String rocketName, List<RocketComponent> stages) {
Vector root = new Vector(); Vector root = new Vector();
Vector toAddTo = root; Vector toAddTo = root;
if (stages != null) { if (stages != null) {
if (stages.length > 1) { if (stages.size() > 1) {
final Vector parent = new NamedVector(rocketName != null ? rocketName : "Rocket"); final Vector parent = new NamedVector(rocketName != null ? rocketName : "Rocket");
root.add(parent); root.add(parent);
toAddTo = parent; toAddTo = parent;
} }
for (RocketComponent stage : stages) { for (RocketComponent stage : stages) {
if (stage instanceof Stage) { if (stage instanceof Stage) {
toAddTo.add(createNamedVector(stage.getName(), createPrintTreeNode(true), stage.getStageNumber())); toAddTo.add(createNamedVector(stage.getName(), createPrintTreeNode(true), stage.getStageNumber()));
} }
} }
} }
toAddTo.add(new CheckBoxNode(OpenRocketPrintable.DESIGN_REPORT.getDescription(), toAddTo.add(new CheckBoxNode(OpenRocketPrintable.DESIGN_REPORT.getDescription(),
INITIAL_CHECKBOX_SELECTED)); INITIAL_CHECKBOX_SELECTED));
RocketPrintTree tree = new RocketPrintTree(root); RocketPrintTree tree = new RocketPrintTree(root);
tree.addTreeWillExpandListener tree.addTreeWillExpandListener
(new TreeWillExpandListener() { (new TreeWillExpandListener() {
public void treeWillExpand (TreeExpansionEvent e) { @Override
} public void treeWillExpand(TreeExpansionEvent e) {
}
public void treeWillCollapse (TreeExpansionEvent e) @Override
throws ExpandVetoException { public void treeWillCollapse(TreeExpansionEvent e)
throw new ExpandVetoException(e, "you can't collapse this JTree"); throws ExpandVetoException {
} throw new ExpandVetoException(e, "you can't collapse this JTree");
}); }
});
return tree; return tree;
} }
/** /**
* Factory method to create a specialized JTree. This version is for a rocket with only one stage. * Factory method to create a specialized JTree. This version is for a rocket with only one stage.
* *
* @param rocketName the name of the rocket * @param rocketName the name of the rocket
* *
* @return an instance of JTree * @return an instance of JTree
*/ */
public static RocketPrintTree create (String rocketName) { public static RocketPrintTree create(String rocketName) {
Vector root = new Vector(); Vector root = new Vector();
root.add(new NamedVector(rocketName != null ? rocketName : "Rocket", createPrintTreeNode(false))); root.add(new NamedVector(rocketName != null ? rocketName : "Rocket", createPrintTreeNode(false)));
RocketPrintTree tree = new RocketPrintTree(root); RocketPrintTree tree = new RocketPrintTree(root);
tree.addTreeWillExpandListener tree.addTreeWillExpandListener
(new TreeWillExpandListener() { (new TreeWillExpandListener() {
public void treeWillExpand (TreeExpansionEvent e) { @Override
} public void treeWillExpand(TreeExpansionEvent e) {
}
public void treeWillCollapse (TreeExpansionEvent e) @Override
throws ExpandVetoException { public void treeWillCollapse(TreeExpansionEvent e)
throw new ExpandVetoException(e, "you can't collapse this JTree"); throws ExpandVetoException {
} throw new ExpandVetoException(e, "you can't collapse this JTree");
}); }
});
return tree; return tree;
} }
/** /**
* This tree needs to have access both to the normal selection model (for the textual row) which is managed by the * This tree needs to have access both to the normal selection model (for the textual row) which is managed by the
* superclass, as well as the selection model for the check boxes. This mutator method allows an external class to * superclass, as well as the selection model for the check boxes. This mutator method allows an external class to
* set the model back onto this class. Because of some unfortunate circular dependencies this cannot be set at * set the model back onto this class. Because of some unfortunate circular dependencies this cannot be set at
* construction. * construction.
* <p/> * <p/>
* TODO: Ensure these circular references get cleaned up properly at dialog disposal so everything can be GC'd. * TODO: Ensure these circular references get cleaned up properly at dialog disposal so everything can be GC'd.
* *
* @param checkBoxSelectionModel the selection model used to keep track of the check box state * @param checkBoxSelectionModel the selection model used to keep track of the check box state
*/ */
public void setCheckBoxSelectionModel (TreeSelectionModel checkBoxSelectionModel) { public void setCheckBoxSelectionModel(TreeSelectionModel checkBoxSelectionModel) {
theCheckBoxSelectionModel = checkBoxSelectionModel; theCheckBoxSelectionModel = checkBoxSelectionModel;
} }
/** /**
* Add a selection path to the internal check box selection model. The normal JTree selection model is unaffected. * Add a selection path to the internal check box selection model. The normal JTree selection model is unaffected.
* This has the effect of "selecting" the check box, but not highlighting the row. * This has the effect of "selecting" the check box, but not highlighting the row.
* *
* @param path the path (row) * @param path the path (row)
*/ */
public void addSelectionPath (TreePath path) { @Override
theCheckBoxSelectionModel.addSelectionPath(path); public void addSelectionPath(TreePath path) {
} theCheckBoxSelectionModel.addSelectionPath(path);
}
/** /**
* Helper to construct a named vector. * Helper to construct a named vector.
* *
* @param name the name of the vector * @param name the name of the vector
* @param nodes the array of nodes to put into the vector * @param nodes the array of nodes to put into the vector
* @param stage the stage number * @param stage the stage number
* *
* @return a NamedVector suitable for adding to a JTree * @return a NamedVector suitable for adding to a JTree
*/ */
private static Vector createNamedVector (String name, CheckBoxNode[] nodes, int stage) { private static Vector createNamedVector(String name, CheckBoxNode[] nodes, int stage) {
return new NamedVector(name, nodes, stage); return new NamedVector(name, nodes, stage);
} }
/** /**
* Helper to construct the set of check box rows for each stage. * Helper to construct the set of check box rows for each stage.
* *
* @param onlyStageSpecific if true then only stage specific OpenRocketPrintable rows are represented (in this part * @param onlyStageSpecific if true then only stage specific OpenRocketPrintable rows are represented (in this part
* of the tree). * of the tree).
* *
* @return an array of CheckBoxNode * @return an array of CheckBoxNode
*/ */
private static CheckBoxNode[] createPrintTreeNode (boolean onlyStageSpecific) { private static CheckBoxNode[] createPrintTreeNode(boolean onlyStageSpecific) {
List<CheckBoxNode> nodes = new ArrayList<CheckBoxNode>(); List<CheckBoxNode> nodes = new ArrayList<CheckBoxNode>();
OpenRocketPrintable[] printables = OpenRocketPrintable.values(); OpenRocketPrintable[] printables = OpenRocketPrintable.values();
for (OpenRocketPrintable openRocketPrintable : printables) { for (OpenRocketPrintable openRocketPrintable : printables) {
if (!onlyStageSpecific || openRocketPrintable.isStageSpecific() ) { if (!onlyStageSpecific || openRocketPrintable.isStageSpecific()) {
nodes.add(new CheckBoxNode(openRocketPrintable.getDescription(), nodes.add(new CheckBoxNode(openRocketPrintable.getDescription(),
INITIAL_CHECKBOX_SELECTED)); INITIAL_CHECKBOX_SELECTED));
} }
} }
return nodes.toArray(new CheckBoxNode[nodes.size()]); return nodes.toArray(new CheckBoxNode[nodes.size()]);
} }
/** /**
* Get the set of items to be printed, as selected by the user. * Get the set of items to be printed, as selected by the user.
* *
* @return the things to be printed, returned as an Iterator<PrintableContext> * @return the things to be printed, returned as an Iterator<PrintableContext>
*/ */
public Iterator<PrintableContext> getToBePrinted () { public Iterator<PrintableContext> getToBePrinted() {
final DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) getModel().getRoot(); final DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) getModel().getRoot();
PrintableContext pc = new PrintableContext(); PrintableContext pc = new PrintableContext();
add(pc, mutableTreeNode); add(pc, mutableTreeNode);
return pc.iterator(); return pc.iterator();
} }
/** /**
* Walk a tree, finding everything that has been selected and aggregating it into something that can be iterated upon * Walk a tree, finding everything that has been selected and aggregating it into something that can be iterated upon
* This method is recursive. * This method is recursive.
* *
* @param pc the printable context that aggregates the choices into an iterator * @param pc the printable context that aggregates the choices into an iterator
* @param theMutableTreeNode the root node * @param theMutableTreeNode the root node
*/ */
private void add (final PrintableContext pc, final DefaultMutableTreeNode theMutableTreeNode) { private void add(final PrintableContext pc, final DefaultMutableTreeNode theMutableTreeNode) {
int children = theMutableTreeNode.getChildCount(); int children = theMutableTreeNode.getChildCount();
for (int x = 0; x < children; x++) { for (int x = 0; x < children; x++) {
final DefaultMutableTreeNode at = (DefaultMutableTreeNode) theMutableTreeNode.getChildAt(x); final DefaultMutableTreeNode at = (DefaultMutableTreeNode) theMutableTreeNode.getChildAt(x);
if (at.getUserObject() instanceof CheckBoxNode) { if (at.getUserObject() instanceof CheckBoxNode) {
CheckBoxNode cbn = (CheckBoxNode) at.getUserObject(); CheckBoxNode cbn = (CheckBoxNode) at.getUserObject();
if (cbn.isSelected()) { if (cbn.isSelected()) {
final OpenRocketPrintable printable = OpenRocketPrintable.findByDescription(cbn.getText()); final OpenRocketPrintable printable = OpenRocketPrintable.findByDescription(cbn.getText());
pc.add( pc.add(
printable.isStageSpecific() ? ((NamedVector) theMutableTreeNode.getUserObject()) printable.isStageSpecific() ? ((NamedVector) theMutableTreeNode.getUserObject())
.getStage() : null, .getStage() : null,
printable); printable);
} }
} }
add(pc, at); add(pc, at);
} }
} }
} }
@ -222,31 +228,32 @@ public class RocketPrintTree extends JTree {
* JTree's work off of Vector's (unfortunately). This class is tailored for use with check boxes in the JTree. * JTree's work off of Vector's (unfortunately). This class is tailored for use with check boxes in the JTree.
*/ */
class NamedVector extends Vector<CheckBoxNode> { class NamedVector extends Vector<CheckBoxNode> {
String name; String name;
int stageNumber; int stageNumber;
public NamedVector (String theName) { public NamedVector(String theName) {
name = theName; name = theName;
} }
public NamedVector (String theName, CheckBoxNode elements[], int stage) { public NamedVector(String theName, CheckBoxNode elements[], int stage) {
this(theName, elements); this(theName, elements);
stageNumber = stage; stageNumber = stage;
} }
public NamedVector (String theName, CheckBoxNode elements[]) { public NamedVector(String theName, CheckBoxNode elements[]) {
name = theName; name = theName;
for (int i = 0, n = elements.length; i < n; i++) { for (int i = 0, n = elements.length; i < n; i++) {
add(elements[i]); add(elements[i]);
} }
} }
public String toString () { @Override
return name; public String toString() {
} return name;
}
public int getStage () { public int getStage() {
return stageNumber; return stageNumber;
} }
} }