Skip to content

Commit f73e902

Browse files
committed
reformat code; add method to set initial path for FileChooser
1 parent 2614b2c commit f73e902

File tree

9 files changed

+163
-12
lines changed

9 files changed

+163
-12
lines changed

.idea/dictionaries/ma201.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/cn/rocket/assaignmark/LocalURL.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public final class LocalURL {
1616
private LocalURL() {
1717
}
1818

19+
// User associated
20+
public static final String USER_PATH = System.getProperty("user.home") + "/"; // with /
21+
public static final String FOLDER_PATH = USER_PATH + ".rocketdev/";
22+
public static final String WORK_PATH = FOLDER_PATH + "AssignMark/";
23+
public static final String PROPERTIES_PATH = WORK_PATH + "default.properties";
24+
1925
// Jar associated
2026
public static final String JAR_PATH; // with /
2127
public static final String JAR_PARENT_PATH; // with /

src/main/java/cn/rocket/assaignmark/cmd/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class Main {
1515

16-
protected static final Logger LOGGER = LogManager.getLogger();
16+
protected static final Logger LOGGER = LogManager.getLogger(Main.class);
1717

1818
/**
1919
* 程序主入口

src/main/java/cn/rocket/assaignmark/cmd/Processor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Processor extends Main {
3434

3535
/**
3636
* 命令行工具主入口
37+
*
3738
* @param args 命令行参数
3839
*/
3940
public static void main(String[] args) {
@@ -104,6 +105,7 @@ public static void main(String[] args) {
104105

105106
/**
106107
* 判断文件是否为同一个(绝对)
108+
*
107109
* @param path0 第一个文件路径
108110
* @param path1 第二个文件路径
109111
* @return 构造出的文件是否为同一个(canonical)

src/main/java/cn/rocket/assaignmark/core/AMFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public static void extractTable(String outputPath) throws IOException {
6262

6363
/**
6464
* 尝试提取赋分表模板,会在<code>parentPath</code>中尝试11次,以 <code>"赋分表"+数字+".xlsx"</code> 命名
65+
*
6566
* @param parentPath 尝试新建文件的路径
66-
* @throws IOException 如果无法创建文件
67+
* @throws IOException 如果无法创建文件
6768
* @throws AssigningException 如果过多赋分表已存在在当前路径
6869
*/
6970
public static void tryToExtract(String parentPath) throws IOException, AssigningException {

src/main/java/cn/rocket/assaignmark/core/MarkTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,6 @@ public String toString() {
451451
.append("notifier", notifier)
452452
.append("workbook", markWorkbook)
453453
.append("outputPath", outputPath)
454-
.append("loaded",allMarks!=null).build();
454+
.append("loaded", allMarks != null).build();
455455
}
456456
}

src/main/java/cn/rocket/assaignmark/gui/Controller.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,61 @@
2626

2727
import java.awt.*;
2828
import java.awt.datatransfer.StringSelection;
29-
import java.io.BufferedWriter;
30-
import java.io.File;
31-
import java.io.IOException;
29+
import java.io.*;
3230
import java.net.URL;
3331
import java.nio.charset.StandardCharsets;
3432
import java.nio.file.Files;
3533
import java.nio.file.Path;
3634
import java.nio.file.Paths;
3735
import java.util.Date;
36+
import java.util.Properties;
3837

3938
/**
39+
* 核心窗口的Controller
40+
*
4041
* @author Rocket
41-
* @version 1.0.8
42+
* @version 1.1.8
4243
* @since 1.0.8
4344
*/
4445
public class Controller {
46+
private final Properties properties = new Properties();
4547
private final FileChooser chooser;
4648
private boolean confirmation;
49+
private String openedPath;
4750

4851
private Thread task;
4952

5053
{
54+
File propertiesFile = new File(LocalURL.PROPERTIES_PATH);
55+
boolean canMake = true;
56+
boolean exist = propertiesFile.exists();
57+
if (!exist)
58+
canMake = propertiesFile.getParentFile().mkdirs();
59+
String toBeLoad = LocalURL.JAR_PARENT_PATH;
60+
if (canMake) {
61+
if (exist)
62+
try (FileInputStream in = new FileInputStream(propertiesFile)) {
63+
properties.load(in);
64+
toBeLoad = properties.getProperty("initialPath");
65+
} catch (IOException e) {
66+
e.printStackTrace();
67+
}
68+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
69+
if (openedPath != null) {
70+
try (FileOutputStream out = new FileOutputStream(propertiesFile)) {
71+
properties.setProperty("initialPath", openedPath);
72+
properties.store(out, null);
73+
} catch (IOException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
}));
78+
}
79+
5180
chooser = new FileChooser();
5281
chooser.setTitle("选择您的文件");
5382
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Excel文件", "*.xlsx"));
54-
chooser.setInitialDirectory(new File(LocalURL.JAR_PARENT_PATH));
55-
// TODO 设置初始路径,读取/保存到配置文件
83+
chooser.setInitialDirectory(new File(toBeLoad));
5684
}
5785

5886
public Label progressLabel;
@@ -117,6 +145,7 @@ private void fillField(TextField field, boolean isReading) {
117145
field.setText(file.getAbsolutePath());
118146
field.appendText("");
119147
chooser.setInitialDirectory(file.getParentFile());
148+
openedPath = file.getParentFile().getAbsolutePath();
120149
}
121150

122151
public void setAtM() {
@@ -132,9 +161,12 @@ public void setOutM() {
132161
}
133162

134163
public void startM() {
164+
// 检查三个文本框是否为空
135165
if (atField.getText().isEmpty() || mtField.getText().isEmpty() || outField.getText().isEmpty()
136166
|| task != null && task.isAlive())
137167
return;
168+
169+
// 检查输出文件是否与赋分表一致
138170
if (Processor.fileEqual(atField.getText(), outField.getText()) && !confirmation) {
139171
Alert alert = new Alert("您正在尝试将输出文件覆盖到赋分表,确定吗?",
140172
this, HintType.HINT, true);
@@ -147,11 +179,14 @@ public void startM() {
147179
return;
148180
}
149181
confirmation = false;
182+
183+
// 创建文件路径,准备开始
150184
File outFile = AMFactory.defaultGetFile(outField.getText());
151185
if (!outFile.exists())
152186
// noinspection ResultOfMethodCallIgnored
153187
outFile.getParentFile().mkdirs();
154188
ctrlPane.setDisable(true);
189+
155190
task = new Thread(() -> {
156191
try {
157192
new AMFactory(
@@ -162,6 +197,7 @@ public void startM() {
162197
throw e;
163198
}
164199
}, "Assigning Task Thread");
200+
// 在运行任务时按关闭会先终止任务线程
165201
Launcher.mainStage.setOnCloseRequest(event -> {
166202
if (task.isAlive() && !task.isInterrupted())
167203
task.interrupt();
@@ -196,6 +232,7 @@ public void handle(AMEvent event, String msg) {
196232
progressBar.setProgress((double) (index + 1) / max);
197233
progressLabel.setText(String.format("%d/%d", index + 1, max));
198234
statusLabel.setText(Processor.MSG_ARR[index]);
235+
199236
} else if (index == AMEvent.DONE.getIndex()) {
200237
progressBar.setProgress(1);
201238
progressLabel.setText(String.format("%d/%d", max, max));
@@ -204,6 +241,7 @@ public void handle(AMEvent event, String msg) {
204241
Alert alert = new Alert(Processor.MSG_ARR[index], ctrler, HintType.DONE, false);
205242
alert.setEventHandler(null, null);
206243
alert.show();
244+
207245
} else if (index <= AMEvent.getLastEvent().getIndex()) {
208246
String message = Processor.MSG_ARR[event.ordinal()];
209247
boolean unexpected = false;
@@ -213,15 +251,13 @@ public void handle(AMEvent event, String msg) {
213251
unexpected = true;
214252
message += msg.substring(0, msg.indexOf('\n',
215253
AMEvent.ERR_FAILED_TO_CLOSE.toString().length() + 2)); // ERR_FAILED_TO_CLOSE\n_
216-
217254
String name = new Date().toString();
218255
Path path = Paths.get(LocalURL.JAR_PARENT_PATH, "/", name, ".txt");
219256
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
220257
writer.write(msg);
221258
} catch (IOException e) {
222259
LogManager.getRootLogger().error("Can't write error log out!");
223260
}
224-
225261
//TODO IOException 测试
226262
message += "\n发生意料之外的异常,已保存到jar路径下的文件中," +
227263
"按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue";
@@ -231,11 +267,13 @@ public void handle(AMEvent event, String msg) {
231267
statusLabel.setText(statusLabel.getText() + " 失败!");
232268
Alert alert = new Alert(message, ctrler, HintType.ERROR, true);
233269
EventHandler<ActionEvent> handler = null;
270+
// 复制
234271
if (unexpected)
235272
handler = event1 -> {
236273
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(msg), null);
237274
alert.close();
238275
};
276+
239277
alert.setEventHandler(handler, null);
240278
alert.show();
241279
}

src/main/java/cn/rocket/assaignmark/gui/Launcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public static void launchSelf() {
2727

2828
/**
2929
* {@inheritDoc}
30-
*
30+
* <p>
3131
* 图形化程序“入口”
32+
*
3233
* @param primaryStage
3334
*/
3435
@Override

0 commit comments

Comments
 (0)