From: plaa Date: Sun, 9 Jan 2011 09:02:58 +0000 (+0000) Subject: merge fixing X-Git-Tag: upstream/1.1.4^2~8 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=c43508a326870b1ece3bca7d2b57149507ee607c;p=debian%2Fopenrocket merge fixing git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@97 180e2498-e6e9-4542-8430-84ac67f01cd8 --- diff --git a/src/net/sf/openrocket/gui/print/components/RocketPrintTree.java b/src/net/sf/openrocket/gui/print/components/RocketPrintTree.java index b250abef..d3de7bec 100644 --- a/src/net/sf/openrocket/gui/print/components/RocketPrintTree.java +++ b/src/net/sf/openrocket/gui/print/components/RocketPrintTree.java @@ -3,10 +3,10 @@ */ package net.sf.openrocket.gui.print.components; -import net.sf.openrocket.gui.print.OpenRocketPrintable; -import net.sf.openrocket.gui.print.PrintableContext; -import net.sf.openrocket.rocketcomponent.RocketComponent; -import net.sf.openrocket.rocketcomponent.Stage; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; import javax.swing.JTree; import javax.swing.event.TreeExpansionEvent; @@ -15,238 +15,245 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.ExpandVetoException; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Vector; + +import net.sf.openrocket.gui.print.OpenRocketPrintable; +import net.sf.openrocket.gui.print.PrintableContext; +import net.sf.openrocket.rocketcomponent.RocketComponent; +import net.sf.openrocket.rocketcomponent.Stage; /** * A specialized JTree for displaying various rocket items that can be printed. */ public class RocketPrintTree extends JTree { - - /** - * All check boxes are initially set to true (selected). - */ - public static final boolean INITIAL_CHECKBOX_SELECTED = true; - - /** - * The selection model that tracks the check box state. - */ - private TreeSelectionModel theCheckBoxSelectionModel; - - /** - * Constructor. - * - * @param root the vector of check box nodes (rows) to place into the tree - */ - private RocketPrintTree (Vector root) { - super(root); - - //Remove the little down and sideways arrows. These are not needed because the tree expansion is fixed. - ((javax.swing.plaf.basic.BasicTreeUI) this.getUI()). - setExpandedIcon(null); - ((javax.swing.plaf.basic.BasicTreeUI) this.getUI()). - setCollapsedIcon(null); - } - - /** - * 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 stages the array of all stages - * - * @return an instance of JTree - */ - public static RocketPrintTree create (String rocketName, RocketComponent[] stages) { - Vector root = new Vector(); - Vector toAddTo = root; - - if (stages != null) { - if (stages.length > 1) { - final Vector parent = new NamedVector(rocketName != null ? rocketName : "Rocket"); - - root.add(parent); - toAddTo = parent; - } - for (RocketComponent stage : stages) { - if (stage instanceof Stage) { - toAddTo.add(createNamedVector(stage.getName(), createPrintTreeNode(true), stage.getStageNumber())); - } - } - } - toAddTo.add(new CheckBoxNode(OpenRocketPrintable.DESIGN_REPORT.getDescription(), - INITIAL_CHECKBOX_SELECTED)); - - RocketPrintTree tree = new RocketPrintTree(root); - - tree.addTreeWillExpandListener - (new TreeWillExpandListener() { - public void treeWillExpand (TreeExpansionEvent e) { - } - - public void treeWillCollapse (TreeExpansionEvent e) - throws ExpandVetoException { - throw new ExpandVetoException(e, "you can't collapse this JTree"); - } - }); - - 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 - * - * @return an instance of JTree - */ - public static RocketPrintTree create (String rocketName) { - Vector root = new Vector(); - root.add(new NamedVector(rocketName != null ? rocketName : "Rocket", createPrintTreeNode(false))); - - RocketPrintTree tree = new RocketPrintTree(root); - - tree.addTreeWillExpandListener - (new TreeWillExpandListener() { - public void treeWillExpand (TreeExpansionEvent e) { - } - - public void treeWillCollapse (TreeExpansionEvent e) - throws ExpandVetoException { - throw new ExpandVetoException(e, "you can't collapse this JTree"); - } - }); - - return tree; - } - - /** - * 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 - * set the model back onto this class. Because of some unfortunate circular dependencies this cannot be set at - * construction. - *

- * 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 - */ - public void setCheckBoxSelectionModel (TreeSelectionModel checkBoxSelectionModel) { - theCheckBoxSelectionModel = checkBoxSelectionModel; - } - - /** - * 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. - * - * @param path the path (row) - */ - public void addSelectionPath (TreePath path) { - theCheckBoxSelectionModel.addSelectionPath(path); - } - - /** - * Helper to construct a named vector. - * - * @param name the name of the vector - * @param nodes the array of nodes to put into the vector - * @param stage the stage number - * - * @return a NamedVector suitable for adding to a JTree - */ - private static Vector createNamedVector (String name, CheckBoxNode[] nodes, int stage) { - return new NamedVector(name, nodes, 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 - * of the tree). - * - * @return an array of CheckBoxNode - */ - private static CheckBoxNode[] createPrintTreeNode (boolean onlyStageSpecific) { - List nodes = new ArrayList(); - OpenRocketPrintable[] printables = OpenRocketPrintable.values(); - for (OpenRocketPrintable openRocketPrintable : printables) { - if (!onlyStageSpecific || openRocketPrintable.isStageSpecific() ) { - nodes.add(new CheckBoxNode(openRocketPrintable.getDescription(), - INITIAL_CHECKBOX_SELECTED)); - } - } - return nodes.toArray(new CheckBoxNode[nodes.size()]); - } - - /** - * Get the set of items to be printed, as selected by the user. - * - * @return the things to be printed, returned as an Iterator - */ - public Iterator getToBePrinted () { - final DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) getModel().getRoot(); - PrintableContext pc = new PrintableContext(); - add(pc, mutableTreeNode); - return pc.iterator(); - } - - /** - * Walk a tree, finding everything that has been selected and aggregating it into something that can be iterated upon - * This method is recursive. - * - * @param pc the printable context that aggregates the choices into an iterator - * @param theMutableTreeNode the root node - */ - private void add (final PrintableContext pc, final DefaultMutableTreeNode theMutableTreeNode) { - int children = theMutableTreeNode.getChildCount(); - for (int x = 0; x < children; x++) { - - final DefaultMutableTreeNode at = (DefaultMutableTreeNode) theMutableTreeNode.getChildAt(x); - if (at.getUserObject() instanceof CheckBoxNode) { - CheckBoxNode cbn = (CheckBoxNode) at.getUserObject(); - if (cbn.isSelected()) { - final OpenRocketPrintable printable = OpenRocketPrintable.findByDescription(cbn.getText()); - pc.add( - printable.isStageSpecific() ? ((NamedVector) theMutableTreeNode.getUserObject()) - .getStage() : null, - printable); - } - } - add(pc, at); - } - } - + + /** + * All check boxes are initially set to true (selected). + */ + public static final boolean INITIAL_CHECKBOX_SELECTED = true; + + /** + * The selection model that tracks the check box state. + */ + private TreeSelectionModel theCheckBoxSelectionModel; + + /** + * Constructor. + * + * @param root the vector of check box nodes (rows) to place into the tree + */ + private RocketPrintTree(Vector root) { + super(root); + + //Remove the little down and sideways arrows. These are not needed because the tree expansion is fixed. + ((javax.swing.plaf.basic.BasicTreeUI) this.getUI()). + setExpandedIcon(null); + ((javax.swing.plaf.basic.BasicTreeUI) this.getUI()). + setCollapsedIcon(null); + } + + /** + * 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 stages the array of all stages + * + * @return an instance of JTree + */ + public static RocketPrintTree create(String rocketName, List stages) { + Vector root = new Vector(); + Vector toAddTo = root; + + if (stages != null) { + if (stages.size() > 1) { + final Vector parent = new NamedVector(rocketName != null ? rocketName : "Rocket"); + + root.add(parent); + toAddTo = parent; + } + for (RocketComponent stage : stages) { + if (stage instanceof Stage) { + toAddTo.add(createNamedVector(stage.getName(), createPrintTreeNode(true), stage.getStageNumber())); + } + } + } + toAddTo.add(new CheckBoxNode(OpenRocketPrintable.DESIGN_REPORT.getDescription(), + INITIAL_CHECKBOX_SELECTED)); + + RocketPrintTree tree = new RocketPrintTree(root); + + tree.addTreeWillExpandListener + (new TreeWillExpandListener() { + @Override + public void treeWillExpand(TreeExpansionEvent e) { + } + + @Override + public void treeWillCollapse(TreeExpansionEvent e) + throws ExpandVetoException { + throw new ExpandVetoException(e, "you can't collapse this JTree"); + } + }); + + 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 + * + * @return an instance of JTree + */ + public static RocketPrintTree create(String rocketName) { + Vector root = new Vector(); + root.add(new NamedVector(rocketName != null ? rocketName : "Rocket", createPrintTreeNode(false))); + + RocketPrintTree tree = new RocketPrintTree(root); + + tree.addTreeWillExpandListener + (new TreeWillExpandListener() { + @Override + public void treeWillExpand(TreeExpansionEvent e) { + } + + @Override + public void treeWillCollapse(TreeExpansionEvent e) + throws ExpandVetoException { + throw new ExpandVetoException(e, "you can't collapse this JTree"); + } + }); + + return tree; + } + + /** + * 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 + * set the model back onto this class. Because of some unfortunate circular dependencies this cannot be set at + * construction. + *

+ * 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 + */ + public void setCheckBoxSelectionModel(TreeSelectionModel checkBoxSelectionModel) { + theCheckBoxSelectionModel = checkBoxSelectionModel; + } + + /** + * 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. + * + * @param path the path (row) + */ + @Override + public void addSelectionPath(TreePath path) { + theCheckBoxSelectionModel.addSelectionPath(path); + } + + /** + * Helper to construct a named vector. + * + * @param name the name of the vector + * @param nodes the array of nodes to put into the vector + * @param stage the stage number + * + * @return a NamedVector suitable for adding to a JTree + */ + private static Vector createNamedVector(String name, CheckBoxNode[] nodes, int stage) { + return new NamedVector(name, nodes, 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 + * of the tree). + * + * @return an array of CheckBoxNode + */ + private static CheckBoxNode[] createPrintTreeNode(boolean onlyStageSpecific) { + List nodes = new ArrayList(); + OpenRocketPrintable[] printables = OpenRocketPrintable.values(); + for (OpenRocketPrintable openRocketPrintable : printables) { + if (!onlyStageSpecific || openRocketPrintable.isStageSpecific()) { + nodes.add(new CheckBoxNode(openRocketPrintable.getDescription(), + INITIAL_CHECKBOX_SELECTED)); + } + } + return nodes.toArray(new CheckBoxNode[nodes.size()]); + } + + /** + * Get the set of items to be printed, as selected by the user. + * + * @return the things to be printed, returned as an Iterator + */ + public Iterator getToBePrinted() { + final DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) getModel().getRoot(); + PrintableContext pc = new PrintableContext(); + add(pc, mutableTreeNode); + return pc.iterator(); + } + + /** + * Walk a tree, finding everything that has been selected and aggregating it into something that can be iterated upon + * This method is recursive. + * + * @param pc the printable context that aggregates the choices into an iterator + * @param theMutableTreeNode the root node + */ + private void add(final PrintableContext pc, final DefaultMutableTreeNode theMutableTreeNode) { + int children = theMutableTreeNode.getChildCount(); + for (int x = 0; x < children; x++) { + + final DefaultMutableTreeNode at = (DefaultMutableTreeNode) theMutableTreeNode.getChildAt(x); + if (at.getUserObject() instanceof CheckBoxNode) { + CheckBoxNode cbn = (CheckBoxNode) at.getUserObject(); + if (cbn.isSelected()) { + final OpenRocketPrintable printable = OpenRocketPrintable.findByDescription(cbn.getText()); + pc.add( + 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. */ class NamedVector extends Vector { - String name; - - int stageNumber; - - public NamedVector (String theName) { - name = theName; - } - - public NamedVector (String theName, CheckBoxNode elements[], int stage) { - this(theName, elements); - stageNumber = stage; - } - - public NamedVector (String theName, CheckBoxNode elements[]) { - name = theName; - for (int i = 0, n = elements.length; i < n; i++) { - add(elements[i]); - } - } - - public String toString () { - return name; - } - - public int getStage () { - return stageNumber; - } + String name; + + int stageNumber; + + public NamedVector(String theName) { + name = theName; + } + + public NamedVector(String theName, CheckBoxNode elements[], int stage) { + this(theName, elements); + stageNumber = stage; + } + + public NamedVector(String theName, CheckBoxNode elements[]) { + name = theName; + for (int i = 0, n = elements.length; i < n; i++) { + add(elements[i]); + } + } + + @Override + public String toString() { + return name; + } + + public int getStage() { + return stageNumber; + } } \ No newline at end of file