JavaFX Gluon从视图执行控制器 [英] JavaFX Gluon execute controller from view

查看:79
本文介绍了JavaFX Gluon从视图执行控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用javafx gluon和GCM进行消息传递的移动应用程序当其他设备广播消息时,我想通过调用刷新消息来刷新当前活动视图其控制器。



我使用 Afterburner 框架。它也在 gluon入门中描述



如何在类中扩展MobileApplication从getView()中获取视图控制器?



这里是我创建的类:

  public class MyGCMListenerService extends GcmListenerService {
private final String NOTIFICATION_TAG =NotificationExample;

public MyGCMListenerService(){
}
$ b $ @Override
public void onMessageReceived(String from,Bundle data){
String varMessage = data.getString( 消息);
尝试{
JSONObject json = new JSONObject(varMessage);

字符串messageContent = getStringFromJSON(json,message);
Integer senderId = getIntegerFromJSON(json,senderId);
字符串senderName = getStringFromJSON(json,senderName);
String comId = getStringFromJSON(json,communityId);

SKSApplication.getInstance()。doRefreshMessageUI(messageContent,senderId,senderName,comId);
} catch(JSONException e){
e.printStackTrace();

}}

layout xml file(directmessage.fxml) p>

 <查看xmlns:fx =http://javafx.com/fxml/1fx:id =directMessageViewprefHeight =600.0prefWidth =400.0
xmlns =http://javafx.com/javafx/8.0.40
fx:controller =com.tenma.mobile.message.directmessage.DirectMessagePresenter >
< / View>

类DirectMessageView扩展FXMLView

  public class DirectMessageView extends FXMLView {
}

DirectMessagePresenter类实现可以初始化

  public class DirectMessagePresenter实现了初始化{
public void displayIncomingMessage(String messageContent,Integer senderId,String senderName,String comId ){
//保存到内部数据库
//显示消息
}
}

继承MobileAplication的类SKSAplication

  public class SKSApplication extends MobileApplication {
private static SKSApplication实例;
public static final String DIRECT_MESSAGE_VIEW =DIRECT_MESSAGE_VIEW;
public static final String GROUP_MESSAGE_VIEW =GROUP_MESSAGE_VIEW;

public SKSApplication(){
instance = this;
}

public static SKSApplication getInstance(){
return instance;

$ b addViewFactory(HOME_VIEW,() - > {
HomeView homeView = new HomeView();
homePresenter =(HomePresenter)homeView.getPresenter();
return(View)homeView.getView();
});
$ b addViewFactory(DIRECT_MESSAGE_VIEW,() - > {
DirectMessageView directMessageView = new DirectMessageView();
return(View)directMessageView.getView();
}) ;
$ b addViewFactory(GROUP_MESSAGE_VIEW,() - > {
GroupMessageView groupMessageView = new GroupMessageView();
return(View)groupMessageView.getView();
}) ;
$ b $ public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName,String comId){
if(DIRECT_MESSAGE_VIEW.equals(getView()。getName())){

这里我尝试从getView()里面的doRefreshMessageUI方法中检索直接消息视图,但它确实是
不能工作

  DirectMessageView视图=(DirectMessageView)getView(); 
DirectMessagePresenter演示者=(DirectMessagePresenter)view.getPresenter();
presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
}
}
}

p>

解决方案

您可以在创建视图后检索View的演示者:

 私人DirectMessagePresenter演示者; 
$ b $ @Override
public void init(){
addViewFactory(HOME_VIEW,() - > {
HomeView homeView = new HomeView();
homePresenter =(HomePresenter)homeView.getPresenter();
return(View)homeView.getView();
});
$ b addViewFactory(DIRECT_MESSAGE_VIEW,() - > {
DirectMessageView directMessageView = new DirectMessageView();
presenter =(DirectMessagePresenter)directMessageView.getPresenter();
return (View)directMessageView.getView();
});
}

无论当前显示哪个视图, p>

稍后,当您想传递一些新值时,您可以使用您的方法:

  public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName,String comId){
presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
}

假设在演示者上您可以控制显示新值,只需要设置它们:

pre $ public $ displayIncomingMessage(String messageContent,Integer senderId,String senderName,String comId){
labelContent.setText(messageContent);
labelSenderId.setText(senderId.toString());
labelName.setText(senderName);
labelComId.setText(comId);
}

同样,您可以使用JavaFX属性:

  private final StringProperty content = new SimpleStringProperty(); 
private final IntegerProperty senderId = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();
private final StringProperty comId = new SimpleStringProperty();

@Override
public void initialize(URL url,ResourceBundle rb){
labelContent.textProperty()。bind(content);
labelSenderId.textProperty()。bind(senderId.asString());
labelName.textProperty()。bind(name);
labelComId.textProperty()。bind(comId);

$ b $ public void displayIncomingMessage(String messageContent,Integer senderId,String senderName,String comId){
this.content.set(messageContent);
this.senderId.set(senderId);
this.name.set(senderName);
this.comId.set(comId);
}


I'm develop mobile apps using javafx gluon and GCM for messaging

when other device broadcast a message, I want to refresh current active view with incoming message by calling its controller.

i'm using Afterburner framework. it also described in gluon getting started

How to retreive view controller from getView() in class that extend MobileApplication?

here are classes I have created:

public class MyGCMListenerService extends GcmListenerService {
private final String NOTIFICATION_TAG = "NotificationExample";

public MyGCMListenerService() {
}

@Override
public void onMessageReceived(String from, Bundle data) {
String varMessage = data.getString("message");
    try {
        JSONObject json = new JSONObject(varMessage);

        String messageContent = getStringFromJSON(json, "message");
        Integer senderId = getIntegerFromJSON(json, "senderId");
        String senderName = getStringFromJSON(json, "senderName");
        String comId = getStringFromJSON(json, "communityId");

        SKSApplication.getInstance().doRefreshMessageUI(messageContent,senderId,senderName,comId );
    } catch (JSONException e) {
        e.printStackTrace();
    }
}}

layout xml file (directmessage.fxml)

<View xmlns:fx="http://javafx.com/fxml/1" fx:id="directMessageView" prefHeight="600.0" prefWidth="400.0"
  xmlns="http://javafx.com/javafx/8.0.40"
  fx:controller="com.tenma.mobile.message.directmessage.DirectMessagePresenter">
</View>

Class DirectMessageView extends FXMLView

public class DirectMessageView extends FXMLView {
}

Class DirectMessagePresenter implements Initializable

public class DirectMessagePresenter implements Initializable{
    public void displayIncomingMessage(String messageContent,Integer    senderId,String senderName, String comId){
    //save to internal db
    // display message
    }
}

Class SKSAplication that extends MobileAplication

public class SKSApplication extends MobileApplication{
   private static SKSApplication instance;
   public static final String DIRECT_MESSAGE_VIEW = "DIRECT_MESSAGE_VIEW";
   public static final String GROUP_MESSAGE_VIEW = "GROUP_MESSAGE_VIEW";

   public SKSApplication() {
       instance = this;
   }

   public static SKSApplication getInstance() {
       return instance;
   }

   addViewFactory(HOME_VIEW, () -> {
        HomeView homeView = new HomeView();
        homePresenter = (HomePresenter) homeView.getPresenter();
        return (View) homeView.getView();
    });

    addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
        DirectMessageView directMessageView = new DirectMessageView();
        return (View) directMessageView.getView();
    });

    addViewFactory(GROUP_MESSAGE_VIEW, () -> {
        GroupMessageView groupMessageView = new GroupMessageView();
        return (View) groupMessageView.getView();
    });

    public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName, String comId ) {
       if (DIRECT_MESSAGE_VIEW.equals(getView().getName())) {

here I try to retreive Direct message view from getView() inside doRefreshMessageUI method, but it does not work

      DirectMessageView view = (DirectMessageView) getView(); 
          DirectMessagePresenter presenter = (DirectMessagePresenter) view.getPresenter();
presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
    }
}
 }

Thank you in advance

解决方案

You can retrieve the View's presenter once you create the view:

private DirectMessagePresenter presenter;

@Override
public void init() {
    addViewFactory(HOME_VIEW, () -> {
        HomeView homeView = new HomeView();
        homePresenter = (HomePresenter) homeView.getPresenter();
        return (View) homeView.getView();
    });

    addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
        DirectMessageView directMessageView = new DirectMessageView();
        presenter = (DirectMessagePresenter) directMessageView.getPresenter();
        return (View) directMessageView.getView();
    });
}

This will work no matter which is the current view you are showing.

Later on, when you want to pass some new values you can use your method with it:

public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName, String comId ) {
   presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
}

Assuming that on that presenter you have the controls to display the new values, you just need to set them there:

public void displayIncomingMessage(String messageContent, Integer senderId, String senderName, String comId){
    labelContent.setText(messageContent);
    labelSenderId.setText(senderId.toString());
    labelName.setText(senderName);
    labelComId.setText(comId);
}

As well, you can use JavaFX properties:

private final StringProperty content = new SimpleStringProperty();
private final IntegerProperty senderId = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();
private final StringProperty comId = new SimpleStringProperty();

@Override
public void initialize(URL url, ResourceBundle rb) {
    labelContent.textProperty().bind(content);
    labelSenderId.textProperty().bind(senderId.asString());
    labelName.textProperty().bind(name);
    labelComId.textProperty().bind(comId);
}

public void displayIncomingMessage(String messageContent, Integer senderId, String senderName, String comId){
    this.content.set(messageContent);
    this.senderId.set(senderId);
    this.name.set(senderName);
    this.comId.set(comId);
}

这篇关于JavaFX Gluon从视图执行控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆