diff --git a/.github/ISSUE_TEMPLATE/issue-template-bug.md b/.github/ISSUE_TEMPLATE/issue-template-bug.md index 0e7712d..52acf4c 100644 --- a/.github/ISSUE_TEMPLATE/issue-template-bug.md +++ b/.github/ISSUE_TEMPLATE/issue-template-bug.md @@ -12,7 +12,7 @@ assignees: '' ### Desired outcome | 期望的结果 -### MagicCalculator version | MagicCalculator 版本 +### MagicEncoding version | MagicEncoding 版本 ### Operating system version | 操作系统版本 diff --git a/.github/ISSUE_TEMPLATE/template-question.md b/.github/ISSUE_TEMPLATE/template-question.md index 7d2dbcd..95c954a 100644 --- a/.github/ISSUE_TEMPLATE/template-question.md +++ b/.github/ISSUE_TEMPLATE/template-question.md @@ -12,7 +12,7 @@ assignees: '' ### Desired outcome | 期望的结果 -### MagicCalculator version | MagicCalculator 版本 +### MagicEncoding version | MagicEncoding 版本 ### Operating system version | 操作系统版本 diff --git a/README.md b/README.md index 033f222..02e84e2 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,18 @@ https://github.com/ZZHow1024/MagicEncoding/releases - 选择 .zip压缩包 / .exe安装包 / .msi安装包。 - 下载对应的文件。 - Linux 和 macOS 需要执行安装操作后再运行,Windows 可直接运行 .zip 压缩包中的 .exe 可执行程序或选择 .exe 安装包与 .msi 安装包执行安装操作。 +- 启动 MagicEncoding。 +- 拖拽文件夹获取路径,填入需要筛选的后缀名(多种后缀名用“&”分隔),单击“查找文件”。 +- 选择原始编码与目标编码,单击“开始转换”。 --- ## 功能介绍 +- MagicEncoding2.0.0 + - 拖拽文件夹获取路径。 + - 查找多种后缀名。 + - 支持覆盖原文件。 - MagicEncoding1.0.0 - "GBK" 与 "UTF-8" 之间的编码转换。 - 根据路径查找文件。 @@ -44,6 +51,10 @@ https://github.com/ZZHow1024/MagicEncoding/releases ## **效果图** +![MagicEncoding2.0.0](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F4b165318-6383-451c-8845-110b786c9f0a%2Fcea3d468-2198-4819-a5c3-cb4f3f287dfa%2FMagicEncoding2.0.0.png?table=block&id=34817b71-f9d4-42b9-91e9-a477ad85b262&t=34817b71-f9d4-42b9-91e9-a477ad85b262&width=1624&cache=v2) + +MagicEncoding2.0.0 + ![MagicEncoding1.0.0](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F4b165318-6383-451c-8845-110b786c9f0a%2F94657332-79a5-4e54-b697-8979784a6da3%2FMagicEncoding1.0.0.png?table=block&id=c8dd391f-0e31-455c-8dc3-2b154b44605f&t=c8dd391f-0e31-455c-8dc3-2b154b44605f&width=529&cache=v2) MagicEncoding1.0.0 diff --git a/README_EN.md b/README_EN.md index 1e2fe8e..7686c9b 100644 --- a/README_EN.md +++ b/README_EN.md @@ -30,11 +30,18 @@ https://github.com/ZZHow1024/MagicEncoding/releases - Select .zip package/.exe installation package/.msi installation package. - Download the corresponding file. - Linux and macOS users need to perform the installation operation before running. Windows users can directly run the .exe executable program in the .zip compressed package or select the .exe installation package and .msi installation package to perform the installation operation. +- Start MagicEncoding. +- Drag the folder to obtain the path, enter the suffix name to be filtered (separate multiple suffix names with "&"), and click Find File. +- Select the original encoding and target encoding, and click "Start Conversion". --- ## Function Introduction +- MagicEncoding2.0.0 + - Drag the folder to get the path. + - Search for multiple suffixes. + - Support overwriting original files. - MagicEncoding1.0.0 - Encoding conversion between "GBK" and "UTF-8”. - Find files by path. @@ -44,6 +51,10 @@ https://github.com/ZZHow1024/MagicEncoding/releases ## **Renderings** +![MagicEncoding2.0.0](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F4b165318-6383-451c-8845-110b786c9f0a%2Fbf42de01-57d9-4f5d-bb7c-3490b0db7ee1%2FMagicEncoding2.0.0.png?table=block&id=7ce8da63-3633-47ff-848c-94492101089c&t=7ce8da63-3633-47ff-848c-94492101089c&width=1624&cache=v2) + +MagicEncoding2.0.0 + ![MagicEncoding1.0.0](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F4b165318-6383-451c-8845-110b786c9f0a%2Fa91b3a7d-672f-4ea2-b167-4657fef038dc%2FMagicEncoding1.0.0.png?table=block&id=ed5e4ef9-7816-4961-9740-d423c5681168&t=ed5e4ef9-7816-4961-9740-d423c5681168&width=1504&cache=v2) MagicEncoding1.0.0 diff --git a/src/main/java/com/zzhow/magicencoding/controller/MainController.java b/src/main/java/com/zzhow/magicencoding/controller/MainController.java index 590a6b1..01454c3 100644 --- a/src/main/java/com/zzhow/magicencoding/controller/MainController.java +++ b/src/main/java/com/zzhow/magicencoding/controller/MainController.java @@ -5,9 +5,12 @@ import com.zzhow.magicencoding.ui.About; import com.zzhow.magicencoding.utils.MessageBox; import javafx.fxml.FXML; -import javafx.scene.control.ChoiceBox; -import javafx.scene.control.ListView; -import javafx.scene.control.TextField; +import javafx.scene.control.*; +import javafx.scene.input.DragEvent; +import javafx.scene.input.TransferMode; + +import java.io.File; +import java.util.List; public class MainController { @@ -24,17 +27,39 @@ public class MainController { private TextField endWithTextField; @FXML private ListView filesListView; + @FXML + private Label fileNumber; + @FXML + private CheckBox isOverwriteCheckBox; - public void initialize() { + public void clearFilesPath() { + fileService.clearTargetFileList(); + filesListView.setItems(null); + } + + @FXML + private void initialize() { originChoiceBox.getItems().addAll("GBK", "UTF-8"); originChoiceBox.setValue("GBK"); targetChoiceBox.getItems().addAll("UTF-8", "GBK"); targetChoiceBox.setValue("UTF-8"); } - public void clearFilesPath() { - fileService.clearTargetFileList(); - filesListView.setItems(null); + @FXML + private void handleDragOver(DragEvent event) { + if (event.getGestureSource() != event.getTarget() // 是否从外部拖拽 + && event.getDragboard().hasFiles()) { // 是否拖拽了文件 + event.acceptTransferModes(TransferMode.COPY_OR_MOVE); // 接受拖拽的文件 + } + event.consume(); + } + + @FXML + private void onDragFile(DragEvent event) { + List files = event.getDragboard().getFiles(); + if (!files.isEmpty()) { + pathTextField.setText(files.get(0).getAbsolutePath()); + } } @FXML @@ -42,6 +67,7 @@ private void onReset() { this.clearFilesPath(); pathTextField.setText(""); endWithTextField.setText(""); + fileNumber.setText("文件数目:0"); } @FXML @@ -51,6 +77,7 @@ private void onFindFiles() { String endWith = endWithTextField.getText(); filesListView.setItems(fileService.findFiles(absolutePath, endWith)); + fileNumber.setText("文件数目:" + fileService.getTargetFileList().size()); } @FXML @@ -58,8 +85,9 @@ private void onTransform() { String absolutePath = pathTextField.getText(); String originCharset = originChoiceBox.getValue(); String targetCharset = targetChoiceBox.getValue(); + boolean isOverwrite = this.isOverwriteCheckBox.isSelected(); - if (fileService.transform(absolutePath, originCharset, targetCharset)) { + if (fileService.transform(absolutePath, originCharset, targetCharset, isOverwrite)) { MessageBox.success("执行成功", "已将" + fileService.getTargetFileList().size() + "个文件从 \"" + originCharset + "\" 转为 \"" + targetCharset + "\""); } else { diff --git a/src/main/java/com/zzhow/magicencoding/service/FileService.java b/src/main/java/com/zzhow/magicencoding/service/FileService.java index daad86e..c2799a3 100644 --- a/src/main/java/com/zzhow/magicencoding/service/FileService.java +++ b/src/main/java/com/zzhow/magicencoding/service/FileService.java @@ -39,6 +39,6 @@ public interface FileService { * @param targetCharset 目标字符集 * @return true - 转换成功,false - 转换失败 */ - boolean transform(String absolutePath, String originCharset, String targetCharset); + boolean transform(String absolutePath, String originCharset, String targetCharset, boolean isOverwrite); } diff --git a/src/main/java/com/zzhow/magicencoding/service/impl/FileServiceImpl.java b/src/main/java/com/zzhow/magicencoding/service/impl/FileServiceImpl.java index 1bdb068..1b1be13 100644 --- a/src/main/java/com/zzhow/magicencoding/service/impl/FileServiceImpl.java +++ b/src/main/java/com/zzhow/magicencoding/service/impl/FileServiceImpl.java @@ -6,7 +6,9 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import java.awt.*; import java.io.File; +import java.io.IOException; import java.util.ArrayList; /** @@ -38,23 +40,28 @@ public ObservableList findFiles(String absolutePath, String endWith) { File[] files = currentFolder.listFiles(); if (files == null || files.length == 0) { - MessageBox.error("当前文件夹下没有满足条件的文件", "请检查设置的条件"); + MessageBox.error("当前路径下没有满足条件的文件", "请检查设置的条件"); return null; } - MyFiles.find(absolutePath, endWith, targetFileList); + String[] split = endWith.split("&"); + for (String s : split) { + MyFiles.find(absolutePath, s, targetFileList); + } // 打印满足条件的文件的绝对路径 - for (String string : targetFileList) { - System.out.println(string); - } +// for (String string : targetFileList) { +// System.out.println(string); +// } return FXCollections.observableList(targetFileList); } @Override - public boolean transform(String absolutePath, String originCharset, String targetCharset) { + public boolean transform(String absolutePath, String originCharset, String targetCharset, boolean isOverwrite) { + absolutePath = absolutePath.replace("\\", "/"); + if (targetFileList.isEmpty()) { MessageBox.error("当前没有命中的文件", "请先查找文件"); @@ -62,7 +69,7 @@ public boolean transform(String absolutePath, String originCharset, String targe } // 开始转换编码 - String outputPath = absolutePath + "/MagicEncodingOutput"; + String outputPath = absolutePath + "/" + "MagicEncodingOutput"; File outputFolder = new File(outputPath); if (outputFolder.exists()) { MyFiles.deleteFolder(outputPath); @@ -76,8 +83,25 @@ public boolean transform(String absolutePath, String originCharset, String targe } for (String originPath : targetFileList) { + originPath = originPath.replace("\\", "/"); String targetPath = outputPath + originPath.split(absolutePath)[1]; MyFiles.transform(originPath, targetPath, originCharset, targetCharset); + if (isOverwrite) + MyFiles.overwriteFile(targetPath, originPath); + } + + if (isOverwrite) { + MyFiles.deleteFolder(outputPath); + outputFolder.delete(); + } else { + if (Desktop.isDesktopSupported()) { + Desktop desktop = Desktop.getDesktop(); + try { + desktop.open(outputFolder); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } } return true; diff --git a/src/main/java/com/zzhow/magicencoding/ui/About.java b/src/main/java/com/zzhow/magicencoding/ui/About.java index 575b3f0..e71ebcd 100644 --- a/src/main/java/com/zzhow/magicencoding/ui/About.java +++ b/src/main/java/com/zzhow/magicencoding/ui/About.java @@ -17,7 +17,7 @@ public class About { public static void open() { Stage stage = new Stage(); - stage.setTitle("MagicEncoding 1.0.0 - About"); + stage.setTitle("MagicEncoding 2.0.0 - About"); Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png"))); stage.getIcons().add(icon); stage.setResizable(false); diff --git a/src/main/java/com/zzhow/magicencoding/ui/Application.java b/src/main/java/com/zzhow/magicencoding/ui/Application.java index 89276a2..14ec309 100644 --- a/src/main/java/com/zzhow/magicencoding/ui/Application.java +++ b/src/main/java/com/zzhow/magicencoding/ui/Application.java @@ -13,7 +13,7 @@ public class Application extends javafx.application.Application { @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("main-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 640, 480); + Scene scene = new Scene(fxmlLoader.load(), 700, 535); stage.setTitle("MagicEncoding"); stage.setScene(scene); Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png"))); diff --git a/src/main/java/com/zzhow/magicencoding/utils/MessageBox.java b/src/main/java/com/zzhow/magicencoding/utils/MessageBox.java index 2788c15..20cc47d 100644 --- a/src/main/java/com/zzhow/magicencoding/utils/MessageBox.java +++ b/src/main/java/com/zzhow/magicencoding/utils/MessageBox.java @@ -1,6 +1,11 @@ package com.zzhow.magicencoding.utils; +import com.zzhow.magicencoding.MainClass; import javafx.scene.control.Alert; +import javafx.scene.image.Image; +import javafx.stage.Stage; + +import java.util.Objects; /** * @author ZZHow @@ -14,25 +19,25 @@ public static void alert(Alert.AlertType type, String title, String headerText, alert.setHeaderText(headerText); alert.setContentText(contentText); + Stage stage = (Stage) alert.getDialogPane().getScene().getWindow(); + Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png"))); + stage.getIcons().add(icon); + alert.showAndWait(); } public static void error(String headerText, String contentText) { - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle("错误"); - alert.setHeaderText(headerText); - alert.setContentText(contentText); - - alert.showAndWait(); + alert(Alert.AlertType.ERROR, + "错误", + headerText, + contentText); } public static void success(String headerText, String contentText) { - Alert alert = new Alert(Alert.AlertType.INFORMATION); - alert.setTitle("成功"); - alert.setHeaderText(headerText); - alert.setContentText(contentText); - - alert.showAndWait(); + alert(Alert.AlertType.INFORMATION, + "成功", + headerText, + contentText); } } diff --git a/src/main/java/com/zzhow/magicencoding/utils/MyFiles.java b/src/main/java/com/zzhow/magicencoding/utils/MyFiles.java index 2063d6f..be1c90a 100644 --- a/src/main/java/com/zzhow/magicencoding/utils/MyFiles.java +++ b/src/main/java/com/zzhow/magicencoding/utils/MyFiles.java @@ -48,7 +48,7 @@ public static void transform(String originPath, String targetPath, String origin bufferedWriter.newLine(); } - System.out.println(originPath + "转换成功"); +// System.out.println(originPath + "转换成功"); } catch (IOException e) { System.out.println(e.getMessage()); } @@ -72,4 +72,17 @@ public static void deleteFolder(String path) { } } } + + public static void overwriteFile(String originPath, String targetPath) { + try (FileInputStream fileInputStream = new FileInputStream(originPath); + FileOutputStream fileOutputStream = new FileOutputStream(targetPath, false)) { + byte[] buffer = new byte[1024]; + int length; + while ((length = fileInputStream.read(buffer)) != -1) { + fileOutputStream.write(buffer, 0, length); + } + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index d8c29ed..3103e52 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,6 +1,7 @@ module com.zzhow.magicencoding { requires javafx.controls; requires javafx.fxml; + requires java.desktop; opens com.zzhow.magicencoding to javafx.fxml; diff --git a/src/main/resources/com/zzhow/magicencoding/ui/about-view.fxml b/src/main/resources/com/zzhow/magicencoding/ui/about-view.fxml index 983899d..fb1073d 100644 --- a/src/main/resources/com/zzhow/magicencoding/ui/about-view.fxml +++ b/src/main/resources/com/zzhow/magicencoding/ui/about-view.fxml @@ -6,32 +6,35 @@ - + - + - diff --git a/src/main/resources/com/zzhow/magicencoding/ui/main-view.fxml b/src/main/resources/com/zzhow/magicencoding/ui/main-view.fxml index 29bdcc6..1cd3482 100644 --- a/src/main/resources/com/zzhow/magicencoding/ui/main-view.fxml +++ b/src/main/resources/com/zzhow/magicencoding/ui/main-view.fxml @@ -2,6 +2,7 @@ + @@ -9,32 +10,38 @@ - + - + - + -