🚩 添加 选择文件根路径的功能

This commit is contained in:
gewuyou 2023-10-05 12:42:05 +08:00
parent 981fa41f25
commit 778bb5bb62
4 changed files with 92 additions and 17 deletions

View File

@ -18,8 +18,8 @@ public class SideBarButtonBuilder {
private EventHandler<ActionEvent> eventHandler;
public Button build() {
Optional<Button> container = Optional.of(new Button());
button = container.get();
Optional<Button> container = Optional.ofNullable(button);
button = container.orElseGet(Button::new);
button.setGraphic(imageView);
button.setOnAction(eventHandler);
return button;
@ -31,9 +31,9 @@ public class SideBarButtonBuilder {
}
public SideBarButtonBuilder setButtonEssentialAttribute(Double relativelyPrefWidth, Double relativelyPrefHeight) {
Optional<Double> container = Optional.of(relativelyPrefHeight);
Optional<Double> container = Optional.ofNullable(relativelyPrefHeight);
button.setPrefWidth(imageView.getFitWidth() + container.orElse(20D));
container = Optional.of(relativelyPrefWidth);
container = Optional.ofNullable(relativelyPrefWidth);
button.setPrefHeight(imageView.getFitHeight() + container.orElse(20D));
return this;
}

View File

@ -15,7 +15,6 @@ import org.jcnc.jnotepad.common.manager.ThreadPoolManager;
import org.jcnc.jnotepad.controller.ResourceController;
import org.jcnc.jnotepad.controller.cache.CacheController;
import org.jcnc.jnotepad.controller.config.PluginConfigController;
import org.jcnc.jnotepad.controller.config.UserConfigController;
import org.jcnc.jnotepad.controller.manager.Controller;
import org.jcnc.jnotepad.plugin.manager.PluginManager;
import org.jcnc.jnotepad.util.LogUtil;
@ -140,8 +139,7 @@ public class ApplicationManager {
// 刷新插件配置文件
pluginConfigController.getConfig().setPlugins(PluginManager.getInstance().getPluginDescriptors());
pluginConfigController.writeConfig();
// 保存配置文件
UserConfigController.getInstance().writeConfig();
// 销毁插件可能申请的资源
PluginManager.getInstance().destroyPlugins();
// 保存已打开的文件标签页

View File

@ -16,9 +16,22 @@ import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.jcnc.jnotepad.app.config.AppConfig;
import org.jcnc.jnotepad.app.i18n.UiResourceBundle;
import org.jcnc.jnotepad.app.manager.ApplicationManager;
import org.jcnc.jnotepad.common.constants.TextConstants;
import org.jcnc.jnotepad.common.manager.ApplicationCacheManager;
import org.jcnc.jnotepad.component.stage.dialog.factory.impl.BasicDirectoryChooserFactory;
import org.jcnc.jnotepad.controller.config.AppConfigController;
import org.jcnc.jnotepad.controller.event.handler.menuitem.OpenDirectory;
import org.jcnc.jnotepad.model.entity.Cache;
import org.jcnc.jnotepad.model.enums.CacheExpirationTime;
import org.jcnc.jnotepad.plugin.PluginManagerInterface;
import org.jcnc.jnotepad.util.PopUpUtil;
import org.jcnc.jnotepad.util.UiUtil;
import java.io.File;
import static org.jcnc.jnotepad.common.constants.AppConstants.SCREEN_LENGTH;
import static org.jcnc.jnotepad.common.constants.AppConstants.SCREEN_WIDTH;
@ -42,6 +55,8 @@ public class SetStage extends Stage {
private static SetStage instance;
private StackPane contentDisplay;
private final ApplicationCacheManager cacheManager = ApplicationCacheManager.getInstance();
/**
* 私有构造方法以实现单例模式
*/
@ -202,8 +217,6 @@ public class SetStage extends Stage {
DeveloperDebugStage debugPage = new DeveloperDebugStage();
debugPage.start(new Stage());
});
generalLayout.getChildren().addAll(devBox);
return generalLayout;
@ -218,23 +231,50 @@ public class SetStage extends Stage {
VBox generalLayout = new VBox(10);
generalLayout.setPadding(new Insets(25));
var hBox=new HBox(5);
var hBox = new HBox(5);
var fileChooseText =new Text("路径选择: ");
var fileChooseText = new Text("文件根路径: ");
fileChooseText.setFont(new Font(18));
var fileChoose =new CustomTextField("");
AppConfig config = AppConfigController.getInstance().getConfig();
var fileChoose = new CustomTextField(config.getRootPath());
fileChoose.getStyleClass().add(Styles.SMALL);
fileChoose.setPrefWidth(420);
var fileChooseBtn =new Button();
var fileChooseBtn = new Button();
fileChooseBtn.setText("选择文件夹");
fileChooseBtn.getStyleClass().addAll(Styles.SMALL);
BasicDirectoryChooserFactory directoryChooserFactory = BasicDirectoryChooserFactory.getInstance();
fileChooseBtn.setOnAction(event -> {
// TODO: 2023/10/4 选择文件
// 获取打开目录缓存
Cache cache = cacheManager.getCache(OpenDirectory.GROUP, "openDirectory");
File file = directoryChooserFactory.createDirectoryChooser(
UiResourceBundle.getContent(TextConstants.OPEN),
cache == null ? null : new File((String) cache.getCacheData()))
.showDialog(UiUtil.getAppWindow());
if (file == null) {
return;
}
if (file.equals(new File(config.getRootPath()))) {
PopUpUtil.errorAlert("错误", "路径不能和默认路径相同", "请重新选择路径", null, null);
return;
}
// 设置缓存
if (cache == null) {
cacheManager.addCache(cacheManager.createCache(OpenDirectory.GROUP, "openDirectory", file.getAbsolutePath(), CacheExpirationTime.NEVER_EXPIRES.getValue()));
} else {
cache.setCacheData(file.getParent());
cacheManager.addCache(cache);
}
config.setRootPath(file.getAbsolutePath());
PopUpUtil.questionAlert("更改", "设置程序文件根路径", "设置成功,请重启程序以应用路径更改!", appDialog -> {
appDialog.close();
ApplicationManager.getInstance().restart();
}, null, "重启", "以后再说");
Stage stage = (Stage) fileChooseBtn.getScene().getWindow();
stage.close();
});
hBox.getChildren().addAll(fileChooseText,fileChoose,fileChooseBtn);
hBox.getChildren().addAll(fileChooseText, fileChoose, fileChooseBtn);
generalLayout.getChildren().addAll(hBox);

View File

@ -9,7 +9,9 @@ import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@ -157,7 +159,42 @@ public class FileUtil {
}
}
}
return dirFileModel;
}
/**
* 文件夹迁移
*
* @param sourceFolder 源文件夹
* @param targetFolder 目标文件夹
* @since 2023/10/5 12:18
*/
private static void migrateFolder(File sourceFolder, File targetFolder) {
// 创建目标文件夹
targetFolder.mkdirs();
// 获取源文件夹中的所有文件和文件夹
File[] files = sourceFolder.listFiles();
if (files != null) {
// 遍历源文件夹中的每个文件和文件夹
for (File file : files) {
if (file.isDirectory()) {
// 如果是文件夹递归调用自身进行迁移
migrateFolder(file, new File(targetFolder, file.getName()));
} else {
// 如果是文件将文件复制到目标文件夹中
Path sourceFilePath = file.toPath();
Path targetFilePath = new File(targetFolder, file.getName()).toPath();
try {
Files.copy(sourceFilePath, targetFilePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new AppException(e);
}
}
}
}
}
}