android插件编写一般用IntelliJ IDEA 基本的插件编写流程 不做赘述,

本文主要记录编写插件的UI交互弹框,以及在指定目录下生成具体的类,并且将指定的代码写入到类文件里

1、首选创建对应的Action类如 SecondActionClassName这个类 并将其注册到plugin.xml中,设置其触发的为位置以及快捷键

 SecondActionClassName具体的代码如下:

package com.testdemo.example;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;

import java.io.IOException;

public class SecondActionClassName extends AnAction {

    String templateString = 
            "class PdfViewerAdapter(context: Context, pdfPath: String?) : BasePDFPagerAdapter(context, pdfPath) {\n" +
            "\n" +
            "    override fun instantiateItem(container: ViewGroup, position: Int): Any {\n" +
            "        val photoView = PhotoView(context)\n" +
            "        if (renderer == null || count < position) {\n" +
            "            return photoView\n" +
            "        }\n" +
            "        val page = getPDFPage(renderer, position)\n" +
            "\n" +
            "        var bitmap = bitmapContainer[position]\n" +
            "        bitmap?.let {\n" +
            "            bitmap = drawBitmapBg(getColor(R.color.C_FFFFFF), bitmap)\n" +
            "        }\n" +
            "        page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)\n" +
            "        page.close()\n" +
            "\n" +
            "        photoView.setImageBitmap(bitmap)\n" +
            "        container.addView(\n" +
            "            photoView,\n" +
            "            0,\n" +
            "            ViewGroup.LayoutParams(\n" +
            "                ViewGroup.LayoutParams.MATCH_PARENT,\n" +
            "                ViewGroup.LayoutParams.MATCH_PARENT\n" +
            "            )\n" +
            "        )\n" +
            "        return photoView\n" +
            "    }\n" +
            "\n" +
            "    fun drawBitmapBg(color: Int, originBitmap: Bitmap): Bitmap? {\n" +
            "        val paint = Paint()\n" +
            "        paint.color = color\n" +
            "        val bitmap =\n" +
            "            Bitmap.createBitmap(originBitmap.width, originBitmap.height, originBitmap.config)\n" +
            "        val canvas = Canvas(bitmap)\n" +
            "        canvas.drawRect(0f, 0f, originBitmap.width.toFloat(), originBitmap.height.toFloat(), paint)\n" +
            "        canvas.drawBitmap(originBitmap, 0f, 0f, paint)\n" +
            "        return bitmap\n" +
            "    }\n" +
            "}";
    @Override
    public void actionPerformed(AnActionEvent e) {

        //调起测试弹框 并设置了测试弹框点击OK按钮时的回调,其中可以回调用户在测试弹框里的输入内容
        TestShowDialog dialog = new TestShowDialog(str -> {
            //当我们想要向文件写入代码的时候需要调用此异步方法
            WriteCommandAction.runWriteCommandAction(e.getProject(), () -> {
                //获取当前鼠标所选的文件目录
                PsiDirectory chooseDirectory = e.getRequiredData(LangDataKeys.IDE_VIEW).getOrChooseDirectory();
                if (chooseDirectory != null) {
                    //在此目录下创建具体的文件
                    PsiFile file = chooseDirectory.createFile("TestClass.kt");
                    VirtualFile virtualFile = file.getVirtualFile();
                    try {
                        //向文件写入具体的代码
                        virtualFile.setBinaryContent(templateString.getBytes());
                    } catch (IOException ex) {
                        //这个是个message弹框
                        Messages.showMessageDialog("errrrrrr", "tillle22222: " + str, Messages.getInformationIcon());
                        throw new RuntimeException(ex);
                    }
                }

                PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
                PsiElement psiElement = e.getData(LangDataKeys.PSI_ELEMENT);
                //此方法可以获取当前文件的Editor对象 有了
                Editor editor = e.getData(CommonDataKeys.EDITOR);
                if (editor != null) {
                    Document document = editor.getDocument();
                    //向文件的指定位置插入具体的代码
                    document.insertString(0, "12345qwert\nmnbv\n" + str);
                    //这个是个message弹框
                    Messages.showMessageDialog("titlllle", "tillle22222: " + str, Messages.getInformationIcon());
                }
            });
        });
        dialog.setSize(500, 500);
        dialog.setVisible(true);
    }

}

actionPerformed方法在用户触发插件的入口时会被调用 ,通过以上代码就完成了在鼠标选中指定目录 然后触发插件弹出弹框,

并获取用户输入的内容,去创建对应文件并写入代码到具体文件 

弹框的代码如下:

 

package com.testdemo.example;

import javax.swing.*;
import java.awt.event.*;

public class TestShowDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;

    private JTextField tfUserName;

    private JLabel lbUserName;

    interface CustomClickListener {
        void onConfirmClick(String str);
    }

    private CustomClickListener mCustomClickListener;

    public TestShowDialog(CustomClickListener customClickListener) {
        mCustomClickListener = customClickListener;
        createUIComponents();
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        String inputStr = tfUserName.getText();
        if (mCustomClickListener != null) {
            mCustomClickListener.onConfirmClick(inputStr);
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    private void createUIComponents() {
        contentPane = new JPanel();
        lbUserName = new JLabel();
        lbUserName.setText("input userName: ");
        tfUserName = new JTextField();
        tfUserName.setText("user_name");
        lbUserName.setBounds(30, 30, 100, 50);
        tfUserName.setBounds(200, 30, 200, 50);
        contentPane.add(lbUserName);
        contentPane.add(tfUserName);

        buttonOK = new JButton("ok");
        buttonCancel = new JButton("cancel");
        buttonOK.setBounds(100, 460, 100, 50);
        buttonCancel.setBounds(350, 460, 100, 50);
        contentPane.add(buttonOK);
        contentPane.add(buttonCancel);
        contentPane.setBounds(100, 100, 300, 300);
    }
}

 

posted on 2023-08-23 13:54  毕哥  阅读(24)  评论(0编辑  收藏  举报