events - GWT CellTree with an optional pop-up menu triggered on click of a TreeNode -



events - GWT CellTree with an optional pop-up menu triggered on click of a TreeNode -

i craft gwt celltree optional pop-up menu triggered on click of treenode.

so i've crafted customtreemodel. here is:

public class customtreemodel implements treeviewmodel { /** * save visited url. we'll utilize later determine if tree node needs opened. * decode query string in url token has chance of matching (e.g., convert %20 space). */ private final string url = url.decodequerystring(window.location.gethref()); private final navnode navnode; private final tokenservice<maineventbus> tokenservice; /** * selection model shared across nodes in tree. */ private final singleselectionmodel<navnode> selectionmodel = new singleselectionmodel<navnode>(); public customtreemodel(navnode navnode, tokenservice tokenservice) { this.navnode = navnode; this.tokenservice = tokenservice; } @override public <t> nodeinfo<?> getnodeinfo(t value) { defaultnodeinfo<navnode> result = null; if (value == null) { // level 0. // passed null root value. homecoming immediate descendants. result = new defaultnodeinfo<navnode>(getdataprovider(navnode), getcell(), selectionmodel, null); } else if (value instanceof navnode) { // other levels // pass node, homecoming immediate descendants. // select node if url contains params in node's target or 1 of node's option's target navnode currnode = (navnode) value; if (isselected(currnode)) { selectionmodel.setselected(currnode, true); } if (currnode.hasoptions()) { // add together pop-up menu node if has options result = new defaultnodeinfo<navnode>(getdataprovider(currnode), getcell(), selectionmodel, new nodeselectioneventmanager(currnode), null); } else { result = new defaultnodeinfo<navnode>(getdataprovider(currnode), getcell(), selectionmodel, null); } } homecoming result; } @override public boolean isleaf(object value) { boolean result = true; if (value == null) { if (navnode.haschildren()) { result = false; } } else if (value instanceof navnode) { navnode currentnode = (navnode) value; if (currentnode.haschildren()) { result = false; } } homecoming result; } // create info provider contains immediate descendants. private listdataprovider<navnode> getdataprovider(navnode node) { homecoming new listdataprovider<navnode>(node.getchildren()); } // create cell display descendant. private cell<navnode> getcell() { cell<navnode> cell = new abstractcell<navnode>() { @override public void render(context context, navnode value, safehtmlbuilder sb) { if (value != null) { sb.appendescaped(value.getname()); } } }; homecoming cell; } private boolean isselected(navnode node) { boolean selected = false; if (node != null) { if (url.contains(tokenservice.gettoken(node))) { selected = true; } else { (navoption option: node.getoptions()) { if (url.contains(tokenservice.gettoken(option))) { selected = true; break; } } } } homecoming selected; } class navnodeselectionhandler implements selectionchangeevent.handler { private final verticalpanel optionscontainer; private final decoratedpopuppanel optionspopup; public navnodeselectionhandler() { optionspopup = new decoratedpopuppanel(true); optionscontainer = new verticalpanel(); optionscontainer.setwidth("125px"); // todo provide debug id... necessitate generation of unique key optionspopup.setwidget(optionscontainer); } @override public void onselectionchange(selectionchangeevent event) { navnode node = selectionmodel.getselectedobject(); (navoption option: node.getoptions()) { optionscontainer.add(new hyperlink(option.getname(), tokenservice.gettoken(option))); } // reposition popup relative node uiobject source = (uiobject) event.getsource(); int left = source.getabsoluteleft() + 25; int top = source.getabsolutetop(); optionspopup.setpopupposition(left, top); // show popup optionspopup.show(); } } class nodeselectioneventmanager implements cellpreviewevent.handler<navnode> { private final verticalpanel optionscontainer; private final decoratedpopuppanel optionspopup; public nodeselectioneventmanager(navnode node) { optionspopup = new decoratedpopuppanel(true); optionscontainer = new verticalpanel(); optionscontainer.setwidth("125px"); (navoption option: node.getoptions()) { optionscontainer.add(new hyperlink(option.getname(), tokenservice.gettoken(option))); } // todo provide debug id... necessitate generation of unique key optionspopup.setwidget(optionscontainer); } @override public void oncellpreview(cellpreviewevent<navnode> event) { // reposition popup relative node uiobject source = (uiobject) event.getdisplay(); int left = source.getabsoluteleft() + 25; int top = source.getabsolutetop(); optionspopup.setpopupposition(left, top); // show popup optionspopup.show(); } }

}

i'm using generic bean (navnode) help me determine when have leaf , when have alternative (navoption) or options contain target used hyperlink construction.

i want, when click on node (treenode) in celltree, pop-up menu (decoratedpopuppanel) appears, nodes have options.

i have tried employ either of inner handler implementations (on construction of defaultnodeinfo) no success. above code sample can see i'm trying do.

here's variant adds selectionchangeevent.handler singleselectionmodel

if (currnode.hasoptions()) { // add together pop-up menu node if has options selectionmodel.addselectionchangehandler(new navnodeselectionhandler()); result = new defaultnodeinfo<navnode>(getdataprovider(currnode), getcell(), selectionmodel, null); }

what's happening effort cast event fails classcastexception. want handle on uiobject can position popup. think need handle on treenode, cannot see how it.

the celltree, treeviewmodel, selectionmodel , friends of obtuse api i've come across.

would appreciate help gwt expert!

a smart colleague of mine able sleuth solution.

here's wound with:

public class customtreemodel implements treeviewmodel { /** * save visited url. we'll utilize later determine if tree node needs opened. * decode query string in url token has chance of matching (e.g., convert %20 space). */ private final string url = url.decodequerystring(window.location.gethref()); private final navnode navnode; private final tokenservice<maineventbus> tokenservice; /** * selection model shared across nodes in tree. */ private final singleselectionmodel<navnode> selectionmodel = new singleselectionmodel<navnode>(); public customtreemodel(navnode navnode, tokenservice tokenservice) { this.navnode = navnode; this.tokenservice = tokenservice; } @override public <t> nodeinfo<?> getnodeinfo(t value) { defaultnodeinfo<navnode> result = null; if (value == null) { // level 0. // passed null root value. homecoming immediate descendants. result = new defaultnodeinfo<navnode>(getdataprovider(navnode), getcell(), selectionmodel, null); } else if (value instanceof navnode) { // other levels // pass node, homecoming immediate descendants. // select node if url contains params in node's target or 1 of node's option's target navnode currnode = (navnode) value; if (isselected(currnode)) { selectionmodel.setselected(currnode, true); } result = new defaultnodeinfo<navnode>(getdataprovider(currnode), getcell(), selectionmodel, null); } homecoming result; } @override public boolean isleaf(object value) { boolean result = true; if (value == null) { if (navnode.haschildren()) { result = false; } } else if (value instanceof navnode) { navnode currentnode = (navnode) value; if (currentnode.haschildren()) { result = false; } } homecoming result; } // create info provider contains immediate descendants. private listdataprovider<navnode> getdataprovider(navnode node) { homecoming new listdataprovider<navnode>(navnodeutil.getheadedchildren(node.getchildren(), 1)); } // create cell display descendant. private cell<navnode> getcell() { homecoming new treecell(); } private boolean isselected(navnode node) { boolean selected = false; if (node != null) { if (url.contains(tokenservice.gettoken(node))) { selected = true; } else { (navoption option: node.getoptions()) { if (url.contains(tokenservice.gettoken(option))) { selected = true; break; } } } } homecoming selected; } class treecell extends abstractcell<navnode> { public treecell() { super("click", "keydown"); } @override public void onbrowserevent(context context, element parent, navnode currnode, nativeevent event, valueupdater<navnode> valueupdater) { // check value not null. if (currnode == null) { return; } if (currnode.hasoptions()) { // add together pop-up menu node if has options final decoratedpopuppanel optionspopup = new decoratedpopuppanel(true); final verticalpanel optionscontainer = new verticalpanel(); optionscontainer.setwidth("125px"); (navoption option: currnode.getoptions()) { optionscontainer.add(new hyperlink(option.getname(), tokenservice.gettoken(option))); } // todo provide debug id... necessitate generation of unique key optionspopup.setwidget(optionscontainer); // reposition popup relative node final int left = parent.getabsoluteleft() + 25; final int top = parent.getabsolutetop(); optionspopup.setpopuppositionandshow(new popuppanel.positioncallback() { @override public void setposition(int offsetwidth, int offsetheight) { optionspopup.setpopupposition(left, top); } }); } super.onbrowserevent(context, parent, currnode, event, valueupdater); } @override public void render(context context, navnode value, safehtmlbuilder sb) { if (value != null) { sb.appendescaped(value.getname()); } } }

}

note treecell overrides onbrowserevent. here can handle on node , position pop-up. pop-up instantiated callback. weird!

navnodeutil magic counts children , adds a,b,c...z categorical headings node's children exceed threshold.

events gwt tree selection

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -