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