前言:
不要盲目的复制粘贴,我的版本比较高,很多语法不兼容!!!
代码:github上可查看,欢迎start
消息通知类,用于提示一些错误,或者警告
Notification notification = new Notification(StaticString.GROUP_ID, StaticString.CONTENT, NotificationType.INFORMATION);
Notifications.Bus.notify(notification);
效果图:
弹窗通知,用于提示错误或警告(会阻碍idea的运行)
Messages.showMessageDialog("内容", "标题", Messages.getInformationIcon());
效果图:
创建一个自己的toolWindow,类似idea中的通知小铃铛之类的
创建窗体之后就可根据自己的需求进行组件拖拽
//创建一个自己的windowsFactory,之后实现createToolWindowContent方法,将自己创建的窗体添加到idea中
public class TempPluginWindowsFactory implements ToolWindowFactory {
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
TempPluginUI tempPluginUI = new TempPluginUI(project, toolWindow);
ContentFactory contentFactory = ContentFactory.getInstance();
Content content = contentFactory.createContent(tempPluginUI.getPanel1(), "", false);
toolWindow.getContentManager().addContent(content);
}
}
//注意配置!!!,在这个extensions标签下添加toolWindows
<extensions defaultExtensionNs="com.intellij">
<toolWindow factoryClass="com.example.tempplugin.factory.TempPluginWindowsFactory" id="tempPluginWindowsFactory" anchor="right" icon="AllIcons.Actions.Colors" />
</extensions>
效果图:
添加一个表格在toolwindows里面
请在JScrollPane下面添加table,否则会不显示table的header的
添加完成之后,在创建好的UI类中添加数据
public class TempData {
/**
* 创建一个表头
*/
public static String[] HEADER = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
/**
* 创建数据便于演示
*/
public static Object[][] TABLE_DATA = {
{"Kathy", "Smith", "Snowboarding", 5, false},
{"John", "Doe", "Rowing", 3, true},
{"Sue", "Black", "Knitting", 2, false},
{"Jane", "White", "Speed reading", 20, true},
{"Joe", "Brown", "Pool", 10, false}
};
public static DefaultTableModel DEFAULT_TABLE_MODEL = new DefaultTableModel(TABLE_DATA, HEADER);
}
// 将数据放入表格中
table1.setModel(TempData.DEFAULT_TABLE_MODEL);
table1.setVisible(true);
效果图:
使用idea插件操作编辑器中的内容,实现内容的修改
操作编辑器的方法如下:
package com.example.tempplugin.util;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.richcopy.settings.RichCopySettings;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
/**
* 用于编辑选中的文本
*
* @author mr.ahai
*/
public class EditTextUtil {
private EditTextUtil() {
}
/**
* 将选中的内容放到剪切板中
*
* @return 返回选中的内容
*/
public static String getSysClipboardText() {
String ret = "";
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
//获取剪切板中的内容
Transferable contents = sysClip.getContents(null);
if (contents != null) {
//检查是否是文本类型
if (!contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return ret;
}
try {
ret = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
e.printStackTrace();
}
}
return ret;
}
/**
* 将剪切板的内容设为空
*
* @param writeText 之前剪切板存在的内容
*/
public static void setSysClipboardText(String writeText) {
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(writeText);
systemClipboard.setContents(stringSelection, null);
}
/**
* 替换选中的文字
*
* @param e AnActionEvent
*/
public static void replaceMsg(AnActionEvent e) {
RichCopySettings settings = RichCopySettings.getInstance();
settings.setEnabled(true);
ActionManager.getInstance().getAction("EditorCopy").actionPerformed(e);
settings.setEnabled(settings.isEnabled());
String sysClipboardText = getSysClipboardText();
//弹出一个弹框
Messages.showMessageDialog(sysClipboardText, "您确定要替换吗", Messages.getInformationIcon());
String tempText = "我是要替换的文字!!!!!";
Project project = e.getData(CommonDataKeys.PROJECT);
Editor editor = e.getData(CommonDataKeys.EDITOR);
if (null == editor) {
return;
}
e.getPresentation().setVisible(project != null && editor.getSelectionModel().hasSelection());
Document document = editor.getDocument();
SelectionModel selectionModel = editor.getSelectionModel();
int start = selectionModel.getSelectionStart();
int end = selectionModel.getSelectionEnd();
Runnable runnable = () -> document.replaceString(start, end, tempText);
WriteCommandAction.runWriteCommandAction(project, runnable);
selectionModel.removeSelection();
setSysClipboardText(sysClipboardText);
}
}