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
- 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.)
- 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.
- 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.
- 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.
- 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:
- Create a new module project. Project name: "MyBannerMode". Code name base: "org.netbeans.modules.mybannermode". Click Finish.
- Add this section to the XML layer:
<file name="banner.wsmode" url="modes/banner.wsmode"/>
- 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.
- Copy the content of navigator.wsmode (from the Windows2Local/Modes folder) into your empty file, but change name unique="navigator" to name unique="banner".
- Check that the Projects window now looks as follows:
- Right-click the module and choose "Install/Reload in Development IDE".
- 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):
- 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:
- 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.
- 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.