2023年6月2日18:28:13
parent
9476743c77
commit
5e6dac1670
|
|
@ -1,9 +1,13 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import cn.hutool.core.io.resource.ClassPathResource;
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
import com.zhangmeng.tools.components.RecursiveFileList;
|
||||
import com.zhangmeng.tools.languages.ProcessGO;
|
||||
import com.zhangmeng.tools.languages.ProcessJava;
|
||||
import com.zhangmeng.tools.utils.AlertUtils;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.SplitPane;
|
||||
|
|
@ -22,12 +26,16 @@ import org.fxmisc.richtext.model.StyleSpans;
|
|||
import org.fxmisc.richtext.model.StyleSpansBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
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.List;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
@ -57,8 +65,16 @@ public class FileEditController {
|
|||
|
||||
private TabPane tabPane = null;
|
||||
|
||||
private static String path = null;
|
||||
private final ObservableList<String> filePaths_list = FXCollections.observableArrayList();
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
|
||||
//读取本地加载记录
|
||||
Properties s = config_path(false);
|
||||
|
||||
|
||||
recursiveFileList = new RecursiveFileList(null);
|
||||
tabPane = new TabPane();
|
||||
splitPane.getItems().add(0, recursiveFileList);
|
||||
|
|
@ -68,7 +84,19 @@ public class FileEditController {
|
|||
}
|
||||
|
||||
@FXML
|
||||
public void open_file_dir(ActionEvent event) {
|
||||
public void open_file_menu(){
|
||||
Stage stage = new Stage();
|
||||
FileChooser dc = new FileChooser();
|
||||
dc.setTitle("文件选择");
|
||||
dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.java", "*.go","*.txt","*.json"));
|
||||
File file = dc.showOpenDialog(stage);
|
||||
if (file != null) {
|
||||
refresh(file,true);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void open_file_dir_menu(ActionEvent event) {
|
||||
open_file_dir();
|
||||
}
|
||||
|
||||
|
|
@ -76,6 +104,7 @@ public class FileEditController {
|
|||
CodeArea codeArea = new CodeArea();
|
||||
URL resource = this.getClass().getClassLoader().getResource("css/code.css");
|
||||
codeArea.getStylesheets().add(resource.toExternalForm());
|
||||
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
|
||||
byte[] bytes = new byte[0];
|
||||
try {
|
||||
bytes = Files.readAllBytes(file.toPath());
|
||||
|
|
@ -83,12 +112,10 @@ public class FileEditController {
|
|||
e.printStackTrace();
|
||||
}
|
||||
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(file_content, type));
|
||||
codeArea.replaceText(0, 0, file_content);
|
||||
Tab tab = new Tab(file.getName());
|
||||
tab.setContent(new VirtualizedScrollPane<>(codeArea));
|
||||
tabPane.getTabs().add(tab);
|
||||
|
|
@ -103,81 +130,72 @@ public class FileEditController {
|
|||
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("exe")) {
|
||||
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) {
|
||||
Type type = Type.JAVA;
|
||||
if (extension.equals("go")) {
|
||||
type = Type.GO;
|
||||
}
|
||||
|
||||
open_file(newValue.getValue(), type);
|
||||
} else {
|
||||
AlertUtils.alert_warning("该文件不支持!");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
splitPane.getItems().add(0, recursiveFileList);
|
||||
splitPane.getItems().add(1, tabPane);
|
||||
splitPane.setDividerPosition(0, 0.20);
|
||||
splitPane.setDividerPosition(1, 0.80);
|
||||
refresh(file,false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void refresh(File file,boolean is_open_file){
|
||||
splitPane.getItems().clear();
|
||||
recursiveFileList = new RecursiveFileList(file);
|
||||
recursiveFileList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (newValue.getValue().isFile()) {
|
||||
file_checked(file);
|
||||
}
|
||||
});
|
||||
|
||||
splitPane.getItems().add(0, recursiveFileList);
|
||||
splitPane.getItems().add(1, tabPane);
|
||||
splitPane.setDividerPosition(0, 0.20);
|
||||
splitPane.setDividerPosition(1, 0.80);
|
||||
if (is_open_file){
|
||||
file_checked(file);
|
||||
}
|
||||
}
|
||||
|
||||
public void file_checked(File file){
|
||||
String extension = FilenameUtils.getExtension(file.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("exe")) {
|
||||
flag = false;
|
||||
}
|
||||
if (flag) {
|
||||
Type type = Type.JAVA;
|
||||
if (extension.equals("go")) {
|
||||
type = Type.GO;
|
||||
}
|
||||
|
||||
open_file(file, type);
|
||||
} else {
|
||||
AlertUtils.alert_warning("该文件不支持!");
|
||||
}
|
||||
}
|
||||
|
||||
public static StyleSpans<Collection<String>> computeHighlighting(String text, Type type) {
|
||||
Matcher matcher = ProcessJava.PATTERN.matcher(text);
|
||||
if (type.equals(Type.GO)) {
|
||||
|
|
@ -208,4 +226,63 @@ public class FileEditController {
|
|||
return spansBuilder.create();
|
||||
}
|
||||
|
||||
public static String config_path() {
|
||||
Resource resource = new ClassPathResource("config/fileEdit.properties");
|
||||
InputStream inputStream = resource.getStream();
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(inputStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return properties.getProperty("open.file.path");
|
||||
}
|
||||
|
||||
public static Properties config_path(boolean openFile) {
|
||||
Resource resource = new ClassPathResource("config/fileEdit.properties");
|
||||
InputStream inputStream = resource.getStream();
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(inputStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setPath(String path, List<String> filePaths){
|
||||
Resource resource = new ClassPathResource("config/fileEdit.properties");
|
||||
InputStream inputStream = resource.getStream();
|
||||
FileOutputStream fileOutputStream = null;
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(inputStream);
|
||||
String value = properties.getProperty("open.file.path");
|
||||
log.info("path:{}",value);
|
||||
properties.setProperty("open.file.path",path);
|
||||
|
||||
int i = 0;
|
||||
if (filePaths.size()>0){
|
||||
for (String filePath : filePaths) {
|
||||
properties.setProperty("open.file.list[" + i + "]",filePath);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
log.info("properties:{}",properties);
|
||||
String out_path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "config/fileEdit.properties";
|
||||
fileOutputStream = new FileOutputStream(out_path);
|
||||
properties.store(fileOutputStream,"保存");
|
||||
} catch (IOException e) {
|
||||
log.info("选择路径出错!");
|
||||
}finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
if (fileOutputStream != null){
|
||||
fileOutputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
open.file.path=
|
||||
open.file.list[0] =
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import com.zhangmeng.tools.music.musicPlayer.MusicPlayer?>
|
||||
<?import java.util.ArrayList?>
|
||||
|
||||
<ArrayList>
|
||||
<MusicPlayer file_path="a" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="b" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="b" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="d" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="e" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="f" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="g" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="h" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="i" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="j" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="k" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="l" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="m" file_lyric="18" singer="1"/>
|
||||
<MusicPlayer file_path="n" file_lyric="18" singer="1"/>
|
||||
|
||||
</ArrayList>
|
||||
|
|
@ -14,7 +14,8 @@
|
|||
<menus>
|
||||
<Menu mnemonicParsing="false" text="文件">
|
||||
<items>
|
||||
<MenuItem mnemonicParsing="false" text="打开文件夹" onAction="#open_file_dir" />
|
||||
<MenuItem mnemonicParsing="false" text="打开文件" onAction="#open_file_menu" />
|
||||
<MenuItem mnemonicParsing="false" text="打开文件夹" onAction="#open_file_dir_menu" />
|
||||
<MenuItem mnemonicParsing="false" text="保存"/>
|
||||
</items>
|
||||
</Menu>
|
||||
|
|
|
|||
Loading…
Reference in New Issue