Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

Adding an Action to the Tab of a TopComponent

12.18.2010
| 5470 views |
  • submit to reddit
I've been working on a 7.0 branch for the JFugue Music Notepad project and added a "Rename Score" action on the tab of the Score window:

Here's how, in the TopComponent:

@Override
public Action[] getActions() {
    int existingActions = super.getActions().length;
    int existingActionsPlusRename = existingActions + 1;
    Action[] scoreActions = new Action[existingActionsPlusRename];
    List<Action> scoreList = new ArrayList(existingActionsPlusRename);
    scoreList.add(new AbstractAction("Rename Score") {
        public void actionPerformed(ActionEvent e) {
            NotifyDescriptor.InputLine newname = new NotifyDescriptor.InputLine("Name: ", "Rename Score");
            DialogDisplayer.getDefault().notify(newname);
            setDisplayName(newname.getInputText());
        }
    });
    scoreList.addAll(Arrays.asList(super.getActions()));
    for (int i = 0; i < scoreList.size(); i++) {
        Action action = scoreList.get(i);
        scoreActions[i] = action;
    }
    return scoreActions;
}

Can't promise it's the best solution, though. Instead of hard coding the Rename Action, it could be defined in the layer and then read from its folder (Utilities.actionsForPath), which would make the tab extensible from the outside.

 

Published at DZone with permission of its author, Geertjan Wielenga.