package com.physalix.jfx;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * Main class.
 *
 * @author Fabrice Sommavilla <fs@physalix.com>
 */
public class Main extends Application {

    private static final String STYLESHEET_PATH = "/styles/styles.css";

    private static final ObservableList<String> data
            = FXCollections.observableArrayList(
                    "chocolate", "salmon", "gold", "coral", "darkorchid",
                    "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
                    "blueviolet", "brown");

    private final ListView<String> listView = new ListView();

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        scene.getStylesheets().add(STYLESHEET_PATH);
        stage.setTitle("Draggable Tags Panel Sample");
        stage.setWidth(600);
        stage.setHeight(400);

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setPrefHeight(400);
        anchorPane.setPrefWidth(600);

        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        listView.setLayoutX(14.0);
        listView.setLayoutY(14.0);
        listView.setPrefHeight(300.0);
        listView.setPrefWidth(165.0);

        listView.setItems(data);
        listView.setOnDragDetected(onDragListDetectedEventHandler);

        DraggableTagsPane tagPane = new DraggableTagsPane(371.0, 300.0);
        tagPane.setLayoutX(200.0);
        tagPane.setLayoutY(14.0);

        tagPane.getTagNames().addListener(itemChangeListener);

        anchorPane.getChildren().addAll(listView, tagPane);

        ((Group) scene.getRoot()).getChildren().addAll(anchorPane);

        stage.setScene(scene);
        stage.show();
    }

    /**
     *
     */
    private final ListChangeListener<Tag> itemChangeListener = new ListChangeListener<Tag>() {
        @Override
        public void onChanged(
                ListChangeListener.Change<? extends Tag> c) {

            while (c.next()) {
                for (Tag tag : c.getRemoved()) {
                    System.out.println("REMOVED: " + tag.getName());
                }
                for (Tag tag : c.getAddedSubList()) {
                    System.out.println("ADDED: " + tag.getName());
                }
            }
        }
    };

    /**
     * Create the onDragDetected tags event. The drag-and-drop gesture can only
     * be started by calling the startDragAndDrop method inside the handler of
     * the DRAG_DETECTED event on a gesture source. It is here that transfer
     * modes supported by the gesture source are defined, and the data to be
     * transferred is placed onto the dragboard.
     */
    private final EventHandler<MouseEvent> onDragListDetectedEventHandler
            = new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    // drag was detected, start drag-and-drop gesture
                    String item = listView.getItems().get(listView.getSelectionModel().getSelectedIndex());

                    if (item != null) {
                        Dragboard db = listView.startDragAndDrop(TransferMode.COPY);

                        ClipboardContent content = new ClipboardContent();
                        content.putString(item);
                        db.setContent(content);
                        event.consume();
                    }
                }
            };

// ---------- ---------- ---------- ---------- ----------
    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

@@ -876,28 +876,16 @@
 ng%3E data
-%0A           
  = FXCol
@@ -914,28 +914,16 @@
 ayList(%0A
-            
         
@@ -935,16 +935,25 @@
 olate%22, 
+%0A        
 %22salmon%22
@@ -958,24 +958,51 @@
 n%22, 
-%22gold%22, %22coral%22,
+%0A        %22gold%22, %0A        %22coral%22, %0A       
  %22da
@@ -1020,28 +1020,16 @@
         
-      
-      
 %22darkgol
@@ -1037,16 +1037,25 @@
 enrod%22, 
+%0A        
 %22lightsa
@@ -1065,16 +1065,34 @@
 n%22, 
-%22black%22,
+%0A        %22black%22, %0A       
  %22ro
@@ -1105,15 +1105,8 @@
 n%22, 
-%22blue%22,
 %0A   
@@ -1110,26 +1110,32 @@
         
-  
+%22blue%22,%0A
         
   %22bluev
@@ -1122,26 +1122,24 @@
 e%22,%0A        
-  
 %22blueviolet%22
@@ -1144,15 +1144,29 @@
 t%22, 
+%0A        
 %22brown%22
+%0A    
 );%0A%0A
@@ -2513,33 +2513,16 @@
 Changed(
-%0A                
 ListChan
@@ -3349,28 +3349,16 @@
 tHandler
-%0A           
  = new E
@@ -3385,27 +3385,24 @@
 nt%3E() %7B%0A
-%0A  
         
       @O
@@ -3393,22 +3393,16 @@
         
-      
 @Overrid
@@ -3403,24 +3403,16 @@
 verride%0A
-        
         
@@ -3462,24 +3462,16 @@
         
- 
-       
 // drag 
@@ -3512,24 +3512,16 @@
 gesture%0A
-        
         
@@ -3625,24 +3625,16 @@
         
-        
 if (item
@@ -3661,24 +3661,16 @@
         
-      
-  
 Dragboar
@@ -3722,22 +3722,16 @@
 .COPY);%0A
-%0A     
         
@@ -3738,19 +3738,16 @@
         
-   
 Clipboar
@@ -3805,24 +3805,16 @@
         
-    
-    
 content.
@@ -3846,24 +3846,16 @@
         
-        
 db.setCo
@@ -3886,24 +3886,16 @@
         
-   
-     
 event.co
@@ -3919,34 +3919,8 @@
     
-        %7D%0A                
 %7D%0A  
@@ -3921,24 +3921,26 @@
   %7D%0A        
+%7D%0A
     %7D;%0A%0A// -

Update Main.java