Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:46:18+00:00 2026-05-28T06:46:18+00:00

I would like to craft a GWT CellTree with an optional pop-up menu triggered

  • 0

I would like to craft a GWT CellTree with an optional pop-up menu triggered on click of a TreeNode.

So I’ve crafted a CustomTreeModel. Here it is:

public class CustomTreeModel implements TreeViewModel {

/**
 * Save visited URL.  We'll use it later to determine if tree node needs to be opened.
 * We decode the query string in URL so that token has a chance of matching (e.g., convert %20 to space).
 */
private final String url = URL.decodeQueryString(Window.Location.getHref());

private final NavNode navNode;
private final TokenService<MainEventBus> tokenService;

/**
 * A selection model shared across all nodes in the 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.
        // We passed null as the root value. Return the immediate descendants.
        result = new DefaultNodeInfo<NavNode>(getDataProvider(navNode), getCell(), selectionModel, null);

    } else if (value instanceof NavNode) {
        // all other levels
        // We pass a node, return its immediate descendants.

        // select node if URL contains params in node's target or one of node's option's target
        NavNode currNode = (NavNode) value;
        if (isSelected(currNode)) {
            selectionModel.setSelected(currNode, true);
        }
        if (currNode.hasOptions()) { // add pop-up menu to this node if it has options
            result = new DefaultNodeInfo<NavNode>(getDataProvider(currNode), getCell(), selectionModel, new NodeSelectionEventManager(currNode), null);
        } else {
            result = new DefaultNodeInfo<NavNode>(getDataProvider(currNode), getCell(), selectionModel, null);
        }
    }
    return 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;
        }
    }
    return result;
}

// Create a data provider that contains the immediate descendants.
private ListDataProvider<NavNode> getDataProvider(NavNode node) {
    return new ListDataProvider<NavNode>(node.getChildren());
}

// Create a cell to display a 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());
            }
        }
    };
    return cell;
}

private boolean isSelected(NavNode node) {
    boolean selected = false;
    if (node != null) {
        if (url.contains(tokenService.getToken(node))) {
            selected = true;
        } else {
            for (NavOption option: node.getOptions()) {
                if (url.contains(tokenService.getToken(option))) {
                    selected = true;
                    break;
                }
            }
        }
    }
    return 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 a debug id... this will most likely necessitate generation of a unique key
        optionsPopup.setWidget(optionsContainer);
    }

    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
        NavNode node = selectionModel.getSelectedObject();
        for (NavOption option: node.getOptions()) {
            optionsContainer.add(new Hyperlink(option.getName(), tokenService.getToken(option)));
        }
        // Reposition the popup relative to node
        UIObject source = (UIObject) event.getSource();
        int left = source.getAbsoluteLeft() + 25;
        int top = source.getAbsoluteTop();
        optionsPopup.setPopupPosition(left, top);

        // Show the 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");
        for (NavOption option: node.getOptions()) {
            optionsContainer.add(new Hyperlink(option.getName(), tokenService.getToken(option)));
        }
        // TODO provide a debug id... this will most likely necessitate generation of a unique key
        optionsPopup.setWidget(optionsContainer);
    }

    @Override
    public void onCellPreview(CellPreviewEvent<NavNode> event) {
        // Reposition the popup relative to node
        UIObject source = (UIObject) event.getDisplay();
        int left = source.getAbsoluteLeft() + 25;
        int top = source.getAbsoluteTop();
        optionsPopup.setPopupPosition(left, top);

        // Show the popup
        optionsPopup.show();

    }

}

}

I’m using a generic bean (NavNode) to help me determine when I have a leaf and when I have an option (NavOption) or options that contain a target used for Hyperlink construction.

I want, when I click on a node (TreeNode) in the CellTree, that a pop-up menu (DecoratedPopupPanel) appears, but only for those nodes that have options.

I have tried to employ either of the inner Handler implementations (on construction of a DefaultNodeInfo) to no success. Hopefully from the above code sample you can see what I’m trying to do.

Here’s a variant that adds a SelectionChangeEvent.Handler to SingleSelectionModel

if (currNode.hasOptions()) { // add pop-up menu to this node if it has options
            selectionModel.addSelectionChangeHandler(new NavNodeSelectionHandler());
            result = new DefaultNodeInfo<NavNode>(getDataProvider(currNode), getCell(), selectionModel, null);
        }

What’s happening is that the attempt to cast the Event fails with a ClassCastException.
I want to get a handle on an UIObject so I can position the popup. I think I need a handle on a TreeNode, but cannot see how to do it.

The CellTree, TreeViewModel, SelectionModel and friends are some of the most obtuse API I’ve come across.

Would really appreciate some help from a GWT expert!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:46:19+00:00Added an answer on May 28, 2026 at 6:46 am

    A smart colleague of mine was able to sleuth a solution.

    Here’s what we wound up with:

    public class CustomTreeModel implements TreeViewModel {
    
    /**
     * Save visited URL.  We'll use it later to determine if tree node needs to be opened.
     * We decode the query string in URL so that token has a chance of matching (e.g., convert %20 to space).
     */
    private final String url = URL.decodeQueryString(Window.Location.getHref());
    
    private final NavNode navNode;
    private final TokenService<MainEventBus> tokenService;
    
    /**
     * A selection model shared across all nodes in the 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.
            // We passed null as the root value. Return the immediate descendants.
            result = new DefaultNodeInfo<NavNode>(getDataProvider(navNode), getCell(), selectionModel, null);
    
        } else if (value instanceof NavNode) {
            // all other levels
            // We pass a node, return its immediate descendants.
    
            // select node if URL contains params in node's target or one 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);
        }
        return 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;
            }
        }
        return result;
    }
    
    // Create a data provider that contains the immediate descendants.
    private ListDataProvider<NavNode> getDataProvider(NavNode node) {
        return new ListDataProvider<NavNode>(NavNodeUtil.getHeadedChildren(node.getChildren(), 1));
    }
    
    // Create a cell to display a descendant.
    private Cell<NavNode> getCell() {
        return new TreeCell();
    }
    
    private boolean isSelected(NavNode node) {
        boolean selected = false;
        if (node != null) {
            if (url.contains(tokenService.getToken(node))) {
                selected = true;
            } else {
                for (NavOption option: node.getOptions()) {
                    if (url.contains(tokenService.getToken(option))) {
                        selected = true;
                        break;
                    }
                }
            }
        }
        return 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 that the value is not null.
            if (currNode == null) {
                return;
            }
    
            if (currNode.hasOptions()) { // add pop-up menu to this node if it has options
                final DecoratedPopupPanel optionsPopup = new DecoratedPopupPanel(true);
                final VerticalPanel optionsContainer = new VerticalPanel();
                optionsContainer.setWidth("125px");
                for (NavOption option: currNode.getOptions()) {
                    optionsContainer.add(new Hyperlink(option.getName(), tokenService.getToken(option)));
                }
                // TODO provide a debug id... this will most likely necessitate generation of a unique key
                optionsPopup.setWidget(optionsContainer);
                // Reposition the popup relative to 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 the TreeCell overrides the onBrowserEvent. From here we can get a handle on the node and position the pop-up. The pop-up is instantiated with a callback. Weird!

    NavNodeUtil does some magic where it counts the children and adds A,B,C…Z categorical headings for a node’s children that exceed a certain threshold.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to craft a case-insensitive regex (for JavaScript) that matches street names,
I am getting a tool ready to make the source public. I would like
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.