From ba40379e97ea38e0fc41401433fa9545a5af267f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=94=80?= Date: Wed, 7 Sep 2016 21:00:41 +0800 Subject: [PATCH] =?UTF-8?q?QQ=E6=8E=88=E6=9D=83=E5=88=87=E6=8D=A2=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E7=9A=84=E6=97=B6=E5=80=99=E7=9A=84confirm=E7=94=A8ja?= =?UTF-8?q?vafx=E9=87=8D=E6=96=B0=E8=8E=B7=E5=8F=96=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E5=86=99=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 相当于重新绘制了confirm的页面,还需要一个css --- .../design/mainframe/bbs/UserInfoLabel.java | 7 +- .../src/com/fr/design/DesignerEnvManager.java | 10 ++ .../com/fr/design/extra/LoginWebBridge.java | 110 ++++++++++++++- .../com/fr/design/extra/QQLoginWebBridge.java | 3 + .../com/fr/design/extra/QQLoginWebPane.java | 130 +++++++++++++++++- .../extra/exe/GetLoginInfoExecutor.java | 5 +- .../src/com/fr/design/extra/modal-dialog.css | 36 +++++ 7 files changed, 287 insertions(+), 14 deletions(-) create mode 100644 designer_base/src/com/fr/design/extra/modal-dialog.css diff --git a/designer/src/com/fr/design/mainframe/bbs/UserInfoLabel.java b/designer/src/com/fr/design/mainframe/bbs/UserInfoLabel.java index 727539d889..16129b4f15 100644 --- a/designer/src/com/fr/design/mainframe/bbs/UserInfoLabel.java +++ b/designer/src/com/fr/design/mainframe/bbs/UserInfoLabel.java @@ -78,7 +78,10 @@ public class UserInfoLabel extends UILabel{ this.addMouseListener(userInfoAdapter); this.setHorizontalAlignment(SwingConstants.CENTER); this.setText(userName); - setUserName(userName); + + LoginWebBridge loginWebBridge = new LoginWebBridge(); + loginWebBridge.setUserName(userName, UserInfoLabel.this); + LoginCheckContext.addLoginCheckListener(new LoginCheckListener() { @Override public void loginChecked() { @@ -187,7 +190,6 @@ public class UserInfoLabel extends UILabel{ if(StringUtils.isEmpty(this.userName)){ updateMessageCount(); } - //往designerenvmanger里写一下 DesignerEnvManager.getEnvManager().setBBSName(userName); this.userName = userName; @@ -216,7 +218,6 @@ public class UserInfoLabel extends UILabel{ } catch (Exception e) { } } - sleep(CHECK_MESSAGE_TIME); } } diff --git a/designer_base/src/com/fr/design/DesignerEnvManager.java b/designer_base/src/com/fr/design/DesignerEnvManager.java index f43096c61c..e1e3465523 100644 --- a/designer_base/src/com/fr/design/DesignerEnvManager.java +++ b/designer_base/src/com/fr/design/DesignerEnvManager.java @@ -93,6 +93,8 @@ public class DesignerEnvManager implements XMLReadable, XMLWriter { private String bbsName; //当前设计器用户的论坛密码 private String bbsPassword; + //当前设计器用户的昵称显示(带消息) + private String inShowBBsName; //上一次登录弹窗的时间, 为了控制一天只弹一次窗口 private String lastShowBBSTime; //上一次资讯弹窗时间, 为了控制一天只弹一次 @@ -1148,6 +1150,14 @@ public class DesignerEnvManager implements XMLReadable, XMLWriter { public void setBBSPassword(String bbsPassword) { this.bbsPassword = bbsPassword; } + + public void setInShowBBsName(String inShowBBsName) { + this.inShowBBsName = inShowBBsName; + } + + public String getInShowBBsName() { + return inShowBBsName; + } public String getLastShowBBSTime() { return lastShowBBSTime; diff --git a/designer_base/src/com/fr/design/extra/LoginWebBridge.java b/designer_base/src/com/fr/design/extra/LoginWebBridge.java index 4e1e93b761..6dd4f0f830 100644 --- a/designer_base/src/com/fr/design/extra/LoginWebBridge.java +++ b/designer_base/src/com/fr/design/extra/LoginWebBridge.java @@ -8,6 +8,7 @@ import com.fr.design.mainframe.DesignerContext; import com.fr.general.FRLogger; import com.fr.general.SiteCenter; import com.fr.general.http.HttpClient; +import com.fr.json.JSONObject; import com.fr.stable.EncodeConstants; import com.fr.stable.StringUtils; import javafx.scene.web.WebEngine; @@ -18,12 +19,21 @@ import java.net.URI; import javax.swing.*; import java.awt.*; import java.net.URLEncoder; +import java.util.HashMap; -/** - * Created by zhaohehe on 16/8/1. - */ public class LoginWebBridge { + //默认查询消息时间, 30s + private static final long CHECK_MESSAGE_TIME = 30 * 1000L; + //数据查询正常的标志 ok + private static final String SUCCESS_MESSAGE_STATUS = "ok"; + + //消息条数 + private int messageCount; + + //最低消息的条数 + private static final int MIN_MESSAGE_COUNT = 0; + private static final String LOGIN_SUCCESS_FLAG = "http://bbs.finereport.com"; private static final String LOGININ = "0"; private static final String LOGIN_INFO_EMPTY = "-1"; @@ -34,6 +44,11 @@ public class LoginWebBridge { private static com.fr.design.extra.LoginWebBridge helper; private UIDialog uiDialog; private UILabel uiLabel; + private String userName; + + public int getMessageCount() { + return messageCount; + } private boolean testConnection() { HttpClient client = new HttpClient(SiteCenter.getInstance().acquireUrlByKind("bbs.test")); @@ -60,9 +75,6 @@ public class LoginWebBridge { private WebEngine webEngine; - private LoginWebBridge() { - } - public void setEngine(WebEngine webEngine) { this.webEngine = webEngine; } @@ -75,6 +87,90 @@ public class LoginWebBridge { this.uiLabel = uiLabel; } + public LoginWebBridge() { + String username = DesignerEnvManager.getEnvManager().getBBSName(); + setUserName(username, uiLabel); + } + + public void setUserName(String userName, UILabel label) { + if (uiLabel == null) { + this.uiLabel = label; + } + if(StringUtils.isEmpty(userName)){ + return; + } + + if(!StringUtils.isEmpty(this.userName)){ + updateMessageCount(); + } + //往designerenvmanger里写一下 + DesignerEnvManager.getEnvManager().setBBSName(userName); + this.userName = userName; + } + + private void updateMessageCount(){ + //启动获取消息更新的线程 + //登陆状态, 根据存起来的用户名密码, 每1分钟发起一次请求, 更新消息条数. + Thread updateMessageThread = new Thread(new Runnable() { + + @Override + public void run() { + sleep(CHECK_MESSAGE_TIME); + //从env中获取username, 因为如果注销的话, env的里username会被清空. + while(StringUtils.isNotEmpty(DesignerEnvManager.getEnvManager().getBBSName())){ + HashMap para = new HashMap(); + para.put("username", encode(encode(userName))); + HttpClient getMessage = new HttpClient(SiteCenter.getInstance().acquireUrlByKind("bbs.message"), para); + getMessage.asGet(); + if(getMessage.isServerAlive()){ + try { + String res = getMessage.getResponseText(); + JSONObject jo = new JSONObject(res); + if (jo.getString("status").equals(SUCCESS_MESSAGE_STATUS)) { + setMessageCount(Integer.parseInt(jo.getString("message"))); + } + } catch (Exception e) { + FRContext.getLogger().info(e.getMessage()); + } + } + sleep(CHECK_MESSAGE_TIME); + } + } + }); + updateMessageThread.start(); + } + + public void setMessageCount(int count) { + if (count == MIN_MESSAGE_COUNT) { + uiLabel.setText(DesignerEnvManager.getEnvManager().getBBSName()); + DesignerEnvManager.getEnvManager().setInShowBBsName(DesignerEnvManager.getEnvManager().getBBSName()); + return; + } + this.messageCount = count; + StringBuilder sb = new StringBuilder(); + sb.append(StringUtils.BLANK).append(this.userName) + .append("(").append(this.messageCount) + .append(")").append(StringUtils.BLANK); + DesignerEnvManager.getEnvManager().setInShowBBsName(sb.toString()); + uiLabel.setText(sb.toString()); + } + + private String encode(String str){ + try { + return URLEncoder.encode(str, EncodeConstants.ENCODING_UTF_8); + } catch (UnsupportedEncodingException e) { + return str; + } + } + + private void sleep(long millis){ + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + FRContext.getLogger().error(e.getMessage()); + } + } + /** * 注册页面 */ @@ -113,6 +209,7 @@ public class LoginWebBridge { if (login(username, password)) { updateUserInfo(username, password); loginSuccess(username); + setUserName(username, uiLabel); return LOGININ; }else { return LOGININFO_ERROR; @@ -152,6 +249,7 @@ public class LoginWebBridge { public void updateUserInfo(String username,String password) { DesignerEnvManager.getEnvManager().setBBSName(username); DesignerEnvManager.getEnvManager().setBBSPassword(password); + this.userName = username; } /** diff --git a/designer_base/src/com/fr/design/extra/QQLoginWebBridge.java b/designer_base/src/com/fr/design/extra/QQLoginWebBridge.java index b386e836c2..1538e71c2c 100644 --- a/designer_base/src/com/fr/design/extra/QQLoginWebBridge.java +++ b/designer_base/src/com/fr/design/extra/QQLoginWebBridge.java @@ -7,6 +7,7 @@ import com.fr.general.FRLogger; import com.fr.general.SiteCenter; import javafx.scene.web.WebEngine; import org.json.JSONObject; +import netscape.javascript.JSObject; import javax.swing.*; import java.awt.*; @@ -68,6 +69,8 @@ public class QQLoginWebBridge { username = DesignerEnvManager.getEnvManager().getBBSName(); } + private static JSObject window; + public static com.fr.design.extra.QQLoginWebBridge getHelper(WebEngine webEngine) { getHelper(); helper.setEngine(webEngine); diff --git a/designer_base/src/com/fr/design/extra/QQLoginWebPane.java b/designer_base/src/com/fr/design/extra/QQLoginWebPane.java index cdb1d17483..e9fab4fe17 100644 --- a/designer_base/src/com/fr/design/extra/QQLoginWebPane.java +++ b/designer_base/src/com/fr/design/extra/QQLoginWebPane.java @@ -1,21 +1,38 @@ package com.fr.design.extra; +import com.fr.base.FRContext; import com.fr.general.ComparatorUtils; import com.fr.general.FRLogger; +import com.fr.general.Inter; import com.fr.general.SiteCenter; import javafx.application.Platform; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; +import javafx.concurrent.Worker; import javafx.embed.swing.JFXPanel; +import javafx.event.ActionEvent; import javafx.event.EventHandler; +import javafx.scene.Group; +import javafx.scene.Node; import javafx.scene.Scene; +import javafx.scene.control.ButtonBuilder; +import javafx.scene.control.LabelBuilder; +import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.HBoxBuilder; +import javafx.scene.paint.Color; import javafx.scene.web.WebEngine; import javafx.scene.web.WebEvent; import javafx.scene.web.WebView; +import javafx.stage.*; +import javafx.util.Callback; import netscape.javascript.JSObject; import javax.swing.*; +import java.awt.*; /** * Created by zhaohehe on 16/7/28. @@ -24,6 +41,17 @@ public class QQLoginWebPane extends JFXPanel { private WebEngine webEngine; + private static JSObject window; + + private static int DEFAULT_PRIMARYSTAGE_WIDTH = 100; + private static int DEFAULT_PRIMARYSTAGE_HEIGHT = 100; + + private static int DEFAULT_CONFIRM_WIDTH = 450; + private static int DEFAULT_CONFIRM_HEIGHT = 160; + private static int DEFAULT_OFFEST = 20; + + class Delta { double x, y; } + public QQLoginWebPane(final String installHome) { Platform.setImplicitExit(false); Platform.runLater(new Runnable() { @@ -32,9 +60,33 @@ public class QQLoginWebPane extends JFXPanel { BorderPane root = new BorderPane(); Scene scene = new Scene(root); QQLoginWebPane.this.setScene(scene); - WebView webView = new WebView(); + final WebView webView = new WebView(); webEngine = webView.getEngine(); webEngine.load("file:///" + installHome + "/scripts/qqLogin/web/qqLogin.html"); + + final Stage primaryStage = new Stage(); + + HBox layout = new HBox(); + try { + primaryStage.initStyle(StageStyle.TRANSPARENT); + primaryStage.setScene(new Scene(layout)); + webView.getScene().getStylesheets().add(getClass().getResource("modal-dialog.css").toExternalForm()); + primaryStage.initStyle(StageStyle.UTILITY); + primaryStage.setScene(new Scene(new Group(), DEFAULT_PRIMARYSTAGE_WIDTH, DEFAULT_PRIMARYSTAGE_HEIGHT)); + primaryStage.setX(0); + primaryStage.setY(Screen.getPrimary().getBounds().getHeight() + DEFAULT_PRIMARYSTAGE_HEIGHT); + primaryStage.show(); + }catch (Exception e) { + FRContext.getLogger().info(e.getMessage()); + } + + webView.getEngine().setConfirmHandler(new Callback() { + @Override public Boolean call(String msg) { + Boolean confirmed = confirm(primaryStage, msg, installHome, webView); + return confirmed; + } + }); + webEngine.locationProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, final String oldValue, String newValue) { @@ -52,8 +104,12 @@ public class QQLoginWebPane extends JFXPanel { showAlert(event.getData()); } }); - JSObject obj = (JSObject) webEngine.executeScript("window"); - obj.setMember("QQLoginHelper", QQLoginWebBridge.getHelper(webEngine)); + webEngine.getLoadWorker().stateProperty().addListener((ObservableValue observable, Worker.State oldValue, Worker.State newValue) -> { + if (newValue == Worker.State.SUCCEEDED) { + window = (JSObject) webEngine.executeScript("window"); + window.setMember("QQLoginHelper", QQLoginWebBridge.getHelper(webEngine)); + } + }); webView.setContextMenuEnabled(false);//屏蔽右键 root.setCenter(webView); } @@ -84,4 +140,72 @@ public class QQLoginWebPane extends JFXPanel { FRLogger.getLogger().error(e.getMessage()); } } + + private Boolean confirm(final Stage parent, String msg, final String installHome,final WebView webView) { + final BooleanProperty confirmationResult = new SimpleBooleanProperty(); + // initialize the confirmation dialog + final Stage dialog = new Stage(StageStyle.UTILITY); + dialog.setTitle(Inter.getLocText("FR-Designer-BBSLogin_Switch-Account")); + dialog.setX(Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2 - DEFAULT_CONFIRM_WIDTH / 2 + DEFAULT_OFFEST); + dialog.setY(Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2 + DEFAULT_OFFEST); + dialog.setHeight(DEFAULT_CONFIRM_HEIGHT); + dialog.setWidth(DEFAULT_CONFIRM_WIDTH); + dialog.setIconified(false); + dialog.setAlwaysOnTop(true); + dialog.initOwner(parent); + dialog.initModality(Modality.WINDOW_MODAL); + dialog.setScene( + new Scene( + HBoxBuilder.create().styleClass("modal-dialog").children( + LabelBuilder.create().text(msg).build(), + ButtonBuilder.create().text(Inter.getLocText("")).defaultButton(true).onAction(new EventHandler() { + @Override public void handle(ActionEvent actionEvent) { + // take action and close the dialog. + confirmationResult.set(true); + webView.getEngine().reload(); + dialog.close(); + } + }).build(), + ButtonBuilder.create().text(Inter.getLocText("FR-Engine_Cancel")).cancelButton(true).onAction(new EventHandler() { + @Override public void handle(ActionEvent actionEvent) { + // abort action and close the dialog. + confirmationResult.set(false); + dialog.close(); + } + }).build() + ).build() + , Color.TRANSPARENT + ) + ); + // allow the dialog to be dragged around. + final Node root = dialog.getScene().getRoot(); + final Delta dragDelta = new Delta(); + + root.setOnMousePressed(new EventHandler() { + @Override public void handle(MouseEvent mouseEvent) { + // record a delta distance for the drag and drop operation. + dragDelta.x = dialog.getX() - mouseEvent.getScreenX(); + dragDelta.y = dialog.getY() - mouseEvent.getScreenY(); + } + }); + root.setOnMouseDragged(new EventHandler() { + @Override public void handle(MouseEvent mouseEvent) { + dialog.setX(mouseEvent.getScreenX() + dragDelta.x); + dialog.setY(mouseEvent.getScreenY() + dragDelta.y); + } + }); + // style and show the dialog. + dialog.getScene().getStylesheets().add(getClass().getResource("modal-dialog.css").toExternalForm()); + + dialog.setOnCloseRequest(new EventHandler(){ + @Override + public void handle(WindowEvent event){ + event.consume(); + dialog.close(); + } + }); + + dialog.showAndWait(); + return confirmationResult.get(); + } } \ No newline at end of file diff --git a/designer_base/src/com/fr/design/extra/exe/GetLoginInfoExecutor.java b/designer_base/src/com/fr/design/extra/exe/GetLoginInfoExecutor.java index 6258d02331..06a0646980 100644 --- a/designer_base/src/com/fr/design/extra/exe/GetLoginInfoExecutor.java +++ b/designer_base/src/com/fr/design/extra/exe/GetLoginInfoExecutor.java @@ -27,9 +27,10 @@ public class GetLoginInfoExecutor implements Executor { @Override public void run(Process process) { String username = DesignerEnvManager.getEnvManager().getBBSName(); - if (StringUtils.isEmpty(username)) { + String inShowUsername = DesignerEnvManager.getEnvManager().getInShowBBsName(); + if (StringUtils.isEmpty(username) && StringUtils.isEmpty(inShowUsername)) { }else { - result = username; + result = inShowUsername; } } } diff --git a/designer_base/src/com/fr/design/extra/modal-dialog.css b/designer_base/src/com/fr/design/extra/modal-dialog.css new file mode 100644 index 0000000000..be39df93b6 --- /dev/null +++ b/designer_base/src/com/fr/design/extra/modal-dialog.css @@ -0,0 +1,36 @@ +/** + * modal-dialog.css + * place in same directory as WebViewConfirm.java + * ensure your build system copies the file to your build output directory + */ + +.root { + -fx-glass-color: white; +} + +.modal-dialog { + -fx-padding: 20; + -fx-spacing: 10; + -fx-alignment: center; + -fx-font-size: 14; + -fx-background-color: linear-gradient(to bottom, derive(-fx-glass-color, 20%), -fx-glass-color); + -fx-border-color: derive(-fx-glass-color, -20%); + -fx-border-width: 5; + -fx-background-insets: 12; + -fx-border-insets: 10; + -fx-border-radius: 6; + -fx-background-radius: 6; +} + +.modal-dialog:pressed { + -fx-cursor: move; +} + +.modal-dialog .button:pressed { + -fx-cursor: default; +} + +.confirmation-results { + -fx-background-color: cornsilk; + -fx-padding: 5; +} \ No newline at end of file