X

Geertjan's Blog

  • October 2, 2008

Opening Multiple TopComponents For One File

Geertjan Wielenga
Product Manager
In Convert your TopComponent to a MultiViewElement I explained, in the first part of that blog entry, how to open a file represented by a node in an explorer view into a TopComponent (instead of into the Source Editor, where a file opens by default). Basically, as described in that blog entry, you need to extend org.openide.loaders.OpenSupport and implement org.openide.cookies.OpenCookie and org.openide.cookies.CloseCookie. Not very nice code, but it works perfectly.

However, what if you want to open multiple TopComponent at the same time whenever you open a file? There might be different views onto the same file, all provided by different TopComponents. In this case, I've discovered that you need to override the open() method in your OpenSupport class. Then you can open the TopComponents in one of two ways: if you want group behavior you will create a TopComponentGroup and then call open on the group, within the overridden open().

@Override
public void open() {
super.open();
TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup("MyGroup");
if (group != null){
group.open();
}
}

Alternatively, just call open() on each of the TopComponents separately and then call active() on the TopComponent that should be active when the file is opened. Note that in this case the TopComponent must be a CloneableTopComponent, but that doesn't necessarily mean that the TopComponent will be cloneable, since only TopComponents in 'editor' modes are cloneable, never TopComponents in 'view' modes (i.e., there's no 'Clone Document' menu item on TopComponents in 'view' modes). Below, instead of creating a TopComponentGroup, I simply opened another TopComponent from the open() in the OpenSupport class:

And here's the OpenSupport class:

class DemoOpenSupport extends OpenSupport implements OpenCookie, CloseCookie {
TwoTopComponent tc;
String name;
public DemoOpenSupport(Entry primaryEntry) {
super(primaryEntry);
DemoDataObject dobj = (DemoDataObject) primaryEntry.getDataObject();
this.name = dobj.getName():
this.tc = new TwoTopComponent();
}
@Override
protected CloneableTopComponent createCloneableTopComponent() {
OneTopComponent tc = new OneTopComponent();
tc.setDisplayName(name);
return tc;
}
@Override
public void open() {
super.open();
tc.setDisplayName(name);
tc.open();
tc.requestActive();
}
}

Join the discussion

Comments ( 1 )
  • Janez Friday, February 13, 2009

    How do you create a TopComponentGroup?

    How do you register it under a specific name (e.g. "MyGroup", as you suggest)?


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services