Confused about custom events

?

-

I have a ConnnectionListener that has connectionOpened and
connectionClosed methods.

I have a ClientConnectionListener that extends ConnectionListener and
has several other specific methods.

I have a Connection class that has
addConnectionListener(ConnectionListener cl) {
...add(ConnectionListener.class, cl);
}

It seems that adding it requires "ConnectionListener.class".
Does this mean that my ClientConnection has to :

1) Override the addConnectionListener and change it to
"ClientConnectionListener.class" or

2) Create a method
addClientConnectionListener(ClientConnectionListener...) ?
 
S

Stefan Schulz

I have a ConnnectionListener that has connectionOpened and
connectionClosed methods.

I have a ClientConnectionListener that extends ConnectionListener and
has several other specific methods.

I have a Connection class that has
addConnectionListener(ConnectionListener cl) {
...add(ConnectionListener.class, cl);
}

It seems that adding it requires "ConnectionListener.class".
Does this mean that my ClientConnection has to :

1) Override the addConnectionListener and change it to
"ClientConnectionListener.class" or

2) Create a method
addClientConnectionListener(ClientConnectionListener...) ?

If you want to fire the events only ClientConnectionListener has, yes. If
you, however, want to use a ClientConnectionListener just as if where a
normal ConnectionListener, then there is no problem at all using the
simple menthod.
 
?

-

Stefan said:
If you want to fire the events only ClientConnectionListener has, yes. If
you, however, want to use a ClientConnectionListener just as if where a
normal ConnectionListener, then there is no problem at all using the
simple menthod.

As mentioned in the EventListenerList class:

Quote:
EventListenerList listenerList = new EventListenerList();
FooEvent fooEvent = null;

public void addFooListener(FooListener l) {
listenerList.add(FooListener.class, l);
}

public void removeFooListener(FooListener l) {
listenerList.remove(FooListener.class, l);
}


// Notify all listeners that have registered interest for
// notification on this event type. The event instance
// is lazily created using the parameters passed into
// the fire method.

protected void fireFooXXX() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners==FooListener.class) {
// Lazily create the event:
if (fooEvent == null)
fooEvent = new FooEvent(this);
((FooListener)listeners[i+1]).fooXXX(fooEvent);
}
}
}
Unquote


Notice that FooListener.class is being used both for the add/remove and
fireFooXX methods.

If I have SubclassFooListener and add to addFooListener(FooListener l) ,
calling fireFooXXX is of no use since it only checks for
FooListener.class and not SubclassFooListener.class.

I couldn't find any existing listeners in the java api that actually
subclasses another listener... Is it because it's not supposed to?
 
S

Stefan Schulz

public void addFooListener(FooListener l) {
listenerList.add(FooListener.class, l);
}

Explicitly uses FooListener, not l.getClass();
public void removeFooListener(FooListener l) {
listenerList.remove(FooListener.class, l);
}

Explicitly uses FooListener, not l.getClass();
If I have SubclassFooListener and add to addFooListener(FooListener l) ,
calling fireFooXXX is of no use since it only checks for
FooListener.class and not SubclassFooListener.class.

That is right. Should it? After all, by calling addFooListener, for the
purposes of this one class, all subclasses of FooListener collapse to
FooListeners.
I couldn't find any existing listeners in the java api that actually
subclasses another listener... Is it because it's not supposed to?

It is usually not really necessary, since most events do not really lend
themselves to extension, but i don't see why it should not be possible.
 
?

-

Stefan said:
That is right. Should it? After all, by calling addFooListener, for the
purposes of this one class, all subclasses of FooListener collapse to
FooListeners.

It's not the case.


public class Foo {

void addFooListener(FooListener fl) {
list.add(FooListener.class, fl);
}

...
}

public interface FooListener {

}

public interface SubFooListener extends FooListener {

}

For the following:

1. Foo.addFooListener(new FooListener());
2. Foo.addFooListener(new SubFooListener()));

when fireFoo is called, only '1' succeeds as

the statement inside fireFoo: listener == FooListener.class
only checks for FooListener class.

It's not identical to 'instanceof' where one would expect SubFooListener
to be detected as FooListener.
 
R

Roedy Green

It seems that adding it requires "ConnectionListener.class".
Does this mean that my ClientConnection has to :

1) Override the addConnectionListener and change it to
"ClientConnectionListener.class" or

2) Create a method
addClientConnectionListener(ClientConnectionListener...) ?

no. This is because every ClientConnectionListener is also a
ConnectionLister. ClientConnectionListener meets the criteria for
addConnectionListener().

This wonderful feature is much of what drove us to OO in the first
place.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top