|
| 1 | +package com.plugins.mybaitslog.console; |
| 2 | + |
| 3 | +import com.intellij.execution.filters.Filter; |
| 4 | +import com.intellij.execution.filters.TextConsoleBuilder; |
| 5 | +import com.intellij.execution.filters.TextConsoleBuilderFactory; |
| 6 | +import com.intellij.execution.ui.ConsoleView; |
| 7 | +import com.intellij.openapi.actionSystem.ActionManager; |
| 8 | +import com.intellij.openapi.actionSystem.ActionToolbar; |
| 9 | +import com.intellij.openapi.actionSystem.AnAction; |
| 10 | +import com.intellij.openapi.actionSystem.DefaultActionGroup; |
| 11 | +import com.intellij.openapi.project.Project; |
| 12 | +import com.intellij.openapi.ui.SimpleToolWindowPanel; |
| 13 | +import com.plugins.mybaitslog.action.gui.FilterSetting; |
| 14 | +import com.plugins.mybaitslog.util.PrintUtil; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import javax.swing.*; |
| 18 | +import java.awt.*; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * console 描述 |
| 24 | + */ |
| 25 | +public class ConsolePanel { |
| 26 | + |
| 27 | + private final List<Filter> myFilterList = new ArrayList<>(); |
| 28 | + |
| 29 | + /** |
| 30 | + * 创建 输出面板 |
| 31 | + * |
| 32 | + * @param project |
| 33 | + * @return |
| 34 | + */ |
| 35 | + private ConsoleView createConsole(@NotNull Project project) { |
| 36 | + TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project); |
| 37 | + consoleBuilder.filters(myFilterList); |
| 38 | + ConsoleView console = consoleBuilder.getConsole(); |
| 39 | + PrintUtil.setConsoleView(console); |
| 40 | + return console; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + private static JComponent createConsolePanel(ConsoleView view) { |
| 45 | + JPanel panel = new JPanel(); |
| 46 | + panel.setLayout(new BorderLayout()); |
| 47 | + panel.add(view.getComponent(), BorderLayout.CENTER); |
| 48 | + return panel; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * 构建面板 |
| 54 | + */ |
| 55 | + public JComponent ConsolePanel(final Project myProject) { |
| 56 | + final ConsoleView consoleView = createConsole(myProject); |
| 57 | + //左边显示的控件 |
| 58 | + final JComponent consolePanel = createConsolePanel(consoleView); |
| 59 | + final ActionToolbar actionToolbar = createActionToolbar(myProject, consolePanel, consoleView); |
| 60 | + actionToolbar.setTargetComponent(consolePanel); |
| 61 | + |
| 62 | + SimpleToolWindowPanel panel = new SimpleToolWindowPanel(false, true); |
| 63 | + panel.setContent(consolePanel); |
| 64 | + panel.setToolbar(actionToolbar.getComponent()); |
| 65 | + |
| 66 | + DefaultActionGroup actions = new DefaultActionGroup(); |
| 67 | + for (AnAction action : consoleView.createConsoleActions()) { |
| 68 | + actions.add(action); |
| 69 | + } |
| 70 | + final JComponent component = panel.getComponent(); |
| 71 | + return component; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 创建工具栏 |
| 76 | + * |
| 77 | + * @param consolePanel |
| 78 | + * @param consoleView |
| 79 | + * @return |
| 80 | + */ |
| 81 | + @NotNull |
| 82 | + private ActionToolbar createActionToolbar(final Project project, JComponent consolePanel, ConsoleView consoleView) { |
| 83 | + final DefaultActionGroup actionGroup = new DefaultActionGroup(); |
| 84 | + ConsoleActionGroup.withFilter(() -> { |
| 85 | + //启动filter配置 |
| 86 | + FilterSetting dialog = new FilterSetting(project); |
| 87 | + dialog.pack(); |
| 88 | + dialog.setSize(600, 400);//配置大小 |
| 89 | + dialog.setLocationRelativeTo(null);//位置居中显示 |
| 90 | + dialog.setVisible(true); |
| 91 | + }); |
| 92 | + actionGroup.add(new ConsoleActionGroup.FilterAction()); |
| 93 | + actionGroup.add(consoleView.createConsoleActions()[2]); |
| 94 | + actionGroup.add(consoleView.createConsoleActions()[3]); |
| 95 | + actionGroup.add(consoleView.createConsoleActions()[5]); |
| 96 | + return ActionManager.getInstance().createActionToolbar("EventLog", actionGroup, false); |
| 97 | + } |
| 98 | +} |
0 commit comments