From c64f00a1f9a91cb82bd1b8308e0c9b29ef5de76f Mon Sep 17 00:00:00 2001
From: Hunter0x7c7 <1125607007@qq.com>
Date: 星期五, 08 九月 2023 09:24:21 +0800
Subject: [PATCH] 版本v1.0.1(2)发布。
---
src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java | 308 +++++++++++++++++++++++++++++++++------------------
1 files changed, 198 insertions(+), 110 deletions(-)
diff --git a/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java b/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
index d99e4b2..d3eb8d5 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
@@ -7,11 +7,13 @@
import com.github.hunter0x7c7.sync.model.storage.Session;
import com.github.hunter0x7c7.sync.utils.*;
import io.reactivex.Observable;
-import io.reactivex.ObservableSource;
+import io.reactivex.Observer;
+import io.reactivex.*;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
+import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import javafx.application.Platform;
import javafx.event.ActionEvent;
@@ -114,10 +116,10 @@
}
- @Override
+ /*@Override
protected void finalize() throws Throwable {
super.finalize();
- }
+ }*/
private void init() {
mCompositeDisposable = new CompositeDisposable();
@@ -177,7 +179,7 @@
public void accept(Object o) throws Exception {
//娣诲姞鍒扮郴缁熸墭鐩�
Stage primaryStage = Session.getInstance().getPrimaryStage();
- String tooltip = String.format("%s v%s", AppName, VersionName);//SyncTools v1.0
+ //String tooltip = String.format("%s v%s", AppName, VersionName);//SyncTools v1.0
String mipmap = "mipmap/ic_chinese_cabbage_16.png";
MenuItem show = new MenuItem("鏄剧ず");//鏄剧ずShow
//缁戝畾绯荤粺鎵樼洏浜嬩欢
@@ -197,77 +199,109 @@
}
};
//娣诲姞鍒扮郴缁熸墭鐩�
- TrayUtil.getInstance().addSystemTray(primaryStage, AppNameString, mipmap, callback, show);
+ TrayUtil.getInstance().addSystemTray(primaryStage, AppName_zh_rCN, mipmap, callback, show);
}
- }).doOnNext(new Consumer<Object>() {
+ }).concatMap(new Function<Object, Observable<ConfigBean>>() {//鍑嗗鍒濆鍖栫殑閰嶇疆鏁版嵁
@Override
- public void accept(Object o) throws Exception {
+ public Observable<ConfigBean> apply(Object o) throws Exception {
//鍒濆鍖栧凡淇濆瓨鐨勯厤缃俊鎭�
- ConfigBean cb = getSaveConfig();
- if (cb != null) {
- //UI绾跨▼
- Platform.runLater(new Runnable() {
- @Override
- public void run() {
- setInputText(tvInputSrcHost, cb.getSrcHost());
- setInputText(tvInputSrcName, cb.getSrcName());
- setInputText(tvInputSrcPwd, getDecryptData(cb.getSrcPwd()));
- setInputText(tvInputSrcDbName, cb.getSrcDbName());
- setInputText(tvInputTargetHost, cb.getTargetHost());
- setInputText(tvInputTargetName, cb.getTargetName());
- setInputText(tvInputTargetPwd, getDecryptData(cb.getTargetPwd()));
- setInputText(tvInputTargetDbName, cb.getTargetDbName());
- setInputText(tvInputFreq, cb.getFreqValue());
- cbSelectFreqUnit.getSelectionModel().select(cb.getFreqUnit());
- }
- });
+ final ConfigBean configBean = getSaveConfig();
+ return Observable.just(configBean != null)
+ .subscribeOn(Schedulers.io())
+ .filter(new Predicate<Boolean>() {
+ @Override
+ public boolean test(Boolean aBoolean) throws Exception {
+ //return false杩囨护, true涓嶈繃婊ゃ�俧alse绔嬪埢鎵цswitchIfEmpty
+ return aBoolean != null && aBoolean;
+ }
+ }).map(new Function<Boolean, ConfigBean>() {
+ @Override
+ public ConfigBean apply(Boolean aBoolean) throws Exception {
+ return configBean;
+ }
+ }).switchIfEmpty(new Observable<ConfigBean>() {
+ @Override
+ protected void subscribeActual(Observer<? super ConfigBean> observer) {
+ //濡傛灉鏄疍ebug鐜鎵嶅垵濮嬪寲娴嬭瘯鏁版嵁
+ if (BuildType.equals(BuildTypeEnum.DEBUG)) {
+ String srcHost = ConfigProperties.SRC_HOST;
+ String srcName = ConfigProperties.SRC_NAME;
+ String srcPwd = ConfigProperties.SRC_PWD;
+ String srcDbName = ConfigProperties.SRC_DB_NAME;
- //榛樿鍚姩鍚屾鍔熻兘
- if (cb.isStartSync()) {
- startSync(); //鍚姩鍚屾鍔熻兘
- }
- } else if (BuildType.equals(BuildTypeEnum.DEBUG)) {
- try {
- String srcHost = ConfigProperties.SRC_HOST;
- String srcName = ConfigProperties.SRC_NAME;
- String srcPwd = ConfigProperties.SRC_PWD;
- String srcDbName = ConfigProperties.SRC_DB_NAME;
+ String targetHost = ConfigProperties.TARGET_HOST;
+ String targetName = ConfigProperties.TARGET_NAME;
+ String targetPwd = ConfigProperties.TARGET_PWD;
+ String targetDbName = ConfigProperties.TARGET_DB_NAME;
- String targetHost = ConfigProperties.TARGET_HOST;
- String targetName = ConfigProperties.TARGET_NAME;
- String targetPwd = ConfigProperties.TARGET_PWD;
- String targetDbName = ConfigProperties.TARGET_DB_NAME;
+ String freqValue = "5";
+ int freqUnitIndex = 1;//绉�
+ ConfigBean cb = new ConfigBean(srcHost, srcName, srcPwd, srcDbName, targetHost, targetName, targetPwd, targetDbName, freqValue, freqUnitIndex, false);
+
+ if (observer != null) {
+ observer.onNext(cb);
+ observer.onComplete();
+ }
+ } else {
+ if (observer != null) {
+ observer.onNext(new ConfigBean());
+ observer.onComplete();
+ }
+ }
+ }
+ });
+ }
+ }).concatMap(new Function<ConfigBean, ObservableSource<ConfigBean>>() {//鏇存柊閰嶇疆鏁版嵁鍒扮晫闈�
+ @Override
+ public ObservableSource<ConfigBean> apply(final ConfigBean cb) throws Exception {
+ //鏁版嵁涓虹┖锛屼笉鏇存柊鐣岄潰
+ if (cb == null) {
+ return Observable.just(new ConfigBean());
+ }
+ //鏇存柊鐣岄潰
+ return Observable.create(new ObservableOnSubscribe<ConfigBean>() {
+ @Override
+ public void subscribe(ObservableEmitter<ConfigBean> emitter) throws Exception {
//UI绾跨▼
Platform.runLater(new Runnable() {
@Override
public void run() {
- setInputText(tvInputSrcHost, srcHost);
- setInputText(tvInputSrcName, srcName);
- setInputText(tvInputSrcPwd, srcPwd);
- setInputText(tvInputSrcDbName, srcDbName);
- setInputText(tvInputTargetHost, targetHost);
- setInputText(tvInputTargetName, targetName);
- setInputText(tvInputTargetPwd, targetPwd);
- setInputText(tvInputTargetDbName, targetDbName);
- setInputText(tvInputFreq, "10");
- cbSelectFreqUnit.getSelectionModel().select(1);
+ //鏇存柊鐣岄潰鏁版嵁
+ setInputText(tvInputSrcHost, cb.getSrcHost());
+ setInputText(tvInputSrcName, cb.getSrcName());
+ setInputText(tvInputSrcPwd, getDecryptData(cb.getSrcPwd()));
+ setInputText(tvInputSrcDbName, cb.getSrcDbName());
+ setInputText(tvInputTargetHost, cb.getTargetHost());
+ setInputText(tvInputTargetName, cb.getTargetName());
+ setInputText(tvInputTargetPwd, getDecryptData(cb.getTargetPwd()));
+ setInputText(tvInputTargetDbName, cb.getTargetDbName());
+ setInputText(tvInputFreq, cb.getFreqValue());
+ cbSelectFreqUnit.getSelectionModel().select(cb.getFreqUnit());
+
+ if (emitter != null) {
+ emitter.onNext(cb);
+ emitter.onComplete();
+ }
}
});
- } catch (Exception e) {
- throw new RuntimeException(e);
}
- }
+ });
}
- }).subscribe(new Consumer<Object>() {
+ }).subscribe(new Consumer<ConfigBean>() {
@Override
- public void accept(Object o) throws Exception {
+ public void accept(ConfigBean cb) throws Exception {
//UI绾跨▼
Platform.runLater(new Runnable() {
@Override
public void run() {
updateResult("鏁版嵁鍚屾宸ュ叿");
+
+ //榛樿鍚姩鍚屾鍔熻兘
+ if (cb != null && cb.isStartSync()) {
+ startSync(); //鍚姩鍚屾鍔熻兘
+ }
}
});
}
@@ -338,24 +372,54 @@
//鐐瑰嚮锛氶��鍑�
private void clickExit() {
- if (isSyncing()) {
- stopSync();//鍋滄鍚屾
- }
- //绯荤粺鎵樼洏
- SystemTray tray = SystemTray.getSystemTray();
- if (tray != null) {
- TrayIcon[] icons = tray.getTrayIcons();
- for (TrayIcon icon : icons) {
- if (icon == null) continue;
- //閫�鍑轰箣鍓嶅厛绉婚櫎绯荤粺鎵樼洏鍥炬爣
- if (APPID.equals(icon.getActionCommand())) {
- tray.remove(icon);
- }
- }
- }
- //閫�鍑�
- Platform.exit();
+ addSubscribe(Observable.just(true)
+ .subscribeOn(Schedulers.newThread())
+ .observeOn(Schedulers.io())
+ .doOnNext(new Consumer<Object>() {
+ @Override
+ public void accept(Object o) throws Exception {
+ if (isSyncing()) {
+ stopSync();//鍋滄鍚屾
+ }
+ }
+ })
+ .doOnNext(new Consumer<Object>() {
+ @Override
+ public void accept(Object o) throws Exception {
+
+ //妫�鏌ョ郴缁熸槸鍚︽敮鎸佹墭鐩�
+ if (SystemTray.isSupported()) {
+ //绯荤粺鎵樼洏
+ SystemTray tray = SystemTray.getSystemTray();
+ if (tray != null) {
+ TrayIcon[] icons = tray.getTrayIcons();
+ for (TrayIcon icon : icons) {
+ if (icon == null) continue;
+ //閫�鍑轰箣鍓嶅厛绉婚櫎绯荤粺鎵樼洏鍥炬爣
+ if (APPID.equals(icon.getActionCommand())) {
+ tray.remove(icon);
+ }
+ }
+ }
+ }
+ }
+ })
+ .subscribe(new Consumer<Boolean>() {
+ @Override
+ public void accept(Boolean aBoolean) throws Exception {
+
+ TrayUtil.getInstance().exitApp();//閫�鍑�
+ }
+ }, new Consumer<Throwable>() {
+ @Override
+ public void accept(Throwable e) throws Exception {
+ e.printStackTrace();
+
+ TrayUtil.getInstance().exitApp();//閫�鍑�
+ }
+ }));
}
+
//鐐瑰嚮锛氬叧浜�
private void clickAbout() {
@@ -394,7 +458,7 @@
, targetFreq, freqSelect, startSync);
//鍐欏叆鏂囦欢
- String path = PathUtil.projectPath + "\\config.json";
+ String path = PathUtil.getConfigFilePath("config.json");
String content = JsonUtil.toJson(cb);
//浣跨敤 BufferedWriter 鍐欐枃浠�
@@ -461,7 +525,7 @@
//浠庢湰鍦伴厤缃枃浠朵腑鑾峰彇閰嶇疆淇℃伅
private ConfigBean getSaveConfig() {
try {
- String json = FileUtil.bufferedReader(PathUtil.projectPath + "\\config.json");
+ String json = FileUtil.bufferedReader(PathUtil.getConfigFilePath("config.json"));
return JsonUtil.fromJson(json, ConfigBean.class);
} catch (Exception e) {
e.printStackTrace();
@@ -483,7 +547,7 @@
stage.setTitle("鍙傛暟璁剧疆");
stage.setResizable(false);
stage.setWidth(360);
- stage.setMinHeight(180);
+ stage.setMinHeight(218);
stage.getIcons().add(new Image("mipmap/ic_chinese_cabbage_48.png"));
//APPLICATION_MODAL 鍏ㄥ眬妯℃�侊紝璇ョ獥鍙h繍琛屾椂锛屽叾浠栫獥鍙d笉鑳借繘琛屾搷浣�
//WINDOW_MODAL 涓虹埗绐楀彛绂佺敤锛屽叾浠栫獥鍙e彲浠ヤ娇鐢�
@@ -503,6 +567,16 @@
generalList.setPadding(new Insets(0, 20, 30, 20));
generalList.setSpacing(4.0);
generalList.getChildren().add(cbStartSync);
+
+ Label foot = new Label(String.format("* 鏇存敼灏嗕簬閲嶆柊鍚姩 %s 鍚庣敓鏁�", AppName));//"* 鏇存敼灏嗕簬閲嶆柊鍚姩 SyncTools 鍚庣敓鏁�");
+ foot.setAlignment(Pos.CENTER);
+ foot.setFont(new Font(12));
+
+ VBox content = new VBox();
+ content.setStyle("-fx-background-color: white");
+ content.setPadding(new Insets(20));
+ content.setSpacing(10);
+ content.getChildren().addAll(title, generalList, foot);
Button defButton = new Button("纭畾");
@@ -539,9 +613,11 @@
saveConfigForIsStartSync(startSync, callback);
}
});
+
Button cancelButton = new Button("鍙栨秷");
cancelButton.setAlignment(Pos.CENTER);
cancelButton.setPrefSize(84, 24);
+ cancelButton.setCancelButton(true);
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
@@ -552,26 +628,16 @@
}
});
- Label label = new Label(String.format("* 鏇存敼灏嗕簬閲嶆柊鍚姩 %s 鍚庣敓鏁�", AppName));//"* 鏇存敼灏嗕簬閲嶆柊鍚姩 SyncTools 鍚庣敓鏁�");
- label.setAlignment(Pos.CENTER);
- label.setFont(new Font(12));
-
- VBox content = new VBox();
- content.setStyle("-fx-background-color: white");
- content.setPadding(new Insets(20));
- content.setSpacing(10);
- content.getChildren().addAll(title, generalList, label);
-
-
HBox operate = new HBox();
operate.setAlignment(Pos.CENTER_RIGHT);
operate.setSpacing(8);
// operate.setStyle("-fx-background-color: #F0F0F0");
- operate.setPadding(new Insets(12, 20, 6, 20));
+ operate.setPadding(new Insets(12, 20, 12, 20));
operate.getChildren().addAll(defButton, cancelButton);
VBox vBox = new VBox();
- vBox.setSpacing(4.0);
+ vBox.setSpacing(0);
+ //vBox.setPadding(getInsets());
vBox.getChildren().addAll(content, operate);
stage.setScene(new Scene(vBox));
@@ -621,7 +687,7 @@
VBox vBox = new VBox();
- vBox.setPadding(new Insets(20, 20, 4, 20));
+ vBox.setPadding(new Insets(20, 20, 10, 20));
vBox.getChildren().addAll(label1, label2, label3, label4);
stage.setScene(new Scene(vBox));
@@ -781,9 +847,9 @@
}
}
})
- .concatMap(new Function<Integer, ObservableSource<String>>() {
+ .concatMap(new Function<Integer, ObservableSource<Object>>() {
@Override
- public ObservableSource<String> apply(Integer freq) throws Exception {
+ public ObservableSource<Object> apply(Integer freq) throws Exception {
int delay = freq > 0 ? freq : 10;
TimeUnit timeUnit = ListUtil.getDataByList(mFreqUnitList, freqSelect);
@@ -791,16 +857,37 @@
//System.out.println("delay:" + delay + " unit:" + unit);
return Observable.timer(delay, unit)
- /*.doOnNext(new Consumer<Long>() {
+ .doOnNext(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
- startSync(srcHost, srcName, srcPwd, srcDbName, targetHost, targetName, targetPwd, targetDbName);
+ //濡傛灉涓�旀煇娆℃墽琛屽け璐ワ紝涓嶈兘瀵艰嚧鏁翠釜璁″垝鐨勫け璐�
+ addSubscribe(getStartSyncObservable(srcHost, srcName, srcPwd, srcDbName, targetHost, targetName, targetPwd, targetDbName)
+ .subscribe(new Consumer<Object>() {
+ @Override
+ public void accept(Object o) throws Exception {
+ //鍚屾鎴愬姛锛�
+ setSyncing(true);
+ mSyncCount++;
+ if (mSyncCount >= Integer.MAX_VALUE) {
+ mSyncCount = 0;
+ }
+ setInputText(txLastSyncTime, DateUtils.formatDate(new Date()));
+ setInputText(txSyncCount, String.valueOf(mSyncCount));
+ updateResult("鍚屾鎴愬姛锛�");
+ }
+ }, new Consumer<Throwable>() {
+ @Override
+ public void accept(Throwable e) throws Exception {
+ e.printStackTrace();
+ //鎵ц澶辫触
+ updateResult(e.getMessage());
+ }
+ }));
}
- })*/
- .concatMap(new Function<Long, ObservableSource<String>>() {
+ }).map(new Function<Long, Object>() {
@Override
- public ObservableSource<String> apply(Long o) throws Exception {
- return getStartSyncObservable(srcHost, srcName, srcPwd, srcDbName, targetHost, targetName, targetPwd, targetDbName);
+ public Object apply(Long aLong) throws Exception {
+ return String.valueOf(aLong);
}
}).repeat();
}
@@ -809,21 +896,14 @@
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
- setSyncing(true);
- mSyncCount++;
- if (mSyncCount >= Integer.MAX_VALUE) {
- mSyncCount = 0;
- }
- setInputText(txLastSyncTime, DateUtils.formatDate(new Date()));
- setInputText(txSyncCount, String.valueOf(mSyncCount));
- updateResult("鍚屾鎴愬姛锛�");
+ //娴佺▼鎵ц瀹屾瘯
}
}, new Consumer<Throwable>() {
@Override
- public void accept(Throwable throwable) throws Exception {
- throwable.printStackTrace();
-
- updateResult(throwable.getMessage());
+ public void accept(Throwable e) throws Exception {
+ e.printStackTrace();
+ //鎵ц澶辫触
+ updateResult(e.getMessage());
}
}));
}
@@ -858,7 +938,7 @@
}
cb.setStartSync(startSync);
- String path = PathUtil.projectPath + "\\config.json";
+ String path = PathUtil.getConfigFilePath("config.json");
String content = JsonUtil.toJson(cb);
//浣跨敤 BufferedWriter 鍐欐枃浠�
@@ -1021,11 +1101,19 @@
}
private void updateResult(String str) {
- System.out.println(str);
+ //杈撳嚭缁撴灉甯︽椂闂�
+ System.out.printf("%s\t%s%n", DateUtils.formatDate(new Date()), str);
setInputText(txOutResult, str);
}
+ private Insets getInsets() {
+ if (SystemUtil.isWindows()) {
+ return new Insets(0, 0, -6, 0);
+ }
+ return new Insets(0);
+ }
+
public boolean isSyncing() {
return mSyncing;
}
--
Gitblit v1.9.1