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.fxml.FXML;
|
||||
import javafx.scene.control.SplitPane;
|
||||
import javafx.scene.control.Tab;
|
||||
import javafx.scene.control.TabPane;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.stage.DirectoryChooser;
|
||||
import javafx.stage.FileChooser;
|
||||
|
|
@ -33,13 +35,13 @@ import java.util.regex.Pattern;
|
|||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-06-01 10:44
|
||||
*
|
||||
* <p>
|
||||
* 文本编辑器
|
||||
*/
|
||||
@Slf4j
|
||||
public class FileEditController {
|
||||
|
||||
public enum Type{
|
||||
public enum Type {
|
||||
|
||||
JAVA,
|
||||
HTML,
|
||||
|
|
@ -51,23 +53,18 @@ public class FileEditController {
|
|||
@FXML
|
||||
public SplitPane splitPane;
|
||||
|
||||
private CodeArea codeArea;
|
||||
private RecursiveFileList recursiveFileList = null;
|
||||
|
||||
private TabPane tabPane = 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,Type.JAVA));
|
||||
});
|
||||
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);
|
||||
tabPane = new TabPane();
|
||||
splitPane.getItems().add(0, recursiveFileList);
|
||||
splitPane.getItems().add(1, tabPane);
|
||||
splitPane.setDividerPosition(0, 0.20);
|
||||
splitPane.setDividerPosition(1, 0.80);
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
|
@ -75,7 +72,10 @@ public class FileEditController {
|
|||
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];
|
||||
try {
|
||||
bytes = Files.readAllBytes(file.toPath());
|
||||
|
|
@ -85,50 +85,56 @@ public class FileEditController {
|
|||
String file_content = new String(bytes, StandardCharsets.UTF_8);
|
||||
codeArea.clear();
|
||||
codeArea.replaceText(0, 0, file_content);
|
||||
|
||||
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();
|
||||
DirectoryChooser dc = new DirectoryChooser();
|
||||
dc.setTitle("文件夹选择");
|
||||
File file = dc.showDialog(stage);
|
||||
if (file != null) {
|
||||
if (file.isDirectory()){
|
||||
if (file.isDirectory()) {
|
||||
splitPane.getItems().clear();
|
||||
recursiveFileList = new RecursiveFileList(file) ;
|
||||
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")){
|
||||
if (extension.equals("mp4")) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if (extension.equals("jpg")){
|
||||
if (extension.equals("jpg")) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if (extension.equals("png")){
|
||||
if (extension.equals("png")) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if (extension.equals("avi")){
|
||||
if (extension.equals("avi")) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if (extension.equals("zip")){
|
||||
if (extension.equals("zip")) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if (extension.equals("ico")){
|
||||
if (extension.equals("ico")) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if (extension.equals("exe")) {
|
||||
flag = false;
|
||||
}
|
||||
//
|
||||
|
|
@ -151,37 +157,35 @@ public class FileEditController {
|
|||
// if (extension.equals(".xml")){
|
||||
// flag = true;
|
||||
// }
|
||||
if (flag){
|
||||
if (flag) {
|
||||
Type type = Type.JAVA;
|
||||
if (extension.equals("go")){
|
||||
if (extension.equals("go")) {
|
||||
type = Type.GO;
|
||||
}
|
||||
|
||||
open_file(newValue.getValue(),type);
|
||||
}else {
|
||||
open_file(newValue.getValue(), type);
|
||||
} 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);
|
||||
splitPane.getItems().add(0, recursiveFileList);
|
||||
splitPane.getItems().add(1, tabPane);
|
||||
splitPane.setDividerPosition(0, 0.20);
|
||||
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);
|
||||
if (type.equals(Type.GO)){
|
||||
if (type.equals(Type.GO)) {
|
||||
matcher = ProcessGO.PATTERN.matcher(text);
|
||||
}
|
||||
|
||||
|
||||
int lastKwEnd = 0;
|
||||
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
|
||||
while(matcher.find()) {
|
||||
while (matcher.find()) {
|
||||
String styleClass =
|
||||
matcher.group("KEYWORD") != null ? "keyword" :
|
||||
matcher.group("PAREN") != null ? "paren" :
|
||||
|
|
@ -192,9 +196,9 @@ public class FileEditController {
|
|||
matcher.group("COMMENT") != null ? "comment" :
|
||||
matcher.group("ANNOTATION") != null ? "annotation" :
|
||||
matcher.group("PARAMS") != null ? "parameter" :
|
||||
matcher.group("METHOD") != null ? "method" :
|
||||
matcher.group("KEYWORD2") != null ? "method2" :
|
||||
null; /* never happens */
|
||||
matcher.group("METHOD") != null ? "method" :
|
||||
matcher.group("KEYWORD2") != null ? "method2" :
|
||||
null; /* never happens */
|
||||
assert styleClass != null;
|
||||
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
|
||||
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
|
||||
|
|
|
|||
Loading…
Reference in New Issue