DragboardChecker

Thanks to JavaFX Drag & Drop function, you can drag something from a third-party(native) application such as Finder(Mac OS).
You just need to implement of the DragOver and DragDropped event hander on a Target(see code1).
This is exciting for me, so I just want to know what kind of content the dragboard has.
This application checks dragboard contents and shows them to the screen.

  • version

  • Figure1

When you drag files from Finder(Max OS 10.9) to this App, the dragboard has files and 1 Dataformat.

  • Figure2

When you drag URL from web browser to this, the dragboard has string, URL, files and 3 of Dataformat.

  • code1(DragboardCheckerController.java)
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        initContextCheckers();
        this.dataFormatListView.setCellFactory(p -> new DataFormatCell()); // <― see code 4
        this.dropHereImage.setOnDragOver(e -> {
            e.acceptTransferModes(TransferMode.ANY);
            e.consume();
        });
        this.dropHereImage.setOnDragDropped(e -> {
            Dragboard db = e.getDragboard();
            this.contextCheckers.forEach(checker -> checker.check(db)); // <― see code2,3
            setDataFormatListView(db);
            e.setDropCompleted(true);
        });
    }
    private void initContextCheckers() {
        this.contextCheckers = Arrays.asList(
            new StringContextChecker(this.stringCheck, this.stringText)
          , new UrlContextChecker(this.urlCheck, this.urlText)
          , new FileContextChecker(this.filesCheck, this.filesText)
          , new RtfContextChecker(this.rtfCheck, this.rtfText)
          , new ImageContextChecker(this.imageCheck, this.imageView)
          , new HtmlContextChecker(this.htmlCheck, this.htmlText)
        );
    }    
    private void setDataFormatListView(Dragboard db) {
        List<DataFormatInfo> list = db.getContentTypes().stream().map(df -> {
            return new DataFormatInfo(df, db.getContent(df).toString());
        }).collect(Collectors.toList());       // <― see code5
        this.dataFormatListView.setItems(FXCollections.observableArrayList(list));
    }
  • code2(ContextChecker.java)

This is the base class for each checker class like StringChecker, UrlChecker so on.

public abstract class ContextChecker<T extends Node> {
    protected BooleanProperty checkboxProp;
    protected T node;
    public ContextChecker(CheckBox checkbox, T node) {
        this.checkboxProp = new SimpleBooleanProperty(false);
        checkbox.selectedProperty().bind(this.checkboxProp);
        this.node = node;
    }
    abstract public void check(Dragboard db);    
}
  • code3(StringContextChecker.java)

Since clipboard(dragboard) has 6 methods for contains data, I create 6 checker class.

type method method checker class
String hasString getString StringContextChecker
Files hasFiles getFiles FileContextChecker
HTML hasHtml getHtml HtmlContextChecker
Image hasImage getImage ImageContextChecker
RTF hasRtf getRtf RtfContextChecker
URL hasUrl getUrl UrlContextChecker
public class StringContextChecker extends ContextChecker<TextArea>{
    public StringContextChecker(CheckBox checkbox, TextArea node) {
        super(checkbox, node);
    }
    @Override
    public void check(Dragboard db) {
        if (db.hasString()) {
            super.checkboxProp.set(true);
            super.node.setText(db.getString());
        } else {
            super.checkboxProp.set(false);
            super.node.clear();
        }
    }   
}
  • code4(DataFormatCell.java)

The clipboard(dragboard) has some custom data which are defined by data format.
To show the custom data, you can implement custom list cell like below.

public class DataFormatCell extends ListCell<DataFormatInfo>{
    GridPane root;
    Label dataFormatLabel;
    Text dataFormatText;
    Label contentLabel;
    Text contentText;
    Label idsLabel;
    ListView idsListView;
    ObservableList<String> idsList;
    
    public DataFormatCell() {
        init();
    }
    @Override
    protected void updateItem(DataFormatInfo info, boolean empty) {
        super.updateItem(info, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            this.dataFormatText.setText(info.getDataFormat());
            this.contentText.setText(info.getContent());
            this.idsList.setAll(info.getIdentifiersList());
            setGraphic(root);
        }
    }
    private void init() {
        this.root = new GridPane();
        this.root.setPadding(new Insets(2));
        this.root.setHgap(2);
        this.root.setVgap(2);
        this.dataFormatLabel = new Label("Data Format:");
        this.dataFormatLabel.setPrefWidth(100);
        this.dataFormatText = new Text();
        this.dataFormatText.prefWidth(400);
        this.contentLabel = new Label("Content:");
        this.contentText = new Text();
        this.idsLabel = new Label("Identifires");        
        this.idsList = FXCollections.observableArrayList();
        this.idsListView = new ListView(this.idsList);
        this.idsListView.setPrefHeight(70);
        this.idsListView.setPrefWidth(500);        
        this.root.add(this.dataFormatLabel, 0, 0);
        this.root.add(this.dataFormatText, 1, 0);
        this.root.add(this.contentLabel, 0, 1);
        this.root.add(this.contentText, 1, 1);
        this.root.add(this.idsLabel, 0, 2);
        this.root.add(this.idsListView, 0, 3, 2, 1);
    }
}
  • code5(DataFormatInfo.java)

This code has custom data information for DataFormatCell(see code4).

public class DataFormatInfo {
    private StringProperty dataFormatProp;
    private StringProperty contentProp;
    private List<String> identifiersList;    

    public DataFormatInfo(DataFormat dataFormat, String content) {
        this.dataFormatProp = new SimpleStringProperty(dataFormat.toString());
        this.contentProp = new SimpleStringProperty(content);
        this.identifiersList = 
            dataFormat.getIdentifiers().stream().collect(Collectors.toList());
    }
    public String getDataFormat(){
        return this.dataFormatProp.getValue();
    }
    public String getContent() {
        return this.contentProp.getValue();
    }
    public List<String> getIdentifiersList() {
        return this.identifiersList;
    }
}

The whole code is heretomoTaka01/DragboardChecker: Dragboard Checker by JavaFX8.
keep coding... ;-)