2023年6月6日18:09:32

master
zhangmeng 2023-06-06 18:09:39 +08:00
parent d3c2ef1400
commit 3cc20a868a
2 changed files with 177 additions and 39 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.Resource; import cn.hutool.core.io.resource.Resource;
import com.zhangmeng.tools.components.RecursiveFileList; import com.zhangmeng.tools.components.RecursiveFileList;
import com.zhangmeng.tools.languages.ProcessGO; import com.zhangmeng.tools.languages.ProcessGO;
import com.zhangmeng.tools.languages.ProcessHtml;
import com.zhangmeng.tools.languages.ProcessJava; import com.zhangmeng.tools.languages.ProcessJava;
import com.zhangmeng.tools.utils.AlertUtils; import com.zhangmeng.tools.utils.AlertUtils;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
@ -13,6 +14,7 @@ import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node; import javafx.scene.Node;
@ -78,7 +80,7 @@ public class FileEditController {
private final SimpleIntegerProperty text_size = new SimpleIntegerProperty(15); private final SimpleIntegerProperty text_size = new SimpleIntegerProperty(15);
private final SimpleIntegerProperty icon_size = new SimpleIntegerProperty(15); private final SimpleIntegerProperty icon_size = new SimpleIntegerProperty(15);
private static final Map<Integer,Tab> map = new HashMap<>(); private static final Map<Path, Tab> map = new HashMap<>();
@FXML @FXML
public void initialize() { public void initialize() {
@ -91,17 +93,6 @@ public class FileEditController {
splitPane.getItems().add(1, tabPane); splitPane.getItems().add(1, tabPane);
splitPane.setDividerPosition(0, 0.20); splitPane.setDividerPosition(0, 0.20);
splitPane.setDividerPosition(1, 0.80); splitPane.setDividerPosition(1, 0.80);
tabPane.getTabs().addListener(new ListChangeListener<Tab>() {
@Override
public void onChanged(Change<? extends Tab> c) {
if (c.next()){
if (c.wasRemoved()){
filePaths_list.remove(c);
}
}
}
});
} }
@FXML @FXML
@ -122,15 +113,15 @@ public class FileEditController {
writer_file(); writer_file();
} }
public void writer_file(){ public void writer_file() {
Tab selectedItem = tabPane.getSelectionModel().getSelectedItem(); Tab selectedItem = tabPane.getSelectionModel().getSelectedItem();
int i = tabPane.getTabs().indexOf(selectedItem); int i = tabPane.getTabs().indexOf(selectedItem);
Path path = filePaths_list.get(i); Path path = filePaths_list.get(i);
log.info("writer_file__path:"+ path.getFileName()); log.info("writer_file__path:" + path.getFileName());
VirtualizedScrollPane virtualizedScrollPane = (VirtualizedScrollPane)selectedItem.getContent(); VirtualizedScrollPane virtualizedScrollPane = (VirtualizedScrollPane) selectedItem.getContent();
CodeArea codeArea = (CodeArea)virtualizedScrollPane.getContent(); CodeArea codeArea = (CodeArea) virtualizedScrollPane.getContent();
String text = codeArea.getText(); String text = codeArea.getText();
try { try {
FileWriter writer = new FileWriter(path.toFile()); FileWriter writer = new FileWriter(path.toFile());
@ -141,20 +132,20 @@ public class FileEditController {
} }
} }
public void init_file_list(){ public void init_file_list() {
this.recursiveFileList.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() { this.recursiveFileList.setCellFactory(new Callback<TreeView<File>, TreeCell<File>>() {
@Override @Override
public TreeCell<File> call(TreeView<File> param) { public TreeCell<File> call(TreeView<File> param) {
return new TreeCell<File>(){ return new TreeCell<File>() {
@Override @Override
protected void updateItem(File item, boolean empty) { protected void updateItem(File item, boolean empty) {
super.updateItem(item, empty); super.updateItem(item, empty);
if (!empty){ if (!empty) {
HBox hBox = new HBox(10); HBox hBox = new HBox(10);
ImageView iv = null; ImageView iv = null;
if (this.getTreeItem().isExpanded()){ if (this.getTreeItem().isExpanded()) {
iv = new ImageView(new Image("image/向下.png")); iv = new ImageView(new Image("image/向下.png"));
}else { } else {
iv = new ImageView(new Image("image/向右.png")); iv = new ImageView(new Image("image/向右.png"));
} }
iv.setPreserveRatio(true); iv.setPreserveRatio(true);
@ -166,38 +157,38 @@ public class FileEditController {
//判断是否是文件 //判断是否是文件
boolean directory = item.isDirectory(); boolean directory = item.isDirectory();
if (directory){ if (directory) {
ImageView dir = new ImageView(new Image("image/dir.png")); ImageView dir = new ImageView(new Image("image/dir.png"));
dir.setPreserveRatio(true); dir.setPreserveRatio(true);
dir.setFitWidth(icon_size.get()); dir.setFitWidth(icon_size.get());
hBox.getChildren().add(dir); hBox.getChildren().add(dir);
}else { } else {
if (item.isFile()){ if (item.isFile()) {
ImageView file = null; ImageView file = null;
String extension = FilenameUtils.getExtension(item.getName()); String extension = FilenameUtils.getExtension(item.getName());
if (extension.equals("java")){ if (extension.equals("java")) {
file = new ImageView(new Image("image/java.png")); file = new ImageView(new Image("image/java.png"));
} }
if (extension.equals("html")){ if (extension.equals("html")) {
file = new ImageView(new Image("image/html.png")); file = new ImageView(new Image("image/html.png"));
} }
if (extension.equals("jar")){ if (extension.equals("jar")) {
file = new ImageView(new Image("image/jar.png")); file = new ImageView(new Image("image/jar.png"));
} }
if (extension.equals("json")){ if (extension.equals("json")) {
file = new ImageView(new Image("image/json.png")); file = new ImageView(new Image("image/json.png"));
} }
if (extension.equals("jpg")){ if (extension.equals("jpg")) {
file = new ImageView(new Image("image/jpg.png")); file = new ImageView(new Image("image/jpg.png"));
} }
if (extension.equals("png")){ if (extension.equals("png")) {
file = new ImageView(new Image("image/png.png")); file = new ImageView(new Image("image/png.png"));
} }
if (file != null){ if (file != null) {
file.setPreserveRatio(true); file.setPreserveRatio(true);
file.setFitWidth(icon_size.get()); file.setFitWidth(icon_size.get());
hBox.getChildren().add(file); hBox.getChildren().add(file);
@ -218,7 +209,7 @@ public class FileEditController {
}); });
this.setGraphic(hBox); this.setGraphic(hBox);
}else { } else {
this.setGraphic(null); this.setGraphic(null);
this.setDisclosureNode(null); this.setDisclosureNode(null);
} }
@ -237,8 +228,11 @@ public class FileEditController {
Path path = file.toPath(); Path path = file.toPath();
if (filePaths_list.contains(path)) {//size -1 ,szie -2 if (filePaths_list.contains(path)) {//size -1 ,szie -2
//切换到一打开的tab //切换到一打开的tab
int i = filePaths_list.indexOf(path); Tab tab = map.get(path);
tabPane.getSelectionModel().select(i); if (!tabPane.getTabs().contains(tab)){
tabPane.getTabs().add(tab);
}
tabPane.getSelectionModel().select(tab);
} else { } else {
CodeArea codeArea = new CodeArea(); CodeArea codeArea = new CodeArea();
URL resource = this.getClass().getClassLoader().getResource("css/code.css"); URL resource = this.getClass().getClassLoader().getResource("css/code.css");
@ -267,14 +261,19 @@ public class FileEditController {
}); });
codeArea.replaceText(0, 0, file_content); codeArea.replaceText(0, 0, file_content);
Tab tab = new Tab(file.getName()); Tab tab = new Tab(file.getName());
tab.setOnCloseRequest(new EventHandler<Event>() {
@Override
public void handle(Event event) {
// log.info("tabtab.setOnCloseRequest:--->" + tab.getText());
tabPane.getTabs().remove(tab);
}
});
tab.setContent(new VirtualizedScrollPane<>(codeArea)); tab.setContent(new VirtualizedScrollPane<>(codeArea));
filePaths_list.add(path); filePaths_list.add(path);
int i = index.get(); tabPane.getTabs().add( tab);
map.put(i,tab);
tabPane.getTabs().add(i,tab);
tabPane.getSelectionModel().select(tab); tabPane.getSelectionModel().select(tab);
i ++; map.put(path,tab);
index.set(i);
} }
} }
@ -345,6 +344,9 @@ public class FileEditController {
if (extension.equals("go")) { if (extension.equals("go")) {
type = Type.GO; type = Type.GO;
} }
if (extension.equals("html")) {
type = Type.HTML;
}
open_file(file, type); open_file(file, type);
} else { } else {
@ -357,6 +359,9 @@ public class FileEditController {
if (type.equals(Type.GO)) { if (type.equals(Type.GO)) {
matcher = ProcessGO.PATTERN.matcher(text); matcher = ProcessGO.PATTERN.matcher(text);
} }
if (type.equals(Type.HTML)) {
matcher = ProcessHtml.PATTERN.matcher(text);
}
int lastKwEnd = 0; int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
while (matcher.find()) { while (matcher.find()) {

View File

@ -0,0 +1,133 @@
package com.zhangmeng.tools.languages;
import java.util.regex.Pattern;
/**
* @author :
* @version : 1.0
* @date : 2023-06-01 11:01
*/
public class ProcessHtml {
public static final String[] KEYWORDS = new String[] {
"<!DOCTYPE html>", "<html>", "<head>", "</head>", "<body>",
"</body>", "</html>", "<p>", "<br>",
"<title>", "</title>", "style", "<img",
"<h1>", "</h1>", "<h3>", ">",
"<h2>", "</h2>", "</h3>", "<hr>",
"<a", "</a>", "<table","<div","<div>","</div>","<span","<span>","</span>",
"<ul>", "</ul>", "<dl>", "</dl>", "<dd>","</dl>"
};
public static final String[] KEYWORDS2 = new String[] {
"href", "name", "style", "finally", "id", "src",
"return"
};
public static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
public static final String KEYWORD_PATTERN2 = "\\b(" + String.join("|", KEYWORDS2) + ")\\b";
public static final String PAREN_PATTERN = "\\(|\\)";
public static final String BRACE_PATTERN = "\\{|\\}";
public static final String BRACKET_PATTERN = "\\[|\\]";
public static final String SEMICOLON_PATTERN = "\\;";
public static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
public static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
public static final String ANNOTATION_PATTERN = "@[a-zA-Z]+";
public static final String PARAMS_PATTERN = "\\\\b([a-zA-Z]+)\\\\s*=\\\\s*([a-zA-Z0-9]+)\\\\b";
public static final String METHOD_PATTERN = "\\w+\\((\\w+,\\s*)*\\w*\\)";
public static final Pattern PATTERN = Pattern.compile(
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
+ "|(?<KEYWORD2>" + KEYWORD_PATTERN2 + ")"
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
+ "|(?<STRING>" + STRING_PATTERN + ")"
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
+ "|(?<ANNOTATION>" + ANNOTATION_PATTERN + ")"
+ "|(?<PARAMS>" + PARAMS_PATTERN + ")"
+ "|(?<METHOD>" + METHOD_PATTERN + ")"
);
public static String SAMPLE_CODE = "package com.dashidao.server.model;\n" +
"\n" +
"import java.util.Date;\n" +
"\n" +
"public class PosterGroupCouponCode extends BaseEntity<Long> {\n" +
"\n" +
"\tprivate Date addTime;\n" +
"\tprivate Boolean deleteStatus;\n" +
"\tprivate String groupCouponCode;\n" +
"\tprivate Integer groupCouponStatus;\n" +
"\tprivate Date writeOffTime;\n" +
"\tprivate Long orderFormId;\n" +
"\tprivate Long posterGoodsCartId;\n" +
"\tprivate Long tuiKuanShouHouId;\n" +
"\tprivate Long orderMealUserId;\n" +
"\tprivate String groupSkusJson;\n" +
"\n" +
"\tpublic Date getAddTime() {\n" +
"\t\treturn addTime;\n" +
"\t}\n" +
"\tpublic void setAddTime(Date addTime) {\n" +
"\t\tthis.addTime = addTime;\n" +
"\t}\n" +
"\tpublic Boolean getDeleteStatus() {\n" +
"\t\treturn deleteStatus;\n" +
"\t}\n" +
"\tpublic void setDeleteStatus(Boolean deleteStatus) {\n" +
"\t\tthis.deleteStatus = deleteStatus;\n" +
"\t}\n" +
"\tpublic String getGroupCouponCode() {\n" +
"\t\treturn groupCouponCode;\n" +
"\t}\n" +
"\tpublic void setGroupCouponCode(String groupCouponCode) {\n" +
"\t\tthis.groupCouponCode = groupCouponCode;\n" +
"\t}\n" +
"\tpublic Integer getGroupCouponStatus() {\n" +
"\t\treturn groupCouponStatus;\n" +
"\t}\n" +
"\tpublic void setGroupCouponStatus(Integer groupCouponStatus) {\n" +
"\t\tthis.groupCouponStatus = groupCouponStatus;\n" +
"\t}\n" +
"\tpublic Date getWriteOffTime() {\n" +
"\t\treturn writeOffTime;\n" +
"\t}\n" +
"\tpublic void setWriteOffTime(Date writeOffTime) {\n" +
"\t\tthis.writeOffTime = writeOffTime;\n" +
"\t}\n" +
"\tpublic Long getOrderFormId() {\n" +
"\t\treturn orderFormId;\n" +
"\t}\n" +
"\tpublic void setOrderFormId(Long orderFormId) {\n" +
"\t\tthis.orderFormId = orderFormId;\n" +
"\t}\n" +
"\tpublic Long getPosterGoodsCartId() {\n" +
"\t\treturn posterGoodsCartId;\n" +
"\t}\n" +
"\tpublic void setPosterGoodsCartId(Long posterGoodsCartId) {\n" +
"\t\tthis.posterGoodsCartId = posterGoodsCartId;\n" +
"\t}\n" +
"\tpublic Long getTuiKuanShouHouId() {\n" +
"\t\treturn tuiKuanShouHouId;\n" +
"\t}\n" +
"\tpublic void setTuiKuanShouHouId(Long tuiKuanShouHouId) {\n" +
"\t\tthis.tuiKuanShouHouId = tuiKuanShouHouId;\n" +
"\t}\n" +
"\tpublic Long getOrderMealUserId() {\n" +
"\t\treturn orderMealUserId;\n" +
"\t}\n" +
"\tpublic void setOrderMealUserId(Long orderMealUserId) {\n" +
"\t\tthis.orderMealUserId = orderMealUserId;\n" +
"\t}\n" +
"\tpublic String getGroupSkusJson() {\n" +
"\t\treturn groupSkusJson;\n" +
"\t}\n" +
"\tpublic void setGroupSkusJson(String groupSkusJson) {\n" +
"\t\tthis.groupSkusJson = groupSkusJson;\n" +
"\t}\n" +
"\n" +
"}\n";
}