jkteater
jkteater

Reputation: 1391

How can I tell if a listener is already running?

I am adding a event listener to my Singleton class. I am adding the listener in a add method.

 public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
  String revMasterForm;

  tcRevision.getSession().addAIFComponentEventListener(this);

  try {
     revMasterForm = tcRevision.getRelatedComponent("IMAN_master_form_rev").getUid();
     RevDataset pp = new RevDataset(tcRevision, selectedDataset, revMasterForm);
     if (!rds.contains(pp)) {
        rds.add(pp);
     }     
  }
  catch (TCException e) {

     e.printStackTrace();
  }

  fireTableDataChanged();
}

I only want the lister to be adding once. So I figure there has to be some sort of a check. something like

if (listener value == null) {
    tcRevision.getSession().addAIFComponentEventListener(this);
}

But I am not sure how to get the listener value?

Upvotes: 0

Views: 1694

Answers (3)

Spina
Spina

Reputation: 9366

The listener pattern requires that the session (returned by getSession) maintain a collection of things that are listening to it. When the session changes, it goes through this collection and tells each listener that it changed. It does this by invoking a method on each listener. The listener is allowed to run whatever code it wants to in that method.

If you're trying to make sure that you don't register listening multiple times, you can try to get access to the collection (which is probably actually an array). If you can access that array, then before calling tcRevision.getSession().addAIFComponentEventListener(this); you would go through the array and check if any of the entries are 'this'. My suggestion would look something like this:

boolean alreadyAdded = false;
for(AIFComponentEventListener currentListener: tcRevision.getSession().getAIFEventListeners()){
    if(currentListener == this){
        alreadyAdded = true;
    }
}
if( ! alreadyAdded ){
    tcRevision.getSession().addAIFComponentEventListener(this);
}

Note that I'm guessing that there is a method called getAIFEventListeners. If there isn't, there may be a method with a different name but the same behavior. If the session has no such method, then you're forced to use another more complicated approach (such as keeping a list of sessions which you're already listening to.

Hope this helps!

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70949

Event listeners generally don't "run". They are "called" by the items that they are "listening to" or are (sometimes) called by a framework.

The reason they are called "listeners" and not just aggregate object collections is because the interface to attach and remove "listening" objects is defined by an interface that makes no assumptions about the concrete classes. Instead the call back operation occurs only through interfaces (or very rarely abstract classes).

Now, with any class, perhaps the class does "run", but that's completely independent of the listening aspect.

--- Edited with suggestion about adding a listener only once ---

Since a the object responsible for adding a listener is typically not part of the code that is being listened to, why not make the "adding" object query the listeners before deciding to add itself as a listener?

Upvotes: 0

Cratylus
Cratylus

Reputation: 54094

I don't know what is the TCComponentItemRevision class but since this code is inside a Sigleton anyway you could use a boolean addedListener inside the Sigleton to check if the listener has been added:

if (!addedListener) {  
    tcRevision.getSession().addAIFComponentEventListener(this);  
    addedListener = true;  
}

Upvotes: 1

Related Questions