| Default behaviour for find TableItem associated to a TableViewer element [message #330317] | 
Fri, 25 July 2008 03:45   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi, 
 
I'm using a TableViewer.  Inside the TableViewer I place ArrayList objects. 
I had a strange problem.  After debugging a while I've discovered that  
StructuredViewer#findItem(Object element) fails, it gives me the wrong  
index for an element. 
 
The cause is that instead of using reference equality, uses content  
equality. 
AbstractTableViewer#doFindItem() 
 
So If I ask for the TableItem corresponding to an object whose index is 1,  
it returns me the object whose index 0, when it has identical values. 
 
I consider this default behaviour dangerous. 
I have redefined the equals method of my ArrayList object: 
 
    @Override public boolean equals(Object o) { 
        return o == this; 
    } 
 
and now it works ok. 
What do the Eclipse experts think?
 |  
 |  
  | 
 | 
 | 
| Re: Default behaviour for find TableItem associated to a TableViewer element [message #330579 is a reply to message #330387] | 
Mon, 04 August 2008 08:21   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
File a bug and attach a patch against HEAD with your proposed JavaDoc  
comment. 
 
Tom 
 
David Perez schrieb: 
> Thanks Boris, 
>  
> I didn't take into account the hashCode(). 
>  
> Now, my custom row object is like this: 
>  
> class MyObject extends ArrayList<Object> { 
>   .... 
>  
>    @Override public boolean equals(Object o) { 
>        return o == this; 
>    } 
>  
>    @Override public int hashCode() { 
>        return System.identityHashCode(this); 
>    } 
> } 
>  
> It would be nice if the Platform documentation emphasized that the  
> equals() method implementation is important. 
>  
> Boris Bokowski wrote: 
>  
>> Make sure that you also override hashCode() whenever you override  
>> equals(). 
>  
>> If the native equals and hashCode implementation is not what you need  
>> in the viewer, you can plug in different implementations, see  
>> StructuredViewer.setComparer(IElementComparer). 
>  
>  
 
 
--  
B e s t S o l u t i o n . at 
 ------------------------------------------------------------ -------- 
Tom Schindl                                          JFace-Committer 
 ------------------------------------------------------------ --------
 |  
 |  
  | 
Powered by 
FUDForum. Page generated in 0.04367 seconds