Skip to content

Commit 1466e61

Browse files
authored
Merge pull request #4 from ZZHow1024/develop
MagicEncoding2.0.0
2 parents 437598f + 981c6a7 commit 1466e61

File tree

14 files changed

+163
-60
lines changed

14 files changed

+163
-60
lines changed

Diff for: .github/ISSUE_TEMPLATE/issue-template-bug.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ assignees: ''
1212

1313
### Desired outcome | 期望的结果
1414

15-
### MagicCalculator version | MagicCalculator 版本
15+
### MagicEncoding version | MagicEncoding 版本
1616

1717
### Operating system version | 操作系统版本
1818

Diff for: .github/ISSUE_TEMPLATE/template-question.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ assignees: ''
1212

1313
### Desired outcome | 期望的结果
1414

15-
### MagicCalculator version | MagicCalculator 版本
15+
### MagicEncoding version | MagicEncoding 版本
1616

1717
### Operating system version | 操作系统版本
1818

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ https://github.com/ZZHow1024/MagicEncoding/releases
3030
- 选择 .zip压缩包 / .exe安装包 / .msi安装包。
3131
- 下载对应的文件。
3232
- Linux 和 macOS 需要执行安装操作后再运行,Windows 可直接运行 .zip 压缩包中的 .exe 可执行程序或选择 .exe 安装包与 .msi 安装包执行安装操作。
33+
- 启动 MagicEncoding。
34+
- 拖拽文件夹获取路径,填入需要筛选的后缀名(多种后缀名用“&”分隔),单击“查找文件”。
35+
- 选择原始编码与目标编码,单击“开始转换”。
3336

3437
---
3538

3639
## 功能介绍
3740

41+
- MagicEncoding2.0.0
42+
- 拖拽文件夹获取路径。
43+
- 查找多种后缀名。
44+
- 支持覆盖原文件。
3845
- MagicEncoding1.0.0
3946
- "GBK" 与 "UTF-8" 之间的编码转换。
4047
- 根据路径查找文件。
@@ -44,6 +51,10 @@ https://github.com/ZZHow1024/MagicEncoding/releases
4451

4552
## **效果图**
4653

54+
![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)
55+
56+
MagicEncoding2.0.0
57+
4758
![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)
4859

4960
MagicEncoding1.0.0

Diff for: README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ https://github.com/ZZHow1024/MagicEncoding/releases
3030
- Select .zip package/.exe installation package/.msi installation package.
3131
- Download the corresponding file.
3232
- 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.
33+
- Start MagicEncoding.
34+
- Drag the folder to obtain the path, enter the suffix name to be filtered (separate multiple suffix names with "&"), and click Find File.
35+
- Select the original encoding and target encoding, and click "Start Conversion".
3336

3437
---
3538

3639
## Function Introduction
3740

41+
- MagicEncoding2.0.0
42+
- Drag the folder to get the path.
43+
- Search for multiple suffixes.
44+
- Support overwriting original files.
3845
- MagicEncoding1.0.0
3946
- Encoding conversion between "GBK" and "UTF-8”.
4047
- Find files by path.
@@ -44,6 +51,10 @@ https://github.com/ZZHow1024/MagicEncoding/releases
4451

4552
## **Renderings**
4653

54+
![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)
55+
56+
MagicEncoding2.0.0
57+
4758
![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)
4859

4960
MagicEncoding1.0.0

Diff for: src/main/java/com/zzhow/magicencoding/controller/MainController.java

+36-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import com.zzhow.magicencoding.ui.About;
66
import com.zzhow.magicencoding.utils.MessageBox;
77
import javafx.fxml.FXML;
8-
import javafx.scene.control.ChoiceBox;
9-
import javafx.scene.control.ListView;
10-
import javafx.scene.control.TextField;
8+
import javafx.scene.control.*;
9+
import javafx.scene.input.DragEvent;
10+
import javafx.scene.input.TransferMode;
11+
12+
import java.io.File;
13+
import java.util.List;
1114

1215
public class MainController {
1316

@@ -24,24 +27,47 @@ public class MainController {
2427
private TextField endWithTextField;
2528
@FXML
2629
private ListView<String> filesListView;
30+
@FXML
31+
private Label fileNumber;
32+
@FXML
33+
private CheckBox isOverwriteCheckBox;
2734

28-
public void initialize() {
35+
public void clearFilesPath() {
36+
fileService.clearTargetFileList();
37+
filesListView.setItems(null);
38+
}
39+
40+
@FXML
41+
private void initialize() {
2942
originChoiceBox.getItems().addAll("GBK", "UTF-8");
3043
originChoiceBox.setValue("GBK");
3144
targetChoiceBox.getItems().addAll("UTF-8", "GBK");
3245
targetChoiceBox.setValue("UTF-8");
3346
}
3447

35-
public void clearFilesPath() {
36-
fileService.clearTargetFileList();
37-
filesListView.setItems(null);
48+
@FXML
49+
private void handleDragOver(DragEvent event) {
50+
if (event.getGestureSource() != event.getTarget() // 是否从外部拖拽
51+
&& event.getDragboard().hasFiles()) { // 是否拖拽了文件
52+
event.acceptTransferModes(TransferMode.COPY_OR_MOVE); // 接受拖拽的文件
53+
}
54+
event.consume();
55+
}
56+
57+
@FXML
58+
private void onDragFile(DragEvent event) {
59+
List<File> files = event.getDragboard().getFiles();
60+
if (!files.isEmpty()) {
61+
pathTextField.setText(files.get(0).getAbsolutePath());
62+
}
3863
}
3964

4065
@FXML
4166
private void onReset() {
4267
this.clearFilesPath();
4368
pathTextField.setText("");
4469
endWithTextField.setText("");
70+
fileNumber.setText("文件数目:0");
4571
}
4672

4773
@FXML
@@ -51,15 +77,17 @@ private void onFindFiles() {
5177
String endWith = endWithTextField.getText();
5278

5379
filesListView.setItems(fileService.findFiles(absolutePath, endWith));
80+
fileNumber.setText("文件数目:" + fileService.getTargetFileList().size());
5481
}
5582

5683
@FXML
5784
private void onTransform() {
5885
String absolutePath = pathTextField.getText();
5986
String originCharset = originChoiceBox.getValue();
6087
String targetCharset = targetChoiceBox.getValue();
88+
boolean isOverwrite = this.isOverwriteCheckBox.isSelected();
6189

62-
if (fileService.transform(absolutePath, originCharset, targetCharset)) {
90+
if (fileService.transform(absolutePath, originCharset, targetCharset, isOverwrite)) {
6391
MessageBox.success("执行成功", "已将" + fileService.getTargetFileList().size()
6492
+ "个文件从 \"" + originCharset + "\" 转为 \"" + targetCharset + "\"");
6593
} else {

Diff for: src/main/java/com/zzhow/magicencoding/service/FileService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public interface FileService {
3939
* @param targetCharset 目标字符集
4040
* @return true - 转换成功,false - 转换失败
4141
*/
42-
boolean transform(String absolutePath, String originCharset, String targetCharset);
42+
boolean transform(String absolutePath, String originCharset, String targetCharset, boolean isOverwrite);
4343

4444
}

Diff for: src/main/java/com/zzhow/magicencoding/service/impl/FileServiceImpl.java

+31-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import javafx.collections.FXCollections;
77
import javafx.collections.ObservableList;
88

9+
import java.awt.*;
910
import java.io.File;
11+
import java.io.IOException;
1012
import java.util.ArrayList;
1113

1214
/**
@@ -38,31 +40,36 @@ public ObservableList<String> findFiles(String absolutePath, String endWith) {
3840
File[] files = currentFolder.listFiles();
3941

4042
if (files == null || files.length == 0) {
41-
MessageBox.error("当前文件夹下没有满足条件的文件", "请检查设置的条件");
43+
MessageBox.error("当前路径下没有满足条件的文件", "请检查设置的条件");
4244

4345
return null;
4446
}
4547

46-
MyFiles.find(absolutePath, endWith, targetFileList);
48+
String[] split = endWith.split("&");
49+
for (String s : split) {
50+
MyFiles.find(absolutePath, s, targetFileList);
51+
}
4752

4853
// 打印满足条件的文件的绝对路径
49-
for (String string : targetFileList) {
50-
System.out.println(string);
51-
}
54+
// for (String string : targetFileList) {
55+
// System.out.println(string);
56+
// }
5257

5358
return FXCollections.observableList(targetFileList);
5459
}
5560

5661
@Override
57-
public boolean transform(String absolutePath, String originCharset, String targetCharset) {
62+
public boolean transform(String absolutePath, String originCharset, String targetCharset, boolean isOverwrite) {
63+
absolutePath = absolutePath.replace("\\", "/");
64+
5865
if (targetFileList.isEmpty()) {
5966
MessageBox.error("当前没有命中的文件", "请先查找文件");
6067

6168
return false;
6269
}
6370

6471
// 开始转换编码
65-
String outputPath = absolutePath + "/MagicEncodingOutput";
72+
String outputPath = absolutePath + "/" + "MagicEncodingOutput";
6673
File outputFolder = new File(outputPath);
6774
if (outputFolder.exists()) {
6875
MyFiles.deleteFolder(outputPath);
@@ -76,8 +83,25 @@ public boolean transform(String absolutePath, String originCharset, String targe
7683
}
7784

7885
for (String originPath : targetFileList) {
86+
originPath = originPath.replace("\\", "/");
7987
String targetPath = outputPath + originPath.split(absolutePath)[1];
8088
MyFiles.transform(originPath, targetPath, originCharset, targetCharset);
89+
if (isOverwrite)
90+
MyFiles.overwriteFile(targetPath, originPath);
91+
}
92+
93+
if (isOverwrite) {
94+
MyFiles.deleteFolder(outputPath);
95+
outputFolder.delete();
96+
} else {
97+
if (Desktop.isDesktopSupported()) {
98+
Desktop desktop = Desktop.getDesktop();
99+
try {
100+
desktop.open(outputFolder);
101+
} catch (IOException e) {
102+
System.out.println(e.getMessage());
103+
}
104+
}
81105
}
82106

83107
return true;

Diff for: src/main/java/com/zzhow/magicencoding/ui/About.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class About {
1818
public static void open() {
1919
Stage stage = new Stage();
20-
stage.setTitle("MagicEncoding 1.0.0 - About");
20+
stage.setTitle("MagicEncoding 2.0.0 - About");
2121
Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png")));
2222
stage.getIcons().add(icon);
2323
stage.setResizable(false);

Diff for: src/main/java/com/zzhow/magicencoding/ui/Application.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Application extends javafx.application.Application {
1313
@Override
1414
public void start(Stage stage) throws IOException {
1515
FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("main-view.fxml"));
16-
Scene scene = new Scene(fxmlLoader.load(), 640, 480);
16+
Scene scene = new Scene(fxmlLoader.load(), 700, 535);
1717
stage.setTitle("MagicEncoding");
1818
stage.setScene(scene);
1919
Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png")));
+17-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.zzhow.magicencoding.utils;
22

3+
import com.zzhow.magicencoding.MainClass;
34
import javafx.scene.control.Alert;
5+
import javafx.scene.image.Image;
6+
import javafx.stage.Stage;
7+
8+
import java.util.Objects;
49

510
/**
611
* @author ZZHow
@@ -14,25 +19,25 @@ public static void alert(Alert.AlertType type, String title, String headerText,
1419
alert.setHeaderText(headerText);
1520
alert.setContentText(contentText);
1621

22+
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
23+
Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png")));
24+
stage.getIcons().add(icon);
25+
1726
alert.showAndWait();
1827
}
1928

2029
public static void error(String headerText, String contentText) {
21-
Alert alert = new Alert(Alert.AlertType.ERROR);
22-
alert.setTitle("错误");
23-
alert.setHeaderText(headerText);
24-
alert.setContentText(contentText);
25-
26-
alert.showAndWait();
30+
alert(Alert.AlertType.ERROR,
31+
"错误",
32+
headerText,
33+
contentText);
2734
}
2835

2936
public static void success(String headerText, String contentText) {
30-
Alert alert = new Alert(Alert.AlertType.INFORMATION);
31-
alert.setTitle("成功");
32-
alert.setHeaderText(headerText);
33-
alert.setContentText(contentText);
34-
35-
alert.showAndWait();
37+
alert(Alert.AlertType.INFORMATION,
38+
"成功",
39+
headerText,
40+
contentText);
3641
}
3742

3843
}

Diff for: src/main/java/com/zzhow/magicencoding/utils/MyFiles.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void transform(String originPath, String targetPath, String origin
4848
bufferedWriter.newLine();
4949
}
5050

51-
System.out.println(originPath + "转换成功");
51+
// System.out.println(originPath + "转换成功");
5252
} catch (IOException e) {
5353
System.out.println(e.getMessage());
5454
}
@@ -72,4 +72,17 @@ public static void deleteFolder(String path) {
7272
}
7373
}
7474
}
75+
76+
public static void overwriteFile(String originPath, String targetPath) {
77+
try (FileInputStream fileInputStream = new FileInputStream(originPath);
78+
FileOutputStream fileOutputStream = new FileOutputStream(targetPath, false)) {
79+
byte[] buffer = new byte[1024];
80+
int length;
81+
while ((length = fileInputStream.read(buffer)) != -1) {
82+
fileOutputStream.write(buffer, 0, length);
83+
}
84+
} catch (IOException e) {
85+
System.out.println(e.getMessage());
86+
}
87+
}
7588
}

Diff for: src/main/java/module-info.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module com.zzhow.magicencoding {
22
requires javafx.controls;
33
requires javafx.fxml;
4+
requires java.desktop;
45

56

67
opens com.zzhow.magicencoding to javafx.fxml;

Diff for: src/main/resources/com/zzhow/magicencoding/ui/about-view.fxml

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,35 @@
66
<?import javafx.scene.layout.VBox?>
77
<?import javafx.scene.text.Font?>
88

9-
<VBox alignment="CENTER" prefHeight="287.0" prefWidth="471.0" spacing="20.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
9+
<VBox alignment="CENTER" prefHeight="287.0" prefWidth="471.0" spacing="20.0" xmlns="http://javafx.com/javafx/21"
10+
xmlns:fx="http://javafx.com/fxml/1">
1011
<padding>
11-
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
12+
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
1213
</padding>
1314
<AnchorPane prefHeight="287.0" prefWidth="471.0">
1415
<children>
15-
<Label alignment="CENTER" layoutX="96.0" layoutY="14.0" prefHeight="75.0" prefWidth="280.0" text="神奇编码 1.0.0&#10;MagicEncoding 1.0.0" textAlignment="CENTER">
16+
<Label alignment="CENTER" layoutX="96.0" layoutY="14.0" prefHeight="75.0" prefWidth="280.0"
17+
text="神奇编码 2.0.0&#10;MagicEncoding 2.0.0" textAlignment="CENTER">
1618
<font>
17-
<Font size="28.0" />
19+
<Font size="28.0"/>
1820
</font>
1921
</Label>
20-
<Label alignment="CENTER" layoutX="11.0" layoutY="123.0" prefHeight="150.0" prefWidth="450.0" text="1. &quot;GBK&quot;&quot;UTF-8&quot; 之间的编码转换。&#10; Encoding conversion between &quot;GBK&quot; and &quot;UTF-8&quot;.&#10;2. 根据路径查找文件。&#10; Find files by path.&#10;3. 支持根据文件后缀名筛选文件。&#10; Support filtering files based on file extensions.">
22+
<Label alignment="CENTER" layoutX="3.0" layoutY="123.0" prefHeight="150.0" prefWidth="450.0"
23+
text="1. 拖拽文件夹获取路径。&#10; Drag the folder to get the path.&#10;2. 查找多种后缀名。&#10; Search for multiple suffixes.&#10;3. 支持覆盖原文件。&#10; Support overwriting original files.">
2124
<font>
22-
<Font size="18.0" />
25+
<Font size="18.0"/>
2326
</font>
2427
</Label>
2528
<Label layoutX="14.0" layoutY="100.0" prefHeight="23.0" prefWidth="90.0" text="Features:">
2629
<font>
27-
<Font size="18.0" />
30+
<Font size="18.0"/>
2831
</font>
2932
</Label>
3033
</children>
3134
</AnchorPane>
3235
<Label layoutX="183.0" layoutY="287.0" text="ZZHow.com">
3336
<font>
34-
<Font size="20.0" />
37+
<Font size="20.0"/>
3538
</font>
3639
</Label>
3740
</VBox>

0 commit comments

Comments
 (0)