Announcement

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

    comunication between 2 different classes

    hi i'm new to use smartgwt and i need some help

    i have 2 class

    first class button and second class tab

    each is put in different layer

    i need to click the button in first class and send the command to open tab in second class

    some firends have tell me i must use getMember but i need more help

    anyone have a example to see ? or some other help?

    thx you very very much for answer

    #2
    Umm I think you need to be more specific. This is just a very general JAVA programming question, has nothing to do with smartgwt. I would recommend either post some simplified code of what your doing or better yet find a tutorial on calling object methods in JAVA.

    You can use Override or pass in parameters or any number of tricks.

    Comment


      #3
      ok i try to put some code
      i have main panel

      Code:
      public class MainPanel extends VLayout {
      
          private static MainPanel istance = null;
      
          public static MainPanel getInstance() {
      
              if (istance == null) {
                  istance = new MainPanel();
      
              }
              return istance;
          }
      
      private MainPanel() {
      
      VLayout vLayout = new VLayout();
      
              HLayout top = new HLayout(2); // top layer
              top.setID("TOP");
      
              HLayout down = new HLayout(2); // down layer
              down.setID("DOWN");
              
              ButtonPanel button = new ButtonPanel(); //class with button
              button.setID("BUTTON");
      
              TabPanel tab = new TabPanel();// class with tab
              tab.setID("TABLAYOUT");
              
              top.addMember(button);
              down.addMember(tab);
              vLayout.addMember(top);
              vLayout.addMember(down);
              addMember(vLayout);
      this is tab class called "TabPanel"
      Code:
       final TabSet toptabset = new TabSet();
              toptabset.setID("TABSET");
      
              toptabset.setTabBarPosition(Side.TOP);
              toptabset.setTabBarAlign(Side.LEFT);
              toptabset.setWidth100();
              toptabset.setHeight100();
      
              Tab maintab = new Tab("MAIN", "");
              maintab.setID("MAINTAB");
              MainTabPanel startTab = new MainTabPanel();
              maintab.setPane(startTab); 
              toptabset.addTab(maintab);  //add a new tab to toptabset
      this is button panel
      Code:
       IButton hideButton = new IButton();
              hideButton.setID("SHOW");
              hideButton.setTitle("show");
              hideButton.addClickHandler(new ClickHandler() {
      
                  public void onClick(ClickEvent event) {
                       
                    WHAT I PUT HERE FOR send command to open another tab in tab class?????????
      
      
                  }
              });
      Last edited by jbrown; 9 Jul 2010, 01:14.

      Comment


        #4
        Help me please

        Comment


          #5
          Your code makes little sense and your using terms Im not sure you fully understand. All you would have to do is call toptabset.addTab() and be careful of using Singletons if you don't have a strong reasons for it.

          Comment


            #6
            toptabset.addTab()

            not run in button class because toptabset is declared in tabPanel class

            some friend have tell me about a function called getMember but i need more info about that
            Last edited by jbrown; 9 Jul 2010, 10:34.

            Comment


              #7
              How about your Button class constructor takes a reference to your Tab class. Then you can access it later.

              Code:
              class MyButton {
                private TabSet myTab = null;
              
                 public MyButton(MyTab t) {
                   myTab = t;
                 }
              }
              Again this is just a basic JAVA programming issue. You could use getMember() but your MainPanel has multiple nested layouts, so you'll have mutiple nested getMember() calls before you arrive at the TabSet.

              Comment


                #8
                sorry but i don't understand

                this is class button

                Code:
                public class ButtonPanel extends VLayout {
                
                    public ButtonPanel() {
                
                        IButton hideButton = new IButton();
                        hideButton.setID("SHOW");
                        hideButton.setTitle("show");
                        hideButton.addClickHandler(new ClickHandler() {
                
                            public void onClick(ClickEvent event) {
                                 
                              WHAT I PUT HERE FOR send command to open another tab in tab class?????????
                
                
                            }
                        });
                
                addMember(hideButton);
                }
                the onclick method i think had send something for command at class TabPanel to open other tab ( if i put the button in same class it run but i need to do different class)

                http://www.smartclient.com/smartgwt/...abs_add_remove
                in this example run becasue is all in one
                but i have 3 class (Main panel, buttonpanel and tabpanel)
                Last edited by jbrown; 9 Jul 2010, 11:00.

                Comment


                  #9
                  You need to buy a JAVA book.
                  Code:
                  public class ButtonPanel extends VLayout {
                      public ButtonPanel(final TabPanel p) {
                          IButton hideButton = new IButton();
                          hideButton.setID("SHOW");
                          hideButton.setTitle("show");
                          hideButton.addClickHandler(new ClickHandler() {
                              public void onClick(ClickEvent event) {
                                // ...
                                p.addTab(...);
                              }
                          });
                  ...
                  MyTabPanel p = new MyTabPanel();
                  MyButtonPanel b = new MyButtonPanel(p);

                  Comment


                    #10
                    If I may lend a hand, svjard...

                    Another design pattern that might be handy and that I've used quite a bit in my SmartGWT application is the concept of an EventBus. This allows a degree of separation between different smartGWT components where passing references no longer is practical.

                    Basic GWT provides a HandlerManager class you can use for this, i.e.

                    http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/event/shared/HandlerManager.html

                    The use of the HandlerManager is described in the GWT MVP design pattern. Not all aspects of the MVP design pattern make sense for SmartGWT (certainly the division of labor between a Presenter and a View is too cumbersome to use with SmartGWT) but the centralized eventBus design pattern is easily added to SmartGWT

                    Here is some sample code:

                    Event:

                    Code:
                    public class SearchEvent extends GwtEvent<SearchEventHandler> {
                    	public static Type<SearchEventHandler> TYPE= new Type<SearchEventHandler>();
                    	protected String searchString;
                    	
                    	
                    	
                    	public SearchEvent(String searchString) {
                    		super();
                    		this.searchString= searchString;
                    	}
                    
                    
                    	@Override
                    	protected void dispatch(SearchEventHandler handler) {
                    		handler.onSearch(this);
                    		
                    	}
                    
                    	@Override
                    	public Type<SearchEventHandler> getAssociatedType() {
                    		return TYPE;
                    	}
                    
                    	/**
                    	 * @return the search
                    	 */
                    	public String getSearchString() {
                    		return searchString;
                    	}
                    
                    	
                    	public void setSearchString(String searchString) {
                    		this.searchString= searchString;
                    	}
                    EventHandler:

                    Code:
                    public interface SearchEventHandler extends EventHandler {
                    	public void onSearch(SearchEvent event);
                    
                    }
                    Component that generates events:

                    Code:
                    public class Filter extends HStack 
                    {
                    
                    private DynamicForm form;
                    private TextItem searchField;
                    private ButtonItem searchButton;
                    private FilterController controller;
                    
                    public FilterView(HandlerManager eventBus)
                    {
                      form = new DynamicForm();
                      searchField= new TextItem();
                      searchField.setTitle("Enter search term:");
                      searchButton = new searchButton();
                      searchButton.setTitle("Search");
                    
                    controller = new FilterController();
                    controller.bind();
                    }
                    
                    
                    private class FilterController
                    {
                    
                      HandlerManager eventBus;
                      public FilterController(HandlerManager eventBus)
                     {
                      this.eventBus = eventBus;
                      }
                    
                      public void bind()
                     {
                        searchButton.addClickHandler(new ClickHandler(){
                        @Override
                        public void onClick(ClickEvent event)
                       {
                         eventBus.fireEvent(new SearchEvent(searchField.getValue());
                       }
                    });
                     }
                    }
                    
                    }
                    Component that receives events:

                    Code:
                    public class SearchResults extends ListGrid implements SearchEventHandler{
                    
                    public SearchResults(HandlerManager eventBus)
                    {
                     DataSource dataSource = dataSource.get("myDS");
                     this.setDataSource(dataSource);
                    
                    eventBus.addHandler(SearchEvent.TYPE, this);
                    }
                    
                    @Override 
                    public void onSearch(SearchEvent searchEvent)
                    {
                      Criteria criteria = new Criteria();
                    criteria.add("text", searchEvent.getSearchString());
                    this.fetchData(criteria);
                    }
                    
                    }

                    Explanation:

                    The Filter component and the SearchResults component in this sample code don't know about each other and don't need direct references to one another. However, because the SearchResults component is registered with the eventBus as an eventHandler for SearchEvents, it is notified when a search occurs. The Filter component in turn generates Search events and fires these to eventBus; without needing to know what components receive these events and what they do with them

                    Obviously, this is totally overkill for this simplied example; you could just put the ListGrid and the form for searching into one component. But as your application grows in complexity; and especially if you need multiple components to do something based on a user action, this design pattern starts making a lot more sense (for instance, let's say you had 4 listgrids, all which had to be updated based on a single user action).

                    Even then, not every event needs to pass through the EventBus. For instance, I have an application where users can create tabs with listgrids on them on the fly and in this type of scenario, an eventBus is the only pratical way to handle updating multiple listgrids across multiple tabs based on a user action; but something that happens within the tab, I still wire directly without involving the eventBus. You just need to use common sense.

                    One final caveat - if you have components that are not just created on the fly but also removed on the fly (for instance, tabs that can be added and closed), it's important to add logic to unregister components from the eventBus when they are removed. If you don't do this, they will continue to receive events and execute logic, even though they aren't displayed anymore. They also can't be garbage collected as a reference remains to them via the eventBus. I haven't shown this in the code to try and keep the example simple; but it's definitely worth implementing.

                    Hope this helps.

                    Comment


                      #11
                      Originally posted by svjard
                      You need to buy a JAVA book.
                      Code:
                                    p.addTab(...);
                                  }
                              });
                      ...
                      MyTabPanel p = new MyTabPanel();
                      MyButtonPanel b = new MyButtonPanel(p);
                      sorry but is not so easy

                      not run because addTab is not a method of myclass but is make in a standard class call TabSet make by smartgwt

                      Comment


                        #12
                        Then make it a method. Your problem is that you have no member variables besides you don't understand very basic Object-Oriented programming concepts.
                        Code:
                        public class TabPanel extends VLayout {
                          private TabSet myTabSet = null;
                        
                          public TabPanel() {
                            myTabSet = new TabSet();
                          }
                        
                          public void addTab(Tab t) {
                            myTabSet.addTab(t);
                          }
                        }
                        
                        public class ButtonPanel extends VLayout {
                            public ButtonPanel(final TabPanel p) {
                                IButton hideButton = new IButton();
                                hideButton.setID("SHOW");
                                hideButton.setTitle("show");
                                hideButton.addClickHandler(new ClickHandler() {
                                    public void onClick(ClickEvent event) {
                                      // ...
                                      p.addTab(...);
                                    }
                                });
                        ...
                        MyTabPanel p = new MyTabPanel();
                        MyButtonPanel b = new MyButtonPanel(p);

                        Comment


                          #13
                          not solve the problem because i have a chaos in the panel ( for example panel open but empty) i think is the comunication with main pannel between the 2 classes

                          Comment


                            #14
                            ok i done but not run correctly

                            this is my code
                            Code:
                            public class TabPanel extends VLayout {
                            
                              final TabSet toptabset = new TabSet();
                            
                              public TabPanel() {
                                        Tab welcomeTab = new Tab("WELCOME TAB");// WELCOME TAB
                                       toptabset.addTab(welcomeTab);
                              }
                            
                              public void addTab(Tab t) {
                                myTabSet.addTab(t);
                              }
                               addMember(toptabset);
                               show();
                            }
                            when I start the program I should see only ONE WELCOME TAB , but instead displays 4 (FOUR) all the same
                            WELCOME TAB ,WELCOME TAB,WELCOME TAB,WELCOME TAB

                            why happen this? i call only one toptabset.addTab(maintab);
                            Last edited by jbrown; 12 Jul 2010, 03:35.

                            Comment


                              #15
                              any suggestion?

                              i dont now why when i put the methods on my code now apperars four tab at start when i call only one tab

                              maybe exist in java a method for stop the execution or other thing?

                              thx for help

                              Comment

                              Working...
                              X