Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TreeViewer with multiple same objects,adds child to first occurence of its parent
TreeViewer with multiple same objects,adds child to first occurence of its parent [message #905443] Thu, 30 August 2012 11:54 Go to next message
preeti baviskar is currently offline preeti baviskarFriend
Messages: 11
Registered: July 2012
Junior Member
Hi,
I am using Tree with TreeViewer.

Tree treeSWComponent = new Tree(composite, SWT.BORDER);
TreeViewer tree_viewer=new TreeViewer(treeSWComponent);

When I am adding a child in the TreeViewer (on selection of Tree item),

tree_viewer.add(Object parent, Object child);

Since items in the Tree are repeated, TreeViewer contains multiple same objects so child is always adding to very first occurence of its parent.


Thanks.
Re: TreeViewer with multiple same objects,adds child to first occurence of its parent [message #905481 is a reply to message #905443] Thu, 30 August 2012 13:08 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Hi,
I don't know if I understand you correctly but make sure that your objects implement hashCode() and equals() methods in a way that they can be distinguished from each other.

Regards,
Thorsten
Re: TreeViewer with multiple same objects,adds child to first occurence of its parent [message #905787 is a reply to message #905481] Fri, 31 August 2012 04:29 Go to previous messageGo to next message
preeti baviskar is currently offline preeti baviskarFriend
Messages: 11
Registered: July 2012
Junior Member
hashCode() and equal() won't help because objects are repeated.

For eg. below, if second occurence of 'arg1' is selected and if I am trying to add child to it, its getting added to the first occuence.

tree_viewer.add(Object arg1, Object child);

- ASW1(parent)
- arg1(child)
- agr2(child)
- arg1(child)
+ ASW2(parent)


Thanks.


[Updated on: Fri, 31 August 2012 04:32]

Report message to a moderator

Re: TreeViewer with multiple same objects,adds child to first occurence of its parent [message #905815 is a reply to message #905787] Fri, 31 August 2012 05:48 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 2012-08-31 06:30, preeti baviskar wrote:
> hashCode() and equal() won't help because objects are repeated.

This scenario is not supported. You need non-duplicate elements. If you
need to simulate visually equal elements you still need to make the
contained holder objects all different (by equals). A simple workaround
is a holder class like this:

public final class IdElement<T> {

private T value;

private final int id;

public IdElement(T value, int id) {
this.value = value;
this.id = id;
}

public final T getValue() {
return value;
}

public final void setValue(T value) {
this.value = value;
}

public final int getId() {
return id;
}

@Override
public int hashCode() {
// Combine hashCode of int and of value;
}

@Override
public boolean equals(Object obj) {
// Combine equality of int and of value;
}

}

Now when inserting an Object of T into the tree, instead insert a
different value for int each time (e.g. by incrementation) and insert
the corresponding IdElement instead.

HTH & Greetings from Bremen,

Daniel Krügler
Re: TreeViewer with multiple same objects,adds child to first occurence of its parent [message #905874 is a reply to message #905815] Fri, 31 August 2012 08:41 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
I would suspect that there must be a difference between arg1(child) (occurence1) and arg1(child) (occurence2). Otherwise it wouldn't make sense to have two nodes. If you have two green boxes and you ask a friend to put the paper in the green box, then he will naturally ask - which one of the two green boxes. Of course you could also modify your model directly by adding the new node to the user selected arg1(child). But you must be aware that if your nodes are not distinct with respect to hashCode and equals, all viewer related tasks like adding a child, setting a path etc. will not work properly.

Regards,
Thorsten
Re: TreeViewer with multiple same objects,adds child to first occurence of its parent [message #1759977 is a reply to message #905874] Thu, 20 April 2017 18:14 Go to previous message
Tushar Malhotra is currently offline Tushar MalhotraFriend
Messages: 1
Registered: April 2017
Junior Member
Even I am facing a problem of similar sort. In my case my tree structure has a parent with some same children whose name is same but some parameter is different.
I want to look for the duplicate childrens in tree then want to group children's of same name and different parameters under one new parent.
Is this possible by any chance.

For eg I have data like this:

parent 1
child1
a 34
b 24
c 34
a 23
c 35

this should look like
parent 1
child1
a
a 34
a 23
b
b 24
c
c 34
c 35







Previous Topic:How to let JAWs read FieldDecortion
Next Topic:Treeviewer expander gives false indication
Goto Forum:
  


Current Time: Fri Apr 19 16:42:46 GMT 2024

Powered by FUDForum. Page generated in 0.02437 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top