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, ""));
}
}
}
}
Aggregations