Quantcast
Channel: SmartClient Forums
Viewing all articles
Browse latest Browse all 4756

How to Access Nested Components?

$
0
0
This a request for "Smart GWT 101" guidance on component parent/child navigation, walking the tree, so to speak.

The following code does what I want, but produces components that I can't figure out how to access.
Code:

package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

public class HowToAccessNestedComponents implements EntryPoint {

 DataSource dsAnimals = DataSource.get("animals");

 VLayout root;

 @Override
 public void onModuleLoad() {

  root = new VLayout() {
  {
    setID("root");
    setWidth100();
    setHeight100();
  }
  };

  // Dynamically supplied Tab Titles, might change during session...
  String[] arbitraryTabs = { "Carnivore", "Herbivore", "Omnivore" };

  root.addMember(makeTabSet(arbitraryTabs));

  root.draw();

 }

 /******************** makeTabSet ********************/
 private Canvas makeTabSet(String[] arbitraryTabs) {

  TabSet tabSet = new TabSet() {
  {
    setID("tabSetAnimals");
  }
  };

  for (String tabName : arbitraryTabs) {

  tabSet.addTab(makeTab(tabName));

  }

  return tabSet;
 }

 /******************** makeTab ********************/
 private Tab makeTab(final String tabName) {

  Tab tab = new Tab(tabName) {
  {
    setID("tabName" + tabName);
  }
  };

  tab.setPane(gainPane(tabName));

  return tab;
 }

 /******************** gainPane ********************/
 private Canvas gainPane(final String tabName) {

  VLayout pane = new VLayout() {
  {
    setID("pane" + tabName);
  }
  };

  pane.addMember(makeListGrid(tabName));

  return pane;
 }

 /******************** makeListGrid ********************/
 private Canvas makeListGrid(final String tabName) {

  ListGrid listGrid = new ListGrid(dsAnimals) {
  {
    setID("listGrid" + tabName);
    setInitialCriteria(new Criteria("diet", tabName));
    setAutoFetchData(true);
  }
  };

  return listGrid;
 }

}

Assuming the code approach is OK so far, I want to be able to access the nested components, perhaps like this?
Code:

// Pseudo code...
Record[] records = root.getMember("tabSetAnimals").getTab("tabCarnivore").getPane("paneCarnivore").getListGrid("listGridCarnivore").getSelectedRecords();

But the furthest I can seem to get with this approach is
Code:

// Half-Pseudo code...
root.getChildren().getsMurkyHere...
root.getChildren()[0].getWhat?
root.getMember("tabSetAnimals").getLostHere...

It seems I can avoid the issue by adding each nested ListGrid into a top-level ArrayList,
Code:

...
public class HowToAccessNestedComponents implements EntryPoint {
 ...
 ArrayList<ListGrid> gridsList = new ArrayList<ListGrid>();  // *

 ...
 
 private Canvas makeListGrid(final String tabName) {
 
 ListGrid listGrid = new ListGrid(dsAnimals) {
 ...
 gridsList.add(listGrid);  // *
 ...
 }

Ultimately it all goes into the GWT Compiler blender, but I still want to understand the fundamentals of how to properly access nested components in a given lineage. Or why I shouldn't attempt it this way.

Thanks for any guidance.

Viewing all articles
Browse latest Browse all 4756

Trending Articles