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);
@ -84,42 +83,87 @@ public class FileEditController {
} }
@FXML @FXML
public void open_file_menu(){ public void open_file_menu() {
Stage stage = new Stage(); Stage stage = new Stage();
FileChooser dc = new FileChooser(); FileChooser dc = new FileChooser();
dc.setTitle("文件选择"); 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); File file = dc.showOpenDialog(stage);
if (file != null) { 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 @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() {
@ -130,17 +174,17 @@ public class FileEditController {
File file = dc.showDialog(stage); File file = dc.showDialog(stage);
if (file != null) { if (file != null) {
if (file.isDirectory()) { 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(); splitPane.getItems().clear();
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());
} }
}); });
@ -148,12 +192,12 @@ public class FileEditController {
splitPane.getItems().add(1, tabPane); splitPane.getItems().add(1, tabPane);
splitPane.setDividerPosition(0, 0.20); splitPane.setDividerPosition(0, 0.20);
splitPane.setDividerPosition(1, 0.80); splitPane.setDividerPosition(1, 0.80);
if (is_open_file){ if (is_open_file) {
file_checked(file); file_checked(file);
} }
} }
public void file_checked(File file){ public void file_checked(File file) {
String extension = FilenameUtils.getExtension(file.getName()); String extension = FilenameUtils.getExtension(file.getName());
boolean flag = true; boolean flag = true;
@ -250,7 +294,7 @@ public class FileEditController {
return properties; return properties;
} }
public void setPath(String path, List<String> filePaths){ public void setPath(String path, List<String> filePaths) {
Resource resource = new ClassPathResource("config/fileEdit.properties"); Resource resource = new ClassPathResource("config/fileEdit.properties");
InputStream inputStream = resource.getStream(); InputStream inputStream = resource.getStream();
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
@ -258,26 +302,26 @@ public class FileEditController {
try { try {
properties.load(inputStream); properties.load(inputStream);
String value = properties.getProperty("open.file.path"); String value = properties.getProperty("open.file.path");
log.info("path:{}",value); log.info("path:{}", value);
properties.setProperty("open.file.path",path); properties.setProperty("open.file.path", path);
int i = 0; int i = 0;
if (filePaths.size()>0){ if (filePaths.size() > 0) {
for (String filePath : filePaths) { for (String filePath : filePaths) {
properties.setProperty("open.file.list[" + i + "]",filePath); properties.setProperty("open.file.list[" + i + "]", filePath);
i++; i++;
} }
} }
log.info("properties:{}",properties); log.info("properties:{}", properties);
String out_path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "config/fileEdit.properties"; String out_path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "config/fileEdit.properties";
fileOutputStream = new FileOutputStream(out_path); fileOutputStream = new FileOutputStream(out_path);
properties.store(fileOutputStream,"保存"); properties.store(fileOutputStream, "保存");
} catch (IOException e) { } catch (IOException e) {
log.info("选择路径出错!"); log.info("选择路径出错!");
}finally { } finally {
try { try {
inputStream.close(); inputStream.close();
if (fileOutputStream != null){ if (fileOutputStream != null) {
fileOutputStream.close(); fileOutputStream.close();
} }
} catch (IOException e) { } catch (IOException e) {

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>