Gui form to read and write xml file

Answered

Hello,
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.

Regards,

Werther

0
25 comments

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.

0

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.

0
Avatar
Werther Vervloet

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.

Peter, 

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.

 

Best Regards,

Werther

 

 

0

> 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

PsiDocumentManager.getInstance(project).getPsiFile(document)

returns not null in your case, and getFileType() on the result returns Java file type?

0
Avatar
Werther Vervloet

Dmitry,

my code sets the java type (or try at least).

My code:

The EditorTextField name is "codigo":

Language language = Language.findLanguageByID("JAVA");
FileType fileType = language != null ? language.getAssociatedFileType() : null;
Document doc=EditorFactory.getInstance().createDocument("import br.com.aaf.*;");
Set<EditorCustomization> features = new HashSet<EditorCustomization>();
features.add(SoftWrapsEditorCustomization.ENABLED);
features.add(HorizontalScrollBarEditorCustomization.ENABLED);
features.add(AdditionalPageAtBottomEditorCustomization.DISABLED);

Project defaultProject = ProjectManager.getInstance().getDefaultProject();
EditorTextFieldProvider service = ServiceManager.getService(defaultProject, EditorTextFieldProvider.class);

codigo = service.getEditorField(language, getProject(), features);
codigo.setDocument(doc);
codigo.addSettingsProvider(new EditorSettingsProvider() {
@Override
public void customizeSettings(EditorEx editor) {
editor.getSettings().setWhitespacesShown(false);
editor.getSettings().setLineNumbersShown(true);
editor.getSettings().setVariableInplaceRenameEnabled(true);
editor.getSettings().setAutoCodeFoldingEnabled(true);
editor.getSettings().setBlinkCaret(true);
editor.getSettings().setCustomSoftWrapIndent(4);
editor.getSettings().setAdditionalPageAtBottom(true);
editor.getSettings().setIndentGuidesShown(true);
editor.getSettings().setFoldingOutlineShown(true);
editor.getSettings().setLineMarkerAreaShown(true);
editor.getSettings().setShowIntentionBulb(true);
editor.getSettings().setTabSize(4);
editor.getSettings().setWheelFontChangeEnabled(true);
editor.getSettings().setRightMarginShown(true);
editor.getSettings().setUseTabCharacter(true);
editor.getSettings().setSmartHome(true);
editor.getSettings().setRightMargin(10);
editor.getSettings().setDndEnabled(true);
editor.getSettings().setCamelWords(true);
editor.getSettings().setAutoCodeFoldingEnabled(true);
editor.getHighlighter().setText("Teste");
EditorColorsScheme colorsScheme = editor.getColorsScheme();
colorsScheme.setColor(EditorColors.CARET_ROW_COLOR, null);
colorsScheme.setEditorFontName("Verdana");
colorsScheme.setEditorFontSize(18);

editor.setColorsScheme(colorsScheme);

}
});

Regards,

Werther
0

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())).

0
Avatar
Werther Vervloet

Hi Peter,

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?

Regards,

Werther

0

You can put editor.getComponent() into Swing component hierarchy.

0
Avatar
Werther Vervloet

Peter,

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?

Regards,

Werther

0

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.

0
Avatar
Werther Vervloet

Hum, I understand, but can I have a JPanel inside another Jpanel or the editor has to be in the full form?

0

You can certainly have a JPanel inside another JPanel. I don't understand what you mean by "full form"

0
Avatar
Werther Vervloet

OK Peter, I think almost there...

I create this code inside createUIComponents():

Language language = Language.findLanguageByID("JAVA");
PsiFile f=PsiFileFactory.getInstance(getProject()).createFileFromText(language,"import br.com.aaf.*;");
Editor editor=EditorFactory.getInstance().createEditor(f.getViewProvider().getDocument());
editorjp.add(editor.getComponent());

where editorjp is a Jpanel with border layout. It raises me this error:

java.lang.NullPointerException
at au.com.defacto.example.ui.FormEditorWindow.createUIComponents(FormEditorWindow.java:173)
at au.com.defacto.example.ui.FormEditorWindow.$$$setupUI$$$(FormEditorWindow.java)
at au.com.defacto.example.ui.FormEditorWindow.<init>(FormEditorWindow.java:87)

 

The line 173 is "editorjp.add(editor.getComponent());"

0
Avatar
Werther Vervloet

This is the full error stack:

[ 14582] ERROR - tor.impl.FileEditorManagerImpl - null
java.lang.NullPointerException
at au.com.defacto.example.ui.FormEditorWindow.createUIComponents(FormEditorWindow.java:175)
at au.com.defacto.example.ui.FormEditorWindow.$$$setupUI$$$(FormEditorWindow.java)
at au.com.defacto.example.ui.FormEditorWindow.<init>(FormEditorWindow.java:87)
at au.com.defacto.example.editor.FormFileEditorProvider.createEditor(FormFileEditorProvider.java:47)
at au.com.defacto.example.editor.FormFileEditorProvider.createEditor(FormFileEditorProvider.java:22)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl$14.run(FileEditorManagerImpl.java:894)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:857)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:658)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:386)
at com.intellij.ide.IdeEventQueue.pumpEventsForHierarchy(IdeEventQueue.java:944)
at com.intellij.openapi.progress.util.ProgressWindow.startBlocking(ProgressWindow.java:210)
at com.intellij.openapi.progress.util.ProgressWindow.startBlocking(ProgressWindow.java:197)
at com.intellij.openapi.application.impl.ApplicationImpl.runProcessWithProgressSynchronously(ApplicationImpl.java:649)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcessWithProgressSynchronously(CoreProgressManager.java:354)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcessWithProgressSynchronously(ProgressManagerImpl.java:76)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcessWithProgressSynchronously(CoreProgressManager.java:230)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcessWithProgressSynchronously(CoreProgressManager.java:183)
at com.intellij.openapi.project.impl.ProjectManagerImpl.openProject(ProjectManagerImpl.java:372)
at com.intellij.openapi.project.impl.ProjectManagerImpl.loadAndOpenProject(ProjectManagerImpl.java:468)
at com.intellij.ide.impl.ProjectUtil.openProject(ProjectUtil.java:190)
at com.intellij.ide.RecentProjectsManagerImpl.doOpenProject(RecentProjectsManagerImpl.java:60)
at com.intellij.ide.RecentProjectsManagerBase.doReopenLastProject(RecentProjectsManagerBase.java:573)
at com.intellij.ide.RecentProjectsManagerBase$MyAppLifecycleListener.appStarting(RecentProjectsManagerBase.java:607)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.util.messages.impl.MessageBusConnectionImpl.deliverMessage(MessageBusConnectionImpl.java:117)
at com.intellij.util.messages.impl.MessageBusImpl.doPumpMessages(MessageBusImpl.java:372)
at com.intellij.util.messages.impl.MessageBusImpl.pumpMessages(MessageBusImpl.java:359)
at com.intellij.util.messages.impl.MessageBusImpl.sendMessage(MessageBusImpl.java:338)
at com.intellij.util.messages.impl.MessageBusImpl.access$200(MessageBusImpl.java:42)
at com.intellij.util.messages.impl.MessageBusImpl$2.invoke(MessageBusImpl.java:226)
at com.sun.proxy.$Proxy41.appStarting(Unknown Source)
at com.intellij.idea.IdeaApplication$IdeStarter$2.run(IdeaApplication.java:354)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:345)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:329)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:857)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:658)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:386)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0

Please try adding editor to the panel in the class constructor.

BTW you might find JavaLanguage.INSTANCE useful.

0
Avatar
Werther Vervloet

I think the error was based on this bug:

http://stackoverflow.com/questions/9639017/intellij-gui-creator-jpanel-gives-runtime-null-pointer-exception-upon-adding-an

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.

 

Thankas Again,

Werther

0

Actually, if you create files of specific languages like this, you should get most of the highlighting and completion for free.

0
Avatar
Werther Vervloet

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?

Regards,

Werther

0
Avatar
Werther Vervloet

It´s getting better ;)

Now when I create the editor this way:

Editor editor=EditorFactory.getInstance().createEditor(f1.getViewProvider().getDocument(),getProject(),language1.getAssociatedFileType(),false);

I have the features I need.

I will put this plugin in my grails application now and see how it works with grrovy too!

Thanks again,

Werther
0

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.

0
Avatar
Werther Vervloet

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?

Regards,

Werther

0

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.

0
Avatar
Werther Vervloet

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?

 

Regards,

Werther

0

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).

0
Avatar
Werther Vervloet

Hi Peter,

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.

Best Regards,

Werther

0

Please sign in to leave a comment.