< prev index next >

src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java

Print this page
rev 1527 : 6727662: Code improvement and warnings removing from swing packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: malenkov


  47  *
  48  * @author Scott Violet
  49  */
  50 
  51 public class FixedHeightLayoutCache extends AbstractLayoutCache {
  52     /** Root node. */
  53     private FHTreeStateNode    root;
  54 
  55     /** Number of rows currently visible. */
  56     private int                rowCount;
  57 
  58     /**
  59      * Used in getting sizes for nodes to avoid creating a new Rectangle
  60      * every time a size is needed.
  61      */
  62     private Rectangle          boundsBuffer;
  63 
  64     /**
  65      * Maps from TreePath to a FHTreeStateNode.
  66      */
  67     private Hashtable          treePathMapping;
  68 
  69     /**
  70      * Used for getting path/row information.
  71      */
  72     private SearchInfo         info;
  73 
  74     private Stack              tempStacks;
  75 
  76 
  77     public FixedHeightLayoutCache() {
  78         super();
  79         tempStacks = new Stack();
  80         boundsBuffer = new Rectangle();
  81         treePathMapping = new Hashtable();
  82         info = new SearchInfo();
  83         setRowHeight(1);
  84     }
  85 
  86     /**
  87      * Sets the TreeModel that will provide the data.
  88      *
  89      * @param newModel the TreeModel that is to provide the data
  90      */
  91     public void setModel(TreeModel newModel) {
  92         super.setModel(newModel);
  93         rebuild(false);
  94     }
  95 
  96     /**
  97      * Determines whether or not the root node from
  98      * the TreeModel is visible.
  99      *
 100      * @param rootVisible true if the root node of the tree is to be displayed
 101      * @see #rootVisible


 575 
 576     /**
 577      * Adds a mapping for node.
 578      */
 579     private void addMapping(FHTreeStateNode node) {
 580         treePathMapping.put(node.getTreePath(), node);
 581     }
 582 
 583     /**
 584      * Removes the mapping for a previously added node.
 585      */
 586     private void removeMapping(FHTreeStateNode node) {
 587         treePathMapping.remove(node.getTreePath());
 588     }
 589 
 590     /**
 591      * Returns the node previously added for <code>path</code>. This may
 592      * return null, if you to create a node use getNodeForPath.
 593      */
 594     private FHTreeStateNode getMapping(TreePath path) {
 595         return (FHTreeStateNode)treePathMapping.get(path);
 596     }
 597 
 598     /**
 599      * Sent to completely rebuild the visible tree. All nodes are collapsed.
 600      */
 601     private void rebuild(boolean clearSelection) {
 602         Object            rootUO;
 603 
 604         treePathMapping.clear();
 605         if(treeModel != null && (rootUO = treeModel.getRoot()) != null) {
 606             root = createNodeForValue(rootUO, 0);
 607             root.path = new TreePath(rootUO);
 608             addMapping(root);
 609             if(isRootVisible()) {
 610                 rowCount = 1;
 611                 root.row = 0;
 612             }
 613             else {
 614                 rowCount = 0;
 615                 root.row = -1;


 678      * Messages getTreeNodeForPage(path, onlyIfVisible, shouldCreate,
 679      * path.length) as long as path is non-null and the length is > 0.
 680      * Otherwise returns null.
 681      */
 682     private FHTreeStateNode getNodeForPath(TreePath path,
 683                                              boolean onlyIfVisible,
 684                                              boolean shouldCreate) {
 685         if(path != null) {
 686             FHTreeStateNode      node;
 687 
 688             node = getMapping(path);
 689             if(node != null) {
 690                 if(onlyIfVisible && !node.isVisible())
 691                     return null;
 692                 return node;
 693             }
 694             if(onlyIfVisible)
 695                 return null;
 696 
 697             // Check all the parent paths, until a match is found.
 698             Stack                paths;
 699 
 700             if(tempStacks.size() == 0) {
 701                 paths = new Stack();
 702             }
 703             else {
 704                 paths = (Stack)tempStacks.pop();
 705             }
 706 
 707             try {
 708                 paths.push(path);
 709                 path = path.getParentPath();
 710                 node = null;
 711                 while(path != null) {
 712                     node = getMapping(path);
 713                     if(node != null) {
 714                         // Found a match, create entries for all paths in
 715                         // paths.
 716                         while(node != null && paths.size() > 0) {
 717                             path = (TreePath)paths.pop();
 718                             node = node.createChildFor(path.
 719                                                        getLastPathComponent());
 720                         }
 721                         return node;
 722                     }
 723                     paths.push(path);
 724                     path = path.getParentPath();
 725                 }
 726             }
 727             finally {
 728                 paths.removeAllElements();
 729                 tempStacks.push(paths);
 730             }
 731             // If we get here it means they share a different root!
 732             return null;
 733         }
 734         return null;
 735     }
 736 
 737     /**




  47  *
  48  * @author Scott Violet
  49  */
  50 
  51 public class FixedHeightLayoutCache extends AbstractLayoutCache {
  52     /** Root node. */
  53     private FHTreeStateNode    root;
  54 
  55     /** Number of rows currently visible. */
  56     private int                rowCount;
  57 
  58     /**
  59      * Used in getting sizes for nodes to avoid creating a new Rectangle
  60      * every time a size is needed.
  61      */
  62     private Rectangle          boundsBuffer;
  63 
  64     /**
  65      * Maps from TreePath to a FHTreeStateNode.
  66      */
  67     private Hashtable<TreePath, FHTreeStateNode> treePathMapping;
  68 
  69     /**
  70      * Used for getting path/row information.
  71      */
  72     private SearchInfo         info;
  73 
  74     private Stack<Stack<TreePath>> tempStacks;
  75 
  76 
  77     public FixedHeightLayoutCache() {
  78         super();
  79         tempStacks = new Stack<Stack<TreePath>>();
  80         boundsBuffer = new Rectangle();
  81         treePathMapping = new Hashtable<TreePath, FHTreeStateNode>();
  82         info = new SearchInfo();
  83         setRowHeight(1);
  84     }
  85 
  86     /**
  87      * Sets the TreeModel that will provide the data.
  88      *
  89      * @param newModel the TreeModel that is to provide the data
  90      */
  91     public void setModel(TreeModel newModel) {
  92         super.setModel(newModel);
  93         rebuild(false);
  94     }
  95 
  96     /**
  97      * Determines whether or not the root node from
  98      * the TreeModel is visible.
  99      *
 100      * @param rootVisible true if the root node of the tree is to be displayed
 101      * @see #rootVisible


 575 
 576     /**
 577      * Adds a mapping for node.
 578      */
 579     private void addMapping(FHTreeStateNode node) {
 580         treePathMapping.put(node.getTreePath(), node);
 581     }
 582 
 583     /**
 584      * Removes the mapping for a previously added node.
 585      */
 586     private void removeMapping(FHTreeStateNode node) {
 587         treePathMapping.remove(node.getTreePath());
 588     }
 589 
 590     /**
 591      * Returns the node previously added for <code>path</code>. This may
 592      * return null, if you to create a node use getNodeForPath.
 593      */
 594     private FHTreeStateNode getMapping(TreePath path) {
 595         return treePathMapping.get(path);
 596     }
 597 
 598     /**
 599      * Sent to completely rebuild the visible tree. All nodes are collapsed.
 600      */
 601     private void rebuild(boolean clearSelection) {
 602         Object            rootUO;
 603 
 604         treePathMapping.clear();
 605         if(treeModel != null && (rootUO = treeModel.getRoot()) != null) {
 606             root = createNodeForValue(rootUO, 0);
 607             root.path = new TreePath(rootUO);
 608             addMapping(root);
 609             if(isRootVisible()) {
 610                 rowCount = 1;
 611                 root.row = 0;
 612             }
 613             else {
 614                 rowCount = 0;
 615                 root.row = -1;


 678      * Messages getTreeNodeForPage(path, onlyIfVisible, shouldCreate,
 679      * path.length) as long as path is non-null and the length is > 0.
 680      * Otherwise returns null.
 681      */
 682     private FHTreeStateNode getNodeForPath(TreePath path,
 683                                              boolean onlyIfVisible,
 684                                              boolean shouldCreate) {
 685         if(path != null) {
 686             FHTreeStateNode      node;
 687 
 688             node = getMapping(path);
 689             if(node != null) {
 690                 if(onlyIfVisible && !node.isVisible())
 691                     return null;
 692                 return node;
 693             }
 694             if(onlyIfVisible)
 695                 return null;
 696 
 697             // Check all the parent paths, until a match is found.
 698             Stack<TreePath> paths;
 699 
 700             if(tempStacks.size() == 0) {
 701                 paths = new Stack<TreePath>();
 702             }
 703             else {
 704                 paths = tempStacks.pop();
 705             }
 706 
 707             try {
 708                 paths.push(path);
 709                 path = path.getParentPath();
 710                 node = null;
 711                 while(path != null) {
 712                     node = getMapping(path);
 713                     if(node != null) {
 714                         // Found a match, create entries for all paths in
 715                         // paths.
 716                         while(node != null && paths.size() > 0) {
 717                             path = paths.pop();
 718                             node = node.createChildFor(path.
 719                                                        getLastPathComponent());
 720                         }
 721                         return node;
 722                     }
 723                     paths.push(path);
 724                     path = path.getParentPath();
 725                 }
 726             }
 727             finally {
 728                 paths.removeAllElements();
 729                 tempStacks.push(paths);
 730             }
 731             // If we get here it means they share a different root!
 732             return null;
 733         }
 734         return null;
 735     }
 736 
 737     /**


< prev index next >