changeset 6512:9823085ebf0e

RT-32573: Change Legend to extend from TilePane rather than managing its own layout. Reviewed by: Martin
author David Grieve<david.grieve@oracle.com>
date Wed, 19 Mar 2014 10:42:24 -0400
parents 579fbfe34c8c
children 8a0dd88b5456
files modules/controls/src/main/java/com/sun/javafx/charts/Legend.java modules/controls/src/main/java/javafx/scene/chart/Chart.java
diffstat 2 files changed, 16 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/charts/Legend.java	Tue Mar 18 16:44:34 2014 -0700
+++ b/modules/controls/src/main/java/com/sun/javafx/charts/Legend.java	Wed Mar 19 10:42:24 2014 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -34,17 +34,18 @@
 import javafx.collections.FXCollections;
 import javafx.collections.ListChangeListener;
 import javafx.collections.ObservableList;
-import javafx.geometry.Dimension2D;
+import javafx.geometry.Orientation;
 import javafx.geometry.Pos;
 import javafx.scene.Node;
 import javafx.scene.control.ContentDisplay;
 import javafx.scene.control.Label;
 import javafx.scene.layout.Region;
+import javafx.scene.layout.TilePane;
 
 /**
  * A chart legend that displays a list of items with symbols in a box
  */
-public class Legend extends Region {
+public class Legend extends TilePane {
 
     private static final int GAP = 5;
 
@@ -65,7 +66,7 @@
      */
     private BooleanProperty vertical = new BooleanPropertyBase(false) {
         @Override protected void invalidated() {
-            requestLayout();
+            setOrientation(get() ? Orientation.VERTICAL : Orientation.HORIZONTAL);
         }
 
         @Override
@@ -116,79 +117,14 @@
     // -------------- CONSTRUCTORS ----------------------------------------------
 
     public Legend() {
+        super(GAP, GAP);
+        setTileAlignment(Pos.CENTER_LEFT);
         setItems(FXCollections.<LegendItem>observableArrayList());
         getStyleClass().setAll("chart-legend");
     }
 
     // -------------- METHODS ---------------------------------------------------
 
-    private Dimension2D getTileSize(){
-        double maxWidth = 0;
-        double maxHeight = 0;
-        for(LegendItem item: getItems()) {
-            maxWidth = Math.max(maxWidth, item.label.prefWidth(-1));
-            maxHeight = Math.max(maxHeight, item.label.prefHeight(-1));
-        }
-        return new Dimension2D(Math.ceil(maxWidth), Math.ceil(maxHeight));
-    }
-
-    @Override protected double computePrefWidth(double height) {
-        if (getItems().isEmpty()) return 0; // RT-31157 : no data - dont show legend
-        final double contentHeight = height - snappedTopInset() - snappedBottomInset();
-        Dimension2D tileSize = getTileSize();
-        if(height == -1) {
-            if(columns <= 1) return tileSize.getWidth() + snappedLeftInset() + snappedRightInset();
-        } else {
-            rows = (int) Math.max(1, Math.floor( contentHeight / (tileSize.getHeight() + GAP) ));
-            columns = (int)Math.ceil(getItems().size() / (double)rows);
-        }
-        if(columns == 1) rows = Math.min(rows, getItems().size());
-        return (columns*(tileSize.getWidth()+GAP)) - GAP + snappedLeftInset() + snappedRightInset();
-    }
-
-    @Override protected double computePrefHeight(double width) {
-        if (getItems().isEmpty()) return 0; // RT-31157 : no data - dont show legend
-        final double contentWidth = width - snappedLeftInset() - snappedRightInset();
-        Dimension2D tileSize = getTileSize();
-        if(width == -1) {
-            if(rows <= 1) return tileSize.getHeight() + snappedTopInset() + snappedBottomInset();
-        } else {
-            columns = (int) Math.max(1, Math.floor( contentWidth / (tileSize.getWidth() + GAP) ));
-            rows = (int)Math.ceil(getItems().size() / (double)columns);
-        }
-        if(rows == 1) columns = Math.min(columns, getItems().size());
-        return (rows*(tileSize.getHeight()+GAP)) - GAP + snappedTopInset() + snappedBottomInset();
-    }
-
-    @Override protected void layoutChildren() {
-        Dimension2D tileSize = getTileSize();
-        if(isVertical()) {
-            double left = snappedLeftInset();
-            outer: for (int col=0; col < columns; col++) {
-                double top = snappedTopInset();
-                for (int row=0; row < rows; row++) {
-                    int itemIndex = (col*rows) + row;
-                    if(itemIndex >= getItems().size()) break outer;
-                    getItems().get(itemIndex).label.resizeRelocate(left,top,tileSize.getWidth(),tileSize.getHeight());
-                    top += tileSize.getHeight() + GAP;
-                }
-                left += tileSize.getWidth() + GAP;
-            }
-        } else {
-            double top = snappedTopInset();
-            outer: for (int row=0; row < rows; row++) {
-                double left = snappedLeftInset();
-                for (int col=0; col < columns; col++) {
-                    int itemIndex = (row*columns) + col;
-                    if(itemIndex >= getItems().size()) break outer;
-                    getItems().get(itemIndex).label.resizeRelocate(left,top,tileSize.getWidth(),tileSize.getHeight());
-                    left += tileSize.getWidth() + GAP;
-                }
-                top += tileSize.getHeight() + GAP;
-            }
-        }
-    }
-
     /** A item to be displayed on a Legend */
     public static class LegendItem {
 
--- a/modules/controls/src/main/java/javafx/scene/chart/Chart.java	Tue Mar 18 16:44:34 2014 -0700
+++ b/modules/controls/src/main/java/javafx/scene/chart/Chart.java	Wed Mar 19 10:42:24 2014 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -353,36 +353,36 @@
         if (legend != null) {
             boolean shouldShowLegend = isLegendVisible();
             if (shouldShowLegend) {
-                if (getLegendSide().equals(Side.TOP)) {
+                if (getLegendSide() == Side.TOP) {
                     final double legendHeight = snapSize(legend.prefHeight(width-left-right));
-                    final double legendWidth = snapSize(legend.prefWidth(-1));
+                    final double legendWidth = snapSize(legend.prefWidth(legendHeight));
                     legend.resizeRelocate(left + (((width - left - right)-legendWidth)/2), top, legendWidth, legendHeight);
                     if ((height - bottom - top - legendHeight) < MIN_HEIGHT_TO_LEAVE_FOR_CHART_CONTENT) {
                         shouldShowLegend = false;
                     } else {
                         top += legendHeight;
                     }
-                } else if (getLegendSide().equals(Side.BOTTOM)) {
+                } else if (getLegendSide() == Side.BOTTOM) {
                     final double legendHeight = snapSize(legend.prefHeight(width-left-right));
-                    final double legendWidth = snapSize(legend.prefWidth(-1));
+                    final double legendWidth = snapSize(legend.prefWidth(legendHeight));
                     legend.resizeRelocate(left + (((width - left - right)-legendWidth)/2), height-bottom-legendHeight, legendWidth, legendHeight);
                     if ((height - bottom - top - legendHeight) < MIN_HEIGHT_TO_LEAVE_FOR_CHART_CONTENT) {
                         shouldShowLegend = false;
                     } else {
                         bottom += legendHeight;
                     }
-                } else if (getLegendSide().equals(Side.LEFT)) {
+                } else if (getLegendSide() == Side.LEFT) {
                     final double legendWidth = snapSize(legend.prefWidth(height-top-bottom));
-                    final double legendHeight = snapSize(legend.prefHeight(-1));
+                    final double legendHeight = snapSize(legend.prefHeight(legendWidth));
                     legend.resizeRelocate(left,top +(((height-top-bottom)-legendHeight)/2),legendWidth,legendHeight);
                     if ((width - left - right - legendWidth) < MIN_WIDTH_TO_LEAVE_FOR_CHART_CONTENT) {
                         shouldShowLegend = false;
                     } else {
                         left += legendWidth;
                     }
-                } else if (getLegendSide().equals(Side.RIGHT)) {
+                } else if (getLegendSide() == Side.RIGHT) {
                     final double legendWidth = snapSize(legend.prefWidth(height-top-bottom));
-                    final double legendHeight = snapSize(legend.prefHeight(-1));
+                    final double legendHeight = snapSize(legend.prefHeight(legendWidth));
                     legend.resizeRelocate(width-right-legendWidth,top +(((height-top-bottom)-legendHeight)/2),legendWidth,legendHeight);
                     if ((width - left - right - legendWidth) < MIN_WIDTH_TO_LEAVE_FOR_CHART_CONTENT) {
                         shouldShowLegend = false;