|
Using lazy-loading |
|
WebGalileo Tree supports useful feature of lazy loading. It means that the Tree with its data model is not fully loaded and initialized on the client side but partially after corrresponding user requests.
When user clicks on the image that is responsible for branch opening the tree sends request to server and gets necessary data.
For additional convenience , it's possible to update tree data model at the moment of lazy loading by implementing corresponding listener. It can be useful if the data model is not fully initialized on the server side (i.e. data model is stored in data base or dynamically created).
Lazy loading mode is useful also if you want to minimize the traffic beetween sever and client side ehnancing the overall user experience(usability) with an application.
Base lazy loading mode
If you have data model fully initialized on the server side or defined in JSP page you should set useServerSideLazyLoad="true" in Tree tag. This way tree will be automatically updated and rendered from server side data model by user requests.
Example of tree definition:
<tree:tree id="frameWorkContent1"
varName="frameWorkContent"
rootNodeVisible="false"
useServerSideLazyLoad="true"
lazyLoadTimeout="2000"
nodeImageHeight="20"
fontFamily="Comic Sans MS"
fontStyle="oblique">
<tree:treeData model="frameWorkContentDataModel" />
<tree:treeSelection model="treeSelectionModel13"/>
</tree:tree>
Advaced lazy loading mode
In this mode you have to define your implementation of listener to update tree data model dynamically by user request from the client side.The mode allows to perform only necessary data model update and avoid initial full data model initialization as in the base lazy loading mode(see the item above). This is very useful when you have very big hierarchical data models.
You should set treeBranchOpenedListener property to specify your lazy loading listener. It must contain fully qualified listener class name. This listener class must implement TreeBranchOpenedListener.
Example of tree definition:
<tree:tree id="frameWorkContent"
varName="frameWorkContent"
rootNodeVisible="true"
nodeImageHeight="20"
fontFamily="Comic Sans MS"
fontStyle="oblique"
nodeStatusText="Point mouse over to expose/collapse"
treeBranchOpenedListener="testingapplication.tree.MyTreeBranchOpenedListener"
serverSideAction="true">
<tree:treeData model="JSFLazyLoadingDataModel" scope="session" />
</tree:tree>
Example of listener
package testingapplication.tree;
import com.softaspects.jsf.component.tree.event.TreeBranchOpenedEvent;
import com.softaspects.jsf.component.tree.event.TreeBranchOpenedListener;
import com.softaspects.jsf.component.tree.Tree;
import com.softaspects.jsf.component.tree.TreeItem;
import javax.faces.event.AbortProcessingException;
public class MyTreeBranchOpenedListener implements TreeBranchOpenedListener {
public MyTreeBranchOpenedListener () {
}
static int count = 4;
public void processTreeBranchOpened ( TreeBranchOpenedEvent event )
throws AbortProcessingException {
System.out.println ( "MyTreeBranchOpenedListener called ! " +
"Component id = " + event.getComponent ().getId () );
Tree tree = (Tree) event.getComponent();
String selection = event.getFolderToOpenIndex();
System.out.println("selection = " + selection);
TreeItem parent = (TreeItem) tree.getDataModel().getElementAt(selection);
TreeItem child_1 = new TreeItem();
child_1.setType("folder");
child_1.setData("CHILD FOLDER " + count);
child_1.setDescription("CHILD FOLDER " + count);
System.out.println(child_1.getDescription() + " added");
count++;
TreeItem child_2 = new TreeItem();
child_2.setType("folder");
child_2.setData("CHILD FOLDER " + count);
child_2.setDescription("CHILD FOLDER " + count);
System.out.println(child_2.getDescription() + " added");
count++;
TreeItem child_3 = new TreeItem();
child_3.setType("document");
child_3.setData("CHILD ITEM " + count);
child_3.setDescription("CHILD ITEM " + count);
System.out.println(child_3.getDescription() + " added");
count++;
parent.addElement(child_1);
parent.addElement(child_2);
parent.addElement(child_3);
}
}
Note:
Make sure that the both lazy loading modes aren't used at the same time.
This means that if you have useServerSideLazyLoad set to true and treeBranchOpenedListener
then Tree won't function correctly.