Search in sources :

Example 26 with Transferable

use of java.awt.datatransfer.Transferable in project adempiere by adempiere.

the class VTreeTransferHandler method importData.

public boolean importData(TransferHandler.TransferSupport info) {
    if (!canImport(info))
        return false;
    JTree tree = (JTree) info.getComponent();
    AdempiereTreeModel model = (AdempiereTreeModel) tree.getModel();
    Transferable t = info.getTransferable();
    MTreeNode to = null;
    MTreeNode from = null;
    int index;
    try {
        from = (MTreeNode) t.getTransferData(TransferableTreeNode.TREE_NODE_FLAVOR);
    } catch (Exception e) {
        return false;
    }
    if (info.isDrop()) {
        JTree.DropLocation dl = (JTree.DropLocation) info.getDropLocation();
        to = (MTreeNode) dl.getPath().getLastPathComponent();
        if (from == to)
            return false;
        index = dl.getChildIndex();
        if (index == -1)
            // insert as first child
            index = 0;
    } else {
        // it's a paste
        MTreeNode selected = (MTreeNode) tree.getSelectionPath().getLastPathComponent();
        if (selected.isLeaf()) {
            to = (MTreeNode) selected.getParent();
            // insert after selected
            index = to.getIndex(selected) + 1;
        } else {
            to = selected;
            index = 0;
        }
    }
    model.insertNodeInto(from, to, index);
    // display from's new location
    tree.scrollPathToVisible(new TreePath(from.getPath()));
    model.saveChangedNodes(from, to);
    return true;
}
Also used : MTreeNode(org.compiere.model.MTreeNode) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) Transferable(java.awt.datatransfer.Transferable)

Example 27 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 28 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 29 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 30 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)

Aggregations

Transferable (java.awt.datatransfer.Transferable)89 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)28 IOException (java.io.IOException)27 List (java.util.List)15 ArrayList (java.util.ArrayList)13 Clipboard (java.awt.datatransfer.Clipboard)11 DataFlavor (java.awt.datatransfer.DataFlavor)11 File (java.io.File)10 Point (java.awt.Point)8 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 DataContext (com.intellij.openapi.actionSystem.DataContext)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2