Over a million developers have joined DZone.
Silver Partner

Adding an Action to the Tab of a TopComponent

· Java Zone

The Java Zone is brought to you in partnership with JetBrains.  Learn about instant and clever code completion, on-the-fly code analysis and reliable refactoring tools with IntelliJ IDEA.

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.

 

The Java Zone is brought to you in partnership with JetBrains.  Learn about instant and clever code completion, on-the-fly code analysis and reliable refactoring tools with IntelliJ IDEA.

Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}