Android Open Source - DragonGoApp Event Manager






From Project

Back to project page DragonGoApp.

License

The source code is released under:

GNU General Public License

If you think the Android project DragonGoApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fr.xtof54.jsgo;
//from ww w .ja va 2s  .  c  o m
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * This class is used to receive and dispatch events
 * 
 * @author xtof
 *
 */
public class EventManager {
  enum eventType {loginStarted, loginEnd, downloadListStarted, downloadListEnd, downloadListGamesEnd, downloadGameStarted, downloadGameEnd, GameOK, moveSentStart, moveSentEnd, gobanReady,
    msgSendStart, msgSendEnd, ladderStart, ladderEnd, ladderChallengeStart, ladderChallengeEnd, showMessage, copyEidogoStart, copyEidogoEnd};
  
  private static EventManager em = new EventManager();
  public static EventManager getEventManager() {
    return em;
  }
  private EventManager() {}
  
  public interface EventListener {
    public void reactToEvent();
    public String getName();
  }
  private HashMap<eventType, List<EventListener>> listeners = new HashMap<EventManager.eventType, List<EventListener>>();
  
  public void registerListener(eventType e, EventListener f) {
      System.out.println("registering event listener "+e+" "+f.getName());
    List<EventListener> l = listeners.get(e);
    final String name = f.getName();
    if (l==null) {
      l=new ArrayList<EventManager.EventListener>();
      listeners.put(e, l);
    } else {
        for (EventListener el : l)
            if (el.getName().equals(name)) {
                System.out.println("refusing event "+el.getName());
                // refuse to register 2 times the same listener !
                return;
            }
    }
    l.add(f);
  }
  public void unregisterListener(eventType e, EventListener f) {
        System.out.println("unregistering event listener "+e+" "+f.getName());
    List<EventListener> l = listeners.get(e);
    if (l!=null) {
      l.remove(f);
      if (l.size()==0) listeners.remove(e);
    }
  }
  
  public String message=null;
    public void sendEvent(final eventType e, String msg) {
        message=""+msg;
        sendEvent(e);
    }
  public void sendEvent(final eventType e) {
    Thread tt = new Thread(new Runnable() {
      @Override
      public void run() {
        List<EventListener> l = listeners.get(e);
        System.out.println("Event sent: "+e+" "+l);
        if (l!=null) {
            // make a copy of the listeners list to avoid concurrent modification of the original list
            ArrayList<EventListener> list = new ArrayList<EventManager.EventListener>();
            list.addAll(l);
                for (EventListener f : list)
                    f.reactToEvent();
        }
      }
    });
    tt.start();
  }
}




Java Source Code List

fr.xtof54.jsgo.AndroidServerConnection.java
fr.xtof54.jsgo.EventManager.java
fr.xtof54.jsgo.Forums.java
fr.xtof54.jsgo.GUI.java
fr.xtof54.jsgo.Game.java
fr.xtof54.jsgo.GoJsActivity.java
fr.xtof54.jsgo.Ladder.java
fr.xtof54.jsgo.Message.java
fr.xtof54.jsgo.PrefUtils.java
fr.xtof54.jsgo.Reviews.java
fr.xtof54.jsgo.ServerConnection.java
fr.xtof54.jsgo.WebAppInterface.java
org.json.CDL.java
org.json.CookieList.java
org.json.Cookie.java
org.json.HTTPTokener.java
org.json.HTTP.java
org.json.JSONArray.java
org.json.JSONException.java
org.json.JSONML.java
org.json.JSONObject.java
org.json.JSONString.java
org.json.JSONStringer.java
org.json.JSONTokener.java
org.json.JSONWriter.java
org.json.Kim.java
org.json.Property.java
org.json.XMLTokener.java
org.json.XML.java