1 /*
   2  * Copyright (c) 2009, 2012, 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  */
  24 package test.scenegraph.app;
  25 
  26 import java.util.ArrayList;
  27 import java.util.HashMap;
  28 import java.util.List;
  29 import java.util.Map;
  30 import java.util.concurrent.ExecutionException;
  31 import javafx.collections.FXCollections;
  32 import javafx.concurrent.Task;
  33 import javafx.event.ActionEvent;
  34 import javafx.event.Event;
  35 import javafx.event.EventHandler;
  36 import javafx.event.EventType;
  37 import javafx.geometry.Insets;
  38 import javafx.geometry.Orientation;
  39 import javafx.scene.Scene;
  40 import javafx.scene.control.*;
  41 import javafx.scene.input.*;
  42 import javafx.scene.layout.BorderPane;
  43 import javafx.scene.layout.FlowPane;
  44 import javafx.scene.layout.VBox;
  45 import test.javaclient.shared.InteroperabilityApp;
  46 import test.javaclient.shared.Utils;
  47 
  48 /**
  49  *
  50  * @author Aleksandr Sakharuk
  51  */
  52 public class SceneEventHandlersApp extends InteroperabilityApp
  53 {
  54 
  55     public static void main(String... args)
  56     {
  57         Utils.launch(SceneEventHandlersApp.class, args);
  58         //System.out.println("fx: " + VersionInfo.getRuntimeVersion());
  59     }
  60 
  61     @Override
  62     protected Scene getScene()
  63     {
  64         eventCombo.setId(EVENTS_COMBO_ID);
  65         actionButton.setId(ACTION_BUTTON_ID);
  66         dragField.setId(DRAG_SOURCE_ID);
  67         dropField.setId(DROP_TARGET_ID);
  68 
  69         eventCombo.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
  70 
  71             public void handle(ActionEvent event) {
  72                 EventType<?> type = eventCombo.getValue().getType();
  73                 scene.addEventHandler(type, new TestHandler(type));
  74             }
  75         });
  76 
  77         eventCombo.setItems(FXCollections.observableArrayList(EventTypes.values()));
  78 
  79         dragField.setOnDragDetected(new EventHandler<MouseEvent>() {
  80 
  81             public void handle(MouseEvent event) {
  82                 final Dragboard db = dragField.startDragAndDrop(TransferMode.ANY);
  83                 final ClipboardContent content = new ClipboardContent();
  84                 content.putString(dragField.getText());
  85                 db.setContent(content);
  86                 event.consume();
  87             }
  88         });
  89 
  90 //        dragField.setOnDragExited(new EventHandler<DragEvent>() {
  91 //
  92 //            public void handle(DragEvent arg0) {
  93 //                System.out.println("Drag exited drag source.");
  94 //            }
  95 //        });
  96 
  97         dropField.setOnDragOver(new EventHandler<DragEvent>() {
  98 
  99             public void handle(DragEvent event) {
 100                 event.acceptTransferModes(TransferMode.COPY);
 101             }
 102         });
 103 
 104         dropField.setOnDragDropped(new EventHandler<DragEvent>() {
 105 
 106             public void handle(DragEvent event) {
 107                 Dragboard db = event.getDragboard();
 108                 boolean success = false;
 109                 if (db.hasString()) {
 110                     dropField.setText(db.getString());
 111                     success = true;
 112                 }
 113                 event.setDropCompleted(success);
 114             }
 115         });
 116 
 117         for(EventTypes t: EventTypes.values())
 118         {
 119                         Label lb = new Label(t.toString());
 120                         lb.setId(t.toString());
 121                         lb.setStyle(NOT_HANDLED_STYLE);
 122             controlLabels.add(lb);
 123         }
 124 
 125         BorderPane root = new BorderPane();
 126         FlowPane flow = new FlowPane(Orientation.VERTICAL);
 127         flow.setVgap(30);
 128         flow.setHgap(1);
 129         flow.setPadding(new Insets(10, 10, 10, 10));
 130         flow.getChildren().addAll(actionButton, dragField, dropField);
 131         root.setTop(eventCombo);
 132         root.setCenter(flow);
 133                 VBox temp = new VBox();
 134                 temp.getChildren().addAll(controlLabels);
 135                 temp.setPadding(new Insets(10, 10, 10, 10));
 136         root.setLeft(temp);
 137         scene = new Scene(root);
 138 
 139         scene.addEventHandler(MouseEvent.DRAG_DETECTED, new EventHandler<MouseEvent>() {
 140 
 141             public void handle(MouseEvent arg0) {
 142                 //System.out.println("startFullDrag handled");
 143                 scene.startFullDrag();
 144             }
 145         });
 146 
 147         return scene;
 148     }
 149 
 150     public Map<EventType<Event>, Boolean> getResults()
 151     {
 152         return handled;
 153     }
 154 
 155     private ComboBox<EventTypes> eventCombo = new ComboBox<EventTypes>();
 156     private Button actionButton = new Button("Action");
 157     private List<Label> controlLabels = new ArrayList<Label>();
 158     private TextField dragField = new TextField("Drag source");
 159     private TextField dropField = new TextField("Drop target");
 160     private Scene scene;
 161 
 162     private Map<EventType<Event>, Boolean> handled = new HashMap<EventType<Event>, Boolean>();
 163 
 164     public static final String NOT_HANDLED_STYLE = "-fx-text-fill: red;";
 165     public static final String HANDLED_STYLE = "-fx-text-fill: green;";
 166     public static final String EVENTS_COMBO_ID = "events_combo";
 167     public static final String ACTION_BUTTON_ID = "action_button";
 168     public static final String DRAG_SOURCE_ID = "drag_field";
 169     public static final String DROP_TARGET_ID = "drop_field";
 170 
 171     public static enum EventTypes
 172     {
 173 
 174         ACTION(ActionEvent.ACTION),
 175         CONTEXT_MENU_REQUESTED(ContextMenuEvent.CONTEXT_MENU_REQUESTED),
 176         DRAG_DONE(DragEvent.DRAG_DONE),
 177         DRAG_DROPPED(DragEvent.DRAG_DROPPED),
 178         DRAG_ENTERED(DragEvent.DRAG_ENTERED),
 179         DRAG_ENTERED_TARGET(DragEvent.DRAG_ENTERED_TARGET),
 180         DRAG_EXITED(DragEvent.DRAG_EXITED),
 181         DRAG_EXITED_TARGET(DragEvent.DRAG_EXITED_TARGET),
 182         DRAG_OVER(DragEvent.DRAG_OVER),
 183         //INPUT_METHOD_TEXT_CHANGED(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED),
 184         KEY_PRESSED(KeyEvent.KEY_PRESSED),
 185         KEY_RELEASED(KeyEvent.KEY_RELEASED),
 186         KEY_TYPED(KeyEvent.KEY_TYPED),
 187         DRAG_DETECTED(MouseEvent.DRAG_DETECTED),
 188         MOUSE_CLICKED(MouseEvent.MOUSE_CLICKED),
 189         MOUSE_DRAGGED(MouseEvent.MOUSE_DRAGGED),
 190         MOUSE_ENTERED(MouseEvent.MOUSE_ENTERED),
 191         MOUSE_ENTERED_TARGET(MouseEvent.MOUSE_ENTERED_TARGET),
 192         MOUSE_EXITED(MouseEvent.MOUSE_EXITED),
 193         MOUSE_EXITED_TARGET(MouseEvent.MOUSE_EXITED_TARGET),
 194         MOUSE_MOVED(MouseEvent.MOUSE_MOVED),
 195         MOUSE_PRESSED(MouseEvent.MOUSE_PRESSED),
 196         MOUSE_RELEASED(MouseEvent.MOUSE_RELEASED),
 197         MOUSE_DRAG_ENTERED(MouseDragEvent.MOUSE_DRAG_ENTERED),
 198         MOUSE_DRAG_ENTERED_TARGET(MouseDragEvent.MOUSE_DRAG_ENTERED_TARGET),
 199         MOUSE_DRAG_EXITED(MouseDragEvent.MOUSE_DRAG_EXITED),
 200         MOUSE_DRAG_EXITED_TARGET(MouseDragEvent.MOUSE_DRAG_EXITED_TARGET),
 201         MOUSE_DRAG_OVER(MouseDragEvent.MOUSE_DRAG_OVER),
 202         MOUSE_DRAG_RELEASED(MouseDragEvent.MOUSE_DRAG_RELEASED),
 203         SCROLL(ScrollEvent.SCROLL),
 204         //MEDIA_ERROR(MediaErrorEvent.MEDIA_ERROR),
 205         //ALERT(WebEvent.ALERT),
 206         //RESIZED(WebEvent.RESIZED),
 207         //TATUS_CHANGED(WebEvent.STATUS_CHANGED),
 208         //VISIBILITY_CHANGED(WebEvent.VISIBILITY_CHANGED),
 209         //WINDOW_CLOSE_REQUEST(WindowEvent.WINDOW_CLOSE_REQUEST),
 210         //WINDOW_HIDDEN(WindowEvent.WINDOW_HIDDEN),
 211         //WINDOW_HIDING(WindowEvent.WINDOW_HIDING),
 212         //WINDOW_SHOWING(WindowEvent.WINDOW_SHOWING),
 213         //WINDOW_SHOWN(WindowEvent.WINDOW_SHOWN),
 214         //WORKER_STATE_CANCELLED(WorkerStateEvent.WORKER_STATE_CANCELLED),
 215         //WORKER_STATE_FAILED(WorkerStateEvent.WORKER_STATE_FAILED),
 216         //WORKER_STATE_READY(WorkerStateEvent.WORKER_STATE_READY),
 217         //WORKER_STATE_RUNNING(WorkerStateEvent.WORKER_STATE_RUNNING),
 218         //WORKER_STATE_SCHEDULED(WorkerStateEvent.WORKER_STATE_SCHEDULED),
 219         //WORKER_STATE_SUCCEEDED(WorkerStateEvent.WORKER_STATE_SUCCEEDED)
 220         ;
 221 
 222         private EventTypes(EventType<? extends Event> type)
 223         {
 224             this.type = type;
 225         }
 226 
 227         public EventType<? extends Event> getType()
 228         {
 229             return type;
 230         }
 231 
 232         public static EventTypes get(EventType<? extends Event> type)
 233         {
 234             for(EventTypes t: EventTypes.values())
 235             {
 236                 if(t.type.equals(type))
 237                 {
 238                     return t;
 239                 }
 240             }
 241             return null;
 242         }
 243 
 244         private EventType<? extends Event> type;
 245 
 246     }
 247 
 248     private class TestHandler<T extends EventType<Event>> implements EventHandler<Event>
 249     {
 250 
 251         public TestHandler(T eventType)
 252         {
 253             this.eventType = eventType;
 254             handled.put(eventType, Boolean.FALSE);
 255         }
 256 
 257         @Override
 258         public void handle(Event event)
 259         {
 260             handled.put(eventType, Boolean.TRUE);
 261             System.out.println(event + " handled.");
 262             controlLabels.get(EventTypes.get(event.getEventType()).ordinal()).setStyle(HANDLED_STYLE);
 263             scene.removeEventHandler(event.getEventType(), this);
 264         }
 265 
 266         private EventType<Event> eventType;
 267 
 268     }
 269 
 270 }