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,238 +15,245 @@ 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)
throws ExpandVetoException { @Override
throw new ExpandVetoException(e, "you can't collapse this JTree"); public void treeWillCollapse(TreeExpansionEvent e)
} 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.
* /**
* @param rocketName the name of the rocket * Factory method to create a specialized JTree. This version is for a rocket with only one stage.
* *
* @return an instance of JTree * @param rocketName the name of the rocket
*/ *
public static RocketPrintTree create (String rocketName) { * @return an instance of JTree
Vector root = new Vector(); */
root.add(new NamedVector(rocketName != null ? rocketName : "Rocket", createPrintTreeNode(false))); public static RocketPrintTree create(String rocketName) {
Vector root = new Vector();
RocketPrintTree tree = new RocketPrintTree(root); root.add(new NamedVector(rocketName != null ? rocketName : "Rocket", createPrintTreeNode(false)));
tree.addTreeWillExpandListener RocketPrintTree tree = new RocketPrintTree(root);
(new TreeWillExpandListener() {
public void treeWillExpand (TreeExpansionEvent e) { tree.addTreeWillExpandListener
} (new TreeWillExpandListener() {
@Override
public void treeWillCollapse (TreeExpansionEvent e) public void treeWillExpand(TreeExpansionEvent e) {
throws ExpandVetoException { }
throw new ExpandVetoException(e, "you can't collapse this JTree");
} @Override
}); public void treeWillCollapse(TreeExpansionEvent e)
throws ExpandVetoException {
return tree; throw new ExpandVetoException(e, "you can't collapse this JTree");
} }
});
/**
* This tree needs to have access both to the normal selection model (for the textual row) which is managed by the return tree;
* 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
* construction. /**
* <p/> * This tree needs to have access both to the normal selection model (for the textual row) which is managed by the
* TODO: Ensure these circular references get cleaned up properly at dialog disposal so everything can be GC'd. * 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
* @param checkBoxSelectionModel the selection model used to keep track of the check box state * construction.
*/ * <p/>
public void setCheckBoxSelectionModel (TreeSelectionModel checkBoxSelectionModel) { * TODO: Ensure these circular references get cleaned up properly at dialog disposal so everything can be GC'd.
theCheckBoxSelectionModel = checkBoxSelectionModel; *
} * @param checkBoxSelectionModel the selection model used to keep track of the check box state
*/
/** public void setCheckBoxSelectionModel(TreeSelectionModel checkBoxSelectionModel) {
* Add a selection path to the internal check box selection model. The normal JTree selection model is unaffected. theCheckBoxSelectionModel = checkBoxSelectionModel;
* This has the effect of "selecting" the check box, but not highlighting the row. }
*
* @param path the path (row) /**
*/ * Add a selection path to the internal check box selection model. The normal JTree selection model is unaffected.
public void addSelectionPath (TreePath path) { * This has the effect of "selecting" the check box, but not highlighting the row.
theCheckBoxSelectionModel.addSelectionPath(path); *
} * @param path the path (row)
*/
/** @Override
* Helper to construct a named vector. public void addSelectionPath(TreePath path) {
* theCheckBoxSelectionModel.addSelectionPath(path);
* @param name the name of the vector }
* @param nodes the array of nodes to put into the vector
* @param stage the stage number /**
* * Helper to construct a named vector.
* @return a NamedVector suitable for adding to a JTree *
*/ * @param name the name of the vector
private static Vector createNamedVector (String name, CheckBoxNode[] nodes, int stage) { * @param nodes the array of nodes to put into the vector
return new NamedVector(name, nodes, stage); * @param stage the stage number
} *
* @return a NamedVector suitable for adding to a JTree
/** */
* Helper to construct the set of check box rows for each stage. private static Vector createNamedVector(String name, CheckBoxNode[] nodes, int stage) {
* return new NamedVector(name, nodes, stage);
* @param onlyStageSpecific if true then only stage specific OpenRocketPrintable rows are represented (in this part }
* of the tree).
* /**
* @return an array of CheckBoxNode * Helper to construct the set of check box rows for each stage.
*/ *
private static CheckBoxNode[] createPrintTreeNode (boolean onlyStageSpecific) { * @param onlyStageSpecific if true then only stage specific OpenRocketPrintable rows are represented (in this part
List<CheckBoxNode> nodes = new ArrayList<CheckBoxNode>(); * of the tree).
OpenRocketPrintable[] printables = OpenRocketPrintable.values(); *
for (OpenRocketPrintable openRocketPrintable : printables) { * @return an array of CheckBoxNode
if (!onlyStageSpecific || openRocketPrintable.isStageSpecific() ) { */
nodes.add(new CheckBoxNode(openRocketPrintable.getDescription(), private static CheckBoxNode[] createPrintTreeNode(boolean onlyStageSpecific) {
INITIAL_CHECKBOX_SELECTED)); List<CheckBoxNode> nodes = new ArrayList<CheckBoxNode>();
} OpenRocketPrintable[] printables = OpenRocketPrintable.values();
} for (OpenRocketPrintable openRocketPrintable : printables) {
return nodes.toArray(new CheckBoxNode[nodes.size()]); if (!onlyStageSpecific || openRocketPrintable.isStageSpecific()) {
} nodes.add(new CheckBoxNode(openRocketPrintable.getDescription(),
INITIAL_CHECKBOX_SELECTED));
/** }
* Get the set of items to be printed, as selected by the user. }
* return nodes.toArray(new CheckBoxNode[nodes.size()]);
* @return the things to be printed, returned as an Iterator<PrintableContext> }
*/
public Iterator<PrintableContext> getToBePrinted () { /**
final DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) getModel().getRoot(); * Get the set of items to be printed, as selected by the user.
PrintableContext pc = new PrintableContext(); *
add(pc, mutableTreeNode); * @return the things to be printed, returned as an Iterator<PrintableContext>
return pc.iterator(); */
} public Iterator<PrintableContext> getToBePrinted() {
final DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) getModel().getRoot();
/** PrintableContext pc = new PrintableContext();
* Walk a tree, finding everything that has been selected and aggregating it into something that can be iterated upon add(pc, mutableTreeNode);
* This method is recursive. return pc.iterator();
* }
* @param pc the printable context that aggregates the choices into an iterator
* @param theMutableTreeNode the root node /**
*/ * Walk a tree, finding everything that has been selected and aggregating it into something that can be iterated upon
private void add (final PrintableContext pc, final DefaultMutableTreeNode theMutableTreeNode) { * This method is recursive.
int children = theMutableTreeNode.getChildCount(); *
for (int x = 0; x < children; x++) { * @param pc the printable context that aggregates the choices into an iterator
* @param theMutableTreeNode the root node
final DefaultMutableTreeNode at = (DefaultMutableTreeNode) theMutableTreeNode.getChildAt(x); */
if (at.getUserObject() instanceof CheckBoxNode) { private void add(final PrintableContext pc, final DefaultMutableTreeNode theMutableTreeNode) {
CheckBoxNode cbn = (CheckBoxNode) at.getUserObject(); int children = theMutableTreeNode.getChildCount();
if (cbn.isSelected()) { for (int x = 0; x < children; x++) {
final OpenRocketPrintable printable = OpenRocketPrintable.findByDescription(cbn.getText());
pc.add( final DefaultMutableTreeNode at = (DefaultMutableTreeNode) theMutableTreeNode.getChildAt(x);
printable.isStageSpecific() ? ((NamedVector) theMutableTreeNode.getUserObject()) if (at.getUserObject() instanceof CheckBoxNode) {
.getStage() : null, CheckBoxNode cbn = (CheckBoxNode) at.getUserObject();
printable); if (cbn.isSelected()) {
} final OpenRocketPrintable printable = OpenRocketPrintable.findByDescription(cbn.getText());
} pc.add(
add(pc, at); printable.isStageSpecific() ? ((NamedVector) theMutableTreeNode.getUserObject())
} .getStage() : null,
} printable);
}
}
add(pc, at);
}
}
} }
/** /**
* 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 () {
return stageNumber; public int getStage() {
} return stageNumber;
}
} }