!147 feature: #I85JRT 增加编译代码的功能
Merge pull request !147 from Luke/feature-I85JRT
This commit is contained in:
commit
8f639ce292
@ -28,7 +28,7 @@ module org.jcnc.jnotepad {
|
|||||||
exports org.jcnc.jnotepad.controller.config;
|
exports org.jcnc.jnotepad.controller.config;
|
||||||
exports org.jcnc.jnotepad.controller.manager;
|
exports org.jcnc.jnotepad.controller.manager;
|
||||||
exports org.jcnc.jnotepad.controller.i18n;
|
exports org.jcnc.jnotepad.controller.i18n;
|
||||||
exports org.jcnc.jnotepad.controller.event.handler.setting;
|
exports org.jcnc.jnotepad.controller.event.handler.toolbar;
|
||||||
exports org.jcnc.jnotepad.controller.event.handler.menuitem;
|
exports org.jcnc.jnotepad.controller.event.handler.menuitem;
|
||||||
exports org.jcnc.jnotepad.component.module.interfaces;
|
exports org.jcnc.jnotepad.component.module.interfaces;
|
||||||
opens org.jcnc.jnotepad.app.config;
|
opens org.jcnc.jnotepad.app.config;
|
||||||
|
|||||||
@ -22,7 +22,7 @@ import org.jcnc.jnotepad.common.constants.TextConstants;
|
|||||||
import org.jcnc.jnotepad.common.manager.ApplicationCacheManager;
|
import org.jcnc.jnotepad.common.manager.ApplicationCacheManager;
|
||||||
import org.jcnc.jnotepad.component.stage.dialog.factory.impl.BasicDirectoryChooserFactory;
|
import org.jcnc.jnotepad.component.stage.dialog.factory.impl.BasicDirectoryChooserFactory;
|
||||||
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
import org.jcnc.jnotepad.controller.config.AppConfigController;
|
||||||
import org.jcnc.jnotepad.controller.event.handler.menuitem.OpenDirectory;
|
import org.jcnc.jnotepad.controller.event.handler.toolbar.OpenDirectory;
|
||||||
import org.jcnc.jnotepad.model.entity.Cache;
|
import org.jcnc.jnotepad.model.entity.Cache;
|
||||||
import org.jcnc.jnotepad.model.enums.CacheExpirationTime;
|
import org.jcnc.jnotepad.model.enums.CacheExpirationTime;
|
||||||
import org.jcnc.jnotepad.plugin.PluginManagerInterface;
|
import org.jcnc.jnotepad.plugin.PluginManagerInterface;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package org.jcnc.jnotepad.controller.event.handler.setting;
|
package org.jcnc.jnotepad.controller.event.handler.toolbar;
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package org.jcnc.jnotepad.controller.event.handler.menuitem;
|
package org.jcnc.jnotepad.controller.event.handler.toolbar;
|
||||||
|
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package org.jcnc.jnotepad.controller.event.handler.toolbar;
|
||||||
|
|
||||||
|
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author luke
|
||||||
|
*/
|
||||||
|
public class RunBtn implements EventHandler<ActionEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(ActionEvent actionEvent) {
|
||||||
|
// TODO: 2023/10/6 点击按钮,打开下方侧边栏的运行输出栏
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package org.jcnc.jnotepad.controller.event.handler.setting;
|
package org.jcnc.jnotepad.controller.event.handler.toolbar;
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.event.EventHandler;
|
import javafx.event.EventHandler;
|
||||||
@ -4,7 +4,7 @@ import javafx.beans.value.ChangeListener;
|
|||||||
import javafx.scene.control.SplitPane;
|
import javafx.scene.control.SplitPane;
|
||||||
import javafx.scene.control.TreeItem;
|
import javafx.scene.control.TreeItem;
|
||||||
import org.jcnc.jnotepad.common.manager.ApplicationCacheManager;
|
import org.jcnc.jnotepad.common.manager.ApplicationCacheManager;
|
||||||
import org.jcnc.jnotepad.controller.event.handler.menuitem.OpenDirectory;
|
import org.jcnc.jnotepad.controller.event.handler.toolbar.OpenDirectory;
|
||||||
import org.jcnc.jnotepad.model.entity.DirFileModel;
|
import org.jcnc.jnotepad.model.entity.DirFileModel;
|
||||||
import org.jcnc.jnotepad.util.FileUtil;
|
import org.jcnc.jnotepad.util.FileUtil;
|
||||||
import org.jcnc.jnotepad.views.root.center.main.MainBorderPane;
|
import org.jcnc.jnotepad.views.root.center.main.MainBorderPane;
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package org.jcnc.jnotepad.views.manager;
|
|||||||
import org.jcnc.jnotepad.views.root.RootBorderPane;
|
import org.jcnc.jnotepad.views.root.RootBorderPane;
|
||||||
import org.jcnc.jnotepad.views.root.bottom.RootBottomSideBarVerticalBox;
|
import org.jcnc.jnotepad.views.root.bottom.RootBottomSideBarVerticalBox;
|
||||||
import org.jcnc.jnotepad.views.root.center.main.MainBorderPane;
|
import org.jcnc.jnotepad.views.root.center.main.MainBorderPane;
|
||||||
import org.jcnc.jnotepad.views.root.left.sidebar.tools.ToolHorizontalBox;
|
|
||||||
import org.jcnc.jnotepad.views.root.left.sidebar.tools.ToolVerticalBox;
|
import org.jcnc.jnotepad.views.root.left.sidebar.tools.ToolVerticalBox;
|
||||||
import org.jcnc.jnotepad.views.root.right.RootRightSideBarVerticalBox;
|
import org.jcnc.jnotepad.views.root.right.RootRightSideBarVerticalBox;
|
||||||
import org.jcnc.jnotepad.views.root.top.RootTopBorderPane;
|
import org.jcnc.jnotepad.views.root.top.RootTopBorderPane;
|
||||||
@ -37,7 +36,6 @@ public class RootBorderPaneManager {
|
|||||||
// 中间,用于显示主界面
|
// 中间,用于显示主界面
|
||||||
rootBorderPane.setCenterComponent(MainBorderPane.getInstance());
|
rootBorderPane.setCenterComponent(MainBorderPane.getInstance());
|
||||||
// 主界面的左边,工具栏
|
// 主界面的左边,工具栏
|
||||||
// rootBorderPane.setLeftComponent(ToolHorizontalBox.getInstance());
|
|
||||||
rootBorderPane.setLeftComponent(ToolVerticalBox.getInstance());
|
rootBorderPane.setLeftComponent(ToolVerticalBox.getInstance());
|
||||||
// 主界面的右边,侧边栏
|
// 主界面的右边,侧边栏
|
||||||
rootBorderPane.setRightComponent(RootRightSideBarVerticalBox.getInstance());
|
rootBorderPane.setRightComponent(RootRightSideBarVerticalBox.getInstance());
|
||||||
|
|||||||
@ -5,8 +5,9 @@ import javafx.scene.image.Image;
|
|||||||
import javafx.scene.image.ImageView;
|
import javafx.scene.image.ImageView;
|
||||||
import org.jcnc.jnotepad.api.core.views.manager.AbstractManager;
|
import org.jcnc.jnotepad.api.core.views.manager.AbstractManager;
|
||||||
import org.jcnc.jnotepad.api.core.views.manager.builder.SideBarButtonBuilder;
|
import org.jcnc.jnotepad.api.core.views.manager.builder.SideBarButtonBuilder;
|
||||||
import org.jcnc.jnotepad.controller.event.handler.setting.DirTreeBtn;
|
import org.jcnc.jnotepad.controller.event.handler.toolbar.DirTreeBtn;
|
||||||
import org.jcnc.jnotepad.controller.event.handler.setting.SetBtn;
|
import org.jcnc.jnotepad.controller.event.handler.toolbar.RunBtn;
|
||||||
|
import org.jcnc.jnotepad.controller.event.handler.toolbar.SetBtn;
|
||||||
import org.jcnc.jnotepad.views.root.left.sidebar.tools.SidebarToolBar;
|
import org.jcnc.jnotepad.views.root.left.sidebar.tools.SidebarToolBar;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -53,7 +54,13 @@ public class SidebarToolBarManager extends AbstractManager<Node> {
|
|||||||
.setImageViewEssentialAttribute(10D, 10D, true, 2.5D, 2.5D)
|
.setImageViewEssentialAttribute(10D, 10D, true, 2.5D, 2.5D)
|
||||||
.setButtonEssentialAttribute(20D, 20D)
|
.setButtonEssentialAttribute(20D, 20D)
|
||||||
.setEventHandler(new DirTreeBtn()).build());
|
.setEventHandler(new DirTreeBtn()).build());
|
||||||
|
registerNode(
|
||||||
|
new SideBarButtonBuilder()
|
||||||
|
.setButton(sidebarToolBar.getRunButton())
|
||||||
|
.setImageView(new ImageView(new Image("directory.png")))
|
||||||
|
.setImageViewEssentialAttribute(10D, 10D, true, 2.5D, 2.5D)
|
||||||
|
.setButtonEssentialAttribute(20D, 20D)
|
||||||
|
.setEventHandler(new RunBtn()).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -30,9 +30,9 @@ public class SidebarToolBar extends javafx.scene.control.ToolBar {
|
|||||||
Button dirTreeButton = new Button();
|
Button dirTreeButton = new Button();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工具栏上的文本侧边栏按钮
|
* 工具栏上的运行侧边栏按钮
|
||||||
*/
|
*/
|
||||||
Button fileButton = new Button();
|
Button runButton = new Button();
|
||||||
|
|
||||||
private SidebarToolBar() {
|
private SidebarToolBar() {
|
||||||
// 垂直排列
|
// 垂直排列
|
||||||
@ -69,4 +69,13 @@ public class SidebarToolBar extends javafx.scene.control.ToolBar {
|
|||||||
public Button getDirTreeButton() {
|
public Button getDirTreeButton() {
|
||||||
return dirTreeButton;
|
return dirTreeButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取工具栏上的运行按钮。
|
||||||
|
*
|
||||||
|
* @return 运行按钮
|
||||||
|
*/
|
||||||
|
public Button getRunButton() {
|
||||||
|
return runButton;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import javafx.scene.control.Menu;
|
|||||||
import javafx.scene.control.MenuItem;
|
import javafx.scene.control.MenuItem;
|
||||||
import org.jcnc.jnotepad.api.core.views.top.menu.AbstractTopMenu;
|
import org.jcnc.jnotepad.api.core.views.top.menu.AbstractTopMenu;
|
||||||
import org.jcnc.jnotepad.controller.event.handler.menuitem.*;
|
import org.jcnc.jnotepad.controller.event.handler.menuitem.*;
|
||||||
|
import org.jcnc.jnotepad.controller.event.handler.toolbar.OpenDirectory;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|||||||
@ -1,9 +1,16 @@
|
|||||||
package org.jcnc.jnotepad.views.root.top.menubar.menu;
|
package org.jcnc.jnotepad.views.root.top.menubar.menu;
|
||||||
|
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
import javafx.scene.control.Menu;
|
import javafx.scene.control.Menu;
|
||||||
import javafx.scene.control.MenuItem;
|
import javafx.scene.control.MenuItem;
|
||||||
|
import javafx.scene.control.TextArea;
|
||||||
import org.jcnc.jnotepad.api.core.views.top.menu.AbstractTopMenu;
|
import org.jcnc.jnotepad.api.core.views.top.menu.AbstractTopMenu;
|
||||||
|
import org.jcnc.jnotepad.util.LogUtil;
|
||||||
|
import org.jcnc.jnotepad.views.manager.CenterTabPaneManager;
|
||||||
|
import org.jcnc.jnotepad.views.root.center.main.center.tab.CenterTab;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -16,6 +23,7 @@ import static org.jcnc.jnotepad.common.constants.TextConstants.RUN;
|
|||||||
* @author gewuyou
|
* @author gewuyou
|
||||||
*/
|
*/
|
||||||
public class RunTopMenu extends AbstractTopMenu {
|
public class RunTopMenu extends AbstractTopMenu {
|
||||||
|
CenterTab centerTab = CenterTabPaneManager.getInstance().getSelected();
|
||||||
private static final RunTopMenu INSTANCE = new RunTopMenu();
|
private static final RunTopMenu INSTANCE = new RunTopMenu();
|
||||||
private final Map<String, MenuItem> runMenuItems = new HashMap<>();
|
private final Map<String, MenuItem> runMenuItems = new HashMap<>();
|
||||||
|
|
||||||
@ -53,14 +61,89 @@ public class RunTopMenu extends AbstractTopMenu {
|
|||||||
return runMenuItems;
|
return runMenuItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EventHandler<ActionEvent> codeRun = event -> {
|
||||||
|
// 创建一个TextArea用于输出编译后的结果
|
||||||
|
TextArea resultTextArea = new TextArea();
|
||||||
|
resultTextArea.setPrefRowCount(10);
|
||||||
|
resultTextArea.setPrefColumnCount(40);
|
||||||
|
resultTextArea.setEditable(false); // 禁止编辑
|
||||||
|
|
||||||
|
// 获取TextCodeArea的文本内容
|
||||||
|
|
||||||
|
CenterTab centerTab = CenterTabPaneManager.getInstance().getSelected();
|
||||||
|
String code = centerTab.getLineNumberTextArea().getText();
|
||||||
|
|
||||||
|
// TextCodeArea的当前文本内容
|
||||||
|
System.out.println("TextCodeArea的当前文本内容:" + code);
|
||||||
|
|
||||||
|
String fileName = "temp.c";
|
||||||
|
|
||||||
|
// 将C代码写入临时文件
|
||||||
|
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) {
|
||||||
|
writer.write(code);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
LogUtil.getLogger(this.getClass()).info("正在写入:{}", code);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 编译和运行C代码
|
||||||
|
compileAndRunCode(fileName, resultTextArea);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编译和运行C代码的方法
|
||||||
|
*/
|
||||||
|
private void compileAndRunCode(String fileName, TextArea resultTextArea) {
|
||||||
|
try {
|
||||||
|
// 创建ProcessBuilder并指定GCC编译命令
|
||||||
|
ProcessBuilder processBuilder = new ProcessBuilder("gcc", fileName, "-o", "temp");
|
||||||
|
|
||||||
|
// 设置工作目录
|
||||||
|
processBuilder.directory(null);
|
||||||
|
|
||||||
|
// 启动编译器进程
|
||||||
|
Process compileProcess = processBuilder.start();
|
||||||
|
|
||||||
|
// 读取编译器的输出信息
|
||||||
|
BufferedReader compileReader = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = compileReader.readLine()) != null) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待编译完成并获取返回值
|
||||||
|
int compileExitCode = compileProcess.waitFor();
|
||||||
|
if (compileExitCode == 0) {
|
||||||
|
System.out.println("编译成功!");
|
||||||
|
|
||||||
|
// 运行编译后的可执行文件
|
||||||
|
Process runProcess = new ProcessBuilder("./temp").start();
|
||||||
|
|
||||||
|
// 读取运行结果
|
||||||
|
BufferedReader runReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
while ((line = runReader.readLine()) != null) {
|
||||||
|
result.append(line).append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示运行结果
|
||||||
|
resultTextArea.setText(result.toString());
|
||||||
|
} else {
|
||||||
|
System.out.println("编译失败,返回代码:" + compileExitCode);
|
||||||
|
}
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册顶部菜单
|
* 注册顶部菜单
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void registerTopMenu() {
|
protected void registerTopMenu() {
|
||||||
|
|
||||||
// 运行
|
// 运行
|
||||||
registerMenuItem(topMenuBar.getRunItem(), RUN, "runItem", null);
|
registerMenuItem(topMenuBar.getRunItem(), RUN, "runItem", codeRun);
|
||||||
|
|
||||||
|
|
||||||
// 调试
|
// 调试
|
||||||
registerMenuItem(topMenuBar.getDeBugItem(), DE_BUG, "deBugItem", null);
|
registerMenuItem(topMenuBar.getDeBugItem(), DE_BUG, "deBugItem", null);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user