|
|
|
package com.fr.startup.ui;
|
|
|
|
|
|
|
|
import com.fr.concurrent.FineExecutors;
|
|
|
|
import com.fr.concurrent.NamedThreadFactory;
|
|
|
|
import com.fr.design.gui.iprogressbar.ProgressDialog;
|
|
|
|
import com.fr.design.i18n.Toolkit;
|
|
|
|
import com.fr.design.ui.util.UIUtil;
|
|
|
|
import com.fr.event.Event;
|
|
|
|
import com.fr.event.EventDispatcher;
|
|
|
|
import com.fr.event.Listener;
|
|
|
|
|
|
|
|
import java.awt.Frame;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 启动加载面板
|
|
|
|
*
|
|
|
|
* @author Harrison
|
|
|
|
* @version 11.0
|
|
|
|
* created by Harrison on 2022/11/08
|
|
|
|
**/
|
|
|
|
public class StartupLoadingPanel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 每次更新的步伐
|
|
|
|
*/
|
|
|
|
private static final int STEP = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 40ms更新进度
|
|
|
|
*/
|
|
|
|
private static final int STEP_HEARTBEAT = 40;
|
|
|
|
|
|
|
|
private final Listener<String> MODULE_LISTENER = new Listener<String>() {
|
|
|
|
@Override
|
|
|
|
public void on(Event event, String param) {
|
|
|
|
moduleId = param;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private ProgressDialog progressDialog;
|
|
|
|
private String moduleId;
|
|
|
|
private int progress;
|
|
|
|
|
|
|
|
public StartupLoadingPanel(Frame frame) {
|
|
|
|
this.progressDialog = new ProgressDialog(frame);
|
|
|
|
this.moduleId = Toolkit.i18nText("Fine-Design_Basic_Initializing");
|
|
|
|
|
|
|
|
initListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 隐藏
|
|
|
|
*/
|
|
|
|
public void hide() {
|
|
|
|
this.progress = progressDialog.getProgressMaximum();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 展示
|
|
|
|
*/
|
|
|
|
public void show() {
|
|
|
|
|
|
|
|
final ScheduledExecutorService scheduler = FineExecutors.newScheduledThreadPool(1,
|
|
|
|
new NamedThreadFactory("StartupLoadingPanel"));
|
|
|
|
scheduler.scheduleAtFixedRate(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
UIUtil.invokeAndWaitIfNeeded(() -> {
|
|
|
|
if (isComplete()) {
|
|
|
|
scheduler.shutdown();
|
|
|
|
progressDialog.dispose();
|
|
|
|
resetListeners();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!progressDialog.isVisible()) {
|
|
|
|
progressDialog.setVisible(true);
|
|
|
|
String moduleId = getModuleId();
|
|
|
|
progressDialog.updateLoadingText(moduleId);
|
|
|
|
}
|
|
|
|
progressDialog.setProgressValue(incrementProgress());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 0, STEP_HEARTBEAT, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initListeners() {
|
|
|
|
/// 后续换成其他进度方式
|
|
|
|
//EventDispatcher.listen(ModuleEvent.MajorModuleStarting, MODULE_LISTENER);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void resetListeners() {
|
|
|
|
|
|
|
|
EventDispatcher.stopListen(MODULE_LISTENER);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isComplete() {
|
|
|
|
return this.progress >= progressDialog.getProgressMaximum();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getModuleId() {
|
|
|
|
|
|
|
|
return this.moduleId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int incrementProgress() {
|
|
|
|
|
|
|
|
if (progress != progressDialog.getProgressMaximum()) {
|
|
|
|
progress += STEP;
|
|
|
|
}
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|