NetBeans 7.2 custom FileTypes – synchronizing

NetBeans 7.2 has a new “new FileType” wizard. ( Here’s a tutorial describing what you can do with it: Tutorial).
To try it out, I created a little Editor for a filetype that describes worlds for popular physics engine Box2D and it’s Java Port JBox2D. The editor should consist of a plain Text Editor (mostly for debugging) and a Visual Editor for Drag & Drop. Fortunately JBox2D already uses Google Protocol Buffers which can be serialized to a String.

I basically did the same steps outlined in the tutorial to create support for a “box2d” Mimetype, but I wanted to have the views synchronized. So when someone changes the file, even without saving, the changes should be reflected in the visual editor and the other way round. So instead of listening for the file being saved, as in Geertjan’s example, in my MultiViewElement I’m listening for changes to the document.

        DataEditorSupport cookie = obj.getLookup().lookup(DataEditorSupport.class);
        document = cookie.getOpenedPanes()[0].getDocument();
        documentListenerImpl = new DocumentListenerImpl();

Whenever the document is changed I set a flag, so I know I need to update my MultiViewElement, when the user switches to the visual representation. That’s what we can use componentActivated for:

    public void componentActivated() {
        if (!synced) {

On the other hand I only want to update the document, when I switch back to the editor, so again I only set a flag if someone changes the visual representation. I’m using componentHidden for this:

    public void componentHidden() {
        if (syncDocument) {
            syncDocument = false;

That’s not perfect, because the code will also be called when user changes to a different view than the Source View, but it certainly reduces the number of calls. Not sure if this is the best way of doing it, but it’s really simple to create a two-way editor like this:

The complete project is available on GitHub. Enjoy!

Dieser Eintrag wurde in netbeans geschrieben. Link merken.

$ Meinungen über "$ s"

  1. Habe gerade Deinen Sync-Mechanismus für meine file-types implementiert. Ist wirklich simple und funktioniert prima. Da ich bei einigen file-types mit sehr grossen Dateien arbeite ist es allerdings etwas störend, dass unnötig oft ein update erfolgt. Ich werde daher versuchen den Mechanismus zu erweitern: herausfinden zwischen welchen Topcomponents gewechselt wird …

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>