写一个亲爱的idea工具的插件 2

package com.github.moocstudent.plugin1.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.pom.Navigatable;
import org.jetbrains.annotations.NotNull;

/**
 * @Author: zhangQi
 * @Date: 2021-12-29 12:25
 */
public class TextBoxes extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        //Using the event, create and show a dialog
        Project currentProject = e.getProject();
        StringBuffer dlgMsg = new StringBuffer(e.getPresentation().getText() + "Selected!");
        String dlgTitle = e.getPresentation().getDescription();
        //If an element is selected in the editor, add info about it.
        Navigatable nav = e.getData(CommonDataKeys.NAVIGATABLE);
        if(nav!=null){
            dlgMsg.append(String.format("\nSelected Element: %s",nav.toString()));
        }
        Messages.showMessageDialog(currentProject,dlgMsg.toString(),dlgTitle,Messages.getInformationIcon());

    }

    /**
     * In this example, the update() method relies on a Project object being available.
     * This requirement means the user must have at least one project open in the IDE for
     * the ....XXXAction to be available. So the update() method disables the action
     * for contexts where a Project object isn't defined.
     * @param e
     */
    @Override
    public void update(AnActionEvent e){
        //Set the availability based on whether a project is open
        Project project = e.getProject();
        e.getPresentation().setEnabledAndVisible(project!=null);
    }

    @Override
    public boolean isDumbAware() {
        return false;
    }
}


上一期的action代码

上一期既然能实现了给idea加一个tool按钮和点击后的动作(action)弹出
那么看了idea platform的SDK后其实感觉还能做点其它事情

参考:
https://www.cnblogs.com/ukzq/p/15741104.html
https://codingdict.com/sources/java/com.intellij.openapi.actionSystem/45688.html
https://plugins.jetbrains.com/docs/marketplace/create-your-marketplace-listing.html

原文地址:https://www.cnblogs.com/ukzq/p/15744595.html