!2 refactor: #I7XMJG 工厂改为单例模式

Merge pull request !2 from songdragon/refactor-factory
This commit is contained in:
格物方能致知 2023-08-31 15:10:30 +00:00 committed by Gitee
commit f9a9cb99f8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 28 additions and 21 deletions

View File

@ -40,9 +40,8 @@ public class OpenFile implements EventHandler<ActionEvent> {
*/ */
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
FileChooserFactory factory = new TextFileChooserFactory();
// 显示文件选择对话框并获取选中的文件 // 显示文件选择对话框并获取选中的文件
File file = factory.createFileChooser( File file = TextFileChooserFactory.getInstance().createFileChooser(
UiResourceBundle.getContent(TextConstants.OPEN), UiResourceBundle.getContent(TextConstants.OPEN),
null, null,
null, null,

View File

@ -88,12 +88,10 @@ public class RenameFile implements EventHandler<ActionEvent> {
* @since 2023/8/31 21:47 * @since 2023/8/31 21:47
*/ */
private void handleRenameRelevanceFile(JNotepadTab jnotepadtab) { private void handleRenameRelevanceFile(JNotepadTab jnotepadtab) {
// 创建文件选择器对话框工厂选择文本文件对话框工厂
FileChooserFactory factory = new TextFileChooserFactory();
// 获取原始文件对象 // 获取原始文件对象
File file = (File) jnotepadtab.getUserData(); File file = (File) jnotepadtab.getUserData();
// 获取应用窗口并绑定 // 获取应用窗口并绑定
File newFile = factory File newFile = TextFileChooserFactory.getInstance()
.createFileChooser( .createFileChooser(
UiResourceBundle.getContent(TextConstants.RENAME), UiResourceBundle.getContent(TextConstants.RENAME),
jnotepadtab.getText(), jnotepadtab.getText(),

View File

@ -67,23 +67,23 @@ public class SaveFile implements EventHandler<ActionEvent> {
* @see LogUtil * @see LogUtil
*/ */
protected void saveTab(Class<?> currentClass) { protected void saveTab(Class<?> currentClass) {
FileChooserFactory factory = new TextFileChooserFactory();
JNotepadTab selectedTab = UiUtil.getJnotepadtab(); JNotepadTab selectedTab = UiUtil.getJnotepadtab();
if (selectedTab != null) { if (selectedTab == null) {
File file = factory.createFileChooser( return;
UiResourceBundle.getContent(TextConstants.SAVE_AS), }
selectedTab.getText(), File file = TextFileChooserFactory.getInstance().createFileChooser(
null, UiResourceBundle.getContent(TextConstants.SAVE_AS),
new FileChooser.ExtensionFilter("All types", "*.*")) selectedTab.getText(),
.showSaveDialog(UiUtil.getAppWindow()); null,
if (file != null) { new FileChooser.ExtensionFilter("All types", "*.*"))
LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName()); .showSaveDialog(UiUtil.getAppWindow());
selectedTab.save(file); if (file != null) {
// 将保存后的文件设置为已关联 LogUtil.getLogger(currentClass).info("正在保存文件:{}", file.getName());
selectedTab.setRelevance(true); selectedTab.save(file);
// 更新Tab页标签上的文件名 // 将保存后的文件设置为已关联
selectedTab.setText(file.getName()); selectedTab.setRelevance(true);
} // 更新Tab页标签上的文件名
selectedTab.setText(file.getName());
} }
} }
} }

View File

@ -13,6 +13,16 @@ import java.io.File;
*/ */
public class TextFileChooserFactory implements FileChooserFactory { public class TextFileChooserFactory implements FileChooserFactory {
private TextFileChooserFactory() {
}
private static final TextFileChooserFactory INSTANCE = new TextFileChooserFactory();
public static TextFileChooserFactory getInstance() {
return INSTANCE;
}
/** /**
* 创建原始文件选择对话框 * 创建原始文件选择对话框
* *