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 6011449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:15:42+00:00 2026-05-23T02:15:42+00:00

If there are more tree items based on the same object in the tree

  • 0

If there are more tree items based on the same object in the tree viewer, making a TreePath and passing it to TreeViewer.setSelection() does not select the item correctly when the current selection is equal to the one I want to navigate.

Example:
There’s a tree with 2 items that show the same object (BigDecimal.ONE in this case). They have different paths (different parents):
tree

I want that when I am on one BigDecimal.ONE item, to click on the link and navigate to the other BigDecimal.ONE. On the selection listener of the link I make a TreeSelection with a correct TreePath. Then I call setSelection. But the navigation does not work. However, if the root item is initially collapsed, I notice that it expands it, but does not navigate to the correct item.

The code is this:

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.*;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class TreeViewerExample {

    static class Model {
        String   name;
        Number[] children;

        public Model(String name, Number... numbers) {
            this.name = name;
            this.children = numbers;
        }

        public String toString() {
            return name;
        }
    }

    static class ModelTreeProvider extends LabelProvider implements ITableLabelProvider, ITreeContentProvider {

        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof Model) {
                return ((Model) parentElement).children;
            } else {
                return new Object[0];
            }
        }
        public Object getParent(Object element) {
            System.err.println("requesting the parent for " + element);
            return null;
        }
        public boolean hasChildren(Object element)
        {
            return getChildren(element) == null ? false : getChildren(element).length > 0;
        }
        public Object[] getElements(Object inputElement)
        {
            return (inputElement instanceof List)? ((List) inputElement).toArray():new Object[0];
        }
        public void dispose()
        {
        }
        public void inputChanged(Viewer arg0, Object arg1, Object arg2)
        {
        }
        public String getColumnText(Object element, int columnIndex)
        {
            return element.toString();
        }
        public Image getColumnImage(Object element, int columnIndex)
        {
            return null;
        }
    }

    public static void main(String[] args) {
        final List<Model> models = new ArrayList<Model>();
        models.add(new Model("Zero and one", BigDecimal.ZERO, BigDecimal.ONE));
        models.add(new Model("One and ten", BigDecimal.ONE, BigDecimal.TEN));

        Window app = new ApplicationWindow(null) {
            private TreeViewer treeViewer;
            private Link       link;
            protected Control createContents(Composite parent) {
                Composite composite = new Composite(parent, SWT.NONE);
                composite.setLayout(new FillLayout());
                treeViewer = new TreeViewer(composite);
                ModelTreeProvider provider = new ModelTreeProvider();
                treeViewer.setContentProvider(provider);
                treeViewer.setLabelProvider(provider);
                treeViewer.setInput(models);
                treeViewer.getTree().addSelectionListener(new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        if (((TreeItem) e.item).getText().equals("1")) {
                            link.setText("This is from "+((TreeItem) e.item).getParentItem().getText()
                                    + "\r\n<a href=\"go\">Go to the other " + ((TreeItem) e.item).getText() + "</a>");
                        } else {
                            link.setText(" - ");
                        }
                        link.setData(e.item);
                    }
                });
                link = new Link(composite, SWT.NONE);
                link.setText(" - ");
                link.addSelectionListener(new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent e) {
                        List<Object> path = new ArrayList<Object>();
                        if (treeViewer.getTree().indexOf(treeViewer.getTree().getSelection()[0].getParentItem()) == 0)
                        {// if the first is selected, go to the second
                            path.add(treeViewer.getTree().getItem(1).getData());
                        } else {
                            path.add(treeViewer.getTree().getItem(0).getData());
                        }
                        path.add(BigDecimal.ONE);
                        treeViewer.setSelection(new TreeSelection(new TreePath(path.toArray())), true);
                    }
                });
                return composite;
            }
        };
        app.setBlockOnOpen(true);
        app.open();
    }
}

My question is if this a jface bug or am I not doing this the right way?


EDIT:

It looks that there is already a posted bug on eclipse.org: https://bugs.eclipse.org/bugs/show_bug.cgi?id=332736

  • 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-23T02:15:43+00:00Added an answer on May 23, 2026 at 2:15 am

    It looks like this is a bug in JFace. After all, you are passing TreePath so it should figure it out which exactly instance of your object you want.

    The problem is the org.eclipse.jface.viewers.AbstractTreeViewer.isSameSelection(List, Item[]) method. It incorrectly determine, based on the element found in the items (the BigIntegers) that the selection is the same. It should have checked the whole tree path to make sure that the selection will be indeed the same. Fortunately, you can fix this in your code by overriding the isSameSelection(List, Item[]) method and correctly check against the tree paths instead of the elements themselves:

           treeViewer = new TreeViewer(composite) {
                protected boolean isSameSelection(List items, Item[] current) {
                    // If they are not the same size then they are not equivalent
                    int n = items.size();
                    if (n != current.length) {
                        return false;
                    }
                    Set itemSet = new HashSet(n * 2 + 1);
                    for (Iterator i = items.iterator(); i.hasNext();) {
                        Item item = (Item) i.next();
                        itemSet.add(getTreePathFromItem(item));
                    }
                    // Go through the items of the current collection
                    // If there is a mismatch return false
                    for (int i = 0; i < current.length; i++) {
                        if (current[i].getData() == null
                                || !itemSet.contains(getTreePathFromItem(current[i]))) {
                            return false;
                        }
                    }
                    return true;
                }
           };
    

    This however will fix this particular problem, but there is no guarantee that there are other problems waiting to be found. Personally, I always try to stay away of having the same elements in different places of the tree, just in case.

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

Sidebar

Related Questions

I am making tree with the help of jquery in the tree whenever there
I did some HTTP monitoring with WireShark. Are there more tools like this that
Are there any more generic tools that can compile or basically merge multiple PHP
Is there a more efficient way to convert an HTMLCollection to an Array, other
Would there a more elegant way of writing the following syntax? Thread t0 =
Is there a more concise/standard idiom (e.g., a JDK method) for piping an input
If there is more than one way, please list them. I only know of
Is there a more efficient way to clamp real numbers than using if statements
Is there a more elegant solution to convert an ' Arraylist ' into a
Is there are more efficient way than the following for selecting the third parent?

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.