how do I locate the Object in the EventList that matches the clicked row

0 views
Skip to first unread message

Pete

unread,
Jul 23, 2008, 4:35:09 PM7/23/08
to GWT-Stuff
I'm sure there must be an easy answer to this?

TableRow.MouseListener

public void onClick(final TableRow row, final Event event)
{

}

How can I locate the model Object from my ObjectListTable's event list
that corresponds to the clicked row in the event above ?

I can see I have access to the TableRow so can get to the TableCell's
but only solution I can think of is to have a hidden table column that
is the Id of my object, then locate that TableCell, then iterate
through objectListTable.getObjects(); until the id matches.

Surely there's an easier way ?

I guess any solution needs to still work if the tables is re-ordered
or filtered.

thanks

Sandy McArthur

unread,
Jul 26, 2008, 10:03:24 AM7/26/08
to GWT-Stuff
When you create a table row mouse listener simply keep a reference to
that particular object. This won't work if you use the same listener
instance for all rows but I only use those if I don't need row-
specific logic (eg: highlighting)

class MyTableRowMouseListener extends TableRow.MouseListener {
private final Object obj;
public MyTableRowMouseListener(Object obj) {
this.obj = obj;
}
public void onClick(final TableRow row, final Event event) {
// do stuff with obj
}
}

and elsewhere:

TableRow.MouseListener listener = new MyTableRowMouseListener(Object
obj);

Pete

unread,
Jul 30, 2008, 5:13:08 AM7/30/08
to GWT-Stuff
Thanks, makes sense, but does lead me to another issue, if I have a
different mouseListener instance created for each model Object then
how do I detach them i.e remember them all to detach correct one ?.

for example in onAttach I create the specific listener

public void onAttach(final Object modelObject, final
TableBodyGroup rowGroup) {
rowGroup.addMouseListener(rowGroupMouseListener);
final Iterator iter = rowGroup.getRows().iterator();
while (iter.hasNext()) {
final TableRow tr = (TableRow)iter.next();
tr.addMouseListener(new
RowMouseListener(modelObject));
}
}

but then in detach.. I don't know which instance to detach, unless I
maintain some kind of Map maybe of modelObject key to value
mouseListener.

public void onDetach(final Object obj, final TableBodyGroup
rowGroup) {
rowGroup.removeMouseListener(rowGroupMouseListener);
final Iterator iter = rowGroup.getRows().iterator();
while (iter.hasNext()) {
final TableRow tr = (TableRow)iter.next();
tr.removeMouseListener(?????);
}
}

Any thoughts?
I must admit I don't fully understand all the attach/detach stuff but
appreicate I need it to allow references to be released.

Doing it the other way, with a single mouseListener for all rows
works, but as mentioned before I had to add a hidden cell with an Id
in it, and then I work from the clicked row, which cells represents a
hidden Id field, then iterate through the
ObjectListTables.getObjects() to find the matching modelObject. This
does work but seems a little clunky.

Sandy McArthur

unread,
Jul 31, 2008, 1:05:27 PM7/31/08
to GWT-Stuff
I don't believe you need to worry about keeping track of that unless
your Object keeps a reference back to the listener instance.

If you have references that aren't circular, eg:
aTableRow -> aListener -> anObject

Then the garbage collector will free up the listener when it's no
longer reachable.

Actually, I would suggest you register the listener during the call
to: Renderer.render(Object obj, TableBodyGroup bodyGroup). This will
avoid the risk of adding multiple listeners as it only happens once
per row creation. onAttach is called each time the table is "attached"
or "reattached" which could create duplicate listeners depending on
how dynamic your interface is. I'll admit my javadoc documentation is
a little misleading there and I've filed a bug to remind me next time
I work on that code.

The attach/detach stuff with GWT is most important when creating your
own custom widgets behavior because some browsers are bad about
garbage collection when it involves the browser's DOM model and will
leak memory until the page is reloaded.
Reply all
Reply to author
Forward
0 new messages