PasteAction not called when running through IntelliJ?
Hi all,
for my plugin I created my own JTextPane that listen to drap and drop and copy/paste of pictures
This Component is named DragDropTextPane
Basically I registered my own action in my DragDropTextPane constructor
getActionMap().put(DefaultEditorKit.pasteAction, new DrapDropPasteAction());
and here is the source code of my DragDropPastAction
class DrapDropPasteAction extends DefaultEditorKit.PasteAction{
@Override
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if(clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)){
ImageIcon imageFromClipboard = ImageUtility.getInstance().getImageFromClipboard();
if(imageFromClipboard != null) {
insertIcon(imageFromClipboard);
}
}else {
super.actionPerformed(e);
}
}
}
I i test it with following code :
JFrame frame = new JFrame();
DragDropTextPane comp = new DragDropTextPane();
comp.setPreferredSize(new Dimension(500, 400));
JPanel p = new JPanel();
p.add(comp);
frame.add(p);
frame.pack();
frame.show();
when I go to a picture editing tool and copy something from it
when i do a paste ( CTRL + V) within my DragDropTextField the picture is paste like expected
But when run in IntelliJ and do a CTRL + V , put the cursor on my DrapDropTextPane (that is in JDialog that is build with the Form builder) it did not work. In fact it does not event go in the actionPerformed.
The drag and drop of a picture howerver is working in all cases.
Is there something that prevent the plugin from reading the clipboard ? Or to override the defaut paste action ?
If needed I just commited my changes in the dev branch of my plugin : https://github.com/alain57/swissas-dev-tools
Thanks in advance for your help.
Best regards,
Alain
Please sign in to leave a comment.
Implement com.intellij.openapi.actionSystem.DataProvider in your DragDropTextPane and return your com.intellij.ide.PasteProvider implementation for com.intellij.openapi.actionSystem.PlatformDataKeys#PASTE_PROVIDER
Thanks Yann :)
by any chances, do you have a source code I could look at ?
My actual solution has no paste_provider and i admit that i'm a bit lost .
Thank you in advance for your help.
You can find a number of implementations in IntelliJ Community sources https://github.com/JetBrains/intellij-community
Sorry to disturb you again.
I implement the class like you said and wrote this inside of the method
I put a breakpoint in the return.this.picturePasteProvider but it never goes in (i'm doing CTRL + V )
maybe it is easier if I share the entire class ?
I'm not a big fan of implementing all logic in one single class, but here i did it to simplify the code and have just one file.
with the help of someone on the slack page of intellij I implemented following working solution
here the pastePicture of my component
and last important part : needed following change on my plugin.xml file
<editorActionHandler action="EditorPaste" implementationClass="com.swissas.handler.PasteImageHandler" order="first" />
of course all is avalaible open source one my github account ;)
This case can therefore be mark as resolved.
Alain, you may want to add code to catch exceptions generated by the original handler and direct the error report to the plugin causing the exception.
Otherwise, your plugin will get the blame in the IDE exception report and the party responsible will not be the wiser. Over the last couple of years I got over 800 exception reports which were caused by other plugins or the IDE.
See last post in: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000438790-What-is-the-proper-way-to-delegate-paste-handler-execute-method-when-no-customization-is-needed-
Oh nice catch I wouldn't through of that one.
Thanks a lot for sharing. I'll add this for the next version of the plugin :)
I didn't think of it either until I started to get flooded with exception reports that had nothing to do with my plugin. Overall, about 20% of all reports I get are for original paste handler failure.