functional/ControlsTests/src/javafx/draganddrop/DragDropWithControls.java

Print this page




  33 import java.util.HashSet;
  34 import java.util.LinkedList;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Set;
  38 import java.util.concurrent.Callable;
  39 import java.util.concurrent.ConcurrentHashMap;
  40 import javafx.beans.value.ChangeListener;
  41 import javafx.beans.value.ObservableValue;
  42 import javafx.event.ActionEvent;
  43 import javafx.event.EventHandler;
  44 import javafx.factory.ControlsFactory;
  45 import javafx.factory.NodeFactory;
  46 import javafx.factory.Panes;
  47 import javafx.factory.Shapes;
  48 import javafx.geometry.Orientation;
  49 import javafx.scene.Node;
  50 import javafx.scene.Parent;
  51 import javafx.scene.Scene;
  52 import javafx.scene.control.Button;
  53 import javafx.scene.control.ButtonBuilder;
  54 import javafx.scene.control.CheckBox;
  55 import javafx.scene.control.ChoiceBox;
  56 import javafx.scene.control.Label;
  57 import javafx.scene.control.Separator;
  58 import javafx.scene.control.TextField;
  59 import javafx.scene.image.Image;
  60 import javafx.scene.image.ImageViewBuilder;
  61 import javafx.scene.input.Clipboard;
  62 import javafx.scene.input.ClipboardContent;
  63 import javafx.scene.input.DataFormat;
  64 import javafx.scene.input.DragEvent;
  65 import javafx.scene.input.Dragboard;
  66 import javafx.scene.input.MouseEvent;
  67 import javafx.scene.input.TransferMode;
  68 import javafx.scene.layout.HBox;
  69 import javafx.scene.layout.Pane;
  70 import javafx.scene.layout.StackPane;
  71 import javafx.scene.layout.VBox;
  72 import javafx.scene.text.Text;
  73 import javafx.stage.Stage;
  74 import javafx.util.Pair;
  75 import static org.junit.Assert.*;
  76 import test.javaclient.shared.InteroperabilityApp;
  77 import static test.javaclient.shared.TestUtil.isEmbedded;
  78 import test.javaclient.shared.Utils;
  79 
  80 public class DragDropWithControls extends InteroperabilityApp {


 276 
 277         if (stageSource != null) {
 278             stageSource.setTitle(TITLE_SOURCE_STAGE);
 279             stageSource.setScene(localScene);
 280             stageSource.setX(0);
 281             stageSource.setY(30);
 282         }
 283 
 284         return localScene;
 285     }
 286 
 287     private Parent createLeftPane() {
 288         VBox lbox = new VBox(10);
 289         lbox.getChildren().addAll(sourceControlPane, new Separator(),
 290                 new Text("Source control type:"),
 291                 createControlCombo(sourceControlPane, true),
 292                 new Text("Source transfer modes:"),
 293                 createTMSelect(sourceModes));
 294 
 295         VBox rbox = new VBox(10);
 296         rbox.getChildren().addAll(new Text("Data formats:"),
 297                 createFormatSelect(sourceFormats),
 298                 ButtonBuilder.create().text("Put to clipboard")
 299                 .id(ID_TO_CLIPBOARD_BUTTON)
 300                 .onAction(new EventHandler<ActionEvent>() {
 301             public void handle(ActionEvent t) {
 302                 Clipboard.getSystemClipboard().setContent(prepareClipboardContent());
 303             }
 304         }).build());



 305 
 306         HBox hbox = new HBox(10);
 307         hbox.getChildren().addAll(lbox, new Separator(Orientation.VERTICAL), rbox);
 308 
 309         final Text fileHdr = new Text("Files to drag (1):");
 310         final Text fileNames = new Text();
 311         showFileNames(fileNames);
 312         final TextField tb = new TextField("Put full path here");
 313         final Button add = new Button("Add");
 314         add.setOnAction(new EventHandler<ActionEvent>() {
 315             @Override
 316             public void handle(ActionEvent event) {
 317                 File f = new File(tb.getText());
 318                 if (f.exists()) {
 319                     CONTENT_FILES.add(f);
 320                     tb.setText("");
 321                     fileHdr.setText("Files to drag (" + CONTENT_FILES.size() + ")");
 322                     showFileNames(fileNames);
 323                     log("Added file " + f.getPath());
 324                 } else {
 325                     log("File doesn't exist: " + f.getPath());
 326                 }
 327             }
 328         });
 329         final Button clear = new Button("Clear");
 330         clear.setOnAction(new EventHandler<ActionEvent>() {
 331             @Override
 332             public void handle(ActionEvent event) {
 333                 CONTENT_FILES.clear();
 334                 fileHdr.setText("Files to drag (0)");
 335                 log("File list cleared");
 336             }
 337         });
 338 
 339         HBox btns = new HBox();
 340         btns.getChildren().addAll(add, clear);
 341 
 342         useCustomViewCB = new CheckBox("Use custom drag view.");
 343 
 344         VBox box = new VBox(10);



 345         box.getChildren().addAll(hbox, new Separator(), fileHdr,
 346                 fileNames, tb, btns,
 347                 useCustomViewCB, new Text("Image: "),
 348                 ImageViewBuilder.create().image(CONTENT_IMAGE).id(ID_SRC_IMAGE).build(),
 349                 log);
 350         if (parameters.size() > 0) {
 351             box.setStyle("-fx-background-color: " + parameters.get(0) + ";");
 352         }
 353         return box;
 354     }
 355 
 356     private void showFileNames(Text text) {
 357         StringBuilder sb = new StringBuilder();
 358         for (int i = 0; i < CONTENT_FILES.size(); i++) {
 359             File file = CONTENT_FILES.get(i);
 360             sb.append(i + 1).append(": ").append(file.toURI()).append("\n");
 361         }
 362         text.setText(sb.toString());
 363     }
 364 
 365     private Parent createRightPane() {
 366         HBox hbox = new HBox(10);
 367 
 368         VBox lbox = new VBox(10);
 369         lbox.getChildren().addAll(targetControlPane, new Separator(), new Text("Target control type:"),
 370                 createControlCombo(targetControlPane, false), new Text("Target transfer modes:"), createTMSelect(targetModes));
 371 
 372         VBox rbox = new VBox(10);
 373         rbox.getChildren().addAll(new Text("Data formats:"), createFormatSelect(targetFormats),
 374                 ButtonBuilder.create().text("paste from clipboard").id(ID_FROM_CLIPBOARD_BUTTON).onAction(new EventHandler<ActionEvent>() {

 375             public void handle(ActionEvent t) {
 376                 getDataFromClipboard(Clipboard.getSystemClipboard());
 377             }
 378         }).build());

 379 
 380         VBox content = new VBox(10);
 381         content.getChildren().addAll(new Text("Transfered content:"), transferedContentPane);
 382 
 383         hbox.getChildren().addAll(lbox, new Separator(Orientation.VERTICAL), rbox,
 384                 new Separator(Orientation.VERTICAL), content);
 385         if (parameters.size() > 0) {
 386             hbox.setStyle("-fx-background-color: " + parameters.get(0) + ";");
 387         }
 388         return hbox;
 389     }
 390 
 391     private Node createControlCombo(final Pane sourceControlPane, final boolean source) {
 392         ChoiceBox<NodeFactory> cb = new ChoiceBox<NodeFactory>();
 393         cb.setId(ID_NODE_CHOOSER);
 394         cb.getItems().addAll(ControlsFactory.filteredValues());
 395         cb.getItems().addAll(Shapes.values());
 396         cb.getItems().addAll(Panes.values());
 397 
 398         cb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<NodeFactory>() {
 399             @Override
 400             public void changed(ObservableValue<? extends NodeFactory> ov, NodeFactory t, NodeFactory t1) {
 401                 Node ctrl = null;
 402                 ctrl = t1.createNode();



 403                 if (source) {
 404                     ctrl.setId(ID_DRAG_SOURCE);
 405                 } else {
 406                     ctrl.setId(ID_DRAG_TARGET);
 407                 }
 408                 eventList.clear();
 409                 sourceControlPane.getChildren().clear();
 410                 sourceControlPane.getChildren().add(ctrl);
 411 
 412                 final Node control = ctrl;
 413 
 414                 if (source) {
 415                     System.out.println("Source control : " + control);
 416                     control.setOnDragDetected(new EventHandler<MouseEvent>() {
 417                         @Override
 418                         public void handle(MouseEvent event) {
 419                             eventList.add(DragEvents.DRAG_DETECTED);
 420                             System.out.println("DragDetected");
 421                             Dragboard db = control.startDragAndDrop(sourceModes.toArray(new TransferMode[sourceModes.size()]));
 422                             if (db == null) {


 552         if (targetFormats.contains(DataFormat.HTML) && cb.hasHtml()) {
 553             receivedContent.put(DataFormat.HTML, cb.getHtml());
 554             transferedContentPane.getChildren().addAll(new WrappedLabel("Html: " + cb.getHtml()));
 555             log("Dropped HTML: " + cb.getHtml());
 556             gotData = true;
 557         }
 558         if (targetFormats.contains(DataFormat.RTF) && cb.hasRtf()) {
 559             receivedContent.put(DataFormat.RTF, cb.getRtf());
 560             transferedContentPane.getChildren().addAll(new WrappedLabel("Rtf: " + cb.getRtf()));
 561             log("Dropped RTF: " + cb.getRtf());
 562             gotData = true;
 563         }
 564         if (targetFormats.contains(DataFormat.URL) && cb.hasUrl()) {
 565             receivedContent.put(DataFormat.URL, cb.getUrl());
 566             transferedContentPane.getChildren().addAll(new WrappedLabel("Url: " + cb.getUrl()));
 567             log("Dropped URL: " + cb.getUrl());
 568             gotData = true;
 569         }
 570         if (targetFormats.contains(DataFormat.IMAGE) && cb.hasImage()) {
 571             receivedContent.put(DataFormat.IMAGE, cb.getImage());
 572             transferedContentPane.getChildren().addAll(new Text("Image: "), ImageViewBuilder.create().image(cb.getImage()).id(ID_RECEIVED_IMAGE).build());


 573             log("Dropped image: " + cb.getImage());
 574             gotData = true;
 575         }
 576         if (targetFormats.contains(DataFormat.FILES) && cb.hasFiles()) {
 577             log("Dropped files:");
 578             receivedContent.put(DataFormat.FILES, cb.getFiles());
 579             transferedContentPane.getChildren().addAll(new WrappedLabel("Files: "));
 580             for (File f : cb.getFiles()) {
 581                 transferedContentPane.getChildren().addAll(new Label("File: " + f) {
 582                     {
 583                         setWrapText(true);
 584                     }
 585                 });
 586                 log("   " + f.getPath());
 587             }
 588             transferedContentPane.getChildren().addAll(new Separator());
 589             gotData = true;
 590         }
 591         if (targetFormats.contains(DF_CUSTOM_BYTES) && cb.hasContent(DF_CUSTOM_BYTES)) {
 592             byte[] b = (byte[]) cb.getContent(DF_CUSTOM_BYTES);




  33 import java.util.HashSet;
  34 import java.util.LinkedList;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Set;
  38 import java.util.concurrent.Callable;
  39 import java.util.concurrent.ConcurrentHashMap;
  40 import javafx.beans.value.ChangeListener;
  41 import javafx.beans.value.ObservableValue;
  42 import javafx.event.ActionEvent;
  43 import javafx.event.EventHandler;
  44 import javafx.factory.ControlsFactory;
  45 import javafx.factory.NodeFactory;
  46 import javafx.factory.Panes;
  47 import javafx.factory.Shapes;
  48 import javafx.geometry.Orientation;
  49 import javafx.scene.Node;
  50 import javafx.scene.Parent;
  51 import javafx.scene.Scene;
  52 import javafx.scene.control.Button;

  53 import javafx.scene.control.CheckBox;
  54 import javafx.scene.control.ChoiceBox;
  55 import javafx.scene.control.Label;
  56 import javafx.scene.control.Separator;
  57 import javafx.scene.control.TextField;
  58 import javafx.scene.image.Image;
  59 import javafx.scene.image.ImageView;
  60 import javafx.scene.input.Clipboard;
  61 import javafx.scene.input.ClipboardContent;
  62 import javafx.scene.input.DataFormat;
  63 import javafx.scene.input.DragEvent;
  64 import javafx.scene.input.Dragboard;
  65 import javafx.scene.input.MouseEvent;
  66 import javafx.scene.input.TransferMode;
  67 import javafx.scene.layout.HBox;
  68 import javafx.scene.layout.Pane;
  69 import javafx.scene.layout.StackPane;
  70 import javafx.scene.layout.VBox;
  71 import javafx.scene.text.Text;
  72 import javafx.stage.Stage;
  73 import javafx.util.Pair;
  74 import static org.junit.Assert.*;
  75 import test.javaclient.shared.InteroperabilityApp;
  76 import static test.javaclient.shared.TestUtil.isEmbedded;
  77 import test.javaclient.shared.Utils;
  78 
  79 public class DragDropWithControls extends InteroperabilityApp {


 275 
 276         if (stageSource != null) {
 277             stageSource.setTitle(TITLE_SOURCE_STAGE);
 278             stageSource.setScene(localScene);
 279             stageSource.setX(0);
 280             stageSource.setY(30);
 281         }
 282 
 283         return localScene;
 284     }
 285 
 286     private Parent createLeftPane() {
 287         VBox lbox = new VBox(10);
 288         lbox.getChildren().addAll(sourceControlPane, new Separator(),
 289                 new Text("Source control type:"),
 290                 createControlCombo(sourceControlPane, true),
 291                 new Text("Source transfer modes:"),
 292                 createTMSelect(sourceModes));
 293 
 294         VBox rbox = new VBox(10);
 295         Button b = new Button("Put to clipboard");
 296         b.setId(ID_TO_CLIPBOARD_BUTTON);
 297         b.setOnAction(new EventHandler<ActionEvent>() {


 298             public void handle(ActionEvent t) {
 299                 Clipboard.getSystemClipboard().setContent(prepareClipboardContent());
 300             }
 301         });
 302         rbox.getChildren().addAll(new Text("Data formats:"),
 303                 createFormatSelect(sourceFormats),
 304                 b);
 305 
 306         HBox hbox = new HBox(10);
 307         hbox.getChildren().addAll(lbox, new Separator(Orientation.VERTICAL), rbox);
 308 
 309         final Text fileHdr = new Text("Files to drag (1):");
 310         final Text fileNames = new Text();
 311         showFileNames(fileNames);
 312         final TextField tb = new TextField("Put full path here");
 313         final Button add = new Button("Add");
 314         add.setOnAction(new EventHandler<ActionEvent>() {
 315             @Override
 316             public void handle(ActionEvent event) {
 317                 File f = new File(tb.getText());
 318                 if (f.exists()) {
 319                     CONTENT_FILES.add(f);
 320                     tb.setText("");
 321                     fileHdr.setText("Files to drag (" + CONTENT_FILES.size() + ")");
 322                     showFileNames(fileNames);
 323                     log("Added file " + f.getPath());
 324                 } else {
 325                     log("File doesn't exist: " + f.getPath());
 326                 }
 327             }
 328         });
 329         final Button clear = new Button("Clear");
 330         clear.setOnAction(new EventHandler<ActionEvent>() {
 331             @Override
 332             public void handle(ActionEvent event) {
 333                 CONTENT_FILES.clear();
 334                 fileHdr.setText("Files to drag (0)");
 335                 log("File list cleared");
 336             }
 337         });
 338 
 339         HBox btns = new HBox();
 340         btns.getChildren().addAll(add, clear);
 341 
 342         useCustomViewCB = new CheckBox("Use custom drag view.");
 343 
 344         VBox box = new VBox(10);
 345         ImageView i = new ImageView();
 346         i.setImage(CONTENT_IMAGE);
 347         i.setId(ID_SRC_IMAGE);
 348         box.getChildren().addAll(hbox, new Separator(), fileHdr,
 349                 fileNames, tb, btns,
 350                 useCustomViewCB, new Text("Image: "),
 351                 i,
 352                 log);
 353         if (parameters.size() > 0) {
 354             box.setStyle("-fx-background-color: " + parameters.get(0) + ";");
 355         }
 356         return box;
 357     }
 358 
 359     private void showFileNames(Text text) {
 360         StringBuilder sb = new StringBuilder();
 361         for (int i = 0; i < CONTENT_FILES.size(); i++) {
 362             File file = CONTENT_FILES.get(i);
 363             sb.append(i + 1).append(": ").append(file.toURI()).append("\n");
 364         }
 365         text.setText(sb.toString());
 366     }
 367 
 368     private Parent createRightPane() {
 369         HBox hbox = new HBox(10);
 370 
 371         VBox lbox = new VBox(10);
 372         lbox.getChildren().addAll(targetControlPane, new Separator(), new Text("Target control type:"),
 373                 createControlCombo(targetControlPane, false), new Text("Target transfer modes:"), createTMSelect(targetModes));
 374 
 375         VBox rbox = new VBox(10);
 376         Button b = new Button("paste from clipboard");
 377         b.setId(ID_FROM_CLIPBOARD_BUTTON);
 378         b.setOnAction(new EventHandler<ActionEvent>() {
 379             public void handle(ActionEvent t) {
 380                 getDataFromClipboard(Clipboard.getSystemClipboard());
 381             }
 382         });
 383         rbox.getChildren().addAll(new Text("Data formats:"), createFormatSelect(targetFormats), b);
 384 
 385         VBox content = new VBox(10);
 386         content.getChildren().addAll(new Text("Transfered content:"), transferedContentPane);
 387 
 388         hbox.getChildren().addAll(lbox, new Separator(Orientation.VERTICAL), rbox,
 389                 new Separator(Orientation.VERTICAL), content);
 390         if (parameters.size() > 0) {
 391             hbox.setStyle("-fx-background-color: " + parameters.get(0) + ";");
 392         }
 393         return hbox;
 394     }
 395 
 396     private Node createControlCombo(final Pane sourceControlPane, final boolean source) {
 397         ChoiceBox<NodeFactory> cb = new ChoiceBox<NodeFactory>();
 398         cb.setId(ID_NODE_CHOOSER);
 399         cb.getItems().addAll(ControlsFactory.filteredValues());
 400         cb.getItems().addAll(Shapes.values());
 401         cb.getItems().addAll(Panes.values());
 402 
 403         cb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<NodeFactory>() {
 404             @Override
 405             public void changed(ObservableValue<? extends NodeFactory> ov, NodeFactory t, NodeFactory t1) {
 406                 Node ctrl = null;
 407                 ctrl = t1.createNode();
 408                 if (ctrl instanceof ImageView) {
 409                     ctrl.setPickOnBounds(true);
 410                 }
 411                 if (source) {
 412                     ctrl.setId(ID_DRAG_SOURCE);
 413                 } else {
 414                     ctrl.setId(ID_DRAG_TARGET);
 415                 }
 416                 eventList.clear();
 417                 sourceControlPane.getChildren().clear();
 418                 sourceControlPane.getChildren().add(ctrl);
 419 
 420                 final Node control = ctrl;
 421 
 422                 if (source) {
 423                     System.out.println("Source control : " + control);
 424                     control.setOnDragDetected(new EventHandler<MouseEvent>() {
 425                         @Override
 426                         public void handle(MouseEvent event) {
 427                             eventList.add(DragEvents.DRAG_DETECTED);
 428                             System.out.println("DragDetected");
 429                             Dragboard db = control.startDragAndDrop(sourceModes.toArray(new TransferMode[sourceModes.size()]));
 430                             if (db == null) {


 560         if (targetFormats.contains(DataFormat.HTML) && cb.hasHtml()) {
 561             receivedContent.put(DataFormat.HTML, cb.getHtml());
 562             transferedContentPane.getChildren().addAll(new WrappedLabel("Html: " + cb.getHtml()));
 563             log("Dropped HTML: " + cb.getHtml());
 564             gotData = true;
 565         }
 566         if (targetFormats.contains(DataFormat.RTF) && cb.hasRtf()) {
 567             receivedContent.put(DataFormat.RTF, cb.getRtf());
 568             transferedContentPane.getChildren().addAll(new WrappedLabel("Rtf: " + cb.getRtf()));
 569             log("Dropped RTF: " + cb.getRtf());
 570             gotData = true;
 571         }
 572         if (targetFormats.contains(DataFormat.URL) && cb.hasUrl()) {
 573             receivedContent.put(DataFormat.URL, cb.getUrl());
 574             transferedContentPane.getChildren().addAll(new WrappedLabel("Url: " + cb.getUrl()));
 575             log("Dropped URL: " + cb.getUrl());
 576             gotData = true;
 577         }
 578         if (targetFormats.contains(DataFormat.IMAGE) && cb.hasImage()) {
 579             receivedContent.put(DataFormat.IMAGE, cb.getImage());
 580             ImageView i = new ImageView(cb.getImage());
 581             i.setId(ID_RECEIVED_IMAGE);
 582             transferedContentPane.getChildren().addAll(new Text("Image: "), i);
 583             log("Dropped image: " + cb.getImage());
 584             gotData = true;
 585         }
 586         if (targetFormats.contains(DataFormat.FILES) && cb.hasFiles()) {
 587             log("Dropped files:");
 588             receivedContent.put(DataFormat.FILES, cb.getFiles());
 589             transferedContentPane.getChildren().addAll(new WrappedLabel("Files: "));
 590             for (File f : cb.getFiles()) {
 591                 transferedContentPane.getChildren().addAll(new Label("File: " + f) {
 592                     {
 593                         setWrapText(true);
 594                     }
 595                 });
 596                 log("   " + f.getPath());
 597             }
 598             transferedContentPane.getChildren().addAll(new Separator());
 599             gotData = true;
 600         }
 601         if (targetFormats.contains(DF_CUSTOM_BYTES) && cb.hasContent(DF_CUSTOM_BYTES)) {
 602             byte[] b = (byte[]) cb.getContent(DF_CUSTOM_BYTES);