From a9415a3ada8d5a02cd3eb71fc8f077b7584afdd4 Mon Sep 17 00:00:00 2001 From: zhangmeng <1334717033@qq.com> Date: Sat, 3 Jun 2023 18:18:30 +0800 Subject: [PATCH] =?UTF-8?q?2023=E5=B9=B46=E6=9C=882=E6=97=A518:28:13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/controller/FileEditController.java | 138 ++++++++++++------ src/main/resources/fxml/file-edit.fxml | 2 +- 2 files changed, 92 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/zhangmeng/tools/controller/FileEditController.java b/src/main/java/com/zhangmeng/tools/controller/FileEditController.java index 93c383c..e11b67d 100644 --- a/src/main/java/com/zhangmeng/tools/controller/FileEditController.java +++ b/src/main/java/com/zhangmeng/tools/controller/FileEditController.java @@ -6,10 +6,14 @@ 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.beans.property.SimpleIntegerProperty; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.Node; import javafx.scene.control.SplitPane; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; @@ -25,17 +29,12 @@ import org.fxmisc.richtext.LineNumberFactory; 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.io.*; 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.nio.file.Path; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -49,6 +48,7 @@ import java.util.regex.Pattern; @Slf4j public class FileEditController { + public enum Type { JAVA, @@ -66,15 +66,14 @@ public class FileEditController { private TabPane tabPane = null; private static String path = null; - private final ObservableList filePaths_list = FXCollections.observableArrayList(); + private final ObservableList filePaths_list = FXCollections.observableArrayList(); + private final SimpleIntegerProperty index = new SimpleIntegerProperty(0); + private static final Map map = new HashMap<>(); @FXML public void initialize() { - //读取本地加载记录 Properties s = config_path(false); - - recursiveFileList = new RecursiveFileList(null); tabPane = new TabPane(); splitPane.getItems().add(0, recursiveFileList); @@ -84,42 +83,87 @@ public class FileEditController { } @FXML - public void open_file_menu(){ + 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")); + dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.java", "*.go", "*.txt", "*.json")); File file = dc.showOpenDialog(stage); if (file != null) { - refresh(file,true); + refresh(file, true); } } + //文件保存 + @FXML + public void save_file_menu(ActionEvent event) { + writer_file(); + } + + public void writer_file(){ + Tab selectedItem = tabPane.getSelectionModel().getSelectedItem(); + + int i = tabPane.getTabs().indexOf(selectedItem); + Path path = filePaths_list.get(i); + log.info("writer_file__path:"+ path.getFileName()); + + VirtualizedScrollPane virtualizedScrollPane = (VirtualizedScrollPane)selectedItem.getContent(); + CodeArea codeArea = (CodeArea)virtualizedScrollPane.getContent(); + String text = codeArea.getText(); + try { + FileWriter writer = new FileWriter(path.toFile()); + writer.write(text); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + @FXML public void open_file_dir_menu(ActionEvent event) { open_file_dir(); } 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()); - codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); - byte[] bytes = new byte[0]; - try { - bytes = Files.readAllBytes(file.toPath()); - } catch (IOException e) { - e.printStackTrace(); + Path path = file.toPath(); + if (filePaths_list.contains(path)) {//size -1 ,szie -2 + //切换到一打开的tab + int i = filePaths_list.indexOf(path); + tabPane.getSelectionModel().select(i); + } else { + 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()); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// String file_content = new String(bytes, StandardCharsets.UTF_8); + String file_content = null; + try { + file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + } + + codeArea.textProperty().addListener((obs, oldText, newText) -> { + codeArea.setStyleSpans(0, computeHighlighting(newText, type)); + }); + codeArea.replaceText(0, 0, file_content); + Tab tab = new Tab(file.getName()); + tab.setContent(new VirtualizedScrollPane<>(codeArea)); + filePaths_list.add(path); + int i = index.get(); + map.put(i,tab); + tabPane.getTabs().add(i,tab); + tabPane.getSelectionModel().select(tab); + i ++; + index.set(i); } - String file_content = new String(bytes, StandardCharsets.UTF_8); - codeArea.textProperty().addListener((obs, oldText, newText) -> { - codeArea.setStyleSpans(0, computeHighlighting(newText, type)); - }); - codeArea.replaceText(0, 0, file_content); - Tab tab = new Tab(file.getName()); - tab.setContent(new VirtualizedScrollPane<>(codeArea)); - tabPane.getTabs().add(tab); - tabPane.getSelectionModel().select(tab); } public void open_file_dir() { @@ -130,17 +174,17 @@ public class FileEditController { File file = dc.showDialog(stage); if (file != null) { if (file.isDirectory()) { - refresh(file,false); + refresh(file, false); } } } - public void refresh(File file,boolean is_open_file){ + 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); + file_checked(newValue.getValue()); } }); @@ -148,12 +192,12 @@ public class FileEditController { splitPane.getItems().add(1, tabPane); splitPane.setDividerPosition(0, 0.20); splitPane.setDividerPosition(1, 0.80); - if (is_open_file){ + if (is_open_file) { file_checked(file); } } - public void file_checked(File file){ + public void file_checked(File file) { String extension = FilenameUtils.getExtension(file.getName()); boolean flag = true; @@ -250,7 +294,7 @@ public class FileEditController { return properties; } - public void setPath(String path, List filePaths){ + public void setPath(String path, List filePaths) { Resource resource = new ClassPathResource("config/fileEdit.properties"); InputStream inputStream = resource.getStream(); FileOutputStream fileOutputStream = null; @@ -258,26 +302,26 @@ public class FileEditController { try { properties.load(inputStream); String value = properties.getProperty("open.file.path"); - log.info("path:{}",value); - properties.setProperty("open.file.path",path); + log.info("path:{}", value); + properties.setProperty("open.file.path", path); int i = 0; - if (filePaths.size()>0){ + if (filePaths.size() > 0) { for (String filePath : filePaths) { - properties.setProperty("open.file.list[" + i + "]",filePath); + properties.setProperty("open.file.list[" + i + "]", filePath); i++; } } - log.info("properties:{}",properties); + log.info("properties:{}", properties); String out_path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "config/fileEdit.properties"; fileOutputStream = new FileOutputStream(out_path); - properties.store(fileOutputStream,"保存"); + properties.store(fileOutputStream, "保存"); } catch (IOException e) { log.info("选择路径出错!"); - }finally { + } finally { try { inputStream.close(); - if (fileOutputStream != null){ + if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { diff --git a/src/main/resources/fxml/file-edit.fxml b/src/main/resources/fxml/file-edit.fxml index 438bf97..7e71066 100644 --- a/src/main/resources/fxml/file-edit.fxml +++ b/src/main/resources/fxml/file-edit.fxml @@ -16,7 +16,7 @@ - +