Skip to content

Commit

Permalink
Merge pull request #4 from ZZHow1024/develop
Browse files Browse the repository at this point in the history
MagicEncoding2.0.0
  • Loading branch information
ZZHow1024 authored Sep 6, 2024
2 parents 437598f + 981c6a7 commit 1466e61
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/issue-template-bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ assignees: ''

### Desired outcome | 期望的结果

### MagicCalculator version | MagicCalculator 版本
### MagicEncoding version | MagicEncoding 版本

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

Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/template-question.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ assignees: ''

### Desired outcome | 期望的结果

### MagicCalculator version | MagicCalculator 版本
### MagicEncoding version | MagicEncoding 版本

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

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" 之间的编码转换。
- 根据路径查找文件。
Expand All @@ -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
11 changes: 11 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -24,24 +27,47 @@ public class MainController {
private TextField endWithTextField;
@FXML
private ListView<String> 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<File> files = event.getDragboard().getFiles();
if (!files.isEmpty()) {
pathTextField.setText(files.get(0).getAbsolutePath());
}
}

@FXML
private void onReset() {
this.clearFilesPath();
pathTextField.setText("");
endWithTextField.setText("");
fileNumber.setText("文件数目:0");
}

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

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

@FXML
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -38,31 +40,36 @@ public ObservableList<String> 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("当前没有命中的文件", "请先查找文件");

return false;
}

// 开始转换编码
String outputPath = absolutePath + "/MagicEncodingOutput";
String outputPath = absolutePath + "/" + "MagicEncodingOutput";
File outputFolder = new File(outputPath);
if (outputFolder.exists()) {
MyFiles.deleteFolder(outputPath);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zzhow/magicencoding/ui/About.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zzhow/magicencoding/ui/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/zzhow/magicencoding/utils/MessageBox.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

}
15 changes: 14 additions & 1 deletion src/main/java/com/zzhow/magicencoding/utils/MyFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
}
}
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module com.zzhow.magicencoding {
requires javafx.controls;
requires javafx.fxml;
requires java.desktop;


opens com.zzhow.magicencoding to javafx.fxml;
Expand Down
19 changes: 11 additions & 8 deletions src/main/resources/com/zzhow/magicencoding/ui/about-view.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,35 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<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">
<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">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<AnchorPane prefHeight="287.0" prefWidth="471.0">
<children>
<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">
<Label alignment="CENTER" layoutX="96.0" layoutY="14.0" prefHeight="75.0" prefWidth="280.0"
text="神奇编码 2.0.0&#10;MagicEncoding 2.0.0" textAlignment="CENTER">
<font>
<Font size="28.0" />
<Font size="28.0"/>
</font>
</Label>
<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.">
<Label alignment="CENTER" layoutX="3.0" layoutY="123.0" prefHeight="150.0" prefWidth="450.0"
text="1. 拖拽文件夹获取路径。&#10; Drag the folder to get the path.&#10;2. 查找多种后缀名。&#10; Search for multiple suffixes.&#10;3. 支持覆盖原文件。&#10; Support overwriting original files.">
<font>
<Font size="18.0" />
<Font size="18.0"/>
</font>
</Label>
<Label layoutX="14.0" layoutY="100.0" prefHeight="23.0" prefWidth="90.0" text="Features:">
<font>
<Font size="18.0" />
<Font size="18.0"/>
</font>
</Label>
</children>
</AnchorPane>
<Label layoutX="183.0" layoutY="287.0" text="ZZHow.com">
<font>
<Font size="20.0" />
<Font size="20.0"/>
</font>
</Label>
</VBox>
Loading

0 comments on commit 1466e61

Please sign in to comment.