1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javafx.scene.control.test.utils.ptables;
  26 
  27 import javafx.beans.property.DoubleProperty;
  28 import javafx.beans.property.IntegerProperty;
  29 import javafx.beans.property.Property;
  30 import javafx.beans.value.ChangeListener;
  31 import javafx.beans.value.ObservableValue;
  32 import javafx.event.EventHandler;
  33 import javafx.scene.Node;
  34 import javafx.scene.control.Label;
  35 import javafx.scene.control.Slider;
  36 import javafx.scene.control.SliderBuilder;
  37 import static javafx.scene.control.test.utils.ptables.StaticLogger.*;
  38 import javafx.scene.input.MouseEvent;
  39 import javafx.scene.layout.VBox;
  40 import javafx.stage.Popup;
  41 
  42 /**
  43  * @author Alexander Kirov
  44  *
  45  * You can use ctrl+LMBclick, to change min and max ranges of slider (additional
  46  * popup with logarithmic sliders must appear).
  47  */
  48 public class NumberPropertyValueSetter extends AbstractPropertyValueSetter<Number> {
  49 
  50     private DoubleProperty sliderMinProperty;
  51     private DoubleProperty sliderMaxProperty;
  52 
  53     public NumberPropertyValueSetter(Property listeningProperty, AbstractPropertyValueSetter.BindingType btype, Object testedControl, Number minValue, Number initialValue, Number maxValue) {
  54         try {
  55             this.initialValue1 = minValue;
  56             this.initialValue2 = initialValue;
  57             this.initialValue3 = maxValue;
  58             this.listeningProperty = listeningProperty;
  59 
  60             if ((listeningProperty instanceof DoubleProperty) || (listeningProperty instanceof IntegerProperty)) {
  61                 Slider slider = null;
  62                 if (listeningProperty instanceof DoubleProperty) {
  63                     slider = createSlider((Double) minValue, (Double) maxValue, (Double) initialValue, createId(listeningProperty, btype));
  64                 }
  65                 if (listeningProperty instanceof IntegerProperty) {
  66                     slider = createSlider((Integer) minValue, (Integer) maxValue, (Integer) initialValue, createId(listeningProperty, btype));
  67                 }
  68                 slider.setId(createId(listeningProperty, btype));
  69                 sliderMinProperty = slider.minProperty();
  70                 sliderMaxProperty = slider.maxProperty();
  71                 leadingProperty = (Property) slider.valueProperty();
  72                 leadingControl = slider;
  73 
  74                 if (listeningProperty instanceof IntegerProperty) {
  75                     propertyValueType = PropertyValueType.INTEGER;
  76                 } else {
  77                     propertyValueType = PropertyValueType.DOUBLE;
  78                 }
  79 
  80                 bindComponent(btype, testedControl);
  81                 applyRangesChangers(slider);
  82             }
  83         } catch (Throwable ex) {
  84             log(ex);
  85         }
  86     }
  87 
  88     private void applyRangesChangers(final Slider slider) {
  89         final Popup popupWithControllers = new Popup();
  90         Slider minValue = SliderBuilder.create().prefWidth(300).min(1).max(9).minorTickCount(4).majorTickUnit(2).showTickLabels(true).showTickMarks(true).build();
  91         Slider maxValue = SliderBuilder.create().prefWidth(300).min(1).max(9).minorTickCount(4).majorTickUnit(2).showTickLabels(true).showTickMarks(true).build();
  92 
  93         maxValue.valueProperty().addListener(new ChangeListener() {
  94             public void changed(ObservableValue ov, Object t, Object t1) {
  95                 try {
  96                     slider.setMinorTickCount(0);
  97                     Double newValue = Math.pow(10, (Double) t1);
  98                     slider.setMajorTickUnit(Math.max((newValue - slider.getMin()), 1) / 4);
  99                     slider.maxProperty().setValue(Math.max(newValue, slider.getValue()));
 100                 } catch (Throwable ex) {
 101                     log(ex);
 102                 }
 103             }
 104         });
 105 
 106         minValue.valueProperty().addListener(new ChangeListener() {
 107             public void changed(ObservableValue ov, Object t, Object t1) {
 108                 try {
 109                     slider.setMinorTickCount(0);
 110                     Double newValue = -Math.pow(10, (Double) t1);
 111                     slider.setMajorTickUnit(Math.max((slider.getMax() - newValue), 1) / 4);
 112                     slider.minProperty().setValue(Math.min(newValue, slider.getValue()));
 113                 } catch (Throwable ex) {
 114                     log(ex);
 115                 }
 116             }
 117         });
 118 
 119         VBox vb = new VBox();
 120         vb.getChildren().addAll(new Label("Maximum exponent"), maxValue, new Label("Minimum exponent"), minValue);
 121         vb.setStyle("-fx-background-color: RED;");
 122 
 123         popupWithControllers.setAutoHide(true);
 124         popupWithControllers.setAutoFix(true);
 125         popupWithControllers.getContent().addAll(vb);
 126 
 127         slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
 128             public void handle(MouseEvent t) {
 129                 try {
 130                     if (t.isControlDown()) {
 131                         popupWithControllers.show(slider, slider.getScene().getX() + slider.getLayoutX(), slider.getScene().getY() + slider.getLayoutY());
 132                     }
 133                 } catch (Throwable ex) {
 134                     log(ex);
 135                 }
 136             }
 137         });
 138     }
 139 
 140     private Slider createSlider(int minValue, int maxValue, int defaultValue, String sliderId) {
 141         return createSlider((double) minValue, (double) maxValue, (double) defaultValue, sliderId);
 142     }
 143 
 144     private Slider createSlider(double minValue, double maxValue, double defaultValue, String sliderId) {
 145         try {
 146             return SliderBuilder.create().prefWidth(100).id(sliderId).min(minValue).max(maxValue).value(defaultValue).minorTickCount(4).majorTickUnit((maxValue - minValue) / 4).showTickLabels(true).showTickMarks(true).build();
 147         } catch (Throwable ex) {
 148             log(ex);
 149         }
 150         return null;
 151     }
 152 
 153     public void refresh() {
 154         setBindingState(Boolean.FALSE);
 155         sliderMinProperty.setValue((Number) initialValue1);
 156         leadingProperty.setValue(initialValue2);
 157         sliderMaxProperty.setValue((Number) initialValue3);
 158     }
 159 
 160     public Node getVisualRepresentation() {
 161         return this;
 162     }
 163 }