Convert your TopComponent to a MultiViewElement

A brief overview of how to convert a TopComponent to a visual page in a multiview editor. First we recognize a new file type, then we change the Open menu item to open a TopComponent instead of a data editor, and then we change the TopComponent to a multi view element.

  1. Use File Type wizard to let IDE recognize your new file type.

  2. Use Window Component wizard to create new TopComponent implementation.

  3. Create an OpenSupport class, extending OpenSupport and implementing OpenCookie and CloseCookie. Content of methods:

    public MyOpenSupport(MyDataObject.Entry entry) {
        super(entry);
    }
    protected CloneableTopComponent createCloneableTopComponent() {
        MyDataObject dobj = (MyDataObject)entry.getDataObject();
        MyTopComponent tc = new MyTopComponent(dobj);
        tc.setDisplayName(dobj.getName());
        return tc;
    }

    In the data object, replace the DataEditorSupport's addition to the CookieSet by adding the OpenSupport class to the CookieSet:

    cookies.add((Node.Cookie) new MyOpenSupport(getPrimaryEntry()));

  4. Change the TopComponent's extension class to CloneableTopComponent and receive the data object in the constructor (not doing anything with it here, but useful later). Return null in the getDefault() method.

  5. Install the module and when you double-click (or choose Open from the menu) on the file type, your TopComponent will open.

Now we'll convert the TopComponent to an element in a multiview editor.

  1. Add dependency on "Core - MultiView Windows".

  2. For each page in the multiview editor, create classes that implement MultiViewDescription and Serializable. The method createElement() returns a MultiViewElement. What you want to return here is your TopComponent, which means you must modify the TopComponent to extend JPanel and implement MultiViewElement.

  3. Repeat the last step for each page in the multiview editor.

  4. Change the createCloneableTopComponent method in the OpenSupport class to open your multiview elements (here we have two):

    protected CloneableTopComponent createCloneableTopComponent() {
    
        // Create an array of multiview descriptors:
        MyPanelMultiViewDescriptor main1 = new MyPanelMultiViewDescriptor();
        MyPanelMultiViewDescriptor2 main2 = new MyPanelMultiViewDescriptor2();
        MultiViewDescription[] descArry = { main1, main2 };
    
        // Create the multiview:
        CloneableTopComponent tc = MultiViewFactory.createCloneableMultiView(descArry, main1,  null);
        return tc;
    
    }

  5. Install and open the file again. Now you have a multiview editor (without a source view, currently). The second argument in MultiViewFactory.createCloneableMultiView determines which of the pages is open by default. Here it is "main1", the page defined by MyPanelMultiViewDescriptor1:

A lot more needs to be done (for example, with the above instructions you won't even be able to close the multiview editor), but the above is a start. Thanks Rich, as you can see I'm learning a lot from reviewing your document.

Comments:

Nice :) Note that your MultiViewElement _can_ extend TopComponent, but I think stylistically it's better to make it extend JPanel. Eliminates some confusion about methods in TC that will no longer be relevant, like preferredID() and the persistence type. I'm not entirely sure that my sample code deals with close handlers correctly, so beware as you continue... I should finish up with the code tomorrow or friday.

Posted by Rich Unger on December 06, 2006 at 02:50 AM PST #

Probably there is a lot of missing code that is necessary to a beginner. For example, turning my TopComponent into a JPanel involves a lot of code deletion, and implementing the needed interfaces requires knoweledge about how to implement the various methods. But more important: the first 2 steps about running the File Type wizard and the Window wizard, will actually create an Action associated with the file open. This action by default has an actionPerformed that will find the TopComponent instance, open it and make it active. After changing the code as you suggested for the multiview, I no more have that TopComponent, because I changed it into a JPanel. How do I have to change the actionPerformed to let it open my multiview?

Posted by Gabriele Bulfon on December 18, 2006 at 08:28 PM PST #

Where can I find the complete source of your example? This would be much valuable! ;)

Posted by Gabriele Bulfon on December 18, 2006 at 08:37 PM PST #

...I forgot one...my aim would be to have my designer come up as a second view of an XML view. So, I would end up with the normal xml view available, and my deigner view to visually change the xml file. How can I easily achieve this? And once achieved, how do I get the xml structure used by the xml editor, to be able to modify it and have it reflected in the source (and vice-versa)?

Posted by Gabriele Bulfon on December 18, 2006 at 08:41 PM PST #

Doesn't have to be a JPanel. You can keep the TopComponent, and then the Action will continue to work. In the NetBeans sources, you'll find contrib/manifesteditor, an example. Doesn't have everything you need, but should help.

Posted by Geertjan on December 18, 2006 at 09:27 PM PST #

But there is nowhere shown how to synchronize FileObject, Editor panel and Visual panel. In ManifestSupport example is it not finished

Posted by uf on September 16, 2008 at 10:30 PM PDT #

Thanks for these great tech bits Geertjan! I tried this and works great but getting into trouble when trying to update the title of the TopComponent of the MultiViewElements :( any where I can get more info on this?

Posted by Dushan on November 06, 2008 at 01:03 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today