Search in sources :

Example 1 with CopyPasteManagerEx

use of com.intellij.ide.CopyPasteManagerEx in project intellij-community by JetBrains.

the class MultiplePasteAction method actionPerformed.

@Override
public void actionPerformed(final AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    Component focusedComponent = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    if (!(focusedComponent instanceof JComponent))
        return;
    final CopyPasteManagerEx copyPasteManager = CopyPasteManagerEx.getInstanceEx();
    final ContentChooser<Transferable> chooser = new ContentChooser<Transferable>(project, UIBundle.message("choose.content.to.paste.dialog.title"), true, true) {

        @Override
        protected String getStringRepresentationFor(final Transferable content) {
            try {
                return (String) content.getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException | IOException e1) {
                return "";
            }
        }

        @Override
        protected List<Transferable> getContents() {
            return Arrays.asList(CopyPasteManager.getInstance().getAllContents());
        }

        @Override
        protected void removeContentAt(final Transferable content) {
            copyPasteManager.removeContent(content);
        }
    };
    if (!chooser.getAllContents().isEmpty()) {
        chooser.show();
    } else {
        chooser.close(DialogWrapper.CANCEL_EXIT_CODE);
    }
    if (chooser.isOK()) {
        List<Transferable> selectedContents = chooser.getSelectedContents();
        if (selectedContents.size() == 1) {
            copyPasteManager.moveContentToStackTop(selectedContents.get(0));
        } else {
            copyPasteManager.setContents(new StringSelection(chooser.getSelectedText()));
        }
        if (editor != null) {
            if (editor.isViewer())
                return;
            final AnAction pasteAction = ActionManager.getInstance().getAction(IdeActions.ACTION_PASTE);
            AnActionEvent newEvent = new AnActionEvent(e.getInputEvent(), DataManager.getInstance().getDataContext(focusedComponent), e.getPlace(), e.getPresentation(), ActionManager.getInstance(), e.getModifiers());
            pasteAction.actionPerformed(newEvent);
        } else {
            final Action pasteAction = ((JComponent) focusedComponent).getActionMap().get(DefaultEditorKit.pasteAction);
            if (pasteAction != null) {
                pasteAction.actionPerformed(new ActionEvent(focusedComponent, ActionEvent.ACTION_PERFORMED, ""));
            }
        }
    }
}
Also used : ActionEvent(java.awt.event.ActionEvent) Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) CopyPasteManagerEx(com.intellij.ide.CopyPasteManagerEx) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) StringSelection(java.awt.datatransfer.StringSelection) Project(com.intellij.openapi.project.Project) Editor(com.intellij.openapi.editor.Editor)

Aggregations

CopyPasteManagerEx (com.intellij.ide.CopyPasteManagerEx)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 StringSelection (java.awt.datatransfer.StringSelection)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 ActionEvent (java.awt.event.ActionEvent)1 IOException (java.io.IOException)1