2626
2727import java .awt .*;
2828import java .awt .datatransfer .StringSelection ;
29- import java .io .BufferedWriter ;
30- import java .io .File ;
31- import java .io .IOException ;
29+ import java .io .*;
3230import java .net .URL ;
3331import java .nio .charset .StandardCharsets ;
3432import java .nio .file .Files ;
3533import java .nio .file .Path ;
3634import java .nio .file .Paths ;
3735import 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 */
4445public 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 }
0 commit comments