Skip to content

Commit

Permalink
!47 新增Java、Groovy代码编译执行器,在MooTool工具中可简单执行Java或Groovy代码
Browse files Browse the repository at this point in the history
Merge pull request !47 from felixnan168/develop
  • Loading branch information
rememberber authored and gitee-org committed Oct 26, 2023
2 parents c8fc999 + dcbbcf1 commit 09164a3
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<oshi-core.version>6.4.6</oshi-core.version>
<jfa.version>1.2.0</jfa.version>
<versioncompare.version>1.5.0</versioncompare.version>
<groovy.version>4.0.15</groovy.version>
</properties>

<repositories>
Expand Down Expand Up @@ -260,6 +261,12 @@
<artifactId>versioncompare</artifactId>
<version>${versioncompare.version}</version>
</dependency>

<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>

</dependencies>

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/luoboduner/moo/tool/ui/form/MainWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@
<border type="none"/>
<children/>
</grid>
<grid id="679bb" binding="javaConsolePanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="java"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</tabbedpane>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class MainWindow {
private JPanel cronPanel;
private JPanel regexPanel;
private JPanel imagePanel;
private JPanel javaConsolePanel;

private static MainWindow mainWindow;

Expand Down Expand Up @@ -76,7 +77,7 @@ public void init() {
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);
Expand All @@ -93,6 +94,7 @@ public void init() {
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();
Expand Down Expand Up @@ -163,6 +165,9 @@ public void init() {
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.luoboduner.moo.tool.ui.form.func.JavaConsoleForm">
<grid id="27dc6" binding="javaConsolePanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="12" left="12" bottom="12" right="12"/>
<constraints>
<xy x="26" y="46" width="494" height="567"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="5956" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="1" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="804be" class="javax.swing.JButton" binding="clean">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="清除"/>
</properties>
</component>
<component id="54894" class="javax.swing.JButton" binding="run">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="执行"/>
</properties>
</component>
<component id="8d9f4" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="14"/>
<text value="输入Java或groovy代码点击执行即可"/>
</properties>
</component>
<hspacer id="73c3d">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
<splitpane id="76c59">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
</grid>
</constraints>
<properties>
<continuousLayout value="false"/>
<dividerLocation value="500"/>
<dividerSize value="3"/>
</properties>
<border type="none"/>
<children>
<scrollpane id="7a589">
<constraints>
<splitpane position="left"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="61044" class="javax.swing.JTextArea" binding="codeArea">
<constraints/>
<properties>
<background color="-3608620"/>
<font name="Consolas" size="16"/>
<text value=""/>
</properties>
</component>
</children>
</scrollpane>
<scrollpane id="8cb4d">
<constraints>
<splitpane position="right"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="286fe" class="javax.swing.JTextArea" binding="resultArea">
<constraints/>
<properties>
<background color="-820"/>
<editable value="false"/>
<font name="Consolas" size="16"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</splitpane>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading

0 comments on commit 09164a3

Please sign in to comment.