Creating A New Mode In Your Application

This is a detailed approach to creating a new mode (i.e., a position for a custom window component) in your Swing application. I encountered two problems with it, as explained in the instructions below, but at the end of it, you should have worked your way to a new position for a banner, for example, as shown in the day before yesterday's blog entry
  1. Close down the application (e.g., the IDE). Go to your user directory. (If you don't know where that is, start the application again and go to Help > About. In the Detail tab, look at the User Dir property.)
  2. In the user directory, go to the config folder. In the config folder, delete the Windows2Local folder. This will reset the position for all of the application's windows to their default positions. 
     
  3. Start the application. (If things look weird at this stage, you might need to delete the whole user directory.) Now everything should look as it did when you first started the application, before moving windows around. Now close the application again. 
     
  4. Back in the user directory, go to Windows2Local/Modes. You should see the following:

    All this was generated when you closed the application. The files that end with wsmode each define a mode, which is a position in the IDE. In each of the folders, with names matching the modes, you find a file for the window/s that is/are found in the mode. For example, open the navigator folder and notice that there are two files in there—ComponentInspector.wstcref and navigatorTC.wstcref. If you open these files, you will notice the state-opened element. This determines whether the window is open (true) or closed (false) when the application starts up.  

  5. Now we will create a new window. We will move it to a new position (one that is currently not defined in the application). We will then close the application and return to the Windows2Local/Modes folder. There we will see a new mode (called "anonymousMode_1.wsmode"). The information in that file we will register in the window's XML layer. So, start the IDE and take the following steps:

    1. Create a new module project. Project name: "MyBannerMode". Code name base: "org.netbeans.modules.mybannermode". Click Finish. 
       
    2. Add this section to the XML layer:  
      <folder name="Windows2">
           <folder name="Modes">
               <file name="banner.wsmode" url="modes/banner.wsmode"/>
           </folder>
      </folder>
    3. Right-click the package, choose New > File/Folder. In the Other category, choose Empty File. Click Next. Name the file "banner.wsmode". Append "\\modes" to the Folder field, so that the whole field now reads "src\\org\\netbeans\\modules\\mybannermode\\modes". Click Finish.
    4. Copy the content of navigator.wsmode (from the Windows2Local/Modes folder) into your empty file, but change name unique="navigator" to name unique="banner"
       
    5. Check that the Projects window now looks as follows:

        

    6. Right-click the module and choose "Install/Reload in Development IDE". 
       
    7. You now have a new mode, but it has the navigator mode's values. Let's change that. Create a new module, call it anything you like. Use the New Window Component wizard to create a new TopComponent, with any name you prefer. Once you've completed the wizard, open the XML layer. Change one value (the one in bold below):
      <folder name="Modes">
          <folder name="banner">
    8. Install this second module. In the Window menu, use the new menu item to open your new window. Now you can move the window to the upper part of the application, spanning its length, as shown here:

      Drop it there. Close the application. Back in Windows2Local/Modes, you should see this:

        

    9. Notice that we have a folder called "anonymousMode_1" and a file called "anonymousMode_1.wsmode". Because the IDE doesn't recognize the position to which you moved your window, it generated that folder and file for you, assigning it this anonymous name. But that anonymousMode_1.wsmode contains the values we need! Those are the values for our new "banner" mode. So open that file, copy the content, start the application again, and paste the content into your banner.wsmode file. Then change the name "anonymousMode_1" to "banner". Install the MyBannerMode module again. Then close the application.
    10. Delete the Windows2Local folder again in the user directory. Restart the application. Create a new module with a new window component. In the first panel, you should be able to choose "banner", but I think there's a bug somewhere, because that doesn't happen for me. No "banner" appears. Bummer. Never mind, the mode will work anyway. Finish the wizard. In the XML layer, just change the mode value to "banner" (as done previously in step 7).

    When you install the module and open the window, it may not appear in the "banner" mode right away. You might need to "help" it. After moving it to the correct position the first time, it will stay there. So, there's one or two weird things in this procedure, but basically, at the end of it all, you should at least be able to fiddle your way to getting a new mode for your application. When things aren't working for you, check that Windows2Local/modes/banner contains a wstcref file for each of the new windows that you assigned to "banner" mode. If the wstcref file ends up in the correct folder, the window will be displayed in the correct mode.

Comments:

Hi Geertjan. I did this some time ago, just following the advice from the mailing list. I just want to add that for me it worked without quirks, e.g. I didn't have to "help" the window in finding its new position. For what concern the missing "new_mode" in the new window wizard, I didn't remember about NetBeans 5.0, but I've just tried with 5.5rc2 and the "new_mode" appears correctly.

Posted by Fabrizio Giudici on October 18, 2006 at 05:17 PM PDT #

Hurray! I was using 5.5rc1, so now I'll try it with 5.5rc2. So thanks a lot -- it seems both my problems are not really problems!

Posted by Geertjan on October 18, 2006 at 05:30 PM PDT #

Hi, I also tried creating new mode named "banner". Now I've new mode "banner", but it comes with the header, and it looks weired, banner with header.

I am using netbeans 5.5 have u any idea, how to get rid of that header of new top component?

my e-mail : s.adyut@verchaska.com

Posted by Adyut on August 28, 2007 at 10:46 PM PDT #

Maybe this will help:
http://blogs.sun.com/geertjan/entry/farewell_to_space_consuming_weird

Posted by Geertjan on August 28, 2007 at 10:53 PM PDT #

Thank You Geertjan,

link is very good, but my problem remains, because I want to remove titlebar of a top component, whic his not in editor mode, i.e. banner top component in my case. Still thanks for help...

Posted by Adyut on August 29, 2007 at 04:10 PM PDT #

I don't know what you mean. Please write to dev@openide.netbeans.org, that is the mailing list for the NetBeans Platform. Thanks.

Posted by Geertjan on August 29, 2007 at 04:20 PM PDT #

Hi Geertjan. I was trying to make custom mode following "NetBeans Platform 6.9 Developer's Guide", the way described there is similar to this one, described by You. The thing is that I'm using NetBeans 7.0.1 and these xml files don't work. Can You tell how to create a custom window system mode for NB7 or suggest some article about this (I've searched through lot of NB-related pages and found nothing useful).

Posted by Ihor Mykhalevych on October 06, 2011 at 10:28 AM PDT #

What are you talking about? What does "these xml files don't work" mean? Of course they work. You need to have a layer.xml file, as described in the book, and then you can register your modes there. In 7.1, you'll have a visual Window Layout designer, but you'll then always have a layer file to register the custom mode.

Posted by Geertjan on October 06, 2011 at 10:41 AM PDT #

Yes, they work, my inattention. I thought that with NB 7 way of registering modes have changed to annotations. In the book there is just a piece of xml code and book tells to place it into the filesystem. I thought that there have to be some structure and that code have to be placed in some correct place. But then I've understood that it just have to be placed "in the root" of <filesystem>.

Sorry for disturbing and thanks for the response.

Posted by Ihor Mykhalevych on October 25, 2011 at 10:36 AM PDT #

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