Search in sources :

Example 31 with Transferable

use of java.awt.datatransfer.Transferable in project jmeter by apache.

the class JMeterTreeTransferHandler method importData.

@Override
public boolean importData(TransferHandler.TransferSupport support) {
    if (!canImport(support)) {
        return false;
    }
    // deal with the jmx files
    GuiPackage guiInstance = GuiPackage.getInstance();
    DataFlavor[] flavors = support.getDataFlavors();
    Transferable t = support.getTransferable();
    for (DataFlavor flavor : flavors) {
        // Check for file lists specifically
        if (flavor.isFlavorJavaFileListType()) {
            try {
                return guiInstance.getMainFrame().openJmxFilesFromDragAndDrop(t);
            } catch (Exception e) {
                log.error("Drop file failed", e);
            }
            return false;
        }
    }
    // Extract transfer data.
    JMeterTreeNode[] nodes = getDraggedNodes(t);
    if (nodes == null || nodes.length == 0) {
        return false;
    }
    // Get drop location and mode
    JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
    TreePath dest = dl.getPath();
    JMeterTreeNode target = (JMeterTreeNode) dest.getLastPathComponent();
    nodesForRemoval = new ArrayList<>();
    int index = dl.getChildIndex();
    TreePath[] pathsToSelect = new TreePath[nodes.length];
    int pathPosition = 0;
    JMeterTreeModel treeModel = guiInstance.getTreeModel();
    for (JMeterTreeNode node : nodes) {
        if (index == -1) {
            // drop mode == DropMode.ON
            index = target.getChildCount();
        }
        // Insert a clone of the node, the original one will be removed by the exportDone method
        // the children are not cloned but moved to the cloned node
        // working on the original node would be harder as 
        //    you'll have to deal with the insertion index offset if you re-order a node inside a parent
        JMeterTreeNode copy = (JMeterTreeNode) node.clone();
        // first copy the children as the call to copy.add will modify the collection we're iterating on
        Enumeration<?> enumFrom = node.children();
        List<JMeterTreeNode> tmp = new ArrayList<>();
        while (enumFrom.hasMoreElements()) {
            JMeterTreeNode child = (JMeterTreeNode) enumFrom.nextElement();
            tmp.add(child);
        }
        for (JMeterTreeNode jMeterTreeNode : tmp) {
            copy.add(jMeterTreeNode);
        }
        treeModel.insertNodeInto(copy, target, index++);
        nodesForRemoval.add(node);
        pathsToSelect[pathPosition++] = new TreePath(treeModel.getPathToRoot(copy));
    }
    TreePath treePath = new TreePath(target.getPath());
    // expand the destination node
    JTree tree = (JTree) support.getComponent();
    tree.expandPath(treePath);
    tree.setSelectionPaths(pathsToSelect);
    return true;
}
Also used : Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage)

Example 32 with Transferable

use of java.awt.datatransfer.Transferable in project jmeter by apache.

the class GuiUtils method getPastedText.

/**
     * Get pasted text from clipboard
     *
     * @return String Pasted text
     * @throws UnsupportedFlavorException
     *             if the clipboard data can not be get as a {@link String}
     * @throws IOException
     *             if the clipboard data is no longer available
     */
public static String getPastedText() throws UnsupportedFlavorException, IOException {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable trans = clipboard.getContents(null);
    DataFlavor[] flavourList = trans.getTransferDataFlavors();
    Collection<DataFlavor> flavours = new ArrayList<>(flavourList.length);
    if (Collections.addAll(flavours, flavourList) && flavours.contains(DataFlavor.stringFlavor)) {
        return (String) trans.getTransferData(DataFlavor.stringFlavor);
    } else {
        return null;
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) Clipboard(java.awt.datatransfer.Clipboard) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 33 with Transferable

use of java.awt.datatransfer.Transferable in project jmeter by apache.

the class MainFrame method drop.

/**
     * Handler of Top level Dnd
     */
@Override
public void drop(DropTargetDropEvent dtde) {
    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (DataFlavor flavor : flavors) {
            // Check for file lists specifically
            if (flavor.isFlavorJavaFileListType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                try {
                    openJmxFilesFromDragAndDrop(tr);
                } finally {
                    dtde.dropComplete(true);
                }
                return;
            }
        }
    } catch (UnsupportedFlavorException | IOException e) {
        log.warn("Dnd failed", e);
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 34 with Transferable

use of java.awt.datatransfer.Transferable in project intellij-community by JetBrains.

the class PsiElementListNavigator method navigateOrCreatePopup.

/**
   * listUpdaterTask should be started after alarm is initialized so one-item popup won't blink
   */
@Nullable
public static JBPopup navigateOrCreatePopup(@NotNull final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask, @NotNull final Consumer<Object[]> consumer) {
    if (targets.length == 0)
        return null;
    if (targets.length == 1 && (listUpdaterTask == null || listUpdaterTask.isFinished())) {
        consumer.consume(targets);
        return null;
    }
    final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<>(targets);
    final JBList list = new JBList(model);
    HintUpdateSupply.installSimpleHintUpdateSupply(list);
    list.setTransferHandler(new TransferHandler() {

        @Nullable
        @Override
        protected Transferable createTransferable(JComponent c) {
            final Object[] selectedValues = list.getSelectedValues();
            final PsiElement[] copy = new PsiElement[selectedValues.length];
            for (int i = 0; i < selectedValues.length; i++) {
                copy[i] = (PsiElement) selectedValues[i];
            }
            return new PsiCopyPasteManager.MyTransferable(copy);
        }

        @Override
        public int getSourceActions(JComponent c) {
            return COPY;
        }
    });
    list.setCellRenderer(listRenderer);
    list.setFont(EditorUtil.getEditorFont());
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    if (listRenderer instanceof PsiElementListCellRenderer) {
        ((PsiElementListCellRenderer) listRenderer).installSpeedSearch(builder);
    }
    PopupChooserBuilder popupChooserBuilder = builder.setTitle(title).setMovable(true).setResizable(true).setItemChoosenCallback(() -> {
        int[] ids = list.getSelectedIndices();
        if (ids == null || ids.length == 0)
            return;
        Object[] selectedElements = list.getSelectedValues();
        consumer.consume(selectedElements);
    }).setCancelCallback(() -> {
        HintUpdateSupply.hideHint(list);
        if (listUpdaterTask != null) {
            listUpdaterTask.cancelTask();
        }
        return true;
    });
    final Ref<UsageView> usageView = new Ref<>();
    if (findUsagesTitle != null) {
        popupChooserBuilder = popupChooserBuilder.setCouldPin(popup -> {
            final List<NavigatablePsiElement> items = model.getItems();
            usageView.set(FindUtil.showInUsageView(null, items.toArray(new PsiElement[items.size()]), findUsagesTitle, targets[0].getProject()));
            popup.cancel();
            return false;
        });
    }
    final JBPopup popup = popupChooserBuilder.createPopup();
    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);
    if (listUpdaterTask != null) {
        listUpdaterTask.init((AbstractPopup) popup, list, usageView);
    }
    return popup;
}
Also used : EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) AbstractPopup(com.intellij.ui.popup.AbstractPopup) CollectionListModel(com.intellij.ui.CollectionListModel) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) MouseEvent(java.awt.event.MouseEvent) ListBackgroundUpdaterTask(com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) List(java.util.List) FindUtil(com.intellij.find.FindUtil) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Consumer(com.intellij.util.Consumer) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList) List(java.util.List) CollectionListModel(com.intellij.ui.CollectionListModel) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with Transferable

use of java.awt.datatransfer.Transferable in project intellij-community by JetBrains.

the class FileListPasteProvider method performPaste.

@Override
public void performPaste(@NotNull DataContext dataContext) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final IdeView ideView = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (project == null || ideView == null)
        return;
    if (!FileCopyPasteUtil.isFileListFlavorAvailable())
        return;
    final Transferable contents = CopyPasteManager.getInstance().getContents();
    if (contents == null)
        return;
    final List<File> fileList = FileCopyPasteUtil.getFileList(contents);
    if (fileList == null)
        return;
    if (DumbService.isDumb(project)) {
        DumbService.getInstance(project).showDumbModeNotification("Sorry, file copy/paste is not available during indexing");
        return;
    }
    final List<PsiElement> elements = new ArrayList<>();
    for (File file : fileList) {
        final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
        if (vFile != null) {
            final PsiManager instance = PsiManager.getInstance(project);
            PsiFileSystemItem item = vFile.isDirectory() ? instance.findDirectory(vFile) : instance.findFile(vFile);
            if (item != null) {
                elements.add(item);
            }
        }
    }
    if (elements.size() > 0) {
        final PsiDirectory dir = ideView.getOrChooseDirectory();
        if (dir != null) {
            final boolean move = LinuxDragAndDropSupport.isMoveOperation(contents);
            if (move) {
                new MoveFilesOrDirectoriesHandler().doMove(PsiUtilCore.toPsiElementArray(elements), dir);
            } else {
                new CopyFilesOrDirectoriesHandler().doCopy(PsiUtilCore.toPsiElementArray(elements), dir);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CopyFilesOrDirectoriesHandler(com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler) Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) PsiManager(com.intellij.psi.PsiManager) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) MoveFilesOrDirectoriesHandler(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesHandler) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Transferable (java.awt.datatransfer.Transferable)96 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)30 IOException (java.io.IOException)30 List (java.util.List)18 DataFlavor (java.awt.datatransfer.DataFlavor)14 ArrayList (java.util.ArrayList)14 File (java.io.File)13 Clipboard (java.awt.datatransfer.Clipboard)12 Point (java.awt.Point)9 StringSelection (java.awt.datatransfer.StringSelection)5 Project (com.intellij.openapi.project.Project)4 ClipboardOwner (java.awt.datatransfer.ClipboardOwner)4 TransferHandler (javax.swing.TransferHandler)4 Editor (com.intellij.openapi.editor.Editor)3 ActionEvent (java.awt.event.ActionEvent)3 JComponent (javax.swing.JComponent)3 TreePath (javax.swing.tree.TreePath)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 PsiElement (com.intellij.psi.PsiElement)2