Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Default behaviour for find TableItem associated to a TableViewer element
Default behaviour for find TableItem associated to a TableViewer element [message #330317] Fri, 25 July 2008 07:45 Go to next message
Eclipse UserFriend
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 #330355 is a reply to message #330317] Sat, 26 July 2008 02:41 Go to previous messageGo to next message
Eclipse UserFriend
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).

Boris

"David Perez" <craquerpro@yahoo.es> wrote in message
news:d98328ea7b101ddb59a99f0dcba46bbf$1@www.eclipse.org...
> 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 #330387 is a reply to message #330355] Mon, 28 July 2008 09:40 Go to previous messageGo to next message
Eclipse UserFriend
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).
Re: Default behaviour for find TableItem associated to a TableViewer element [message #330579 is a reply to message #330387] Mon, 04 August 2008 12:21 Go to previous message
Eclipse UserFriend
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
------------------------------------------------------------ --------
Previous Topic:Updating certain columns in a TableViewer
Next Topic:Why ColumnViewerEditorActivationListener isn't called
Goto Forum:
  


Current Time: Mon Feb 10 01:27:47 GMT 2025

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

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

Back to the top