JavaFX Gluon从视图执行控制器

时间:2016-01-27 06:13:02

标签: javafx google-cloud-messaging gluon

我使用javafx gluon和GCM开发移动应用程序进行消息传递

当其他设备广播消息时,我想通过调用其控制器来刷新当前活动视图和传入消息。

我正在使用Afterburner框架。它也在gluon getting started

中描述

如何从扩展MobileApplication的类中的getView()中检索视图控制器?

这是我创建的课程:

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();
    }
}}

布局xml文件(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>

类DirectMessageView扩展了FXMLView

public class DirectMessageView extends FXMLView {
}

类DirectMessagePresenter实现Initializable

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

扩展MobileAplication的类SKSAplication

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())) {

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

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

提前谢谢

1 个答案:

答案 0 :(得分:0)

创建视图后,您可以检索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();
    });
}

无论您正在显示的是哪个视图,这都将有效。

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

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

假设在该演示者上你有控件来显示新值,你只需要在那里设置它们:

public void 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);
}

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);
}