!51 添加重命名功能

Merge pull request !51 from 格物方能致知/feature-I7SOWB
This commit is contained in:
Luke 2023-08-29 16:06:52 +00:00 committed by Gitee
commit 43da503da9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
13 changed files with 140 additions and 39 deletions

View File

@ -20,6 +20,8 @@ public class TextConstants {
public static final String NEW = "NEW"; public static final String NEW = "NEW";
public static final String OPEN = "OPEN"; public static final String OPEN = "OPEN";
public static final String SAVE_AS = "SAVE_AS"; public static final String SAVE_AS = "SAVE_AS";
public static final String RENAME = "RENAME";
public static final String SET = "SET"; public static final String SET = "SET";
public static final String WORD_WRAP = "WORD_WRAP"; public static final String WORD_WRAP = "WORD_WRAP";
public static final String PLUGIN = "PLUGIN"; public static final String PLUGIN = "PLUGIN";
@ -37,8 +39,14 @@ public class TextConstants {
public static final String ENCODE = "ENCODE"; public static final String ENCODE = "ENCODE";
/// Config 文本常量 /// Config 文本常量
/**
* 英文小写
*/
public static final String ENGLISH = "english"; public static final String ENGLISH = "english";
/**
* 中文小写
*/
public static final String CHINESE = "chinese"; public static final String CHINESE = "chinese";
} }

View File

@ -7,7 +7,6 @@ import org.jcnc.jnotepad.constants.TextConstants;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.tool.UiUtil;
import org.jcnc.jnotepad.ui.LineNumberTextArea; import org.jcnc.jnotepad.ui.LineNumberTextArea;
import org.jcnc.jnotepad.ui.tab.JNotepadTab; import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
import org.jcnc.jnotepad.view.manager.ViewManager; import org.jcnc.jnotepad.view.manager.ViewManager;
@ -28,21 +27,22 @@ public class NewFile implements EventHandler<ActionEvent> {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
addNewFileTab(); addNewFileTab();
} }
public void addNewFileTab() { public void addNewFileTab() {
// 创建一个新的文本编辑区 // 创建一个新的文本编辑区
LineNumberTextArea textArea = new LineNumberTextArea(); LineNumberTextArea textArea = new LineNumberTextArea();
// 设置当前标签页与本地文件无关联
textArea.setRelevance(false);
// TODO: refactor统一TextArea新建绑定监听器入口 // TODO: refactor统一TextArea新建绑定监听器入口
ViewManager viewManager = UiUtil.getViewManager(); ViewManager viewManager = UiUtil.getViewManager();
// 创建标签页
JNotepadTab jNotepadTab = new JNotepadTab(
UIResourceBundle.getContent(TextConstants.NEW_FILE)
+ viewManager.selfIncreaseAndGetTabIndex(),
textArea);
// 设置当前标签页与本地文件无关联
jNotepadTab.setRelevance(false);
// 将Tab页添加到TabPane中 // 将Tab页添加到TabPane中
JNotepadTabPane.getInstance().addNewTab(new JNotepadTab(UIResourceBundle.getContent(TextConstants.NEW_FILE) UiUtil.getJnotepadTabPane().addNewTab(jNotepadTab);
+ viewManager.selfIncreaseAndGetTabIndex(),
textArea));
// 更新编码信息 // 更新编码信息
UiUtil.getStatusBox().updateEncodingLabel(); UiUtil.getStatusBox().updateEncodingLabel();
} }

View File

@ -11,7 +11,6 @@ import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.tool.UiUtil;
import org.jcnc.jnotepad.ui.LineNumberTextArea; import org.jcnc.jnotepad.ui.LineNumberTextArea;
import org.jcnc.jnotepad.ui.tab.JNotepadTab; import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.jcnc.jnotepad.ui.tab.JNotepadTabPane;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -74,7 +73,7 @@ public class OpenFile implements EventHandler<ActionEvent> {
* *
* @param file 文件对象 * @param file 文件对象
*/ */
protected void openFile(File file) { public void openFile(File file) {
ThreadPoolManager.getThreadPool().submit(createOpenFileTask(file)); ThreadPoolManager.getThreadPool().submit(createOpenFileTask(file));
} }
@ -85,8 +84,6 @@ public class OpenFile implements EventHandler<ActionEvent> {
*/ */
public void getText(File file) { public void getText(File file) {
LineNumberTextArea textArea = createNewTextArea(); LineNumberTextArea textArea = createNewTextArea();
// 设置当前标签页关联本地文件
textArea.setRelevance(true);
// 检测文件编码 // 检测文件编码
Charset encoding = EncodingDetector.detectEncodingCharset(file); Charset encoding = EncodingDetector.detectEncodingCharset(file);
try (BufferedReader reader = new BufferedReader(new FileReader(file, encoding))) { try (BufferedReader reader = new BufferedReader(new FileReader(file, encoding))) {
@ -100,10 +97,11 @@ public class OpenFile implements EventHandler<ActionEvent> {
Platform.runLater(() -> { Platform.runLater(() -> {
textArea.getMainTextArea().setText(text); textArea.getMainTextArea().setText(text);
JNotepadTab tab = createNewTab(file.getName(), textArea, encoding); JNotepadTab tab = createNewTab(file.getName(), textArea, encoding);
// 设置当前标签页关联本地文件
tab.setRelevance(true);
tab.setUserData(file); tab.setUserData(file);
JNotepadTabPane.getInstance().addNewTab(tab); UiUtil.getJnotepadTabPane().addNewTab(tab);
}); });
} catch (IOException ignored) { } catch (IOException ignored) {
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!"); LogUtil.getLogger(this.getClass()).info("已忽视IO异常!");
} }

View File

@ -0,0 +1,89 @@
package org.jcnc.jnotepad.controller.event.handler;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.stage.FileChooser;
import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.UiUtil;
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.slf4j.Logger;
import java.io.File;
/**
* 重命名文件
*
* @author gewuyou
*/
public class RenameFile implements EventHandler<ActionEvent> {
Logger logger = LogUtil.getLogger(this.getClass());
@Override
public void handle(ActionEvent actionEvent) {
// 获取当前标签页
JNotepadTab jnotepadtab = UiUtil.getJnotepadtab();
if (jnotepadtab == null) {
return;
}
// 判断当前是否为关联文件
if (jnotepadtab.isRelevance()) {
// 创建一个文件窗口
FileChooser fileChooser = new FileChooser();
// 设置窗口名称
fileChooser.setTitle("重命名");
// 设置原始文件名称
fileChooser.setInitialFileName(jnotepadtab.getText());
// 获取原始文件对象
File file = (File) jnotepadtab.getUserData();
// 设置打开的目录
fileChooser.setInitialDirectory(new File(file.getParent()));
// 设置文件类型
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("文本文档", "*.txt"));
// 获取应用窗口并绑定
File newFile = fileChooser.showSaveDialog(UiUtil.getAppWindow());
if (newFile != null) {
boolean rename = file.renameTo(newFile);
if (rename) {
jnotepadtab.setText(newFile.getName());
logger.info("文件重命名成功");
} else {
logger.debug("文件重命名失败");
}
}
}
// 如果当前不是关联文件则重命名标签页
else {
TextField textField = new TextField(jnotepadtab.getText());
// 设置文本框尺寸
textField.setPrefSize(120, 12);
// 清空标签页名称
jnotepadtab.setText("");
// 监听 Enter 完成编辑
textField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
jnotepadtab.setText(textField.getText());
// 可选移除 TextField 的图形
jnotepadtab.setGraphic(null);
// 可选恢复标签页的关闭按钮
jnotepadtab.setClosable(true);
}
});
// 监听失去焦点事件完成编辑
textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
if (Boolean.FALSE.equals(newValue)) {
jnotepadtab.setText(textField.getText());
// 可选移除 TextField 的图形
jnotepadtab.setGraphic(null);
// 可选恢复标签页的关闭按钮
jnotepadtab.setClosable(true);
}
});
jnotepadtab.setClosable(false);
// 设置 TextField 作为标签页的图形
jnotepadtab.setGraphic(textField);
}
}
}

View File

@ -7,7 +7,6 @@ import org.jcnc.jnotepad.controller.config.AppConfigController;
import org.jcnc.jnotepad.controller.i18n.LocalizationController; import org.jcnc.jnotepad.controller.i18n.LocalizationController;
import org.jcnc.jnotepad.tool.LogUtil; import org.jcnc.jnotepad.tool.LogUtil;
import org.jcnc.jnotepad.tool.UiUtil; import org.jcnc.jnotepad.tool.UiUtil;
import org.jcnc.jnotepad.ui.LineNumberTextArea;
import org.jcnc.jnotepad.ui.tab.JNotepadTab; import org.jcnc.jnotepad.ui.tab.JNotepadTab;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -36,10 +35,8 @@ public class SaveFile implements EventHandler<ActionEvent> {
if (selectedTab == null) { if (selectedTab == null) {
return; return;
} }
// 获取当前Tab页的文本编辑区
LineNumberTextArea textArea = (LineNumberTextArea) selectedTab.getContent();
// 打开的是非关联文件则调用另存为api // 打开的是非关联文件则调用另存为api
if (!textArea.isRelevance()) { if (!selectedTab.isRelevance()) {
logger.info("当前保存文件为非关联打开文件,调用另存为方法"); logger.info("当前保存文件为非关联打开文件,调用另存为方法");
saveTab(this.getClass()); saveTab(this.getClass());
} else { } else {
@ -80,7 +77,7 @@ public class SaveFile implements EventHandler<ActionEvent> {
LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName()); LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName());
selectedTab.save(); selectedTab.save();
// 将保存后的文件设置为已关联 // 将保存后的文件设置为已关联
selectedTab.getLineNumberTextArea().setRelevance(true); selectedTab.setRelevance(true);
// 更新Tab页标签上的文件名 // 更新Tab页标签上的文件名
selectedTab.setText(file.getName()); selectedTab.setText(file.getName());
// 将文件对象保存到Tab页的UserData中 // 将文件对象保存到Tab页的UserData中

View File

@ -28,7 +28,6 @@ public class Controller implements ControllerInterface {
* 打开关联文件并创建文本区域 * 打开关联文件并创建文本区域
* *
* @param rawParameters 原始参数列表 * @param rawParameters 原始参数列表
* @return 创建的文本区域
*/ */
@Override @Override
public void openAssociatedFileAndCreateTextArea(List<String> rawParameters) { public void openAssociatedFileAndCreateTextArea(List<String> rawParameters) {
@ -49,7 +48,7 @@ public class Controller implements ControllerInterface {
public void openAssociatedFile(String filePath) { public void openAssociatedFile(String filePath) {
File file = new File(filePath); File file = new File(filePath);
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {
new OpenFile().createOpenFileTask(file); new OpenFile().openFile(file);
} }
} }
} }

View File

@ -9,8 +9,6 @@ import javafx.scene.control.Alert;
*/ */
public class PopUpUtil { public class PopUpUtil {
private static final ThreadLocal<Alert> ERROR_ALERTS = ThreadLocal.withInitial(() -> new Alert(Alert.AlertType.ERROR));
private PopUpUtil() { private PopUpUtil() {
} }
@ -22,11 +20,11 @@ public class PopUpUtil {
* @param message 信息 * @param message 信息
*/ */
public static void errorAlert(String title, String headerText, String message) { public static void errorAlert(String title, String headerText, String message) {
Alert alert = ERROR_ALERTS.get(); Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title); alert.setTitle(title);
alert.setHeaderText(headerText); alert.setHeaderText(headerText);
alert.setContentText(message); alert.setContentText(message);
alert.showAndWait(); alert.showAndWait();
ERROR_ALERTS.remove();
} }
} }

View File

@ -18,10 +18,6 @@ import java.io.IOException;
* @author 许轲 * @author 许轲
*/ */
public class LineNumberTextArea extends BorderPane { public class LineNumberTextArea extends BorderPane {
/**
* 是否与本地文件关联
*/
private boolean isRelevance = false;
private final TextArea mainTextArea; private final TextArea mainTextArea;
private final TextArea lineNumberArea; private final TextArea lineNumberArea;
@ -91,15 +87,6 @@ public class LineNumberTextArea extends BorderPane {
} }
} }
} }
public boolean isRelevance() {
return isRelevance;
}
public void setRelevance(boolean relevance) {
isRelevance = relevance;
}
private void updateLineNumberWidth() { private void updateLineNumberWidth() {
int numOfLines = mainTextArea.getParagraphs().size(); int numOfLines = mainTextArea.getParagraphs().size();
int count = 1; int count = 1;

View File

@ -75,6 +75,10 @@ public class JNotepadMenuBar extends MenuBar {
* 保存 * 保存
*/ */
private MenuItem saveItem; private MenuItem saveItem;
/**
* 重命名
*/
private MenuItem renameItem;
/** /**
* 增加 * 增加
*/ */
@ -207,7 +211,11 @@ public class JNotepadMenuBar extends MenuBar {
UIResourceBundle.bindStringProperty(saveAsItem.textProperty(), SAVE_AS); UIResourceBundle.bindStringProperty(saveAsItem.textProperty(), SAVE_AS);
itemMap.put("saveAsItem", saveAsItem); itemMap.put("saveAsItem", saveAsItem);
fileMenu.getItems().addAll(newItem, openItem, saveItem, saveAsItem); renameItem = new MenuItem();
UIResourceBundle.bindStringProperty(renameItem.textProperty(), RENAME);
itemMap.put("renameItem", renameItem);
fileMenu.getItems().addAll(newItem, openItem, saveItem, saveAsItem, renameItem);
} }
/** /**
@ -269,6 +277,7 @@ public class JNotepadMenuBar extends MenuBar {
saveItem.setOnAction(new SaveFile()); saveItem.setOnAction(new SaveFile());
saveAsItem.setOnAction(new SaveAsFile()); saveAsItem.setOnAction(new SaveAsFile());
openConfigItem.setOnAction(new OpenConfig()); openConfigItem.setOnAction(new OpenConfig());
renameItem.setOnAction(new RenameFile());
lineFeedItem.selectedProperty().addListener((observableValue, before, after) -> { lineFeedItem.selectedProperty().addListener((observableValue, before, after) -> {
// 1. 更新全局配置 // 1. 更新全局配置
AppConfigController.getInstance().setAutoLineConfig(after); AppConfigController.getInstance().setAutoLineConfig(after);

View File

@ -18,9 +18,22 @@ public class JNotepadTab extends Tab {
* 默认关闭自动换行 * 默认关闭自动换行
*/ */
private boolean autoLine = false; private boolean autoLine = false;
/**
* 是否与本地文件关联
*/
private boolean isRelevance = false;
private final LineNumberTextArea lineNumberTextArea; private final LineNumberTextArea lineNumberTextArea;
private Charset charset = Charset.defaultCharset(); private Charset charset = Charset.defaultCharset();
public boolean isRelevance() {
return isRelevance;
}
public void setRelevance(boolean relevance) {
isRelevance = relevance;
}
public JNotepadTab(String tabTitle) { public JNotepadTab(String tabTitle) {
this(tabTitle, new LineNumberTextArea()); this(tabTitle, new LineNumberTextArea());
} }

View File

@ -10,6 +10,7 @@ CHINESE=中文
title=JNotepad title=JNotepad
OPEN=打开 OPEN=打开
OPEN_CONFIGURATION_FILE=打开配置文件 OPEN_CONFIGURATION_FILE=打开配置文件
RENAME=重命名
TOP=窗口置顶 TOP=窗口置顶
WORD_WRAP=自动换行 WORD_WRAP=自动换行
WORD_COUNT=字数 WORD_COUNT=字数

View File

@ -10,6 +10,7 @@ CHINESE=Chinese
title=JNotepad title=JNotepad
OPEN=Open OPEN=Open
OPEN_CONFIGURATION_FILE=Open Configuration File OPEN_CONFIGURATION_FILE=Open Configuration File
RENAME=Rename
TOP=Window Top TOP=Window Top
WORD_WRAP=Word Wrap WORD_WRAP=Word Wrap
WORD_COUNT=Word Count WORD_COUNT=Word Count

View File

@ -10,6 +10,7 @@ CHINESE=中文
title=JNotepad title=JNotepad
OPEN=打开 OPEN=打开
OPEN_CONFIGURATION_FILE=打开配置文件 OPEN_CONFIGURATION_FILE=打开配置文件
RENAME=重命名
TOP=窗口置顶 TOP=窗口置顶
WORD_WRAP=自动换行 WORD_WRAP=自动换行
WORD_COUNT=字数 WORD_COUNT=字数