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.scrollEvent;
  26 
  27 import java.util.HashMap;
  28 import javafx.beans.value.ChangeListener;
  29 import javafx.beans.value.ObservableValue;
  30 import javafx.event.ActionEvent;
  31 import javafx.event.EventHandler;
  32 import javafx.factory.ControlsFactory;
  33 import javafx.factory.NodeFactory;
  34 import javafx.factory.Panes;
  35 import javafx.factory.Shapes;
  36 import javafx.geometry.Pos;
  37 import javafx.scene.Node;
  38 import javafx.scene.Parent;
  39 import javafx.scene.Scene;
  40 import javafx.scene.control.Button;
  41 import javafx.scene.control.CheckBox;
  42 import javafx.scene.control.ChoiceBox;
  43 import javafx.scene.control.ComboBox;
  44 import javafx.scene.control.ContextMenu;
  45 import javafx.scene.control.Control;
  46 import javafx.scene.control.DatePicker;
  47 import javafx.scene.control.Label;
  48 import javafx.scene.control.MenuItem;
  49 import javafx.scene.control.TextField;
  50 import javafx.scene.input.ContextMenuEvent;
  51 import javafx.scene.input.ScrollEvent;
  52 import javafx.scene.layout.FlowPane;
  53 import javafx.scene.layout.HBox;
  54 import javafx.scene.layout.VBox;
  55 import javafx.stage.WindowEvent;
  56 import test.javaclient.shared.InteroperabilityApp;
  57 import test.javaclient.shared.Utils;
  58 import static org.junit.Assert.*;
  59 
  60 /**
  61  *
  62  * @author andrey
  63  * @author Alexander Kirov
  64  */
  65 public class ScrollEventApp extends InteroperabilityApp {
  66 
  67     public static final String ID_NODE_CHOOSER = "nodeChooser";
  68     public static final String ID_TARGET_NODE = "target";
  69     public static final String EVENT_COME_INDICATOR_TEXT_FIELD_ID = "EVENT_COME_INDICATOR_TEXT_FIELD_ID";
  70     public static final String RESET_BUTTON_ID = "RESET_BUTTON_ID";
  71     public static final String COME_EVENT_INDICATION = "Come!";
  72     public static final String CONTEXT_MENU_ID = "CONTEXT_MENU_ID";
  73     public static final String ENABLE_CONTEXT_MENU_CHECK_BOX_ID = "CONTEXT_MENU_CHECK_BOX_ID";
  74 
  75     private VBox spaceForNode;
  76     private FlowPane spaceForListeners;
  77     private HashMap<String, TextField> hm;
  78     private ContextMenu contextMenu;
  79     private CheckBox enableContextMenuTest;
  80 
  81     private static String LISTENER_TEXT_FIELD_SUFFIX = "_LISTENER_TEXT_FIELD_ID";
  82 
  83     public static void main(String[] args) {
  84         Utils.launch(ScrollEventApp.class, args);
  85     }
  86 
  87     public static String getContextMenuOnShownCounterID() {
  88         return getListenerTextFieldID(Options.contextMenuOnShown.toString());
  89     }
  90 
  91     @Override
  92     protected Scene getScene() {
  93         Scene scene = new Scene(getContent(), 700, 300);
  94         return scene;
  95     }
  96 
  97     private Node createNodeChooser() {
  98         VBox vb = new VBox(5);
  99         Label scrollEventCame = new Label("Scroll event came : ");
 100         final TextField eventComeIndicator = new TextField("None");
 101         eventComeIndicator.setId(EVENT_COME_INDICATOR_TEXT_FIELD_ID);
 102         HBox hb = new HBox();
 103         hb.getChildren().addAll(scrollEventCame, eventComeIndicator);
 104 
 105         Button resetButton = new Button("Reset");
 106         resetButton.setId(RESET_BUTTON_ID);
 107         resetButton.setOnAction(new EventHandler<ActionEvent>() {
 108             public void handle(ActionEvent t) {
 109                 eventComeIndicator.setText("None");
 110                 clearListenersState();
 111             }
 112         });
 113 
 114         ChoiceBox<NodeFactory> cb = new ChoiceBox<NodeFactory>();
 115         cb.setId(ID_NODE_CHOOSER);
 116         cb.getItems().addAll(ControlsFactory.filteredValues());
 117         cb.getItems().addAll(Shapes.values());
 118         cb.getItems().addAll(Panes.values());
 119 
 120         cb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<NodeFactory>() {
 121 
 122             @Override
 123             public void changed(ObservableValue<? extends NodeFactory> ov, NodeFactory t, NodeFactory t1) {
 124                 final Node node = t1.createNode();
 125 
 126                 if (!enableContextMenuTest.isSelected()) {
 127                     node.setOnScroll(new EventHandler<ScrollEvent>() {
 128 
 129                         public void handle(ScrollEvent t) {
 130                             eventComeIndicator.setText(COME_EVENT_INDICATION);
 131 
 132                             renewListenerValue("deltaX", t.getDeltaX());
 133                             renewListenerValue("deltaY", t.getDeltaY());
 134                             renewListenerValue("textDeltaX", t.getTextDeltaX());
 135                             renewListenerValue("textDeltaY", t.getTextDeltaY());
 136                             renewListenerValue("textDeltaXUnits", t.getTextDeltaXUnits());
 137                             renewListenerValue("textDeltaYUnits", t.getTextDeltaYUnits());
 138                             renewListenerValue("eventType", t.getEventType());
 139                             renewListenerValue("consumed", t.isConsumed());
 140                             renewListenerValue("x", t.getX());
 141                             renewListenerValue("y", t.getY());
 142                         }
 143                     });
 144                 } else {
 145                     EventHandler<ContextMenuEvent> handler = new EventHandler<ContextMenuEvent>() {
 146                         public void handle(ContextMenuEvent t) {
 147                             renewListenerValue("eventType", t.getEventType());
 148                             renewListenerValue("consumed", t.isConsumed());
 149                             renewListenerValue("x", t.getX());
 150                             renewListenerValue("y", t.getY());
 151                             getContextMenu().show(node, t.getScreenX(), t.getScreenY());
 152                             t.consume();
 153                         }
 154                     };
 155                     node.setOnContextMenuRequested(handler);
 156                     TextField editor = null;
 157                     if (node instanceof DatePicker) {
 158                         editor = ((DatePicker)node).getEditor();
 159                     }
 160                     if (node instanceof ComboBox) {
 161                         editor = ((ComboBox)node).getEditor();
 162                     }
 163                     if (editor != null) {
 164                         editor.setOnContextMenuRequested(handler);
 165                     }
 166                 }
 167 
 168                 node.setId(ID_TARGET_NODE);
 169                 spaceForNode.getChildren().clear();
 170                 spaceForNode.getChildren().add(node);
 171                 node.setPickOnBounds(true);
 172                 if (node instanceof Control) {
 173                     ((Control)node).setPrefWidth(200);
 174                     ((Control)node).setPrefHeight(200);
 175                     ((Control)node).setMinWidth(200);
 176                     ((Control)node).setMinHeight(200);
 177                 }
 178                 clearListenersState();
 179             }
 180         });
 181 
 182         vb.getChildren().addAll(new Label("Choose tested control : "), cb, resetButton, hb);
 183         return vb;
 184     }
 185 
 186     private Parent getContent() {
 187         hm = new HashMap<String, TextField>();
 188 
 189         spaceForNode = new VBox();
 190         spaceForNode.setAlignment(Pos.CENTER);
 191         spaceForNode.setPrefWidth(300);
 192         spaceForNode.setPrefHeight(300);
 193 
 194         spaceForListeners = new FlowPane();
 195         spaceForListeners.setPrefHeight(300);
 196         spaceForListeners.setPrefWidth(300);
 197         for (Options s : Options.values()) {
 198             spaceForListeners.getChildren().add(getListener(s.toString()));
 199         }
 200 
 201         enableContextMenuTest = new CheckBox("Context menu test");
 202         enableContextMenuTest.setId(ENABLE_CONTEXT_MENU_CHECK_BOX_ID);
 203 
 204         VBox controls = new VBox();
 205         controls.setAlignment(Pos.CENTER);
 206         controls.getChildren().add(enableContextMenuTest);
 207         controls.getChildren().add(createNodeChooser());
 208 
 209         HBox hBox = new HBox();
 210         hBox.setAlignment(Pos.CENTER);
 211         hBox.getChildren().addAll(spaceForNode, spaceForListeners, controls);
 212         return hBox;
 213     }
 214 
 215     private HBox getListener(String name) {
 216         HBox hb = new HBox();
 217         Label label = new Label(name + " : ");
 218         TextField tf = new TextField();
 219         tf.setId(getListenerTextFieldID(name));
 220         hm.put(name, tf);
 221 
 222         hb.getChildren().addAll(label, tf);
 223         return hb;
 224     }
 225 
 226     private void renewListenerValue(String name, Object newValue) {
 227         TextField tf = hm.get(name);
 228         if (tf != null) {
 229             tf.setText(newValue.toString());
 230         }
 231     }
 232 
 233     private void clearListenersState() {
 234         for (TextField tf : hm.values()) {
 235             tf.setText("");
 236         }
 237     }
 238 
 239     private void initContextMenu() {
 240         contextMenu = new ContextMenu();
 241         contextMenu.setId(CONTEXT_MENU_ID);
 242         for(int i = 1; i <= 5; i++) {
 243             contextMenu.getItems().add(new MenuItem("item - " + i));
 244         }
 245 
 246         contextMenu.setOnShown(new EventHandler<WindowEvent>() {
 247             public void handle(WindowEvent t) {
 248                 TextField tfCounter = hm.get(Options.contextMenuOnShown.toString());
 249                 assertTrue(tfCounter != null);
 250 
 251                 String text = tfCounter.getText();
 252                 if (text.equals("")) {
 253                     tfCounter.setText("1");
 254                 } else {
 255                     int val = Integer.parseInt(text);
 256                     val++;
 257                     tfCounter.setText(String.valueOf(val));
 258                 }
 259             }
 260         });
 261     }
 262 
 263     private ContextMenu getContextMenu() {
 264         if (contextMenu == null ) {
 265             initContextMenu();
 266         }
 267         return contextMenu;
 268     }
 269 
 270     private static String getListenerTextFieldID(String name) {
 271         return name.toUpperCase() + LISTENER_TEXT_FIELD_SUFFIX;
 272     }
 273 
 274     private enum Options {
 275         eventType, consumed, deltaX, deltaY, textDeltaXUnits, textDeltaX, textDeltaYUnits, textDeltaY, x, y,
 276         contextMenuOnShown
 277     };
 278 }