diff --git a/src/main/java/com/zhangmeng/tools/controller/FileEditController.java b/src/main/java/com/zhangmeng/tools/controller/FileEditController.java index c14fddc..93c383c 100644 --- a/src/main/java/com/zhangmeng/tools/controller/FileEditController.java +++ b/src/main/java/com/zhangmeng/tools/controller/FileEditController.java @@ -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 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> 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 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(); + } + } + } } diff --git a/src/main/resources/config/fileEdit.properties b/src/main/resources/config/fileEdit.properties new file mode 100644 index 0000000..cb97756 --- /dev/null +++ b/src/main/resources/config/fileEdit.properties @@ -0,0 +1,2 @@ +open.file.path= +open.file.list[0] = \ No newline at end of file diff --git a/src/main/resources/config/music-list.fxml b/src/main/resources/config/music-list.fxml deleted file mode 100644 index a883016..0000000 --- a/src/main/resources/config/music-list.fxml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/fxml/file-edit.fxml b/src/main/resources/fxml/file-edit.fxml index 2367e7a..438bf97 100644 --- a/src/main/resources/fxml/file-edit.fxml +++ b/src/main/resources/fxml/file-edit.fxml @@ -14,7 +14,8 @@ - + +