@Override public boolean importData(TransferSupport support) { if (!isDataFlavorValid(support)) return false; DropLocation dl = support.getDropLocation(); if (!(dl instanceof JTree.DropLocation)) return false; dropLocation = (JTree.DropLocation) dl; Transferable transferable = support.getTransferable(); if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { try { LayerTreeModel model = layersTree.getLayerModel(); List<?> files = (List<?>) transferable.getTransferData(DataFlavor.javaFileListFlavor); int i = 0; for (Object o : files) { if (o instanceof File) { File file = (File) o; URL url = file.toURI().toURL(); ILayerDefinition definition = new LayerDefinition(file.getName(), url, null, Icons.file.getURL(), true, false); INode node = LayerNode.createFromLayerDefinition(definition); addNodeToTree(dropLocation, model, node, false, i++); } } layersTree.getUI().relayout(); dropLocation = null; } catch (Exception e) { return false; } } return true; }
@Override protected void exportDone(JComponent source, Transferable data, int action) { if (source == null || dropLocation == null || data == null || !(data instanceof TreeTransferable)) return; TreeTransferable t = (TreeTransferable) data; LayerTreeModel model = layersTree.getLayerModel(); Boolean moveIntoFolders = null; int i = 0; for (TreePath path : t.getPaths()) { if (source == layersTree) { INode node = (INode) path.getLastPathComponent(); addNodeToTree(dropLocation, model, node, true, i++); } else if (source == datasetTree) { DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) path.getLastPathComponent(); if (dmtn != null && dmtn.getUserObject() instanceof ILayerDefinition) { if (moveIntoFolders == null) { String message = "Would you like to include the folder hierarchy with the dragged layers?"; int result = JOptionPane.showConfirmDialog( layersTree, message, "Include folders", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); moveIntoFolders = result == JOptionPane.YES_OPTION; } ILayerDefinition definition = (ILayerDefinition) dmtn.getUserObject(); if (moveIntoFolders) { layersTree.getLayerModel().addLayer(definition, path.getPath()); } else { INode node = LayerNode.createFromLayerDefinition(definition); addNodeToTree(dropLocation, model, node, false, i++); } Rectangle bounds = datasetTree.getPathBounds(path); if (bounds != null) datasetTree.repaint(bounds); } } } layersTree.getUI().relayout(); dropLocation = null; }