简体   繁体   中英

How can you change the icon of the NewProject button [Netbeans IDE plugin development]?

I am writing a Netbeans IDE plugin which should be able to replace some icons in the toolbar. According to this blog entry I searched for the particular actions and wrote the appropiate class IconReplacer which works perfectly:

import org.openide.filesystems.FileUtil;
import org.openide.windows.OnShowing;
import javax.swing.*;

@OnShowing
public class IconReplacer implements Runnable{

public static final String ICON_DIR = "com/geometror/iconreplacer/icons/";

@Override
public void run() {

    replaceIcon("Actions/Project/org-netbeans-modules-project-ui-NewFile.instance",
            "newFile");

}

private void replaceIcon(String configObj, String iconName){
    Action newFileAction =
            FileUtil.getConfigObject(configObj, Action.class);

    newFileAction.putValue("iconBase", ICON_DIR + iconName + ".png");
    newFileAction.putValue("PreferredIconSize", 24);

}
}

However, when I try to replace the icon of the NewProject action in particular, the old icon persists and stays unchanged! After comparing the source code of NewProject.java and NewFile.java(that works) in the projectui module I spotted one strange difference. In NewProject.java the Action is registered with additional annotations:

@ActionID(id = "org.netbeans.modules.project.ui.NewProject", category =     "Project")
@ActionRegistration(displayName = "#LBL_NewProjectAction_Name", iconBase = "org/netbeans/modules/project/ui/resources/newProject.png")
@ActionReferences({
@ActionReference(path = "Shortcuts", name = "DS-N"),
@ActionReference(path = ProjectsRootNode.ACTIONS_FOLDER, position = 100),
@ActionReference(path = "Menu/File", position = 100),
@ActionReference(path = "Toolbars/File", position = 200)
})

These annotations are not present in NewFile.java. There the property "baseIcon" is set by

putValue("iconBase","org/netbeans/modules/project/ui/resources/newFile.png"); //NOI18N

Although these two variants should do the same thing the icon of the NewProject button doesn't change!

You can't change the icon of the NewProject action because its instance is created using a lazy factory. By default all Actions created using @ActionRegistration are created this way, unless the annotation parameter lazy=false is specified.

As explained in the Javadoc here , lazy instanciation is a mechanism to defer the creation of the action instance only when it's invoked ( actionPerformed() is called). Objective is to limit startup overhead. If a UI element (for ex. a button) needs the icon of the action, it is served using metadata (for example the iconBase annotation parameter).

So when you used action.putValue("iconBase", "iconPath") , actually you changed the property of a AlwaysEnabledAction instance created by the annotation (source code here ), not the property of the delegate action NewProject.

As you found it, the NewFile action is not created lazily, that's why changing the icon property works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM