🚩 添加重命名功能
This commit is contained in:
parent
c1fe25e2bb
commit
6164698dd7
@ -20,6 +20,8 @@ public class TextConstants {
|
||||
public static final String NEW = "NEW";
|
||||
public static final String OPEN = "OPEN";
|
||||
public static final String SAVE_AS = "SAVE_AS";
|
||||
|
||||
public static final String RENAME = "RENAME";
|
||||
public static final String SET = "SET";
|
||||
public static final String WORD_WRAP = "WORD_WRAP";
|
||||
public static final String PLUGIN = "PLUGIN";
|
||||
@ -37,8 +39,14 @@ public class TextConstants {
|
||||
public static final String ENCODE = "ENCODE";
|
||||
|
||||
/// Config 文本常量
|
||||
/**
|
||||
* 英文小写
|
||||
*/
|
||||
public static final String ENGLISH = "english";
|
||||
|
||||
/**
|
||||
* 中文小写
|
||||
*/
|
||||
public static final String CHINESE = "chinese";
|
||||
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@ import org.jcnc.jnotepad.constants.TextConstants;
|
||||
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.JNotepadTabPane;
|
||||
import org.jcnc.jnotepad.view.manager.ViewManager;
|
||||
|
||||
|
||||
@ -28,21 +27,22 @@ public class NewFile implements EventHandler<ActionEvent> {
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
addNewFileTab();
|
||||
|
||||
}
|
||||
|
||||
public void addNewFileTab() {
|
||||
// 创建一个新的文本编辑区
|
||||
LineNumberTextArea textArea = new LineNumberTextArea();
|
||||
// 设置当前标签页与本地文件无关联
|
||||
textArea.setRelevance(false);
|
||||
// TODO: refactor:统一TextArea新建、绑定监听器入口
|
||||
ViewManager viewManager = UiUtil.getViewManager();
|
||||
// 创建标签页
|
||||
JNotepadTab jNotepadTab = new JNotepadTab(
|
||||
UIResourceBundle.getContent(TextConstants.NEW_FILE)
|
||||
+ viewManager.selfIncreaseAndGetTabIndex(),
|
||||
textArea);
|
||||
// 设置当前标签页与本地文件无关联
|
||||
jNotepadTab.setRelevance(false);
|
||||
// 将Tab页添加到TabPane中
|
||||
JNotepadTabPane.getInstance().addNewTab(new JNotepadTab(UIResourceBundle.getContent(TextConstants.NEW_FILE)
|
||||
+ viewManager.selfIncreaseAndGetTabIndex(),
|
||||
textArea));
|
||||
|
||||
UiUtil.getJnotepadTabPane().addNewTab(jNotepadTab);
|
||||
// 更新编码信息
|
||||
UiUtil.getStatusBox().updateEncodingLabel();
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@ import org.jcnc.jnotepad.tool.LogUtil;
|
||||
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.JNotepadTabPane;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
@ -85,8 +84,6 @@ public class OpenFile implements EventHandler<ActionEvent> {
|
||||
*/
|
||||
public void getText(File file) {
|
||||
LineNumberTextArea textArea = createNewTextArea();
|
||||
// 设置当前标签页关联本地文件
|
||||
textArea.setRelevance(true);
|
||||
// 检测文件编码
|
||||
Charset encoding = EncodingDetector.detectEncodingCharset(file);
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file, encoding))) {
|
||||
@ -100,10 +97,11 @@ public class OpenFile implements EventHandler<ActionEvent> {
|
||||
Platform.runLater(() -> {
|
||||
textArea.getMainTextArea().setText(text);
|
||||
JNotepadTab tab = createNewTab(file.getName(), textArea, encoding);
|
||||
// 设置当前标签页关联本地文件
|
||||
tab.setRelevance(true);
|
||||
tab.setUserData(file);
|
||||
JNotepadTabPane.getInstance().addNewTab(tab);
|
||||
UiUtil.getJnotepadTabPane().addNewTab(tab);
|
||||
});
|
||||
|
||||
} catch (IOException ignored) {
|
||||
LogUtil.getLogger(this.getClass()).info("已忽视IO异常!");
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,6 @@ import org.jcnc.jnotepad.controller.config.AppConfigController;
|
||||
import org.jcnc.jnotepad.controller.i18n.LocalizationController;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.tool.UiUtil;
|
||||
import org.jcnc.jnotepad.ui.LineNumberTextArea;
|
||||
import org.jcnc.jnotepad.ui.tab.JNotepadTab;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@ -36,10 +35,8 @@ public class SaveFile implements EventHandler<ActionEvent> {
|
||||
if (selectedTab == null) {
|
||||
return;
|
||||
}
|
||||
// 获取当前Tab页的文本编辑区
|
||||
LineNumberTextArea textArea = (LineNumberTextArea) selectedTab.getContent();
|
||||
// 打开的是非关联文件,则调用另存为api
|
||||
if (!textArea.isRelevance()) {
|
||||
if (!selectedTab.isRelevance()) {
|
||||
logger.info("当前保存文件为非关联打开文件,调用另存为方法");
|
||||
saveTab(this.getClass());
|
||||
} else {
|
||||
@ -80,7 +77,7 @@ public class SaveFile implements EventHandler<ActionEvent> {
|
||||
LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName());
|
||||
selectedTab.save();
|
||||
// 将保存后的文件设置为已关联
|
||||
selectedTab.getLineNumberTextArea().setRelevance(true);
|
||||
selectedTab.setRelevance(true);
|
||||
// 更新Tab页标签上的文件名
|
||||
selectedTab.setText(file.getName());
|
||||
// 将文件对象保存到Tab页的UserData中
|
||||
|
||||
@ -28,7 +28,6 @@ public class Controller implements ControllerInterface {
|
||||
* 打开关联文件并创建文本区域。
|
||||
*
|
||||
* @param rawParameters 原始参数列表
|
||||
* @return 创建的文本区域
|
||||
*/
|
||||
@Override
|
||||
public void openAssociatedFileAndCreateTextArea(List<String> rawParameters) {
|
||||
|
||||
@ -9,8 +9,6 @@ import javafx.scene.control.Alert;
|
||||
*/
|
||||
public class PopUpUtil {
|
||||
|
||||
private static final ThreadLocal<Alert> ERROR_ALERTS = ThreadLocal.withInitial(() -> new Alert(Alert.AlertType.ERROR));
|
||||
|
||||
private PopUpUtil() {
|
||||
}
|
||||
|
||||
@ -22,11 +20,11 @@ public class PopUpUtil {
|
||||
* @param 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.setHeaderText(headerText);
|
||||
alert.setContentText(message);
|
||||
alert.showAndWait();
|
||||
ERROR_ALERTS.remove();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -18,10 +18,6 @@ import java.io.IOException;
|
||||
* @author 许轲
|
||||
*/
|
||||
public class LineNumberTextArea extends BorderPane {
|
||||
/**
|
||||
* 是否与本地文件关联
|
||||
*/
|
||||
private boolean isRelevance = false;
|
||||
private final TextArea mainTextArea;
|
||||
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() {
|
||||
int numOfLines = mainTextArea.getParagraphs().size();
|
||||
int count = 1;
|
||||
|
||||
@ -75,6 +75,10 @@ public class JNotepadMenuBar extends MenuBar {
|
||||
* 保存
|
||||
*/
|
||||
private MenuItem saveItem;
|
||||
/**
|
||||
* 重命名
|
||||
*/
|
||||
private MenuItem renameItem;
|
||||
/**
|
||||
* 增加
|
||||
*/
|
||||
@ -207,7 +211,11 @@ public class JNotepadMenuBar extends MenuBar {
|
||||
UIResourceBundle.bindStringProperty(saveAsItem.textProperty(), SAVE_AS);
|
||||
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());
|
||||
saveAsItem.setOnAction(new SaveAsFile());
|
||||
openConfigItem.setOnAction(new OpenConfig());
|
||||
renameItem.setOnAction(new RenameFile());
|
||||
lineFeedItem.selectedProperty().addListener((observableValue, before, after) -> {
|
||||
// 1. 更新全局配置
|
||||
AppConfigController.getInstance().setAutoLineConfig(after);
|
||||
|
||||
@ -18,9 +18,22 @@ public class JNotepadTab extends Tab {
|
||||
* 默认关闭自动换行
|
||||
*/
|
||||
private boolean autoLine = false;
|
||||
/**
|
||||
* 是否与本地文件关联
|
||||
*/
|
||||
private boolean isRelevance = false;
|
||||
|
||||
private final LineNumberTextArea lineNumberTextArea;
|
||||
private Charset charset = Charset.defaultCharset();
|
||||
|
||||
public boolean isRelevance() {
|
||||
return isRelevance;
|
||||
}
|
||||
|
||||
public void setRelevance(boolean relevance) {
|
||||
isRelevance = relevance;
|
||||
}
|
||||
|
||||
public JNotepadTab(String tabTitle) {
|
||||
this(tabTitle, new LineNumberTextArea());
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ CHINESE=中文
|
||||
title=JNotepad
|
||||
OPEN=打开
|
||||
OPEN_CONFIGURATION_FILE=打开配置文件
|
||||
RENAME=重命名
|
||||
TOP=窗口置顶
|
||||
WORD_WRAP=自动换行
|
||||
WORD_COUNT=字数
|
||||
|
||||
@ -10,6 +10,7 @@ CHINESE=Chinese
|
||||
title=JNotepad
|
||||
OPEN=Open
|
||||
OPEN_CONFIGURATION_FILE=Open Configuration File
|
||||
RENAME=Rename
|
||||
TOP=Window Top
|
||||
WORD_WRAP=Word Wrap
|
||||
WORD_COUNT=Word Count
|
||||
|
||||
@ -10,6 +10,7 @@ CHINESE=中文
|
||||
title=JNotepad
|
||||
OPEN=打开
|
||||
OPEN_CONFIGURATION_FILE=打开配置文件
|
||||
RENAME=重命名
|
||||
TOP=窗口置顶
|
||||
WORD_WRAP=自动换行
|
||||
WORD_COUNT=字数
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user