PDA

View Full Version : Show empty TreePanel branch nodes using the branch icon (not leaf icon)



The_Jackal
3 Aug 2010, 5:01 PM
The default TreePanel changes the branch node icon to a leaf node icon if not children exist. This doesn't really make sense to me - in the GXT examples a folder of music gets a music icon if there is no music in it. It's still a folder - just an empty one!

The code below is a ModelIconProvider that fixes this, with the caveat that you must add a property to each leaf model. See the javadoc for an example:

21776


import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreeStyle;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
* Enables TreePanel to have branch nodes with no children preserving the (open and closed) branch node icon.
* Default TreePanel changes the branch node to a leaf node if no children exist.
*
* <p>Usage:
* <pre>
* TreePanel treePanel = new TreePanel<ModelData>(store));
* treePanel.setIconProvider(new BranchPreservingIconProvider<ModelData>(treePanel));
*
* ...
* //Each leaf node model must set the {@link BranchPreservingIconProvider#IS_LEAF} property
* BaseTreeModel treeItem = new BaseTreeModel();
* treeItem.set(BranchPreservingIconProvider.IS_LEAF, Boolean.TRUE);
* </pre>
* </p>
*
* @param <M> ModelData type
* @author Carl Pritchett
*/
public class BranchPreservingIconProvider<M extends ModelData> implements ModelIconProvider<M>
{
public static final String IS_LEAF = "isLeaf";

private TreePanel<M> treePanel;

public BranchPreservingIconProvider(TreePanel<M> treePanel)
{
this.treePanel = treePanel;
}

public AbstractImagePrototype getIcon(M model)
{
TreeStyle ts = treePanel.getStyle();
AbstractImagePrototype style = ts.getLeafIcon();

if (!Boolean.TRUE.equals(model.get(IS_LEAF)))
{
if (treePanel.isExpanded(model))
{
style = ts.getNodeOpenIcon();
}
else
{
style = ts.getNodeCloseIcon();
}
}

return style;
}
}