< prev index next >

src/java.desktop/share/classes/javax/swing/tree/VariableHeightLayoutCache.java

Print this page
rev 47712 : Fixed JDK-8190281: Code cleanup in src\java.desktop\sharelasses\javax\swing ree\VariableHeightLayoutCache.java
Remove the streams related code changes, since it is increasing the class size by 1kb.
Moved the increment of nextIndex back into the conditional, since
moving it out makes it a functional changes, and needs to be accompanied with a testcase.
Properly align the code to the previous state at line no 1000,
so that unnecessary changes are not introduced.
Fixed condition check from == to <=.
Call getRowCount() only once, and cache it.

@@ -923,28 +923,26 @@
       * Returns the index of the row containing location.  If there
       * are no rows, -1 is returned.  If location is beyond the last
       * row index, the last row index is returned.
       */
     private int getRowContainingYLocation(int location) {
-        if(isFixedRowHeight()) {
-            if(getRowCount() == 0)
+        int max = getRowCount();
+
+        if(max <= 0)
                 return -1;
+        if(isFixedRowHeight()) {
             return Math.max(0, Math.min(getRowCount() - 1,
                                         location / getRowHeight()));
         }
 
-        int                    max, maxY, mid, min, minY;
-        TreeStateNode          node;
+        int min = 0, mid = 0;
 
-        if((max = getRowCount()) <= 0)
-            return -1;
-        mid = min = 0;
         while(min < max) {
             mid = (max - min) / 2 + min;
-            node = (TreeStateNode)visibleNodes.elementAt(mid);
-            minY = node.getYOrigin();
-            maxY = minY + node.getPreferredHeight();
+            TreeStateNode node = (TreeStateNode)visibleNodes.elementAt(mid);
+            int minY = node.getYOrigin();
+            int maxY = minY + node.getPreferredHeight();
             if(location < minY) {
                 max = mid - 1;
             }
             else if(location >= maxY) {
                 min = mid + 1;

@@ -1006,13 +1004,13 @@
             node = this.getNode(counter);
             nodeWidth = node.getPreferredWidth() + node.getXOrigin();
             if(nodeWidth > maxWidth)
                 maxWidth = nodeWidth;
         }
+
         return maxWidth;
     }
-
     /**
       * Responsible for creating a TreeStateNode that will be used
       * to track display information about value.
       */
     private TreeStateNode createNodeForValue(Object value) {

@@ -1360,21 +1358,15 @@
             Rectangle       bounds = getNodeDimensions(this.getUserObject(),
                                                        index, getLevel(),
                                                        isExpanded(),
                                                        boundsBuffer);
 
-            if(bounds == null) {
+            if(bounds == null || bounds.height == 0) {
                 xOrigin = 0;
                 preferredWidth = preferredHeight = 0;
                 updateNodeSizes = true;
-            }
-            else if(bounds.height == 0) {
-                xOrigin = 0;
-                preferredWidth = preferredHeight = 0;
-                updateNodeSizes = true;
-            }
-            else {
+            } else {
                 xOrigin = bounds.x;
                 preferredWidth = bounds.width;
                 if(isFixedRowHeight())
                     preferredHeight = getRowHeight();
                 else

@@ -1475,43 +1467,28 @@
                 if (!hasBeenExpanded) {
                     TreeStateNode  newNode;
                     Object         realNode = getValue();
                     TreeModel      treeModel = getModel();
                     int            count = treeModel.getChildCount(realNode);
-
+                    int offset = originalRow == -1 ? -1 : originalRow + 1;
                     hasBeenExpanded = true;
-                    if(originalRow == -1) {
-                        for (int i = 0; i < count; i++) {
-                            newNode = createNodeForValue(treeModel.getChild
-                                                            (realNode, i));
-                            this.add(newNode);
-                            newNode.updatePreferredSize(-1);
-                        }
-                    }
-                    else {
-                        int offset = originalRow + 1;
+
                         for (int i = 0; i < count; i++) {
                             newNode = createNodeForValue(treeModel.getChild
                                                        (realNode, i));
                             this.add(newNode);
                             newNode.updatePreferredSize(offset);
                         }
                     }
-                }
 
                 int i = originalRow;
                 Enumeration<TreeNode> cursor = preorderEnumeration();
                 cursor.nextElement(); // don't add me, I'm already in
 
-                int newYOrigin;
+                int newYOrigin = isFixed || (this == root && !isRootVisible()) ?
+                                    0 : getYOrigin() + this.getPreferredHeight();
 
-                if(isFixed)
-                    newYOrigin = 0;
-                else if(this == root && !isRootVisible())
-                    newYOrigin = 0;
-                else
-                    newYOrigin = getYOrigin() + this.getPreferredHeight();
                 TreeStateNode   aNode;
                 if(!isFixed) {
                     while (cursor.hasMoreElements()) {
                         aNode = (TreeStateNode) cursor.nextElement();
                         if(!updateNodeSizes && !aNode.hasValidSize())

@@ -1742,18 +1719,14 @@
          * the number of children of parent.
          */
         protected boolean updateNextIndex() {
             // nextIndex == -1 identifies receiver, make sure is expanded
             // before descend.
-            if(nextIndex == -1 && !parent.isExpanded())
-                return false;
-
-            // Check that it can have kids
-            if(childCount == 0)
-                return false;
-            // Make sure next index not beyond child count.
-            else if(++nextIndex >= childCount)
+            if((nextIndex == -1 && !parent.isExpanded()) ||
+                childCount == 0 || // Check that it can have kids
+                ++nextIndex >= childCount) // Make sure next index not beyond
+                                             // child count.
                 return false;
 
             TreeStateNode       child = (TreeStateNode)parent.
                                         getChildAt(nextIndex);
 
< prev index next >