• 作者:老汪软件技巧
  • 发表时间:2024-09-24 07:02
  • 浏览量:

通知

右键选中word并弹出选中消息通知。

创建一个action。

我们通过devKit创建一个action。

使用通知接口弹出内容

首先我们要先在Plugin.xml中注册一个通知Group

 class="hljs language-ini" lang="ini">defaultExtensionNs="com.intellij">
    id="ts.notice" displayType="BALLOON"/>    

复制代码

然后开始写代码

Notification notification = new Notification("listener", "标题", content, NotificationType.INFORMATION);  
Notifications.Bus.notify(notification, e.getProject());
复制代码

最后面的notify中要填一个 Project ,这个类型大概是在哪个项目(idea)中弹出。 完成代码如下:

@Override  
public void actionPerformed(AnActionEvent e) {  
    Project project = e.getProject();  
    Editor editor = e.getData(CommonDataKeys.EDITOR);  
    if (project!= null && editor!= null) {  
        // 选中内容  
        String content = editor.getSelectionModel().getSelectedText();  
        if(content!=null ) {  
            //提示消息  
            Notification notification = new Notification("listener", "选中", content, NotificationType.INFORMATION);  
            Notifications.Bus.notify(notification, e.getProject());  
        }  
    }  
}
复制代码

然后我们看下显示的效果

消息窗口提示

将Notification 部分替换为下面代码

Messages.showMessageDialog(e.getProject(), content, "选中", Messages.getInformationIcon());
复制代码

然后我们看下运行效果

国际化支持

有些时候开发的插件需要对多语言进行支持,就需要用到国际化功能。

通知栏框架_通知中心添加插件_

多语言配置

在resources新建一个messages文件夹,存放不同语言的配置文件。

# myfirstplugin_zh.properties 
action.notification.text= 选中
# myfirstplugin_en.properties 
action.notification.text= selected
复制代码

读取配置

java原生提供了ResourceBundle类实现国际化功能。我们要新建工具类,用于读取配置。

import com.intellij.AbstractBundle;  
import org.jetbrains.annotations.NonNls;  
import org.jetbrains.annotations.NotNull;  
import org.jetbrains.annotations.PropertyKey;  
  
import java.text.MessageFormat;  
import java.util.Locale;  
import java.util.ResourceBundle;  
  
/**  
* 国际化支持  
*/  
public final class Bundle extends AbstractBundle {  
    @NonNls  
    public static final String I18N = "messages.myfirstplugin";  
    private static final Bundle INSTANCE = new Bundle();  
    private Bundle() {  
        super(I18N);  
    }  
  
    @NotNull  
    public static String message(@NotNull @PropertyKey(resourceBundle = I18N) String key, @NotNull Object ... params) {  
        System.out.println(key);  
        String string = message(key,Locale.ENGLISH);  
        if(MessageFormat.format(string,params)==null){  
            return MessageFormat.format(string,params);  
        }  
        return INSTANCE.getMessage(key, params);  
    }  
  
    private static String message(String key, Locale locale){  
        ResourceBundle bundle = ResourceBundle.getBundle(I18N,locale);  
        return bundle.getString(key);  
    }  
}
复制代码

应用

在代码中通过Bundle.message()调用。

Bundle.message("action.notification.text", new Object[0])
复制代码

插件国际化

除了提示语使用ResourceBundle来获取对应的值外,插件中的菜单名也需要多语言支持,可以直接按照下面的格式,给注册的myfirstplugin.properties文件添加配置后,可以自动获取对应语言的值。

action..text = Action Text
action..desc = Action Desc
复制代码

示例: plugin.xml中需要注册国际化文件,有下面两种方式

<idea-plugin>
	<resource-bundle>messages.myPluginresource-bundle>
	<action id="NotificationAction" class="com.github.myfirstplugin.action.NotificationAction" text="simple notification"  
    description="simple notification">  
    <add-to-group group-id="EditorPopupMenu" anchor="first"/>  
    action>  
idea-plugin>
复制代码

或者

resource-bundle="messages.myfirstplugin">  
    id="NotificationAction" class="com.github.myfirstplugin.action.NotificationAction" text="simple notification"  
    description="simple notification">  
    group-id="EditorPopupMenu" anchor="first"/>  
      

复制代码

国际化配置文件

# myfirstplugin_zh.properties
action.NotificationAction.text = 选中词语
action.NotificationAction.desc = 菜单1描述
# myfirstplugin_en.properties
action.NotificationAction.text = selected word
action.NotificationAction.desc = Action Desc
复制代码

运行效果如下


上一条查看详情 +Vue 3 中生成动态的 Word 文档
下一条 查看详情 +没有了