Example usage for javax.swing TransferHandler importData

List of usage examples for javax.swing TransferHandler importData

Introduction

In this page you can find the example usage for javax.swing TransferHandler importData.

Prototype

public boolean importData(TransferSupport support) 

Source Link

Document

Causes a transfer to occur from a clipboard or a drag and drop operation.

Usage

From source file:pl.otros.logview.gui.Log4jPatternParserEditor.java

private void enableDragAndDrop() {
    logFileContent.setDragEnabled(true);
    final TransferHandler defaultTransferHandler = logFileContent.getTransferHandler();
    TransferHandler transferHandler = new TransferHandler() {

        @Override//from  w w w. j  a  v  a2 s . c o  m
        public boolean importData(TransferSupport support) {
            if (isText(support)) {
                try {
                    String transferData = (String) support.getTransferable()
                            .getTransferData(DataFlavor.stringFlavor);
                    if (transferData.startsWith("file://")) {
                        String firstLine = transferData;
                        if (firstLine.indexOf('\n') > 0) {
                            firstLine = firstLine.substring(0, firstLine.indexOf('\n') - 1);
                        }
                        loadLogContent(VFS.getManager().resolveFile(firstLine));
                    } else {
                        defaultTransferHandler.importData(support);
                    }
                    return true;

                } catch (UnsupportedFlavorException e) {
                    LOGGER.warning("Can't import data, UnsupportedFlavorException: " + e.getMessage());
                } catch (IOException e) {
                    LOGGER.warning("Can't import data, IOException: " + e.getMessage());
                }
            }

            if (isListOfFiles(support)) {
                try {
                    List data = (List) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                    if (data.size() > 0) {
                        File file = (File) data.get(0);
                        loadLogContent(VFS.getManager().resolveFile(file.getAbsolutePath()));
                        return true;
                    }
                } catch (UnsupportedFlavorException e) {
                    LOGGER.warning("Can't import data, UnsupportedFlavorException: " + e.getMessage());
                } catch (IOException e) {
                    LOGGER.warning("Can't import data, IOException: " + e.getMessage());
                }
            }

            return defaultTransferHandler.importData(support);
        }

        @Override
        public boolean canImport(TransferSupport support) {
            if (isText(support) || isListOfFiles(support)) {
                return true;
            }
            return defaultTransferHandler.canImport(support);
        }

        private boolean isListOfFiles(TransferSupport support) {
            return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
        }

        private boolean isText(TransferSupport support) {
            return DataFlavor.selectBestTextFlavor(support.getDataFlavors()) != null;
        }

    };
    logFileContent.setTransferHandler(transferHandler);
    loadLog.setTransferHandler(transferHandler);
    logFileContentLabel.setTransferHandler(transferHandler);
}