Help with EventHandlers

1 view
Skip to first unread message

Iván López

unread,
Jun 9, 2009, 5:15:06 PM6/9/09
to Google Web Toolkit
Hi all!,

I've a little problem upgrading my code from 1.5.3 to 1.6.4. I've
an AccountManager class that manages the accounts and when a change is
done it fires an event to all the components to update its list of
accounts.

AccountManager:

private ChangeListenerCollection changeListeners;

public void addChangeListener(ChangeListener listener) {
if (changeListeners == null) {
changeListeners = new ChangeListenerCollection();
}
changeListeners.add(listener);
}
public void removeChangeListener(ChangeListener listener) {
if (changeListeners != null) {
changeListeners.remove(listener);
}
}
private void fireChangeEvent() {
if (changeListeners != null) {
changeListeners.fireChange(accountMainPanel);
}
}

private void loadAccounts() {
AccountRemote.Util.getInstance().getAllAccount(new DefaultCallback()
{

@SuppressWarnings("unchecked")
public void onSuccess(Object result) {
if (result != null) {
accounts = (List<Account>)result;

// Save the accounts
accountList.clear();
accountsHM.clear();
for (Account account : accounts) {
accountList.addItem(account.getAccountName());

// Save the accounts
accountsHM.put(account.getAccountName(), account);
}

// Update the list of the
accounts in all clients
fireChangeEvent();
}
}
});
}

And in the clients I have this code:
// ChangeListener to update the account list whenever an account
change is done.
ApplicationParameters.getInstance().getApplication
().getAccountManager().addChangeListener(new ChangeListener() {
public void onChange(Widget sender) {
// Update the accounts
setFilterAccounts(ApplicationParameters.getInstance().getApplication
().getAccountManager().getAccounts());
}
});


Now I'm trying to update this code to use EventHandlers but I've no
idea what I should change. I've try with:
public HandlerRegistration addChangeHandler(ChangeHandler handler) {
return addDomHandler(handler, ChangeEvent.getType());
}

But I cann't control when fire the event, I mean, when I change
something the event is fired and the clients are updated even if it is
not necessary.

Please, could someone bring me to the light :-).

Thanks a lot.

Regards, Iván.

P.S: Excuse me for me poor english.

Kelo

unread,
Jun 9, 2009, 7:51:35 PM6/9/09
to Google Web Toolkit
Acá va un poco de luz ( here's a little bit of light ).


public class AccountManager extends Composite implements
HasValueChangeHandlers<Integer> {
public AccountManager() {
......
......
......
......
initWidget(bar);
}
protected Integer getValue() {
return <some value>;
}
private void loadAccounts() {
AccountRemote.Util.getInstance().getAllAccount(new
DefaultCallback() {
@SuppressWarnings("unchecked")
public void onSuccess(Object result) {
if (result != null) {
accounts = (List<Account>)
result;

// Save the accounts
accountList.clear();
accountsHM.clear();
for (Account account :
accounts) {
accountList.addItem
(account.getAccountName());

// Save the accounts
accountsHM.put
(account.getAccountName(), account);
}

// Update the list of the
accounts in all clients
ValueChangeEvent.fire
(this,getValue());

}
}
});
public HandlerRegistration addValueChangeHandler(final
ValueChangeHandler<Integer> handler) {
return addHandler(handler,ValueChangeEvent.getType());

Iván López

unread,
Jun 10, 2009, 5:43:50 PM6/10/09
to Google Web Toolkit
Thank you very much!. It works ok and now, with my code working I
think I understand a little bit better how handlers works!.

Muchas gracias compañero.

Saludos, Iván.

Kelo

unread,
Jun 11, 2009, 8:05:40 AM6/11/09
to Google Web Toolkit
I'm glad it works fine on your project.

Saludos, Marcelo.
Reply all
Reply to author
Forward
0 new messages