2023年6月1日17:49:06 修改编辑器
parent
ccf0c9c774
commit
9476743c77
|
|
@ -7,6 +7,8 @@ import com.zhangmeng.tools.utils.AlertUtils;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.SplitPane;
|
import javafx.scene.control.SplitPane;
|
||||||
|
import javafx.scene.control.Tab;
|
||||||
|
import javafx.scene.control.TabPane;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.stage.DirectoryChooser;
|
import javafx.stage.DirectoryChooser;
|
||||||
import javafx.stage.FileChooser;
|
import javafx.stage.FileChooser;
|
||||||
|
|
@ -33,13 +35,13 @@ import java.util.regex.Pattern;
|
||||||
* @author : 芊芊墨客
|
* @author : 芊芊墨客
|
||||||
* @version : 1.0
|
* @version : 1.0
|
||||||
* @date : 2023-06-01 10:44
|
* @date : 2023-06-01 10:44
|
||||||
*
|
* <p>
|
||||||
* 文本编辑器
|
* 文本编辑器
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class FileEditController {
|
public class FileEditController {
|
||||||
|
|
||||||
public enum Type{
|
public enum Type {
|
||||||
|
|
||||||
JAVA,
|
JAVA,
|
||||||
HTML,
|
HTML,
|
||||||
|
|
@ -51,23 +53,18 @@ public class FileEditController {
|
||||||
@FXML
|
@FXML
|
||||||
public SplitPane splitPane;
|
public SplitPane splitPane;
|
||||||
|
|
||||||
private CodeArea codeArea;
|
|
||||||
private RecursiveFileList recursiveFileList = null;
|
private RecursiveFileList recursiveFileList = null;
|
||||||
|
|
||||||
|
private TabPane tabPane = null;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
recursiveFileList = new RecursiveFileList(null);
|
recursiveFileList = new RecursiveFileList(null);
|
||||||
codeArea = new CodeArea();
|
tabPane = new TabPane();
|
||||||
URL resource = this.getClass().getClassLoader().getResource("css/code.css");
|
splitPane.getItems().add(0, recursiveFileList);
|
||||||
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
|
splitPane.getItems().add(1, tabPane);
|
||||||
codeArea.textProperty().addListener((obs, oldText, newText) -> {
|
splitPane.setDividerPosition(0, 0.20);
|
||||||
codeArea.setStyleSpans(0, computeHighlighting(newText,Type.JAVA));
|
splitPane.setDividerPosition(1, 0.80);
|
||||||
});
|
|
||||||
codeArea.getStylesheets().add(resource.toExternalForm());
|
|
||||||
splitPane.getItems().add(0,recursiveFileList);
|
|
||||||
splitPane.getItems().add(1,new VirtualizedScrollPane<>(codeArea));
|
|
||||||
splitPane.setDividerPosition(0,0.20);
|
|
||||||
splitPane.setDividerPosition(1,0.80);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
|
@ -75,7 +72,10 @@ public class FileEditController {
|
||||||
open_file_dir();
|
open_file_dir();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void open_file(File file,Type type){
|
public void open_file(File file, Type type) {
|
||||||
|
CodeArea codeArea = new CodeArea();
|
||||||
|
URL resource = this.getClass().getClassLoader().getResource("css/code.css");
|
||||||
|
codeArea.getStylesheets().add(resource.toExternalForm());
|
||||||
byte[] bytes = new byte[0];
|
byte[] bytes = new byte[0];
|
||||||
try {
|
try {
|
||||||
bytes = Files.readAllBytes(file.toPath());
|
bytes = Files.readAllBytes(file.toPath());
|
||||||
|
|
@ -85,50 +85,56 @@ public class FileEditController {
|
||||||
String file_content = new String(bytes, StandardCharsets.UTF_8);
|
String file_content = new String(bytes, StandardCharsets.UTF_8);
|
||||||
codeArea.clear();
|
codeArea.clear();
|
||||||
codeArea.replaceText(0, 0, file_content);
|
codeArea.replaceText(0, 0, file_content);
|
||||||
|
|
||||||
codeArea.textProperty().addListener((obs, oldText, newText) -> {
|
codeArea.textProperty().addListener((obs, oldText, newText) -> {
|
||||||
codeArea.setStyleSpans(0, computeHighlighting(newText,type));
|
codeArea.setStyleSpans(0, computeHighlighting(newText, type));
|
||||||
});
|
});
|
||||||
|
codeArea.setStyleSpans(0, computeHighlighting(file_content, type));
|
||||||
codeArea.setStyleSpans(0, computeHighlighting(file_content,type));
|
Tab tab = new Tab(file.getName());
|
||||||
|
tab.setContent(new VirtualizedScrollPane<>(codeArea));
|
||||||
|
tabPane.getTabs().add(tab);
|
||||||
|
tabPane.getSelectionModel().select(tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void open_file_dir(){
|
public void open_file_dir() {
|
||||||
|
|
||||||
Stage stage = new Stage();
|
Stage stage = new Stage();
|
||||||
DirectoryChooser dc = new DirectoryChooser();
|
DirectoryChooser dc = new DirectoryChooser();
|
||||||
dc.setTitle("文件夹选择");
|
dc.setTitle("文件夹选择");
|
||||||
File file = dc.showDialog(stage);
|
File file = dc.showDialog(stage);
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
if (file.isDirectory()){
|
if (file.isDirectory()) {
|
||||||
splitPane.getItems().clear();
|
splitPane.getItems().clear();
|
||||||
recursiveFileList = new RecursiveFileList(file) ;
|
recursiveFileList = new RecursiveFileList(file);
|
||||||
recursiveFileList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
recursiveFileList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
if (newValue.getValue().isFile()) {
|
if (newValue.getValue().isFile()) {
|
||||||
String extension = FilenameUtils.getExtension(newValue.getValue().getName());
|
String extension = FilenameUtils.getExtension(newValue.getValue().getName());
|
||||||
boolean flag = true;
|
boolean flag = true;
|
||||||
|
|
||||||
if (extension.equals("mp4")){
|
if (extension.equals("mp4")) {
|
||||||
flag = false;
|
flag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extension.equals("jpg")){
|
if (extension.equals("jpg")) {
|
||||||
flag = false;
|
flag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extension.equals("png")){
|
if (extension.equals("png")) {
|
||||||
flag = false;
|
flag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extension.equals("avi")){
|
if (extension.equals("avi")) {
|
||||||
flag = false;
|
flag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extension.equals("zip")){
|
if (extension.equals("zip")) {
|
||||||
flag = false;
|
flag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extension.equals("ico")){
|
if (extension.equals("ico")) {
|
||||||
|
flag = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension.equals("exe")) {
|
||||||
flag = false;
|
flag = false;
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
|
|
@ -151,37 +157,35 @@ public class FileEditController {
|
||||||
// if (extension.equals(".xml")){
|
// if (extension.equals(".xml")){
|
||||||
// flag = true;
|
// flag = true;
|
||||||
// }
|
// }
|
||||||
if (flag){
|
if (flag) {
|
||||||
Type type = Type.JAVA;
|
Type type = Type.JAVA;
|
||||||
if (extension.equals("go")){
|
if (extension.equals("go")) {
|
||||||
type = Type.GO;
|
type = Type.GO;
|
||||||
}
|
}
|
||||||
|
|
||||||
open_file(newValue.getValue(),type);
|
open_file(newValue.getValue(), type);
|
||||||
}else {
|
} else {
|
||||||
AlertUtils.alert_warning("该文件不支持!");
|
AlertUtils.alert_warning("该文件不支持!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
splitPane.getItems().add(0,recursiveFileList);
|
splitPane.getItems().add(0, recursiveFileList);
|
||||||
splitPane.getItems().add(1,new VirtualizedScrollPane<>(codeArea));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static StyleSpans<Collection<String>> computeHighlighting(String text,Type type) {
|
public static StyleSpans<Collection<String>> computeHighlighting(String text, Type type) {
|
||||||
Matcher matcher = ProcessJava.PATTERN.matcher(text);
|
Matcher matcher = ProcessJava.PATTERN.matcher(text);
|
||||||
if (type.equals(Type.GO)){
|
if (type.equals(Type.GO)) {
|
||||||
matcher = ProcessGO.PATTERN.matcher(text);
|
matcher = ProcessGO.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()) {
|
||||||
String styleClass =
|
String styleClass =
|
||||||
matcher.group("KEYWORD") != null ? "keyword" :
|
matcher.group("KEYWORD") != null ? "keyword" :
|
||||||
matcher.group("PAREN") != null ? "paren" :
|
matcher.group("PAREN") != null ? "paren" :
|
||||||
|
|
@ -192,9 +196,9 @@ public class FileEditController {
|
||||||
matcher.group("COMMENT") != null ? "comment" :
|
matcher.group("COMMENT") != null ? "comment" :
|
||||||
matcher.group("ANNOTATION") != null ? "annotation" :
|
matcher.group("ANNOTATION") != null ? "annotation" :
|
||||||
matcher.group("PARAMS") != null ? "parameter" :
|
matcher.group("PARAMS") != null ? "parameter" :
|
||||||
matcher.group("METHOD") != null ? "method" :
|
matcher.group("METHOD") != null ? "method" :
|
||||||
matcher.group("KEYWORD2") != null ? "method2" :
|
matcher.group("KEYWORD2") != null ? "method2" :
|
||||||
null; /* never happens */
|
null; /* never happens */
|
||||||
assert styleClass != null;
|
assert styleClass != null;
|
||||||
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
|
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
|
||||||
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
|
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue