diff --git a/pom.xml b/pom.xml index fcad4319..137af251 100644 --- a/pom.xml +++ b/pom.xml @@ -32,9 +32,9 @@ 32.1.2-jre 4.11.0 3.5.1 - 3.2.1 - 3.2.1 - 3.2.1 + 3.2.5 + 3.2.5 + 3.2.5 2.242 3.3.4 2.0.4 @@ -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/component/RegexRTextScrollPane.java b/src/main/java/com/luoboduner/moo/tool/ui/component/RegexRTextScrollPane.java new file mode 100644 index 00000000..156003d4 --- /dev/null +++ b/src/main/java/com/luoboduner/moo/tool/ui/component/RegexRTextScrollPane.java @@ -0,0 +1,34 @@ +package com.luoboduner.moo.tool.ui.component; + +import com.formdev.flatlaf.FlatLaf; +import com.luoboduner.moo.tool.App; +import org.fife.ui.rtextarea.Gutter; +import org.fife.ui.rtextarea.RTextScrollPane; + +import javax.swing.*; +import java.awt.*; + +public class RegexRTextScrollPane extends RTextScrollPane { + // constructor + public RegexRTextScrollPane(RegexSyntaxTextViewer textArea) { + super(textArea); + + setMaximumSize(new Dimension(-1, -1)); + setMinimumSize(new Dimension(-1, -1)); + + Color defaultBackground = App.mainFrame.getBackground(); + + Gutter gutter = getGutter(); + if (FlatLaf.isLafDark()) { + gutter.setBorderColor(gutter.getLineNumberColor().darker()); + } else { + gutter.setBorderColor(gutter.getLineNumberColor().brighter()); + } + gutter.setBackground(defaultBackground); + Font font2 = new Font(App.config.getFont(), Font.PLAIN, App.config.getFontSize()); + gutter.setLineNumberFont(font2); + gutter.setBackground(UIManager.getColor("Editor.gutter.background")); + gutter.setBorderColor(UIManager.getColor("Editor.gutter.borderColor")); + gutter.setLineNumberColor(UIManager.getColor("Editor.gutter.lineNumberColor")); + } +} diff --git a/src/main/java/com/luoboduner/moo/tool/ui/component/RegexSyntaxTextViewer.java b/src/main/java/com/luoboduner/moo/tool/ui/component/RegexSyntaxTextViewer.java index f53b5aa3..0c76ea15 100644 --- a/src/main/java/com/luoboduner/moo/tool/ui/component/RegexSyntaxTextViewer.java +++ b/src/main/java/com/luoboduner/moo/tool/ui/component/RegexSyntaxTextViewer.java @@ -1,10 +1,68 @@ package com.luoboduner.moo.tool.ui.component; +import com.formdev.flatlaf.FlatLaf; +import com.luoboduner.moo.tool.App; +import com.luoboduner.moo.tool.ui.form.func.TimeConvertForm; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; +import org.fife.ui.rsyntaxtextarea.SyntaxConstants; +import org.fife.ui.rsyntaxtextarea.Theme; + +import javax.swing.*; +import java.awt.*; +import java.io.IOException; public class RegexSyntaxTextViewer extends RSyntaxTextArea { public RegexSyntaxTextViewer() { setDoubleBuffered(true); + + try { + Theme theme; + if (FlatLaf.isLafDark()) { + theme = Theme.load(RegexSyntaxTextViewer.class.getResourceAsStream( + "/org/fife/ui/rsyntaxtextarea/themes/monokai.xml")); + } else { + theme = Theme.load(RegexSyntaxTextViewer.class.getResourceAsStream( + "/org/fife/ui/rsyntaxtextarea/themes/idea.xml")); + } + theme.apply(this); + } catch (IOException ioe) { // Never happens + ioe.printStackTrace(); + } + + setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); + setCodeFoldingEnabled(true); +// setCurrentLineHighlightColor(new Color(52, 52, 52)); +// setUseSelectedTextColor(true); +// setSelectedTextColor(new Color(50, 50, 50)); + + // 初始化背景色 +// Style.blackTextArea(this); + setBackground(TimeConvertForm.getInstance().getTimeHisTextArea().getBackground()); + // 初始化边距 + setMargin(new Insets(10, 10, 10, 10)); + + // 初始化字体 + String fontName = App.config.getJsonBeautyFontName(); + int fontSize = App.config.getJsonBeautyFontSize(); + if (fontSize == 0) { + fontSize = getFont().getSize() + 2; + } + Font font = new Font(fontName, Font.PLAIN, fontSize); + setFont(font); + + setHyperlinksEnabled(false); + + + setBackground(UIManager.getColor("Editor.background")); + setCaretColor(UIManager.getColor("Editor.caretColor")); + setSelectionColor(UIManager.getColor("Editor.selectionBackground")); + setCurrentLineHighlightColor(UIManager.getColor("Editor.currentLineHighlight")); + setMarkAllHighlightColor(UIManager.getColor("Editor.markAllHighlightColor")); + setMarkOccurrencesColor(UIManager.getColor("Editor.markOccurrencesColor")); + setMatchedBracketBGColor(UIManager.getColor("Editor.matchedBracketBackground")); + setMatchedBracketBorderColor(UIManager.getColor("Editor.matchedBracketBorderColor")); + setPaintMatchedBracketPair(true); + setAnimateBracketMatching(false); } } diff --git a/src/main/java/com/luoboduner/moo/tool/ui/component/TopMenuBar.java b/src/main/java/com/luoboduner/moo/tool/ui/component/TopMenuBar.java index 0f9b4c07..20745175 100644 --- a/src/main/java/com/luoboduner/moo/tool/ui/component/TopMenuBar.java +++ b/src/main/java/com/luoboduner/moo/tool/ui/component/TopMenuBar.java @@ -434,7 +434,6 @@ private void changeTheme(String selectedThemeName) { HostForm.getInstance().updateTheme(); HostForm.getInstance().getScrollPane().updateUI(); - RegexForm.getInstance().updateTheme(); RegexForm.getInstance().getScrollPane().updateUI(); } 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..3df34a06 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..c155251c 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 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")); mainWindow.getQuickNotePanel().add(QuickNoteForm.getInstance().getQuickNotePanel(), gridConstraints); mainWindow.getJsonBeautyPanel().add(JsonBeautyForm.getInstance().getJsonBeautyPanel(), gridConstraints); @@ -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(); @@ -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); } /** 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..bc937ca1 --- /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..4e063c6a --- /dev/null +++ b/src/main/java/com/luoboduner/moo/tool/ui/form/func/JavaConsoleForm.java @@ -0,0 +1,131 @@ +package com.luoboduner.moo.tool.ui.form.func; + +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 lombok.Getter; + +import javax.swing.*; +import javax.swing.plaf.FontUIResource; +import javax.swing.text.StyleContext; +import java.awt.*; +import java.util.Locale; + +@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() { + init(); + } + + public static JavaConsoleForm getInstance() { + if (null == javaConsoleForm) { + javaConsoleForm = new JavaConsoleForm(); + } + return javaConsoleForm; + } + + private void init() { + new JavaConsoleListener(getRun(), getClean(), getCodeArea(), getResultArea()) + .addListener(); + } + + { +// GUI initializer generated by IntelliJ IDEA GUI Designer +// >>> IMPORTANT!! <<< +// DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * 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(true); + splitPane1.setDividerLocation(463); + 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(-13947600)); + 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(-13947600)); + 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/form/func/RegexForm.java b/src/main/java/com/luoboduner/moo/tool/ui/form/func/RegexForm.java index f1ff2f4d..9a4dc7a6 100644 --- a/src/main/java/com/luoboduner/moo/tool/ui/form/func/RegexForm.java +++ b/src/main/java/com/luoboduner/moo/tool/ui/form/func/RegexForm.java @@ -2,8 +2,6 @@ import cn.hutool.log.Log; import cn.hutool.log.LogFactory; -import com.formdev.flatlaf.FlatLaf; -import com.formdev.flatlaf.extras.FlatSVGIcon; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; import com.intellij.uiDesigner.core.Spacer; @@ -12,6 +10,7 @@ import com.luoboduner.moo.tool.domain.TFuncContent; import com.luoboduner.moo.tool.ui.FuncConsts; import com.luoboduner.moo.tool.ui.component.CustomizeIcon; +import com.luoboduner.moo.tool.ui.component.RegexRTextScrollPane; import com.luoboduner.moo.tool.ui.component.RegexSyntaxTextViewer; import com.luoboduner.moo.tool.ui.listener.func.RegexListener; import com.luoboduner.moo.tool.util.MybatisUtil; @@ -19,17 +18,12 @@ import com.luoboduner.moo.tool.util.SqliteUtil; import com.luoboduner.moo.tool.util.UndoUtil; import lombok.Getter; -import org.fife.ui.rsyntaxtextarea.SyntaxConstants; -import org.fife.ui.rsyntaxtextarea.Theme; -import org.fife.ui.rtextarea.Gutter; -import org.fife.ui.rtextarea.RTextScrollPane; import javax.swing.*; import javax.swing.border.TitledBorder; import javax.swing.plaf.FontUIResource; import javax.swing.text.StyleContext; import java.awt.*; -import java.io.IOException; import java.util.Locale; /** @@ -74,7 +68,7 @@ public class RegexForm { private JScrollPane commonRegexScrollPane; private RegexSyntaxTextViewer textArea; - private RTextScrollPane scrollPane; + private RegexRTextScrollPane scrollPane; private static RegexForm regexForm; @@ -84,82 +78,11 @@ public class RegexForm { private RegexForm() { textArea = new RegexSyntaxTextViewer(); - scrollPane = new RTextScrollPane(textArea); - - updateTheme(); + scrollPane = new RegexRTextScrollPane(textArea); UndoUtil.register(this); } - public void updateTheme() { - try { - Theme theme; - if (FlatLaf.isLafDark()) { - theme = Theme.load(RegexSyntaxTextViewer.class.getResourceAsStream( - "/org/fife/ui/rsyntaxtextarea/themes/monokai.xml")); - } else { - theme = Theme.load(RegexSyntaxTextViewer.class.getResourceAsStream( - "/org/fife/ui/rsyntaxtextarea/themes/idea.xml")); - } - theme.apply(textArea); - } catch (IOException ioe) { // Never happens - ioe.printStackTrace(); - } - - textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); - textArea.setCodeFoldingEnabled(true); -// setCurrentLineHighlightColor(new Color(52, 52, 52)); -// setUseSelectedTextColor(true); -// setSelectedTextColor(new Color(50, 50, 50)); - - // 初始化背景色 -// Style.blackTextArea(this); - textArea.setBackground(TimeConvertForm.getInstance().getTimeHisTextArea().getBackground()); - // 初始化边距 - textArea.setMargin(new Insets(10, 10, 10, 10)); - - // 初始化字体 - String fontName = App.config.getJsonBeautyFontName(); - int fontSize = App.config.getJsonBeautyFontSize(); - if (fontSize == 0) { - fontSize = textArea.getFont().getSize() + 2; - } - Font font = new Font(fontName, Font.PLAIN, fontSize); - textArea.setFont(font); - - textArea.setHyperlinksEnabled(false); - - - textArea.setBackground(UIManager.getColor("Editor.background")); - textArea.setCaretColor(UIManager.getColor("Editor.caretColor")); - textArea.setSelectionColor(UIManager.getColor("Editor.selectionBackground")); - textArea.setCurrentLineHighlightColor(UIManager.getColor("Editor.currentLineHighlight")); - textArea.setMarkAllHighlightColor(UIManager.getColor("Editor.markAllHighlightColor")); - textArea.setMarkOccurrencesColor(UIManager.getColor("Editor.markOccurrencesColor")); - textArea.setMatchedBracketBGColor(UIManager.getColor("Editor.matchedBracketBackground")); - textArea.setMatchedBracketBorderColor(UIManager.getColor("Editor.matchedBracketBorderColor")); - textArea.setPaintMatchedBracketPair(true); - textArea.setAnimateBracketMatching(false); - - scrollPane.setMaximumSize(new Dimension(-1, -1)); - scrollPane.setMinimumSize(new Dimension(-1, -1)); - - Color defaultBackground = App.mainFrame.getBackground(); - - Gutter gutter = scrollPane.getGutter(); - if (FlatLaf.isLafDark()) { - gutter.setBorderColor(gutter.getLineNumberColor().darker()); - } else { - gutter.setBorderColor(gutter.getLineNumberColor().brighter()); - } - gutter.setBackground(defaultBackground); - Font font2 = new Font(App.config.getFont(), Font.PLAIN, App.config.getFontSize()); - gutter.setLineNumberFont(font2); - gutter.setBackground(UIManager.getColor("Editor.gutter.background")); - gutter.setBorderColor(UIManager.getColor("Editor.gutter.borderColor")); - gutter.setLineNumberColor(UIManager.getColor("Editor.gutter.lineNumberColor")); - } - public static RegexForm getInstance() { if (regexForm == null) { regexForm = new RegexForm(); 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/java/com/luoboduner/moo/tool/ui/listener/func/QuickNoteListener.java b/src/main/java/com/luoboduner/moo/tool/ui/listener/func/QuickNoteListener.java index 14b7a5fc..80cd7cf7 100644 --- a/src/main/java/com/luoboduner/moo/tool/ui/listener/func/QuickNoteListener.java +++ b/src/main/java/com/luoboduner/moo/tool/ui/listener/func/QuickNoteListener.java @@ -36,6 +36,8 @@ import java.text.DecimalFormat; import java.util.Date; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** *
@@ -52,6 +54,9 @@ public class QuickNoteListener {
 
     public static String selectedName;
 
+    // 创建一个单线程的ExecutorService
+    public static ExecutorService executorService = Executors.newSingleThreadExecutor();
+
     public static void addListeners() {
         QuickNoteForm quickNoteForm = QuickNoteForm.getInstance();
         QuickNoteSyntaxTextViewerManager quickNoteSyntaxTextViewerManager = QuickNoteForm.quickNoteSyntaxTextViewerManager;
@@ -613,24 +618,28 @@ private static void deleteFiles(QuickNoteForm quickNoteForm) {
      * @param refreshModifiedTime
      */
     public static void quickSave(boolean refreshModifiedTime, boolean writeLog) {
-        String now = SqliteUtil.nowDateForSqlite();
-        if (selectedName != null) {
-            TQuickNote tQuickNote = new TQuickNote();
-            tQuickNote.setName(selectedName);
 
-            String text = QuickNoteForm.quickNoteSyntaxTextViewerManager.getTextByName(selectedName);
-            if (writeLog) {
-                log.info("save note: " + selectedName + ", content: " + text);
-            }
-            tQuickNote.setContent(text);
-            if (refreshModifiedTime) {
-                tQuickNote.setModifiedTime(now);
+        executorService.submit(() -> {
+            String now = SqliteUtil.nowDateForSqlite();
+            if (selectedName != null) {
+                TQuickNote tQuickNote = new TQuickNote();
+                tQuickNote.setName(selectedName);
+
+                String text = QuickNoteForm.quickNoteSyntaxTextViewerManager.getTextByName(selectedName);
+                if (writeLog) {
+                    log.info("save note: " + selectedName + ", content: " + text);
+                }
+                tQuickNote.setContent(text);
+                if (refreshModifiedTime) {
+                    tQuickNote.setModifiedTime(now);
+                }
+
+                quickNoteMapper.updateByName(tQuickNote);
             }
 
-            quickNoteMapper.updateByName(tQuickNote);
-        }
+            QuickNoteIndicatorTools.showTips("已保存:" + selectedName, QuickNoteIndicatorTools.TipsLevel.SUCCESS);
+        });
 
-        QuickNoteIndicatorTools.showTips("已保存:" + selectedName, QuickNoteIndicatorTools.TipsLevel.SUCCESS);
     }
 
     /**
diff --git a/src/main/resources/icon/java.svg b/src/main/resources/icon/java.svg
new file mode 100755
index 00000000..7e77a1e1
--- /dev/null
+++ b/src/main/resources/icon/java.svg
@@ -0,0 +1 @@
+
\ No newline at end of file