View Javadoc

1   package cz.cuni.amis.pogamut.base.communication.worldview.react;
2   
3   import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
4   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEvent;
5   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObject;
6   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
7   import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
8   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectListener;
9   
10  /**
11   * This abstract class allows you to easily hook a specific event-handling behavior. It automatically
12   * register a listener for a specified {@link IWorldObjectEvent} for you and calls {@link ObjectEventReact#react(IWorldObjectEvent)}
13   * method automatically. The {@link ObjectEventReactOnce#react(IWorldObjectEvent)} will be called only once (upon first event received).
14   * <p><p>
15   * If you need to react on every event, use {@link ObjectEventReact}.
16   * <p><p>
17   * Use {@link ObjectEventReact#enable()} and {@link ObjectEventReact#disable()} to enable react / disable react. The reaction is enabled
18   * as default.
19   * <p><p>
20   * <b>WARNING:</b>Use as anonymous class, but <b>save it as a field</b> of your class! Note, that we're using weak-references to 
21   * listeners and if you do not save pointer to the object, it will be gc()ed!
22   * 
23   * @author Jimmy
24   *
25   * @param <EVENT>
26   */
27  public abstract class ObjectEventReactOnce<OBJECT extends IWorldObject, EVENT extends IWorldObjectEvent<OBJECT>> extends ObjectEventReact<OBJECT, EVENT> {
28  
29  	public ObjectEventReactOnce(Class<?> objectClass, IWorldView worldView) {
30  		super(objectClass, worldView);
31  	}	
32  
33  	public ObjectEventReactOnce(Class<?> objectClass, Class<?> eventClass, IWorldView worldView) {
34  		super(objectClass, eventClass, worldView);
35  	}
36  
37  	public ObjectEventReactOnce(WorldObjectId objectId, IWorldView worldView) {
38  		super(objectId, worldView);
39  	}
40  
41  	public ObjectEventReactOnce(WorldObjectId objectId, Class<?> eventClass, IWorldView worldView) {
42  		super(objectId, eventClass, worldView);
43  	}
44  	
45  	/**
46  	 * Disables the reaction.
47  	 */
48  	@Override
49  	protected void postReact(EVENT event) {
50  		super.postReact(event);
51  		disable();
52  	}
53  	
54  }