Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Events between components?

    Hi, we're really liking SmartGWT and we're finding it a great product. We are currently evaluating SmartGWT EE 1.2.1 and are working on a couple different modules in a single page. The list on the right side fills up with a number of Employees and has a button to create a work item for that Employee. That work item needs to show up in a list on the left side when it's clicked. Currently, I am able to click on the button and the logic tells the server to create that work item. I currently have to refresh the page to get the "fetch" to pull the data into the left side. I would like to have it so I can have that other modules be notified that something has changed and update that list. I am using the DMI mechanism and have a hashmap holding the data (before persisting it) and am putting the objects in the session. I have tried to grab the instance object from the session and populate that hashmap with no change in the GUI. Is there a listener I can use to communicate between the modules?

    Warm Regards,
    Brant
    Last edited by blevinson; 6 Dec 2009, 02:18.

    #2
    few options

    Cant comment on your exact situation. However, look at the eventbus component in SmartGWT extensions. It is a clean and easy way to do publish / subscribe events. I use it whenever I need multiple components to be messaged on the success of something.

    Another option for refresh automatically is using a Timer component from GWT. However, you wil incur some cycles though. I use that in dashboard-style pages where status is refreshed every 5 minutes or so...

    Good luck

    Strawman

    Comment


      #3
      RE: Events between components?

      Thank you for the info strawman! I have been attempting to use this for a while now (about 3 hrs) but can't get the EventBus to work. It would be great if you or somebody could help me out a bit. Here is the relevant code (my alerts are custom functions). I get an alert for both Subscribing (1st) and after I click the button and I get Publishing (2nd). I never get the "Event!" which should happen if it works properly:

      EffortList:
      Code:
       
      public class EffortList implements EntryPoint {
      ...
      public void onModuleLoad() {
      alert("Subscribing...");
      Subscription subscription = EventBus.subscribe("addReviewEvent", new TopicSubscriber<DataSource>() {
      public void onEvent(Subscription subscription, DataSource event) {
      alert("Event!");
      boundList.fetchData();
      EventBus.unsubscribe(subscription); 
      				}
      			});
      	 }
      }
      SearchEmployee:
      Code:
       
      public class SearchEmployee implements EntryPoint {
      public void onModuleLoad() {
      IButton createReviewBtn = new IButton("Create Effort");
      ...
      createReviewBtn.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
      		    alert("Published...");
      		    EventBus.publish("addReviewEvent", event);
      		}
      }
      Regards,
      Brant

      Comment


        #4
        Why are you trying to load multiple different gwt modules in one page? Just have one module with only one onModuleLoad function that creates a smartgwt layout that has multiple widgets in it.

        Comment


          #5
          events

          Yeah, I am not sure why you have multiple Entrypoints..


          However, the subscribing class must implement "TopicSubscriber" interface.

          Strawman

          Comment


            #6
            Hi ,
            I am new to gwt and also to the eventbus.
            I am trying to implement the eventbus and i am not sure how to get the implement event subscriber.So this is what i am doing.

            I want the login displayed on the lower part of the panel when user clicks on "GO" link.
            So in HeaderMenu class, onClick method , i fire the event
            In SearchResultPanel, i want to listen to that event and display the name as "Welcome: blah blah blah"

            2 things here which i am having questions
            Question 1. When i implement TopicSubscriber<AppUser>, i have to implement the following method

            public void onEvent(Subscription subscription, AppUser event) {
            // TODO Auto-generated method stub
            userName = event.getUserName();
            SC.say("This event is not getting fired :"+event.getUserName());
            }

            This event doesnot get fired.Why is that?

            Question 2.
            If i add this as a part of the constructor, it does get called but the userName on th searchPanel still remains null
            public SearchResultPanel(){

            Subscription subscription = EventBus.subscribe("authenticateUser", new TopicSubscriber<AppUser>() {

            public void onEvent(Subscription subscription, AppUser user) {
            // TODO Auto-generated method stub
            userName = user.getUserName();
            SC.say("event called at the time of constructor:"+user.getUserName());
            }

            });



            }


            ********************
            This is my Subscriber class
            ********************
            package com.myapp.web.gwt.client.search;

            import com.myapp.web.gwt.client.eventbus.EventBus;
            import com.myapp.web.gwt.client.eventbus.Subscription;
            import com.myapp.web.gwt.client.eventbus.TopicSubscriber;
            import com.myapp.web.gwt.shared.vo.authentication.AppUser;
            import com.smartgwt.client.util.SC;
            import com.smartgwt.client.widgets.Canvas;
            import com.smartgwt.client.widgets.Label;
            import com.smartgwt.client.widgets.layout.VLayout;
            import com.smartgwt.client.widgets.tab.Tab;
            import com.smartgwt.client.widgets.tab.TabSet;

            public class SearchResultPanel implements TopicSubscriber<AppUser>{

            static String userName ;
            public SearchResultPanel(){

            Subscription subscription = EventBus.subscribe("authenticateUser", new TopicSubscriber<AppUser>() {

            public void onEvent(Subscription subscription, AppUser user) {
            // TODO Auto-generated method stub
            userName = user.getUserName();
            SC.say("event called at the time of constructor:"+user.getUserName());
            }

            });



            }

            public Canvas getBasicSearchResults() {

            VLayout testLayout = new VLayout();
            Label welcome =null;
            welcome =new Label("Welcome "+userName);
            TabSet tabs = new TabSet();
            Tab tab = new Tab("Current Status " );
            tab.setCanClose(false);
            tabs.addTab(tab);
            tabs.setWidth("100%");
            tabs.setHeight("100%");
            testLayout .addMember(welcome);
            testLayout .addMember(tabs);
            return testLayout ;
            }

            public void onEvent(Subscription subscription, AppUser event) {
            // TODO Auto-generated method stub
            userName = event.getUserName();
            SC.say("This event is not getting fired :"+event.getUserName());
            }

            }


            **********************************
            This is the MainClass
            **********************************
            package com.myapp.web.gwt.client;

            import com.google.gwt.core.client.EntryPoint;
            import com.google.gwt.core.client.GWT;
            import com.myapp.web.gwt.client.header.HeaderMenu;
            import com.myapp.web.gwt.client.search.SearchResultPanel;
            import com.smartgwt.client.widgets.layout.VLayout;

            /**
            * Entry point classes define <code>onModuleLoad()</code>.
            */
            public class MyappMain implements EntryPoint {
            /**
            * The message displayed to the user when the server cannot be reached or
            * returns an error.
            */
            private static final String SERVER_ERROR = "An error occurred while "
            + "attempting to contact the server. Please check your network "
            + "connection and try again.";

            /**
            * Create a remote service proxy to talk to the server-side Myapp service.
            */
            private final MyappServiceGWTWrapperAsync myappService = GWT.create(MyappServiceGWTWrapper.class);

            /**
            * This is the entry point method.
            */
            public void onModuleLoad() {
            VLayout fullPage= new VLayout();
            fullPage.setBackgroundColor("#D8D8D8");
            fullPage.setWidth("100%");
            fullPage.setHeight("100%");
            fullPage.addChild(HeaderMenu.getHeaderMenuPage());
            SearchResultPanel searchResultPanel = new SearchResultPanel();
            fullPage.addChild(searchResultPanel.getBasicSearchResults());
            fullPage.draw();
            }

            // Create a handler for the sendButton and nameField
            }

            **********************************
            This is the Class who is publishing the event
            **********************************
            package com.myapp.web.gwt.client.header;

            import com.myapp.web.gwt.client.eventbus.EventBus;
            import com.myapp.web.gwt.shared.vo.authentication.AppUser;
            import com.smartgwt.client.types.Alignment;
            import com.smartgwt.client.util.SC;
            import com.smartgwt.client.widgets.Canvas;
            import com.smartgwt.client.widgets.Img;
            import com.smartgwt.client.widgets.form.DynamicForm;
            import com.smartgwt.client.widgets.form.fields.LinkItem;
            import com.smartgwt.client.widgets.form.fields.PasswordItem;
            import com.smartgwt.client.widgets.form.fields.SpacerItem;
            import com.smartgwt.client.widgets.form.fields.TextItem;
            import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
            import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
            import com.smartgwt.client.widgets.layout.HLayout;
            import com.smartgwt.client.widgets.layout.LayoutSpacer;

            public class HeaderMenu {

            public static HLayout getHeaderMenuPage(){
            HLayout headerMenu = new HLayout();
            headerMenu.setWidth("100%");
            headerMenu.setHeight("5%");

            headerMenu.setBackgroundColor("white");
            Img myLogo = new Img();
            myLogo .setSrc("http://www.abc.com/img/LOGO.JPG");
            myLogo .setHeight("100%");


            LayoutSpacer layoutSpacer = new LayoutSpacer();
            layoutSpacer .setWidth("75%");
            layoutSpacer.setBackgroundColor("black");
            Canvas loginCanvas= getLogin();

            headerMenu.addMember(myLogo);
            headerMenu.addMember(layoutSpacer);
            headerMenu.addMember(loginCanvas);
            return headerMenu;

            }


            public static HLayout getLogin() {
            HLayout loginLayout = new HLayout();
            final DynamicForm loginForm = new DynamicForm();

            final TextItem loginText = new TextItem("loginId");
            loginText .setTitle("Login");
            loginText.setValue("sdss");
            final PasswordItem passwordItem = new PasswordItem ("password");
            passwordItem .setTitle("Password");

            LinkItem submitItem = new LinkItem ("");
            //submitItem .setTitle("LinkItem");
            submitItem .setName("");
            submitItem .setLinkTitle("Go");
            submitItem .setShouldSaveValue(false);
            submitItem .addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event) {
            // TODO Auto-generated method stub
            if (loginForm.getValue("loginId")!=null && loginForm.getValue("loginId").toString().length()>0 &&
            loginForm.getValue("password")!=null && loginForm.getValue("password").toString().length()>0 ){

            AppUser user = new AppUser ();
            System.out.println("im here "+loginForm.getValue("loginId").toString());
            user .setUserName(loginForm.getValue("loginId").toString());
            user .setPassword(loginForm.getValue("password").toString());
            EventBus.publish("authenticateUser", user);
            } else if(loginForm.getValue("loginId")== null || loginForm.getValue("loginId").toString().trim().length() ==0){
            SC.warn("Login Id is required");

            }else if(loginForm.getValue("password")== null || loginForm.getValue("password").toString().trim().length() ==0){
            SC.warn("Password is required");
            }
            }});

            loginForm.setBackgroundColor("");
            loginForm.setNumCols(8);


            loginForm.setID("loginForm");
            SpacerItem spItem = new SpacerItem();
            spItem .setWidth(1);
            loginForm.setItems(loginText,passwordItem,spItem ,submitItem);
            loginForm.setAlign(Alignment.RIGHT);
            loginLayout.addChild(loginForm);
            loginLayout.setWidth("100%");
            return loginLayout;
            }
            }

            Comment


              #7
              Hello.
              The code is too complex to use it as a test case.
              Try to simplify things and start from that to find out what actually does not work.

              Comment


                #8
                i was able to fix my issue but on the pther hand i also saw that we subscribe method is allowing mutiple entries of the same class so i updated the subscribe method as follow

                public static Subscription subscribe(String topic,
                TopicSubscriber<?> listener) {
                Subscription subscription = new Subscription(topic, listener);
                List<Subscription> topicSubscribers = topicSubscribersMap.get(topic);
                if (topicSubscribers == null) {
                topicSubscribers = new ArrayList<Subscription>();
                topicSubscribersMap.put(topic, topicSubscribers);
                }
                boolean subscriberExists= false;
                for (int iLocal=0 ; iLocal<topicSubscribers.size();iLocal++) {
                Subscription localSubscription = (Subscription) topicSubscribers.get(iLocal);
                if (localSubscription.getListener().getClass().toString() .equals(subscription.getListener().getClass().toString())){
                subscriberExists = true;
                }
                }
                if (!subscriberExists ){
                topicSubscribers.add(subscription);
                }
                return subscription;
                }

                Comment

                Working...
                X