I've developed an asynchronous gwt tree, which means the whole tree is not loaded from the begining when the application is started.
-- Nodes are loaded as you open up the tree deeper.
This was developed to minimize caching,and reduce time to start application.
The down side is that when I'm opening my tree to a certain depth, and then close the root, the tree doesn't remember which nodes were open since its asynchronous.
I was trying to keep a map of 'paths' open, however it would not cover all pissiblities.
I'm looking for ideas as to how I can remember which nodes were open, or maybe some gwt configuration I'm not aware of.
Thank you, please see my code attached.
package com.corena.lifeweb.gwt.client.modules.TOC;
import java.io.Serializable;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//import com.treecell.TreeCell.ColorCell.Templates;
import com.aerosoftsys.digidocCMS.TOC.TOC_Item;
import com.corena.lifeweb.gwt.client.RpcClient;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safecss.shared.SafeStyles;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TreeNode;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ImageBundle.Resource;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TreeImages;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.TreeViewModel;
public class TreeCell implements EntryPoint {
private static RpcClient rpc = RpcClient.getInstance();
// Create a data provider.
static class MyDataProvider extends AsyncDataProvider<TOC_Item>{
private TOC_Item dir;
public MyDataProvider(Object currDir){
dir = (TOC_Item)currDir;
}
@Override
protected void onRangeChanged(final HasData<TOC_Item> display) {
//com.google.gwt.user.client.Window.alert("Testing Closing2");
rpc.getValueObject("com.corena.lifeweb.gwt.server.TOC.TOC_ItemProcessor", "getSubItem", new Serializable[]{dir}, new AsyncCallback<Serializable>(){
public void onFailure(Throwable caught) {
//show error message as popup
com.google.gwt.user.client.Window.alert("Error in Loading TOC:" + dir);
}
public void onSuccess(Serializable _result) {
if (dir != null)
{
node = dir.getName();
if (!openDirs.contains(node))
{
openDirs.add(node);
}
//com.google.gwt.user.client.Window.alert("Node:" + node);
String path = "";
for (int i = 0 ; i < openDirs.size() ; i++)
{
if (!openDirs.get(i).equals("ROOT"))
{
path = path + "/" + openDirs.get(i);
}
}
com.google.gwt.user.client.Window.alert("Path:" + path);
setPath("ROOT" + path + "/", rootDir);
}
List<TOC_Item> result = (List<TOC_Item>)_result;
display.setRowData(display.getVisibleRange().getStart(), result);
// com.google.gwt.user.client.Window.alert("Open String: " + open );
String open = newopen.substring(0, newopen.indexOf('/'));
for (int i = 0; i < result.size(); i++){
String treeopen = result.get(i).getName();
if (open.equals(treeopen)){
newopen = newopen.substring(newopen.indexOf('/') + 1);
rootNode = rootNode.setChildOpen(i, true);
}
}
}
});
}
};
static class DirectoryCell extends AbstractCell<TOC_Item> {
interface Templates extends SafeHtmlTemplates {
//@SafeHtmlTemplates.Template("<input type='text' value='tc-'><button type='button'>Search</button>")
@SafeHtmlTemplates.Template("")
SafeHtml cell(SafeStyles styles, SafeHtml value);
}
interface Templates2 extends SafeHtmlTemplates {
@SafeHtmlTemplates.Template("")
SafeHtml cell(SafeStyles styles, SafeHtml value);
}
// private static Templates templates = GWT.create(Templates.class);
// private static Templates2 templates2 = GWT.create(Templates2.class);
@Override
public void render(Context context, TOC_Item value, SafeHtmlBuilder sb) {
/*
* Always do a null check on the value. Cell widgets can pass null to
* cells if the underlying data contains a null, or if the data arrives
* out of order.
*/
if (value == null) {
return;
}
String host = Window.Location.getHost();
// com.google.gwt.user.client.Window.alert("getHost: " + host );
// If the value comes from the user, we escape it to avoid XSS attacks.
//SafeHtml safeValue = SafeHtmlUtils.fromString(value.getLabel());
// Use the template to create the Cell's html.
// SafeStyles styles = SafeStylesUtils.forTrustedColor(safeValue.asString());
if (value.getType().equals("root"))
{
sb.appendEscaped("Amerijet Repository");
}
else
{
if (value.getActionButton() == null)
{
if (value.getType().equals("cd"))
{
sb.appendHtmlConstant("<img src='cd.gif'>");
if (value.getHidden()){
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files' style = 'color:red'>");
sb.appendEscaped(" " + value.getLabel() + "-hidden");
sb.appendHtmlConstant("</a>");
}
else
{
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files'>");
sb.appendEscaped(" " + value.getLabel());
sb.appendHtmlConstant("</a>");
}
}
if (value.getType().equals("tape"))
{
sb.appendHtmlConstant("<img src='tape.gif'>");
if (value.getHidden()){
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files' style = 'color:red'>");
sb.appendEscaped(" " + value.getLabel() + "-hidden");
sb.appendHtmlConstant("</a>");
}
else
{
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files'>");
sb.appendEscaped(" " + value.getLabel());
sb.appendHtmlConstant("</a>");
}
}
if (value.getType().equals("folder"))
{
sb.appendHtmlConstant("<img src='folder.gif'>");
if (value.getHidden()){
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files' style = 'color:red'>");
sb.appendEscaped(" " + value.getLabel() + "-hidden");
sb.appendHtmlConstant("</a>");
}
else
{
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files'>");
sb.appendEscaped(" " + value.getLabel());
sb.appendHtmlConstant("</a>");
}
}
if (value.getType().equals("book"))
{
sb.appendHtmlConstant("<img src='book.gif'>");
if (value.getHidden()){
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files' style = 'color:red'>");
sb.appendEscaped(" " + value.getLabel() + "-hidden");
sb.appendHtmlConstant("</a>");
}
else
{
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files'>");
sb.appendEscaped(" " + value.getLabel());
sb.appendHtmlConstant("</a>");
}
}
if (value.getType().equals("default"))
{
sb.appendHtmlConstant("<img src='default.gif'>");
if (value.getHidden()){
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files' style = 'color:red'>");
sb.appendEscaped(" " + value.getLabel() + "-hidden");
sb.appendHtmlConstant("</a>");
}
else
{
sb.appendHtmlConstant("<a href='http://" + host + "/digidoc/Directory?name=" + value.getURL() + "' target = 'files'>");
sb.appendEscaped(" " + value.getLabel());
sb.appendHtmlConstant("</a>");
}
}
}
else
{
//SafeHtml rendered = templates.cell(styles, safeValue);
sb.appendHtmlConstant("<form style='margin-top: 0px; margin-bottom: 0px;' action='Directory' target='files'>");
sb.appendHtmlConstant("<input name='name' style='margin-top: 0px; margin-bottom: 0px;' type='hidden' value='" + value.getPath() + "'/>");
sb.appendHtmlConstant("<input name='key' id='key' style='font: 10px/normal Verdana; margin-top: 0px; margin-bottom: 0px; font-size-adjust: none; font-stretch: normal;' value='tc-'/>");//TODO should be value searchPrefix
sb.appendHtmlConstant("<input style='font: 10px/normal Verdana; text-align: left; color: rgb(80, 105, 147); margin-right: 0px; margin-left: 0px; font-size-adjust: none; font-stretch: normal;' onClick='javascript:alert(\"I am here\");showWaitingPage();' type='submit' value='" + value.getLabel() + "'/>");
sb.appendHtmlConstant("</form>");
}
}
}
}
static DirectoryCell cell = new DirectoryCell();
/**
* The model that defines the nodes in the tree.
*/
private static class CustomTreeModel implements TreeViewModel {
/**
* This selection model is shared across all leaf nodes.
* A selection model can also be shared across all nodes
* in the tree, or each set of child nodes can have
* its own instance. This gives you flexibility to
* determine how nodes are selected.
*/
// private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
public CustomTreeModel() {
}
/**
* Get the {@link NodeInfo} that provides the children of the
* specified value.
*/
public <T> NodeInfo<?> getNodeInfo(T value) {
MyDataProvider dataProvider = new MyDataProvider(value);
return new DefaultNodeInfo<TOC_Item>(dataProvider, cell);
}
/**
* Check if the specified value represents a leaf node.
* Leaf nodes cannot be opened.
*/
public boolean isLeaf(Object value) {
if (value instanceof String) {
return true;
}
return false;
}
}
static TreeViewModel model = new CustomTreeModel();
private native void createJavascriptCode() /*-{
// alert("createJavascriptCode");
$wnd.setCurrentPath = function(path){
// alert("setCurrentPath [path, " + path + "]");
@com.corena.lifeweb.gwt.client.modules.TOC.TreeCell::setCurrentPath(Ljava/lang/String;)(path); // was set
}
$wnd.showWaitingPage = function(){
alert("showWaitingPage");
}
}-*/ ;
public static void setCurrentPath(String path){
//com.google.gwt.user.client.Window.alert("setCurrentPath is Called");
TreeNode rootNode = tree.getRootTreeNode();
setPath(path, rootNode);
}
public static void showWaitingPage(){
com.google.gwt.user.client.Window.alert("showWaitingPage is Called");
}
public interface CellTreeResources extends CellTree.Resources {
@ImageResource.ImageOptions(flipRtl=true)
@Source("open.png")
ImageResource cellTreeLoading();
@Source("CellTree.css")
com.google.gwt.user.cellview.client.CellTree.Style cellTreeStyle();
}
static String newopen = Window.Location.getParameter("openpath");
static CellTreeResources resources = GWT.create(CellTreeResources.class);
static List<String> openDirs = new ArrayList<String>();
static String node = "";
static HashMap<String, Integer> cache = new HashMap<String, Integer>();
static String toOpen = Window.Location.getParameter("openpath");
final static CellTree tree = new CellTree(model, null, resources);
static TreeNode rootNode = tree.getRootTreeNode();
static TreeNode rootDir = tree.getRootTreeNode();
public void onModuleLoad() {
createJavascriptCode();
// Create a model for the tree.
tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
//Boolean b = resources.cellTreeLoading().isAnimated();
// com.google.gwt.user.client.Window.alert("isAnimated: " + b );
//com.google.gwt.user.client.Window.alert("toOpen: " + toOpen );
//tree.setHeight("800");
//VerticalPanel panel = new VerticalPanel();
//panel.setWidth("470");
// panel.setHeight("800");
//panel.add(tree);
// Add the widgets to the root panel.
RootPanel.get().add(tree);
// com.google.gwt.user.client.Window.alert("pathc: " + toOpen );
// setCurrentPath(toOpen);
}
public static void setPath(String path, TreeNode treenode){
if (path.equals(null))
{
return;
}
com.google.gwt.user.client.Window.alert("path: " + path );
String node = path.substring(0, path.indexOf('/'));
com.google.gwt.user.client.Window.alert("node: " + node);
com.google.gwt.user.client.Window.alert("Child Count: " + treenode.getChildCount());
outerloop:
for (int i = 0 ; i < treenode.getChildCount() ; i++)
{
com.google.gwt.user.client.Window.alert("Inside setPath:" + ((TOC_Item) treenode.getChildValue(i)).getName() + "-" + node);
if (((TOC_Item) treenode.getChildValue(i)).getName().equals(node))
{
com.google.gwt.user.client.Window.alert("Please Open");
if (treenode.isChildOpen(i))
{
path = path.substring(path.indexOf('/') + 1);
setPath(path, treenode.setChildOpen(i, true));
}
else
{
path = path.substring(path.indexOf('/') + 1);
setPath(path, treenode.setChildOpen(i, true));
}
break outerloop;
}
}
}
}
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment