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 in whatever state they're in, automatically, without me having to think about it. Now, that would be cool. Hmmm... I think I should create an issue in Issuezilla for this. Okay, let's do that. Oops. Wait. Section 2.3 of the "New Window System API Changes" is titled "Window Groups". That's, in fact, exactly what I was looking for... So, three cheers for Interface 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. The second was about two modes sharing the 'editor' area of the IDE (or of another application based on the NetBeans Platform). This time, let's look at the "Groups" folder. If you open the "Groups" folder, one of the subfolders is called "debugger". It contains files for 9 TopComponents, which are all opened at the same time when a debug process 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.
<?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.
<?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_ID String that was generated in your TopComponent, when you finished the Window 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>
<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>
TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup("MyGroup");
if (group == null) {
return;
}
group.open();
Put the cursor on the first line above and, when the lightbulb appears, 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.
Read Section 2.3, where you'll find 5 scenarios and the ways that the open/close state of the windows is handled for you.
One question: my application has TopComponents in different packages, and I cannot get the groups to work in that case. I can get your example to work, but if I move one of the classes to another package, for example, move TwoTopComponent and TwoAction to org.netbeans.modules.windowgroupsample.two, the TwoTopComponent no longer can be found in the group. Am I just missing updating an xml file or is this not even possible?
Thanks
I created a group based on the example along with section 8.8 of your book. The group opens my TopComponent (a BeanTreeView) and the properties sheet. When I select a node in my tree, the property shows up in the property sheet. "Clean and build all" and everything works fine on first run. On subsequent runs, however, selected nodes do not show up on the property sheet unless I close and re-open the property sheet (or minimize it in the dock and open it again).
o Is this because of an ordering issue?
o Does the property sheet have to be opened after my top component in order to pick up the ExplorerManager? What if the property sheet remains open because of a different function, then I can't force the order when my group opens anyway?
o Is there more to Groups definition to force this kind of sequential behaviour? Maybe close and open the property sheet? (where can I find a spec for the layer.xml anyway?)
Here is my layer.xml snippet:
<folder name="Groups">
<file name="TestCaseGroup.wsgrp" url="groups/TestCaseGroupWsgrp.xml"/>
<folder name="TestCaseGroup">
<file name="TestCaseTopComponent.wstcgrp" url="groups/TestCaseGroup/TestCaseTopComponentWstcgrp.xml"/>
<file name="properties.wstcgrp" url="groups/TestCaseGroup/propertiesWstcgrp.xml"/>
</folder>
</folder>
------
I am puzzled about another issue. My TopComponent should not be opened until a connection to a db is made. I have created a LookupListener to enable the menu item. I set persistence state to PERSISTENCE_NEVER. But if I open the group, then close the application with the group open, restart the application, then subsequent attempts to open the group appear to do nothing. I have only been able to circumvent this by adding the following to Installer:
public boolean closing() {
// Close window groups
TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup("TestCaseGroup");
if (group != null) {
group.close();
}
return true;
}
I think that somehow on startup, there is an attempt to restore my group and hence TopComponent (which shouldn't because I have PERSISITENCE_NEVER for my component, and which will fail because my db connection isn't there yet). But something is happening, because if I close the group on shutdown, things are back to normal.
If there may be further insites into groups which someone may add, I'd appreciate it.
Thank-you
Cheers!
group.open();
TestCaseDiffTopComponent.getDefault().requestActive();
TestCaseTopComponent.getDefault().requestActive();
Hi, I execute l'example above create window group, but in Window2Local don't build my custom group, everyone could help me??? thanks in advance,
Best regard. Jhonny
1. When I try to find my component group I always get null, so open() never gets called (I followed this set of instructions except I replaced One, Two and My with meaningful names.) It is unclear as to why this should ever return null in the first place unless the group doesn't exist; since the same code that defines the group also looks it up, this suggests that the code which checks for null doesn't need to, and should just assume it isn't.
2. This example assumes that all your windows are singleton ones (the sort which normally have entries in the Window menu.) What about the "usual" case, where you want some singleton windows to automatically appear when editing a certain document type? Are you supposed to leave the document type itself out of the group, and open the group from the TopComponent?
3. It sucks to have to write so much XML... some frameworks seem to replace Java with XML and then try to pretend "look, you didn't have to write any Java", when XML is actually worse. Is there a programmatic way of doing the same thing?
Thank you for good example.
I registe group window2Local folder.
I configured such as one window in explorer the other in editor.
It seems work fine But when the group is closing, just one window in explorer is closed.
After manually closing remaining window in editor, reopening is fine.
when window in editor opened, trying to open the group, then two window are opened in editor.
I am using IDE 8.1. There are no problems with other version?