Over a million developers have joined DZone.

Who Created My Open Windows?

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

Someone recently asked me how to figure out programmatically which module in a NetBeans Platform application (such as, for example, NetBeans IDE) created which window. I don't know why they needed to know that. Maybe they're creating new windows in their own application, like what they see in NetBeans IDE, and then want to find the sources of a particular window. Possibly they want to then learn from the sources or simply copy them. Anyway, how to identify the location of a particular window? Read on...

Basically, wouldn't it be nice if there were one central location where all the classes that define windows were to be registered? It would be, let's say, a "WindowManager". If there was such a thing, and it had a registry, we might be able to ask it which of the windows in its registry are opened. And, maybe, which of the opened windows is currently active. Yes, it would be nice if the world was like that.

But... what if the world is exactly like that?

    int count = 0;

@Override
public void performAction() {
count = count + 1;
//Create tab in Output Window each time the action is invoked:
InputOutput io = IOProvider.getDefault().getIO("All Open TopComponents (" + count + ")", true);
//Open the Output window if closed/bring it forward if in the back:
io.select();
//Get all open windows from the window manager's registry:
Set<TopComponent> openTCs = WindowManager.getDefault().getRegistry().getOpened();
//Create iterator:
Iterator<TopComponent> it = openTCs.iterator();
while (it.hasNext()) {
//Get next window from iterator:
TopComponent onenOpenTC = it.next();
//Get the name:
String name = onenOpenTC.getName();
//Get the class name:
String pkgName = onenOpenTC.getClass().getName();
//Write name and class name to Output Window:
io.getOut().println("\"" + name + "\" is created by this class: \"" + pkgName + "\"");
}
//Also get the activated window from the window manager's registry:
TopComponent activeWin = WindowManager.getDefault().getRegistry().getActivated();
//Write the activated window's name to the Output window:
io.getOut().println("----->\nBut only ONE can be active at a time. Right now it is: \"" + activeWin.getName() + "\"");
}

The above is a CallableSystemAction.performAction. You need to set dependencies on I/O APIs, Utilities API, and Window System API. Then install the plugin, open the windows you are interested in (and bear in mind that each open document is a new window) and then invoke the action, which should get you a result similar to the below:

Now that you know the class name of the window/s you are interested in, you can look in the sources for the package name. At least, you now have more clues than before and are more likely to figure out where the window was defined.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}