Mark document as modified and save.

I'm searching a way to save document. I have a custom GUI editor for xml-like file. When user modifies file, i can easy modifiy xml file DOM representation

I think i shoud catch file change event in VirtualFilemanager or FileDocument listeners. like this:

m_Document = FileDocumentManager.getInstance().getDocument(file);           m_Document.addDocumentListener(new DocumentListener() {                @Override public void beforeDocumentChange(DocumentEvent event) {}                @Override public void documentChanged(DocumentEvent event) {                     saveToFile();                }           });



But I'm realy stuck with notifing any event dispatcher about file change.

My custom editor has method isModified, but events won't fire. I set brekoint and It's look like editor never calls it.

It's strange, may be I'm doing something wrong?

P.S. There is no file rename event too. Look like i have missed something important.

5 comments

If you're creating the PSI file from a real virtual file that exists on disk and modify it through the PSI, you don't need to do anything extra to save the changes. IntelliJ IDEA will take care of it itself.

To show correct modified status for editor tabs, you need to return the result of calling FileDocumentManager.isFileModified() from your implementation of FileEditor.isModified().

0

Thanks! Good, now I think PSI is inevitability.

I see article http://confluence.jetbrains.com/display/IDEADEV/Accessing+XML+through+IntelliJ+IDEA+DOM. Using interfaces looks very promising, but what shoud I do if I have non-constant set of tags.
Like

<root>
  <item1>
    <a></a>
    <b></b>
  </item1>
  <item2>
    <a></a>
    <b></b>
  </item2>
</root>


Can I use some DOM interface to describe that kind of structure? I need simply access <a></a> tag's text.

I get PsiFile

fileViewProvider.getPsi(StdLanguages.XML);

But it gives me a very low-level API, and i need to do a lot of iterations through getChildren() function result, which have a lot of tokens like, whitespaces, XML_START_TAG_START. I'm a bit confused. this kind of Psi usage looks like an overhead? Or I missed some useful utility methods? Sorry for so dummy question, but there isn't much documentation for it.

0

You can use DOM in this situation, but that's quite a bit more complicated that using the XML PSI. Actually the XmlTag interface has a number of higher-level APIs such as findSubTags() and getAttributeValue(), so you needn't use the low-level getChildren() method directly.

0

Hmm. And using XmlTag interface I will change PSI, so will be able to save changes. Thanks! Will try now!

0

Please sign in to leave a comment.