简体   繁体   English

如何更改NewProject按钮的图标【Netbeans IDE插件开发】?

[英]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.我正在编写一个 Netbeans IDE 插件,它应该能够替换工具栏中的一些图标。 According to this blog entry I searched for the particular actions and wrote the appropiate class IconReplacer which works perfectly:根据这个博客条目,我搜索了特定的操作并编写了合适的类 IconReplacer ,它完美地工作:

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!但是,当我尝试替换特别是 NewProject 操作的图标时,旧图标仍然存在并保持不变! After comparing the source code of NewProject.java and NewFile.java(that works) in the projectui module I spotted one strange difference.在比较了 projectui 模块中 NewProject.java 和 NewFile.java(有效)的源代码后,我发现了一个奇怪的区别。 In NewProject.java the Action is registered with additional annotations:在 NewProject.java 中,Action 注册了附加注释:

@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.这些注释不存在于 NewFile.java 中。 There the property "baseIcon" is set by那里的属性“baseIcon”由

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!尽管这两个变体应该做同样的事情,但 NewProject 按钮的图标不会改变!

You can't change the icon of the NewProject action because its instance is created using a lazy factory.您无法更改 NewProject 操作的图标,因为它的实例是使用惰性工厂创建的。 By default all Actions created using @ActionRegistration are created this way, unless the annotation parameter lazy=false is specified.默认情况下,使用@ActionRegistration创建的所有Actions都是以这种方式创建的,除非指定了注释参数lazy=false

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).正如此处的 Javadoc 中所解释的,惰性实例化是一种仅在调用(调用actionPerformed()时延迟创建操作实例的机制。 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).如果 UI 元素(例如按钮)需要操作的图标,则使用元数据(例如iconBase注释参数) iconBase

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.因此,当您使用action.putValue("iconBase", "iconPath") ,实际上您更改了由注释( 此处为源代码)创建的AlwaysEnabledAction实例的属性,而不是委托操作 NewProject 的属性。

As you found it, the NewFile action is not created lazily, that's why changing the icon property works.正如您所发现的,NewFile 操作不是惰性创建的,这就是更改 icon 属性起作用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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