changeset 6222:a874c214a9c7

Small TitledPaneSkin cleanup to simplify code, improve animation quality and remove unnecessary scenegraph layer.
author jgiles
date Fri, 31 Jan 2014 11:14:18 +1300
parents 20396f6a7fac
children d32812219304
files modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TitledPaneBehavior.java modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TitledPaneSkin.java
diffstat 2 files changed, 41 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TitledPaneBehavior.java	Wed Jan 29 14:54:50 2014 +1300
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/behavior/TitledPaneBehavior.java	Fri Jan 31 11:14:18 2014 +1300
@@ -77,8 +77,8 @@
             //tps.getContentRegion().getImpl_traversalEngine().getTopLeftFocusableNode();
             // Is there any focusable node in the TitledPane content
             if (titledPane.isExpanded() &&
-                (!tps.getContentRegion().getImpl_traversalEngine().registeredNodes.isEmpty() ||
-                 TabPaneBehavior.getFirstPopulatedInnerTraversalEngine(tps.getContentRegion().getChildren()) != null)) {
+                (!tps.getContentContainer().getImpl_traversalEngine().registeredNodes.isEmpty() ||
+                 TabPaneBehavior.getFirstPopulatedInnerTraversalEngine(tps.getContentContainer().getChildren()) != null)) {
                 /*
                 ** if we have the focus owner then traverse from it, otherwise
                 ** request focus in the top-left
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TitledPaneSkin.java	Wed Jan 29 14:54:50 2014 +1300
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TitledPaneSkin.java	Fri Jan 31 11:14:18 2014 +1300
@@ -25,17 +25,16 @@
 
 package com.sun.javafx.scene.control.skin;
 
+import com.sun.javafx.PlatformUtil;
 import javafx.animation.Animation.Status;
 import javafx.animation.Interpolator;
 import javafx.animation.KeyFrame;
 import javafx.animation.KeyValue;
 import javafx.animation.Timeline;
 import javafx.beans.property.DoubleProperty;
-import javafx.beans.property.DoublePropertyBase;
 import javafx.beans.property.SimpleDoubleProperty;
 import javafx.event.ActionEvent;
 import javafx.event.EventHandler;
-import javafx.geometry.Bounds;
 import javafx.geometry.HPos;
 import javafx.geometry.Pos;
 import javafx.geometry.VPos;
@@ -44,7 +43,6 @@
 import javafx.scene.control.ContentDisplay;
 import javafx.scene.control.TitledPane;
 import javafx.scene.input.MouseEvent;
-import javafx.scene.layout.Region;
 import javafx.scene.layout.StackPane;
 import javafx.scene.shape.Rectangle;
 import javafx.util.Duration;
@@ -52,7 +50,6 @@
 import com.sun.javafx.scene.control.behavior.TitledPaneBehavior;
 import com.sun.javafx.scene.traversal.Direction;
 import com.sun.javafx.scene.traversal.TraversalEngine;
-import com.sun.javafx.scene.traversal.TraverseListener;
 import javafx.beans.binding.DoubleBinding;
 import javafx.geometry.Insets;
 import javafx.scene.control.Accordion;
@@ -66,9 +63,14 @@
     public static final int MIN_HEADER_HEIGHT = 22;
     public static final Duration TRANSITION_DURATION = new Duration(350.0);
 
+    // caching results in poorer looking text (it is blurry), so we don't do it
+    // unless on a low powered device (admittedly the test below isn't a great
+    // indicator of power, but it'll do for now).
+    private static final boolean CACHE_ANIMATION = PlatformUtil.isEmbedded();
+
     private final TitleRegion titleRegion;
     private final StackPane contentContainer;
-    private final Content contentRegion;
+    private Node content;
     private Timeline timeline;
     private double transitionStartValue;
     private Rectangle clipRect;
@@ -84,7 +86,7 @@
         transitionStartValue = 0;
         titleRegion = new TitleRegion();
 
-        contentRegion = new Content(getSkinnable().getContent());
+        content = getSkinnable().getContent();
         contentContainer = new StackPane() {
             {
                 getStyleClass().setAll("content");
@@ -94,7 +96,15 @@
                 // content will be center aligned.
                 setAlignment(Pos.BOTTOM_CENTER);
 
-                getChildren().setAll(contentRegion);
+                if (content != null) {
+                    getChildren().setAll(content);
+                }
+
+                setImpl_traversalEngine(new TraversalEngine(this, false) {
+                    @Override public void trav(Node owner, Direction dir) {
+                        super.trav(owner, dir);
+                    }
+                });
             }
         };
         contentContainer.setClip(clipRect);
@@ -104,8 +114,8 @@
             setExpanded(titledPane.isExpanded());
         } else {
             setTransition(0.0f);
-            if (contentRegion.getContent() != null) {
-                contentRegion.getContent().setVisible(false);
+            if (content != null) {
+                content.setVisible(false);
             }
         }
 
@@ -124,15 +134,20 @@
         vpos = pos == null ? VPos.CENTER : pos.getVpos();
     }
 
-    public StackPane getContentRegion() {
-        return contentRegion;
+    public StackPane getContentContainer() {
+        return contentContainer;
     }
 
     @Override
     protected void handleControlPropertyChanged(String property) {
         super.handleControlPropertyChanged(property);
         if ("CONTENT".equals(property)) {
-            contentRegion.setContent(getSkinnable().getContent());
+            content = getSkinnable().getContent();
+            if (content == null) {
+                contentContainer.getChildren().clear();
+            } else {
+                contentContainer.getChildren().setAll(content);
+            }
         } else if ("EXPANDED".equals(property)) {
             setExpanded(getSkinnable().isExpanded());
         } else if ("COLLAPSIBLE".equals(property)) {
@@ -179,8 +194,8 @@
             } else {
                 setTransition(0.0f);
             }
-            if (contentRegion.getContent() != null) {
-                contentRegion.getContent().setVisible(expanded);
+            if (content != null) {
+                content.setVisible(expanded);
              }
             getSkinnable().requestLayout();
         }
@@ -235,7 +250,7 @@
     }
 
     @Override protected double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
-        return Math.max(MIN_HEADER_HEIGHT, snapSize(titleRegion.prefHeight(-1)));
+        return Math.max(MIN_HEADER_HEIGHT, snapSize(titleRegion.prefHeight(width)));
     }
 
     @Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
@@ -245,8 +260,8 @@
     }
 
     @Override protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
-        double headerHeight = Math.max(MIN_HEADER_HEIGHT, snapSize(titleRegion.prefHeight(-1)));
-        double contentHeight = contentContainer.prefHeight(-1) * getTransition();
+        double headerHeight = Math.max(MIN_HEADER_HEIGHT, snapSize(titleRegion.prefHeight(width)));
+        double contentHeight = contentContainer.prefHeight(width) * getTransition();
         return headerHeight + snapSize(contentHeight) + topInset + bottomInset;
     }
 
@@ -266,7 +281,7 @@
     }
 
     private void doAnimationTransition() {
-        if (contentRegion.getContent() == null) {
+        if (content == null) {
             return;
         }
 
@@ -289,8 +304,8 @@
                 new EventHandler<ActionEvent>() {
                     @Override public void handle(ActionEvent event) {
                         // start expand
-                        contentRegion.getContent().setCache(true);
-                        contentRegion.getContent().setVisible(true);
+                        if (CACHE_ANIMATION) content.setCache(true);
+                        content.setVisible(true);
                     }
                 },
                 new KeyValue(transitionProperty(), transitionStartValue)
@@ -301,7 +316,7 @@
                     new EventHandler<ActionEvent>() {
                     @Override public void handle(ActionEvent event) {
                         // end expand
-                        contentRegion.getContent().setCache(false);
+                        if (CACHE_ANIMATION) content.setCache(false);
                     }
                 },
                 new KeyValue(transitionProperty(), 1, Interpolator.LINEAR)
@@ -313,7 +328,7 @@
                 new EventHandler<ActionEvent>() {
                     @Override public void handle(ActionEvent event) {
                         // Start collapse
-                        contentRegion.getContent().setCache(true);
+                        if (CACHE_ANIMATION) content.setCache(true);
                     }
                 },
                 new KeyValue(transitionProperty(), transitionStartValue)
@@ -324,8 +339,8 @@
                 new EventHandler<ActionEvent>() {
                     @Override public void handle(ActionEvent event) {
                         // end collapse
-                        contentRegion.getContent().setVisible(false);
-                        contentRegion.getContent().setCache(false);
+                        content.setVisible(false);
+                        if (CACHE_ANIMATION) content.setCache(false);
                     }
                 },
                 new KeyValue(transitionProperty(), 0, Interpolator.LINEAR)
@@ -532,35 +547,4 @@
             return h + labelPadding.getTop() + labelPadding.getBottom();
         }
     }
-
-    class Content extends StackPane {
-        private Node content;
-        private TraversalEngine engine;
-        private Direction direction;
-
-        public Content(Node n) {
-            setContent(n);
-
-            engine = new TraversalEngine(this, false) {
-                @Override public void trav(Node owner, Direction dir) {
-                    direction = dir;
-                    super.trav(owner, dir);
-                }
-            };
-            setImpl_traversalEngine(engine);
-        }
-
-        public final void setContent(Node n) {
-            this.content = n;
-            getChildren().clear();
-            if (n != null) {
-                content.setVisible(getSkinnable().isExpanded());
-                getChildren().setAll(n);
-            }
-        }
-
-        public final Node getContent() {
-            return content;
-        }
-    }
 }