Example usage for javax.swing TransferHandler canImport

List of usage examples for javax.swing TransferHandler canImport

Introduction

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

Prototype

public boolean canImport(TransferSupport support) 

Source Link

Document

This method is called repeatedly during a drag and drop operation to allow the developer to configure properties of, and to return the acceptability of transfers; with a return value of true indicating that the transfer represented by the given TransferSupport (which contains all of the details of the transfer) is acceptable at the current time, and a value of false rejecting the transfer.

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//  w w  w  . j  av a2s  .co  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);
}