Creating a New Window Group

来源:互联网 发布:mac flash 初始化错误 编辑:程序博客网 时间:2024/05/17 07:39

In the IDE, when you're involved in GUI editing, several helperwindows open together with the Design mode of the editor. For example,when you open a TopComponent in Design mode, the Palette and theProperties window open too. However, if you had previously opened theTopComponent in Design mode and closed the Palette, the Palette isn'topened when next you open the TopComponent. Basically, the helperwindows (i.e., in this case, the Palette and Properties window) areonly open if you had them open previously, on the assumption that theirprevious open/closed state are the ones you would like to maintain.

If you had to code that for all your windows, it would be something like this in pseudo code:

if (TopComponentA is opened) {
if (HelperTopComponentB was open previously and is now closed) {
HelperTopComponentB must open again
}
if (HelperTopComponentC was open previously and is now closed) {
HelperTopComponentC must open again
}
}

That would be a lot of cumbersome work, since the above is only for the opening of the TopComponent; there'd have to be something similar for closing the TopComponent. Wouldn't it be cool if I could create a group of components and then simply do this:

group.open();

And, then, later, when I close a TopComponent, I would simply call this:

group.close();

Then, all the opened and closed helper windows would be saved inwhatever state they're in, automatically, without me having to thinkabout it. Now, that would be cool. Hmmm... I think I should create anissue in Issuezilla for this. Okay, let's do that. Oops. Wait. Section 2.3of the "New Window System API Changes" is titled "Window Groups".That's, in fact, exactly what I was looking for... So, three cheers forInterface TopComponentGroup.

So, if you go to config/Windows2Local in your user directory, you should see (after closing the IDE at least once) the following:

In the previous two blog entries, I wrote about the "Modes"folder above. The first blog entry was about creating a new mode. Thesecond was about two modes sharing the 'editor' area of the IDE (or ofanother application based on the NetBeans Platform). This time, let'slook at the "Groups" folder. If you open the "Groups" folder, one ofthe subfolders is called "debugger". It contains files for 9TopComponents, which are all opened at the same time when a debugprocess begins and closed at the same time when it ends.

Let's create our own group, add two TopComponents, and then open the TopComponents simultaneously.

  1. Create a new module project, with org.netbeans.modules.windowgroupsample as the code name base.
  2. Use the Window Component wizard twice, to create twoTopComponents. In the first page of the wizard, choose "editor" for thefirst and "output" for the second (or anything else, it really doesn'tmatter). Make sure that you don't select the checkbox, so that theTopComponent won't be shown at start up. Let's say the first iscalled "OneTopComponent" and the second "TwoTopComponent" (which meansyou should type "One" and "Two" as the prefix in the wizard) and we'llput them both in the main package.
  3. Now we're going to create the window group.Right-click the top package, create a new XML document called"MyGroupWsgrp.xml", within a new subpackage called "groups". Add asubpackage to the new group and call that subpackage "MyGroup". Insideof it, create two XML documents, one called"OneTopComponentWstcgrp.xml" and the other"TwoTopComponentWstcgrp.xml". You should now see this in the Projectswindow:

  4. Next, put this in "MyGroupWsgrp.xml":
    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE group PUBLIC
    "-//NetBeans//DTD Group Properties 2.0//EN"
    "http://www.netbeans.org/dtds/group-properties2_0.dtd">

    <group version="2.0">
    <module name="org.netbeans.modules.windowgroupsample" spec="1.0" />
    <name unique="MyGroup" />
    <state opened="false" />
    </group>

    Note: The value of the state element above specifies that the group will be closed, by default, when the application starts up.

  5. In "OneTopComponentWstcgrp.xml", change the content to this:
    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE tc-group PUBLIC
    "-//NetBeans//DTD Top Component in Group Properties 2.0//EN"
    "http://www.netbeans.org/dtds/tc-group2_0.dtd">

    <tc-group version="2.0">
    <module name="org.netbeans.modules.windowgroupsample" spec="1.0"/>
    <tc-id id="OneTopComponent" />
    <open-close-behavior open="true" close="true" />
    </tc-group>

    Note 1: The value of the tc-id element must match the value of the PREFERRED_IDString that was generated in your TopComponent, when you finished theWindow Component wizard. Have a look, and notice that the two match.

    Note 2: The values of the open-close-behavior element are the flags that indicate what will happen when group.open() and group.close() are called. For example, if the open attribute is set to "true", then by default the TopComponent will open when the group opens.

    Similar to the above, change the content of "TwoTopComponentWstcgrp.xml" to this:

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE tc-group PUBLIC
    "-//NetBeans//DTD Top Component in Group Properties 2.0//EN"
    "http://www.netbeans.org/dtds/tc-group2_0.dtd">

    <tc-group version="2.0">
    <module name="org.netbeans.modules.windowgroupsample" spec="1.0"/>
    <tc-id id="TwoTopComponent" />
    <open-close-behavior open="true" close="true" />
    </tc-group>
  6. Now we will register our new group in the XML Layer. Open the XML Layer and notice the Windows2section at the end (all generated when you created your twoTopComponents). Add the highlighted section below, to register our newgroup:
    <folder name="Windows2">
    <folder name="Components">
    <file name="OneTopComponent.settings" url="OneTopComponentSettings.xml"/>
    <file name="TwoTopComponent.settings" url="TwoTopComponentSettings.xml"/>
    </folder>
    <folder name="Modes">
    <folder name="editor">
    <file name="OneTopComponent.wstcref" url="OneTopComponentWstcref.xml"/>
    </folder>
    <folder name="output">
    <file name="TwoTopComponent.wstcref" url="TwoTopComponentWstcref.xml"/>
    </folder>
    </folder>
    <folder name="Groups">
    <file name="MyGroup.wsgrp" url="groups/MyGroupWsgrp.xml"/>
    <folder name="MyGroup">
    <file name="OneTopComponent.wstcgrp" url="groups/MyGroup/OneTopComponentWstcgrp.xml"/>
    <file name="TwoTopComponent.wstcgrp" url="groups/MyGroup/TwoTopComponentWstcgrp.xml"/>
    </folder>
    </folder>

    </folder>
  7. Save everything. Don't install the module yet, let'sfirst refresh all our window positions to their defaults (just in caseyou've moved things around and things go wrong later, best to haveeverything at their defaults so that we can analyze the situationbetter). Close the IDE. Go to the user directory and delete the Windows2Local folder in the user directory's config folder.
  8. Start the IDE again. Install the module in the IDE. Close the IDE. Go back to the Windows2Local folder and, when you open the Groupsfolder, you should now see your new group definition file as well as afolder, containing a file for each of the two TopComponents thatbelongs to the group (according to your registration entries in the XMLLayer):

  9. Now start the IDE again. Use the New Action wizard twice. The first time, create "ShowMyGroupAction" and stick this in the performAction() event:
    TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup("MyGroup");
    if (group == null) {
    return;
    }
    group.open();

    Put the cursor on the first line above and, when the lightbulbappears, let the IDE generate import statements for these packages:

    import org.openide.windows.TopComponentGroup;
    import org.openide.windows.WindowManager;

    The second time you use the New Action wizard, create "HideMyGroupAction" and stick the following into the performAction() event:

    TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup("MyGroup");
    if (group == null) {
    return;
    }
    group.close();

    Again let the IDE generate import statements for the two required packages.

  10. Install the module again. Now you can use the menuitems to show and hide both TopComponents simultaneously. There's a lotof variations that apply here. If you close one of them after openingboth, it will not be opened next time you use the menu item for showingboth. And that's only one example of the way the Window System API nowdoes all the thinking for you.

 

本文转自 Geertjan 的博客,原文地址:http://blogs.sun.com/geertjan/entry/creating_a_window_group

 

原创粉丝点击