layer.xml

a netbeans rcp blog

TopComponent Actions in a Flamingo Ribbonband

Posted by Nicklas Löf on September 28, 2010


I recently added Flamingo to my RCP application using the OfficeLAF module. Now I wanted to add all my Topcomponents from the Window menu to a Ribbonband. I will add a blog post later about how to integrate OfficeLAF into a NetBeans RCP application.

I found two ways to do this. One that requires separate Actions to be created for each Topcomponent and the other that doesn’t require any changes to the default TopcomponentActions in layer.xml

To use the later way we can use a Lookup to get all Actions from the Window menu but this gives us also the “Close window” “Reset Windows” actions aswell. But I found out that our actions are of the class AlwaysEnabledAction. Unfortuneatly this class is private so it can’t be used in a instanceof if-statement. My solution to this was to use the name from NBBundle to find the correct Actions by the name.

JRibbonBand band = new JRibbonBand(NbBundle.getMessage(VCA2Ribbon.class, "WINDOWS"),new EmptyResizableIcon(32));
     Collection<? extends Action> actions = Lookups.forPath("Actions/Window").lookupAll(Action.class);
     List<String> myTopComponents = Arrays.asList(getMessageFromBundle("CTL_MyEditorAction"), getMessageFromBundle("CTL_MyPropertiesAction"));

Now I can use the collection to match the name of the actions.

for (Action action : actions) {
if (myTopComponents.contains(ActionUtil.lookupText(action))) {
     band.addCommandButton(new BoundCommandButton(JCommandButton.CommandButtonKind.ACTION_ONLY, ActionUtil.lookupText(action), ActionUtil.lookupDescription(action), ActionUtil.lookupIcon(action), ActionUtil.lookupIcon(action, true), action), RibbonElementPriority.TOP);
}

The other way

The other way I found was to create Actions that extends AbstractAction for each of my TopComponents calling them by the root name of the TopComponent without the TopComponent-part. So if the TopComponent is called MyEditorTopComponent create a class called MyEditorAction.

An empty interface also has to be created that we will use to match the actions against.

public interface MyAction {

}
public class MyEditorAction extends AbstractAction implements MyAction{
  public MyEditorAction() {
     super(NbBundle.getMessage(MyEditorAction.class, "CTL_ MyEditorAction"));
     putValue(SMALL_ICON, new ImageIcon(ImageUtilities.loadImage(MyEditorTopComponent.ICON_PATH, true)));
  }
@Override
  public void actionPerformed(ActionEvent e) {
     TopComponent win = MyEditorTopComponent.findInstance();
     win.open();
     win.requestActive();
  }
}

Edit layer.xml so you only have the file tag left for each topcomponent.

And we are done. Now we need to loop over the actions and add them to the Jribbon.

Collection actions = Utilities.actionsForPath("Actions/Window");
for (Action action : actions) {
   if (action instanceof MyAction){
      band.addCommandButton(new BoundCommandButton(JCommandButton.CommandButtonKind.ACTION_ONLY, ActionUtil.lookupText(action), ActionUtil.lookupDescription(action), ActionUtil.lookupIcon(action), ActionUtil.lookupIcon(action, true), action), RibbonElementPriority.TOP);
    }
}

After launching the application the Ribbon will look like below and when you click the icons the TopComponent will be opened up just like it would when selected in the Window menu.

About these ads

6 Responses to “TopComponent Actions in a Flamingo Ribbonband”

  1. Dirk said

    Hi!

    Amazing blog. Thank you!

    I am really interested in adding the OfficeLAF to Netbeans RCP.

    Do you still have plans to do a blog entry about that?

    Best regards.
    Dirk

  2. Is there still a continuation of the Flamingo Blog Series planned?

    btw: I REALLY like your blog, one of the most useful NetBeans Resources out there :)

  3. Tom said

    I tried to build officelaf from the SVN sources but failed.
    Perhaps you could do a topic about that?

    Thank You, love your blog.

    Tom

  4. Mx said

    When you will show “how to integrate OfficeLAF into a NetBeans RCP application”??

  5. […] Actions in a Flamingo Ribbonband http://layerxml.wordpress.com/2010/09/28/topcomponent-actions-in-a-flamingo-ribbonband/ This are the basis of a practical integration featuring […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: