2023年6月2日18:28:13

master
zhangmeng 2023-06-03 18:18:30 +08:00
parent 5e6dac1670
commit a9415a3ada
2 changed files with 92 additions and 48 deletions

View File

@ -6,10 +6,14 @@ import com.zhangmeng.tools.components.RecursiveFileList;
import com.zhangmeng.tools.languages.ProcessGO; import com.zhangmeng.tools.languages.ProcessGO;
import com.zhangmeng.tools.languages.ProcessJava; import com.zhangmeng.tools.languages.ProcessJava;
import com.zhangmeng.tools.utils.AlertUtils; 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.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.SplitPane; import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab; import javafx.scene.control.Tab;
import javafx.scene.control.TabPane; 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.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder; import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Collection; import java.nio.file.Path;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -49,6 +48,7 @@ import java.util.regex.Pattern;
@Slf4j @Slf4j
public class FileEditController { public class FileEditController {
public enum Type { public enum Type {
JAVA, JAVA,
@ -66,15 +66,14 @@ public class FileEditController {
private TabPane tabPane = null; private TabPane tabPane = null;
private static String path = null; private static String path = null;
private final ObservableList<String> filePaths_list = FXCollections.observableArrayList(); private final ObservableList<Path> filePaths_list = FXCollections.observableArrayList();
private final SimpleIntegerProperty index = new SimpleIntegerProperty(0);
private static final Map<Integer,Tab> map = new HashMap<>();
@FXML @FXML
public void initialize() { public void initialize() {
//读取本地加载记录 //读取本地加载记录
Properties s = config_path(false); Properties s = config_path(false);
recursiveFileList = new RecursiveFileList(null); recursiveFileList = new RecursiveFileList(null);
tabPane = new TabPane(); tabPane = new TabPane();
splitPane.getItems().add(0, recursiveFileList); splitPane.getItems().add(0, recursiveFileList);
@ -95,31 +94,76 @@ public class FileEditController {
} }
} }
//文件保存
@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 @FXML
public void open_file_dir_menu(ActionEvent event) { public void open_file_dir_menu(ActionEvent event) {
open_file_dir(); open_file_dir();
} }
public void open_file(File file, Type type) { public void open_file(File file, Type type) {
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(); CodeArea codeArea = new CodeArea();
URL resource = this.getClass().getClassLoader().getResource("css/code.css"); URL resource = this.getClass().getClassLoader().getResource("css/code.css");
codeArea.getStylesheets().add(resource.toExternalForm()); codeArea.getStylesheets().add(resource.toExternalForm());
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
byte[] bytes = new byte[0]; 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 { try {
bytes = Files.readAllBytes(file.toPath()); file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
String file_content = new String(bytes, StandardCharsets.UTF_8);
codeArea.textProperty().addListener((obs, oldText, newText) -> { codeArea.textProperty().addListener((obs, oldText, newText) -> {
codeArea.setStyleSpans(0, computeHighlighting(newText, type)); codeArea.setStyleSpans(0, computeHighlighting(newText, type));
}); });
codeArea.replaceText(0, 0, file_content); codeArea.replaceText(0, 0, file_content);
Tab tab = new Tab(file.getName()); Tab tab = new Tab(file.getName());
tab.setContent(new VirtualizedScrollPane<>(codeArea)); tab.setContent(new VirtualizedScrollPane<>(codeArea));
tabPane.getTabs().add(tab); filePaths_list.add(path);
int i = index.get();
map.put(i,tab);
tabPane.getTabs().add(i,tab);
tabPane.getSelectionModel().select(tab); tabPane.getSelectionModel().select(tab);
i ++;
index.set(i);
}
} }
public void open_file_dir() { public void open_file_dir() {
@ -140,7 +184,7 @@ public class FileEditController {
recursiveFileList = new RecursiveFileList(file); recursiveFileList = new RecursiveFileList(file);
recursiveFileList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { recursiveFileList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.getValue().isFile()) { if (newValue.getValue().isFile()) {
file_checked(file); file_checked(newValue.getValue());
} }
}); });

View File

@ -16,7 +16,7 @@
<items> <items>
<MenuItem mnemonicParsing="false" text="打开文件" onAction="#open_file_menu" /> <MenuItem mnemonicParsing="false" text="打开文件" onAction="#open_file_menu" />
<MenuItem mnemonicParsing="false" text="打开文件夹" onAction="#open_file_dir_menu" /> <MenuItem mnemonicParsing="false" text="打开文件夹" onAction="#open_file_dir_menu" />
<MenuItem mnemonicParsing="false" text="保存"/> <MenuItem mnemonicParsing="false" text="保存" onAction="#save_file_menu"/>
</items> </items>
</Menu> </Menu>
</menus> </menus>