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 static javafx.scene.control.test.utils.ptables.StaticLogger.*;
  37 import javafx.scene.input.MouseEvent;
  38 import javafx.scene.layout.VBox;
  39 import javafx.stage.Popup;
  40 
  41 /**
  42  * @author Alexander Kirov
  43  *
  44  * You can use ctrl+LMBclick, to change min and max ranges of slider (additional
  45  * popup with logarithmic sliders must appear).
  46  */
  47 public class NumberPropertyValueSetter extends AbstractPropertyValueSetter<Number> {
  48 
  49     private DoubleProperty sliderMinProperty;
  50     private DoubleProperty sliderMaxProperty;
  51 
  52     public NumberPropertyValueSetter(Property listeningProperty, AbstractPropertyValueSetter.BindingType btype, Object testedControl, Number minValue, Number initialValue, Number maxValue) {
  53         try {
  54             this.initialValue1 = minValue;
  55             this.initialValue2 = initialValue;
  56             this.initialValue3 = maxValue;
  57             this.listeningProperty = listeningProperty;
  58 
  59             if ((listeningProperty instanceof DoubleProperty) || (listeningProperty instanceof IntegerProperty)) {
  60                 Slider slider = null;
  61                 if (listeningProperty instanceof DoubleProperty) {
  62                     slider = createSlider((Double) minValue, (Double) maxValue, (Double) initialValue, createId(listeningProperty, btype));
  63                 }
  64                 if (listeningProperty instanceof IntegerProperty) {
  65                     slider = createSlider((Integer) minValue, (Integer) maxValue, (Integer) initialValue, createId(listeningProperty, btype));
  66                 }
  67                 slider.setId(createId(listeningProperty, btype));
  68                 sliderMinProperty = slider.minProperty();
  69                 sliderMaxProperty = slider.maxProperty();
  70                 leadingProperty = (Property) slider.valueProperty();
  71                 leadingControl = slider;
  72 
  73                 if (listeningProperty instanceof IntegerProperty) {
  74                     propertyValueType = PropertyValueType.INTEGER;
  75                 } else {
  76                     propertyValueType = PropertyValueType.DOUBLE;
  77                 }
  78 
  79                 bindComponent(btype, testedControl);
  80                 applyRangesChangers(slider);
  81             }
  82         } catch (Throwable ex) {
  83             log(ex);
  84         }
  85     }
  86 
  87     private void applyRangesChangers(final Slider slider) {
  88         final Popup popupWithControllers = new Popup();
  89         Slider minValue = new Slider();
  90         minValue.setPrefWidth(300);
  91         minValue.setMin(1);
  92         minValue.setMax(9);
  93         minValue.setMinorTickCount(4);
  94         minValue.setMajorTickUnit(2);
  95         minValue.setShowTickLabels(true);
  96         minValue.setShowTickMarks(true);
  97         Slider maxValue = new Slider();
  98         maxValue.setPrefWidth(300);
  99         maxValue.setMin(1);
 100         maxValue.setMax(9);
 101         maxValue.setMinorTickCount(4);
 102         maxValue.setMajorTickUnit(2);
 103         maxValue.setShowTickLabels(true);
 104         maxValue.setShowTickMarks(true);
 105 
 106         maxValue.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((newValue - slider.getMin()), 1) / 4);
 112                     slider.maxProperty().setValue(Math.max(newValue, slider.getValue()));
 113                 } catch (Throwable ex) {
 114                     log(ex);
 115                 }
 116             }
 117         });
 118 
 119         minValue.valueProperty().addListener(new ChangeListener() {
 120             public void changed(ObservableValue ov, Object t, Object t1) {
 121                 try {
 122                     slider.setMinorTickCount(0);
 123                     Double newValue = -Math.pow(10, (Double) t1);
 124                     slider.setMajorTickUnit(Math.max((slider.getMax() - newValue), 1) / 4);
 125                     slider.minProperty().setValue(Math.min(newValue, slider.getValue()));
 126                 } catch (Throwable ex) {
 127                     log(ex);
 128                 }
 129             }
 130         });
 131 
 132         VBox vb = new VBox();
 133         vb.getChildren().addAll(new Label("Maximum exponent"), maxValue, new Label("Minimum exponent"), minValue);
 134         vb.setStyle("-fx-background-color: RED;");
 135 
 136         popupWithControllers.setAutoHide(true);
 137         popupWithControllers.setAutoFix(true);
 138         popupWithControllers.getContent().addAll(vb);
 139 
 140         slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
 141             public void handle(MouseEvent t) {
 142                 try {
 143                     if (t.isControlDown()) {
 144                         popupWithControllers.show(slider, slider.getScene().getX() + slider.getLayoutX(), slider.getScene().getY() + slider.getLayoutY());
 145                     }
 146                 } catch (Throwable ex) {
 147                     log(ex);
 148                 }
 149             }
 150         });
 151     }
 152 
 153     private Slider createSlider(int minValue, int maxValue, int defaultValue, String sliderId) {
 154         return createSlider((double) minValue, (double) maxValue, (double) defaultValue, sliderId);
 155     }
 156 
 157     private Slider createSlider(double minValue, double maxValue, double defaultValue, String sliderId) {
 158         try {
 159             Slider temp = new Slider();
 160             temp.setPrefWidth(100);
 161             temp.setId(sliderId);
 162             temp.setMin(minValue);
 163             temp.setMax(maxValue);
 164             temp.setValue(defaultValue);
 165             temp.setMinorTickCount(4);
 166             temp.setMajorTickUnit((maxValue - minValue) / 4);
 167             temp.setShowTickLabels(true);
 168             temp.setShowTickMarks(true);
 169             return temp;
 170         } catch (Throwable ex) {
 171             log(ex);
 172         }
 173         return null;
 174     }
 175 
 176     public void refresh() {
 177         setBindingState(Boolean.FALSE);
 178         sliderMinProperty.setValue((Number) initialValue1);
 179         leadingProperty.setValue(initialValue2);
 180         sliderMaxProperty.setValue((Number) initialValue3);
 181     }
 182 
 183     public Node getVisualRepresentation() {
 184         return this;
 185     }
 186 }