Advertisement
iamaamir

SortedPane

Jul 26th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.List;
  2. import javafx.scene.Node;
  3. import java.util.ArrayList;
  4. import javafx.geometry.HPos;
  5. import javafx.geometry.VPos;
  6. import javafx.scene.layout.Pane;
  7. import javafx.beans.property.DoubleProperty;
  8. import javafx.beans.property.SimpleDoubleProperty;
  9.  
  10. /**
  11.  *
  12.  * @author Aamir Khan
  13.  * A Pane to put your Nodes in a Sorted Order....
  14.  */
  15. public class SortedPane extends Pane {
  16.  
  17.     public SortedPane() {
  18.         super();
  19.         spacingProperty().addListener(e -> requestLayout());
  20.     }
  21.  
  22.     public SortedPane(double spacing) {
  23.         this();
  24.         setSpacing(spacing);
  25.     }
  26.  
  27.     @Override
  28.     protected void layoutChildren() {
  29.         List<Node> sortedChildren = new ArrayList<>(getChildren());
  30.         sortedChildren.sort((node1, node2) -> new Double(node1.prefWidth(-1)).compareTo(new Double(node2.prefWidth(-1))));
  31.         double currentX = getInsets().getLeft();
  32.         for (Node node : sortedChildren) {
  33.             double width = node.prefWidth(-1);
  34.             double height = node.prefHeight(-1);
  35.             layoutInArea(node, currentX, getInsets().getTop(), width, height, 0, HPos.CENTER, VPos.CENTER);
  36.             currentX = currentX + width + getSpacing();
  37.         }
  38.  
  39.     }
  40.  
  41.     @Override
  42.     protected double computePrefHeight(double width) {
  43.         double maxH = 0;
  44.         for (Node node : getChildren()) {
  45.             maxH = Math.max(node.prefHeight(-1), maxH);
  46.         }
  47.         return getInsets().getTop() + getInsets().getBottom() + maxH;
  48.     }
  49.  
  50.     @Override
  51.     protected double computePrefWidth(double height) {
  52.         double width = 0;
  53.         for (Node node : getChildren()) {
  54.             width = width + node.prefWidth(-1);
  55.         }
  56.         return getInsets().getLeft() + getInsets().getRight() + width + getSpacing();
  57.     }
  58.  
  59.     @Override
  60.     protected double computeMaxHeight(double width) {
  61.         return computePrefHeight(width);
  62.     }
  63.  
  64.     @Override
  65.     protected double computeMaxWidth(double height) {
  66.         return computePrefWidth(height);
  67.     }
  68.  
  69.     @Override
  70.     protected double computeMinHeight(double width) {
  71.         return computePrefHeight(width);
  72.     }
  73.  
  74.     @Override
  75.     protected double computeMinWidth(double height) {
  76.         return computePrefWidth(height);
  77.     }
  78.  
  79.     public final void setSpacing(double value) {
  80.         spacingProperty().set(value);
  81.     }
  82.  
  83.     private DoubleProperty spacing;
  84.  
  85.     public final DoubleProperty spacingProperty() {
  86.         if (spacing == null) {
  87.             spacing = new SimpleDoubleProperty(0){
  88.               @Override
  89.               protected void invalidated(){
  90.                   super.invalidated();
  91.                   requestLayout();
  92.               }  
  93.             };
  94.         }
  95.         return spacing;
  96.     }
  97.  
  98.     public double getSpacing() {
  99.         return spacing == null ? 0 : spacing.get();
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement