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 07:54 Go to next message
preeti baviskar is currently offline preeti baviskar
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 09:08 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 208
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 00:29 Go to previous messageGo to next message
preeti baviskar is currently offline preeti baviskar
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 00: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 01:48 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel Krügler
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 04:41 Go to previous message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 208
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
Previous Topic:Treeviewer expander gives false indication
Next Topic:ObservableMap binding to a Combo
Goto Forum:
  


Current Time: Wed Aug 27 23:15:12 EDT 2014

Powered by FUDForum. Page generated in 0.01998 seconds