Gui form to read and write xml file Follow
I´m developing a plugin that uses a form to read an write an xml file in a particular format. I create a gui form and attached to the file type I need. Now I want to show a full editor to write java code with a fragment of the xml file. I used the EditorTextField to put the code and set the FileType to java, but I don´t have the full feature of an editor in the field like code completion, highlights, line number and more. I´m attaching a image of the plugin to illustrate better what I´m writing here. If I can´t use the full editor inside this form, is possible to create a button that opens a dialog with the full editor with my code? What I´m trying to achieve is something like the Local History -> Show History feature of Intellij Idea that contains a full editor in the dialog. I try to find that form in the source codee of the community version to see how it´s done, but don´t have success.
Please sign in to leave a comment.
As for line numbers and other presentation-related options, check com.intellij.ui.EditorTextField#createEditor and com.intellij.ui.EditorTextField#addSettingsProvider methods. You can either override the former, or use the latter to change editor settings.
As for completion/highlighting, you need to add those yourself, probably by delegating to the default implementations. I can't say anything more specific unless you provide more details about which completion/highlighting you need there.
Hello Dmitry and Peter,
thanks for the reply. The addSettingsProvider give me some features I need, but I can't put much features I want there. I can't even use tabs in the field.
what I need is a full editor to write code in Java, Groovy and HTML. These codes are inside the xml I´m reading and I want to edit this code and then save again inside these xml. I already read and write these code but I want to edit it the best way I can, with code completion and highlight the same way it happens when I open a java file, groovy file or html file.
Do you think there is some example code I can see? The Local History -> Show History of intellij, gives me a full editor in a dialog. I'm wondering if they use EditorTextField there, but I can't find this code in the community edition.
> I can't even use tabs in the field.
Do you mean entering \t characters? They are enabled per file type, and corresponding code uses PSI file associated with editor document, to determine that file type. Could you please check that
returns not null in your case, and getFileType() on the result returns Java file type?
my code sets the java type (or try at least).
The EditorTextField name is "codigo":
Please try first creating a file via PsiFileFactory#createFileFromText (with a Java/Groovy/HTML extension), passing eventSystemEnabled=true, and then creating an editor for its document (EditorFactory#createEditor(file.getViewProvider().getDocument())).
thnaks for the response. I can create a PsiFile, but EditorFactory#CreateEditor returns an com.intellij.openapi.editor.Editor and I cannot put this element in a form beacuse it is not derived from JComponent. Is there a way to put Editor in a form? Or can I somehow use this Editor inside EditorTextField?
You can put editor.getComponent() into Swing component hierarchy.
I'm sorry for my ignorance, but what component I should create in the form? And how to bind this component with the Editor I create in the code?
You can just have a JPanel with a BorderLayout, and add editor.getComponent() there. Unfortunately there's no easy way to add editor components using IDEA's GUI designer.
Hum, I understand, but can I have a JPanel inside another Jpanel or the editor has to be in the full form?
You can certainly have a JPanel inside another JPanel. I don't understand what you mean by "full form"
OK Peter, I think almost there...
I create this code inside createUIComponents():
where editorjp is a Jpanel with border layout. It raises me this error:
The line 173 is "editorjp.add(editor.getComponent());"
This is the full error stack:
[ 14582] ERROR - tor.impl.FileEditorManagerImpl - null
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at com.sun.proxy.$Proxy41.appStarting(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
Please try adding editor to the panel in the class constructor.
BTW you might find JavaLanguage.INSTANCE useful.
I think the error was based on this bug:
But now I can create the element. This is awesome. I figure out that I have to change various setting to achieve what i really want like code completion, highlights etc but now I think I´m with the right component.
Do you know where I can find examples on how to use colors , highlights and code completion for each language I want?
Anyways your help was very important here, and I appreciate it.
Actually, if you create files of specific languages like this, you should get most of the highlighting and completion for free.
That´s exactly what I was thinking, but unfortunately that´s not what is happening in my plugin. I don't hava even the colors of the text properly. How can I check if I am setting the language correctly?
It´s getting better ;)
Now when I create the editor this way:
BTW, I believe, you can also use EditorTextField if you wish, you just need to instantiate it directly (via constructor), passing psiFile.getViewProvider().getDocument() as document parameter.
Peter and Dmitry thanks again for your help. My plugin is almost working now ;).
Can you help in one more thing? Now I put the code inside the editor and everything is working fine. I have now to write the contents of the editor, when it is edited, back to xml file. Is there an event that I can listen to write this back to my file?
It depends on how often you need to do that synchronization. The less frequently, the better. Doing that on every document change can result in significant performance overhead.
Peter, that is right. I try to update the contents on every change and it became slow and unpredictable. Now I create a Update button near the code and another trigger to save the contents when the user exits the particular window. It works great now.
I have a new question about this plugin. When I invoke th Run/Debug from my project the plugin works fine, but when I try to install it from the zip file in this same machine it does not work. Is there any way I can check why it´s not working installing this way?
It's hard to tell. Doesn't idea.log (Help | Show log) list the plugin as loaded? There might be many possible reasons for that; for example, since/until build in plugin.xml that don't match IDE's build. Or that the plugin is not found where the plugins are supposed to be found (config/system directory).
I found out what it was. When Idea starts from "Run" it was using Java 8 to load and my plugin was compiled in that version. But when I was clicking in the Idea icon, it was running with Java 7 in my machine. When I changed Idea to uses Java 8 all worked fine.
Thanks for your reply.