JavaFX Gluon execute controller from view

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

Upvotes: 1

Views: 431

Answers (1)

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45476

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

Upvotes: 0

Related Questions