From dcbbcf166efc98940a4df6ce1728de0998e246f0 Mon Sep 17 00:00:00 2001 From: felixnan168 Date: Thu, 26 Oct 2023 13:36:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EJava=E3=80=81Groovy=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=89=A7=E8=A1=8C=E5=99=A8=EF=BC=8C=E5=9C=A8MooTool?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E4=B8=AD=E5=8F=AF=E7=AE=80=E5=8D=95=E6=89=A7?= =?UTF-8?q?=E8=A1=8CJava=E6=88=96Groovy=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 + .../moo/tool/ui/form/MainWindow.form | 9 ++ .../moo/tool/ui/form/MainWindow.java | 7 +- .../tool/ui/form/func/JavaConsoleForm.form | 102 +++++++++++++ .../tool/ui/form/func/JavaConsoleForm.java | 136 ++++++++++++++++++ .../ui/listener/func/JavaConsoleListener.java | 129 +++++++++++++++++ src/main/resources/icon/java.svg | 20 +++ 7 files changed, 409 insertions(+), 1 deletion(-) create mode 100755 src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.form create mode 100755 src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.java create mode 100755 src/main/java/com/luoboduner/moo/tool/ui/listener/func/JavaConsoleListener.java create mode 100755 src/main/resources/icon/java.svg diff --git a/pom.xml b/pom.xml index b0b39fa6..137af251 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ 6.4.6 1.2.0 1.5.0 + 4.0.15 @@ -260,6 +261,12 @@ versioncompare ${versioncompare.version} + + + org.apache.groovy + groovy + ${groovy.version} + diff --git a/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.form b/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.form index 8b29fedb..85868c8c 100644 --- a/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.form +++ b/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.form @@ -154,6 +154,15 @@ + + + + + + + + + diff --git a/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.java b/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.java index eb653658..25c78ec4 100644 --- a/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.java +++ b/src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.java @@ -39,6 +39,7 @@ public class MainWindow { private JPanel cronPanel; private JPanel regexPanel; private JPanel imagePanel; + private JPanel javaConsolePanel; private static MainWindow mainWindow; @@ -76,7 +77,7 @@ public class MainWindow { mainWindow.getTabbedPane().setIconAt(12, new FlatSVGIcon("icon/translate.svg")); mainWindow.getTabbedPane().setIconAt(13, new FlatSVGIcon("icon/schedule.svg", 15, 15)); mainWindow.getTabbedPane().setIconAt(14, new FlatSVGIcon("icon/reg.svg")); - + mainWindow.getTabbedPane().setIconAt(15, new FlatSVGIcon("icon/java.svg", 15, 15)); mainWindow.getQuickNotePanel().add(QuickNoteForm.getInstance().getQuickNotePanel(), gridConstraints); mainWindow.getJsonBeautyPanel().add(JsonBeautyForm.getInstance().getJsonBeautyPanel(), gridConstraints); @@ -93,6 +94,7 @@ public class MainWindow { mainWindow.getCronPanel().add(CronForm.getInstance().getCronPanel(), gridConstraints); mainWindow.getRegexPanel().add(RegexForm.getInstance().getRegexPanel(), gridConstraints); mainWindow.getImagePanel().add(ImageForm.getInstance().getImagePanel(), gridConstraints); + mainWindow.getJavaConsolePanel().add(JavaConsoleForm.getInstance().getJavaConsolePanel(), gridConstraints); mainWindow.getMainPanel().updateUI(); TabListener.addListeners(); @@ -163,6 +165,9 @@ public class MainWindow { regexPanel = new JPanel(); regexPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); tabbedPane.addTab("正则", new ImageIcon(getClass().getResource("/icon/reg.png")), regexPanel); + javaConsolePanel = new JPanel(); + javaConsolePanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); + tabbedPane.addTab("java", javaConsolePanel); } /** diff --git a/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.form b/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.form new file mode 100755 index 00000000..c406b519 --- /dev/null +++ b/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.form @@ -0,0 +1,102 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.java b/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.java new file mode 100755 index 00000000..f6d1f145 --- /dev/null +++ b/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.java @@ -0,0 +1,136 @@ +package com.luoboduner.moo.tool.ui.form.func; + +import com.alibaba.fastjson2.util.IOUtils; +import com.intellij.uiDesigner.core.GridConstraints; +import com.intellij.uiDesigner.core.GridLayoutManager; +import com.intellij.uiDesigner.core.Spacer; +import com.luoboduner.moo.tool.App; +import com.luoboduner.moo.tool.ui.listener.func.JavaConsoleListener; +import groovy.lang.GroovyShell; +import groovy.lang.Script; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; + +import javax.swing.*; +import javax.swing.plaf.FontUIResource; +import javax.swing.text.StyleContext; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.io.*; +import java.util.Date; +import java.util.Locale; +import java.util.Objects; +import java.util.function.Supplier; + +@Getter +public class JavaConsoleForm { + private JPanel javaConsolePanel; + private JButton run; + private JButton clean; + private JTextArea codeArea; + private JTextArea resultArea; + private static JavaConsoleForm javaConsoleForm; + + public JavaConsoleForm() { + $$$setupUI$$$(); + init(); + } + + public static JavaConsoleForm getInstance() { + if (null == javaConsoleForm) { + javaConsoleForm = new JavaConsoleForm(); + } + return javaConsoleForm; + } + + private void init() { + new JavaConsoleListener(getRun(), getClean(), getCodeArea(), getResultArea()) + .addListener(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + javaConsolePanel = new JPanel(); + javaConsolePanel.setLayout(new GridLayoutManager(2, 1, new Insets(12, 12, 12, 12), -1, -1)); + final JPanel panel1 = new JPanel(); + panel1.setLayout(new GridLayoutManager(1, 4, new Insets(10, 0, 0, 0), -1, -1)); + javaConsolePanel.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, 1, 1, null, null, null, 0, false)); + clean = new JButton(); + clean.setText("清除"); + panel1.add(clean, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + run = new JButton(); + run.setText("执行"); + panel1.add(run, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + final JLabel label1 = new JLabel(); + Font label1Font = this.$$$getFont$$$(null, -1, 14, label1.getFont()); + if (label1Font != null) label1.setFont(label1Font); + label1.setText("输入Java或groovy代码点击执行即可"); + panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + final Spacer spacer1 = new Spacer(); + panel1.add(spacer1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); + final JSplitPane splitPane1 = new JSplitPane(); + splitPane1.setContinuousLayout(false); + splitPane1.setDividerLocation(500); + splitPane1.setDividerSize(3); + javaConsolePanel.add(splitPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(200, 200), null, 0, false)); + final JScrollPane scrollPane1 = new JScrollPane(); + splitPane1.setLeftComponent(scrollPane1); + codeArea = new JTextArea(); + codeArea.setBackground(new Color(-3608620)); + Font codeAreaFont = this.$$$getFont$$$("Consolas", -1, 16, codeArea.getFont()); + if (codeAreaFont != null) codeArea.setFont(codeAreaFont); + codeArea.setText(""); + scrollPane1.setViewportView(codeArea); + final JScrollPane scrollPane2 = new JScrollPane(); + splitPane1.setRightComponent(scrollPane2); + resultArea = new JTextArea(); + resultArea.setBackground(new Color(-820)); + resultArea.setEditable(false); + Font resultAreaFont = this.$$$getFont$$$("Consolas", -1, 16, resultArea.getFont()); + if (resultAreaFont != null) resultArea.setFont(resultAreaFont); + scrollPane2.setViewportView(resultArea); + } + + /** + * @noinspection ALL + */ + private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) { + if (currentFont == null) return null; + String resultName; + if (fontName == null) { + resultName = currentFont.getName(); + } else { + Font testFont = new Font(fontName, Font.PLAIN, 10); + if (testFont.canDisplay('a') && testFont.canDisplay('1')) { + resultName = fontName; + } else { + resultName = currentFont.getName(); + } + } + Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize()); + boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac"); + Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize()); + return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return javaConsolePanel; + } + + private void createUIComponents() { + // TODO: place custom component creation code here + Component component = getJavaConsolePanel().getComponent(1); + ((JSplitPane) component).setDividerLocation((int) (App.mainFrame.getWidth() / 5)); + } +} diff --git a/src/main/java/com/luoboduner/moo/tool/ui/listener/func/JavaConsoleListener.java b/src/main/java/com/luoboduner/moo/tool/ui/listener/func/JavaConsoleListener.java new file mode 100755 index 00000000..04dee180 --- /dev/null +++ b/src/main/java/com/luoboduner/moo/tool/ui/listener/func/JavaConsoleListener.java @@ -0,0 +1,129 @@ +package com.luoboduner.moo.tool.ui.listener.func; + +import com.luoboduner.moo.tool.ui.form.func.TimeConvertForm; +import groovy.lang.GroovyShell; +import groovy.lang.Script; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; + +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.io.*; +import java.util.Date; +import java.util.Objects; +import java.util.function.Supplier; + +@Getter +public class JavaConsoleListener { + + private static String TMP_FILE = System.getProperty("java.io.tmpdir") + File.separator + "temp.groovy"; + private StringBuffer log; + + private JButton run; + private JButton clean; + + private JTextArea codeArea; + private JTextArea resultArea; + + public JavaConsoleListener(JButton run, JButton clean, JTextArea codeArea, JTextArea resultArea) { + this.run = run; + this.clean = clean; + this.codeArea = codeArea; + this.resultArea = resultArea; + this.log = new StringBuffer(); + } + + public void addListener() { + this.executeClickEventListener(); + this.cleanClickEventListener(); + } + + private void cleanClickEventListener() { + this.clean.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + getResultArea().setText(""); + log = new StringBuffer(); + } + }); + } + + private void executeClickEventListener() { + run.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + String code = getCodeArea().getText(); + if (StringUtils.isBlank(code)) { + return; + } + + try { + addStr("----- 当前执行时间:" + DateFormatUtils.format(new Date(), TimeConvertForm.TIME_FORMAT) + " -----"); + Object res = compileAndExecute(code); + if (null != res) { + addStr(res.toString()); + } + } catch (Exception ex) { + logException(ex); + } + getResultArea().setText(log.toString()); + } + }); + } + + private Object compileAndExecute(String code) throws Exception { + GroovyShell shell = new GroovyShell(); + File file = new File(TMP_FILE); + if (!file.exists()) { + file.createNewFile(); + } + writeFile(file, code); + Script parse = shell.parse(new FileReader(file)); + return changeOut(() -> parse.run()); + } + + private void addStr(String str) { + if (StringUtils.isNotBlank(str)) { + log.append(str + "\r\n"); + } + } + + private void writeFile(File file, String data) throws Exception { + Objects.requireNonNull(file, "临时文件不能为空!"); + Objects.requireNonNull(data, "代码不能为空!"); + + try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { + fileOutputStream.write(data.getBytes()); + } catch (Exception e) { + throw e; + } + } + + private T changeOut(Supplier supplier) { + PrintStream oldStream = System.out; + try (ByteArrayOutputStream tmpStream = new ByteArrayOutputStream(1024); + PrintStream cacheStream = new PrintStream(tmpStream)) { + System.setOut(cacheStream); + T value = supplier.get(); + String strMsg = tmpStream.toString(); + addStr(strMsg); + return value; + } catch (Exception e) { + logException(e); + } finally { + System.setOut(oldStream); + } + return null; + } + + private void logException(Throwable throwable) { + try (StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter)) { + throwable.printStackTrace(printWriter); + addStr(stringWriter.toString()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/resources/icon/java.svg b/src/main/resources/icon/java.svg new file mode 100755 index 00000000..fa19e3a4 --- /dev/null +++ b/src/main/resources/icon/java.svg @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + +