package com.zhangmeng.tools.controller; import com.zhangmeng.tools.components.RecursiveFileList; import com.zhangmeng.tools.languages.ProcessJava; import com.zhangmeng.tools.utils.AlertUtils; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.SplitPane; import javafx.scene.image.Image; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import javafx.stage.Stage; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FilenameUtils; import org.fxmisc.flowless.VirtualizedScrollPane; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.LineNumberFactory; import org.fxmisc.richtext.model.StyleSpans; import org.fxmisc.richtext.model.StyleSpansBuilder; import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Collection; import java.util.Collections; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author : 芊芊墨客 * @version : 1.0 * @date : 2023-06-01 10:44 * * 文本编辑器 */ @Slf4j public class FileEditController { public SplitPane splitPane; private CodeArea codeArea; private RecursiveFileList recursiveFileList = null; @FXML public void initialize() { recursiveFileList = new RecursiveFileList(null); codeArea = new CodeArea(); URL resource = this.getClass().getClassLoader().getResource("css/code.css"); codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); codeArea.textProperty().addListener((obs, oldText, newText) -> { codeArea.setStyleSpans(0, computeHighlighting(newText)); }); 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 public void open_file_dir(ActionEvent event) { open_file_dir(); } public void open_file(File file){ byte[] bytes = new byte[0]; try { bytes = Files.readAllBytes(file.toPath()); } catch (IOException e) { e.printStackTrace(); } String file_content = new String(bytes, StandardCharsets.UTF_8); codeArea.clear(); codeArea.replaceText(0, 0, file_content); } public void open_file_dir(){ Stage stage = new Stage(); DirectoryChooser dc = new DirectoryChooser(); dc.setTitle("文件夹选择"); File file = dc.showDialog(stage); if (file != null) { if (file.isDirectory()){ splitPane.getItems().clear(); recursiveFileList = new RecursiveFileList(file) ; recursiveFileList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { if (newValue.getValue().isFile()) { String extension = FilenameUtils.getExtension(newValue.getValue().getName()); boolean flag = true; if (extension.equals("mp4")){ flag = false; } if (extension.equals("jpg")){ flag = false; } if (extension.equals("png")){ flag = false; } if (extension.equals("avi")){ flag = false; } if (extension.equals("zip")){ flag = false; } if (extension.equals("ico")){ flag = false; } // // if (extension.equals("json")){ // flag = true; // } // // if (extension.equals("go")){ // flag = true; // } // // if (extension.equals("js")){ // flag = true; // } // // if (extension.equals("css")){ // flag = true; // } // // if (extension.equals(".xml")){ // flag = true; // } if (flag){ open_file(newValue.getValue()); }else { AlertUtils.alert_warning("该文件不支持!"); } } }); splitPane.getItems().add(0,recursiveFileList); splitPane.getItems().add(1,new VirtualizedScrollPane<>(codeArea)); splitPane.setDividerPosition(0,0.20); splitPane.setDividerPosition(1,0.80); } } } public static StyleSpans> computeHighlighting(String text) { Matcher matcher = ProcessJava.PATTERN.matcher(text); int lastKwEnd = 0; StyleSpansBuilder> spansBuilder = new StyleSpansBuilder<>(); while(matcher.find()) { String styleClass = matcher.group("KEYWORD") != null ? "keyword" : matcher.group("PAREN") != null ? "paren" : matcher.group("BRACE") != null ? "brace" : matcher.group("BRACKET") != null ? "bracket" : matcher.group("SEMICOLON") != null ? "semicolon" : matcher.group("STRING") != null ? "string" : matcher.group("COMMENT") != null ? "comment" : matcher.group("ANNOTATION") != null ? "annotation" : matcher.group("PARAMS") != null ? "parameter" : null; /* never happens */ assert styleClass != null; spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start()); lastKwEnd = matcher.end(); } spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd); return spansBuilder.create(); } }