Example usage for javafx.scene.input Clipboard hasString

List of usage examples for javafx.scene.input Clipboard hasString

Introduction

In this page you can find the example usage for javafx.scene.input Clipboard hasString.

Prototype

public final boolean hasString() 

Source Link

Document

Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered on this Clipboard.

Usage

From source file:com.panemu.tiwulfx.table.TableControl.java

/**
 * Paste text on clipboard. Doesn't work on READ mode.
 *///from   w ww .jav  a2  s.  c o m
public void paste() {
    if (mode.get() == Mode.READ) {
        return;
    }
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    if (clipboard.hasString()) {
        final String text = clipboard.getString();
        if (text != null) {
            List<TablePosition> cells = tblView.getSelectionModel().getSelectedCells();
            if (cells.isEmpty()) {
                return;
            }
            TablePosition cell = cells.get(0);
            List<TableColumn<R, ?>> lstColumn = getLeafColumns();
            TableColumn startColumn = null;
            for (TableColumn clm : lstColumn) {
                if (clm instanceof BaseColumn && clm == cell.getTableColumn()) {
                    startColumn = (BaseColumn) clm;
                    break;
                }
            }
            if (startColumn == null) {
                return;
            }
            int rowIndex = cell.getRow();
            String[] arrString = text.split("\n");
            boolean stopPasting = false;
            for (String line : arrString) {
                if (stopPasting) {
                    break;
                }
                R item = null;
                if (rowIndex < tblView.getItems().size()) {
                    item = tblView.getItems().get(rowIndex);
                } else if (mode.get() == Mode.EDIT) {
                    /**
                     * Will ensure the content display to TEXT_ONLY because
                     * there is no way to update cell editors value (in
                     * agile editing mode)
                     */
                    tblView.getSelectionModel().clearSelection();
                    return;//stop pasting as it already touched last row
                }

                if (!lstChangedRow.contains(item)) {
                    if (mode.get() == Mode.INSERT) {
                        //means that selected row is not new row. Let's create new row
                        createNewRow(rowIndex);
                        item = tblView.getItems().get(rowIndex);
                    } else {
                        lstChangedRow.add(item);
                    }
                }

                showRow(rowIndex);
                /**
                 * Handle multicolumn paste
                 */
                String[] stringCellValues = line.split("\t");
                TableColumn toFillColumn = startColumn;
                tblView.getSelectionModel().select(rowIndex, toFillColumn);
                for (String stringCellValue : stringCellValues) {
                    if (toFillColumn == null) {
                        break;
                    }
                    if (toFillColumn instanceof BaseColumn && toFillColumn.isEditable()
                            && toFillColumn.isVisible()) {
                        try {
                            Object oldValue = toFillColumn.getCellData(item);
                            Object newValue = ((BaseColumn) toFillColumn).convertFromString(stringCellValue);
                            PropertyUtils.setSimpleProperty(item, ((BaseColumn) toFillColumn).getPropertyName(),
                                    newValue);
                            if (mode.get() == Mode.EDIT) {
                                ((BaseColumn) toFillColumn).addRecordChange(item, oldValue, newValue);
                            }
                        } catch (Exception ex) {
                            MessageDialog.Answer answer = MessageDialogBuilder.error(ex)
                                    .message("msg.paste.error", stringCellValue, toFillColumn.getText())
                                    .buttonType(MessageDialog.ButtonType.YES_NO)
                                    .yesOkButtonText("continue.pasting").noButtonText("stop")
                                    .show(getScene().getWindow());
                            if (answer == MessageDialog.Answer.NO) {
                                stopPasting = true;
                                break;
                            }
                        }
                    }
                    tblView.getSelectionModel().selectRightCell();
                    TablePosition nextCell = tblView.getSelectionModel().getSelectedCells().get(0);
                    if (nextCell.getTableColumn() instanceof BaseColumn
                            && nextCell.getTableColumn() != toFillColumn) {
                        toFillColumn = (BaseColumn) nextCell.getTableColumn();
                    } else {
                        toFillColumn = null;
                    }
                }
                rowIndex++;
            }

            refresh();

            /**
             * Will ensure the content display to TEXT_ONLY because there is
             * no way to update cell editors value (in agile editing mode)
             */
            tblView.getSelectionModel().clearSelection();
        }
    }
}

From source file:net.rptools.tokentool.controller.TokenTool_Controller.java

@FXML
void editPasteImageMenu_onAction(ActionEvent event) {
    Clipboard clipboard = Clipboard.getSystemClipboard();
    Image originalImage = portraitImageView.getImage();

    // Strangely, we get an error if we try to paste an image we put in the clipboard ourselves but File works ok?
    // -Dprism.order=sw also fixes it but not sure why...
    // So lets just check for File first...
    if (clipboard.hasFiles()) {
        clipboard.getFiles().forEach(file -> {
            try {
                Image cbImage = new Image(file.toURI().toURL().toExternalForm());

                if (cbImage != null)
                    updatePortrait(cbImage);

                updateFileNameTextField(FilenameUtils.getBaseName(file.toURI().toURL().toExternalForm()));
            } catch (Exception e) {
                log.error("Could not load image " + file);
                e.printStackTrace();// w  w w  .j a v  a 2s .  c  o m
            }
        });
    } else if (clipboard.hasImage()) {
        try {
            Image cbImage = clipboard.getImage();
            if (cbImage != null)
                updatePortrait(cbImage);
        } catch (IllegalArgumentException e) {
            log.info(e);
            updatePortrait(originalImage);
        }
    } else if (clipboard.hasUrl()) {
        try {
            Image cbImage = new Image(clipboard.getUrl());
            if (cbImage != null)
                updatePortrait(cbImage);

            updateFileNameTextField(FileSaveUtil.searchURL(clipboard.getUrl()));
        } catch (IllegalArgumentException e) {
            log.info(e);
        }
    } else if (clipboard.hasString()) {
        try {
            Image cbImage = new Image(clipboard.getString());
            if (cbImage != null)
                updatePortrait(cbImage);

            updateFileNameTextField(FileSaveUtil.searchURL(clipboard.getString()));
        } catch (IllegalArgumentException e) {
            log.info(e);
        }
    }
}