// (c) Matti Katila package gzz.modules.pp; import gzz.vob.VobScene; import gzz.util.Pair; import java.awt.event.*; import java.util.*; /** Handles events and Emulates funtion pointer. * STATE: PROTOTYPE */ public class EventHandling { public void pa(String s) { System.out.println(s); } // Singleton static private EventHandling instance; private EventHandling() { } /** get instance */ static public EventHandling i() { if (instance == null) instance = new EventHandling(); return instance; } // private Map callers = new TreeMap(); private Map ev_handlers = new TreeMap(); private final int MASK = 10000000; private final int CLICK = 20000000; private final int DRAG = 30000000; // give cs. public void onClick(int cs, String key, Object[] obs) { callers.put(""+(cs + CLICK), new Pair(key, obs) ); } public void onDrag(int cs, String key, Object[] obs) { callers.put(""+(cs + DRAG), new Pair(key, obs) ); } public void assign(String key, Object obs) { ev_handlers.put(key, obs); } private int last_cs = -10; public void handleEvent(VobScene vs, MouseEvent ev) { int cs = vs.getCSAt(0, ev.getX(), ev.getY(), null); pa("cs: " + cs); switch(ev.getID()) { case MouseEvent.MOUSE_CLICKED: { cs += CLICK; pa("mouse click"); break; } case MouseEvent.MOUSE_PRESSED: { pa("mouse pressed"); break; } case MouseEvent.MOUSE_EXITED: { pa("mouse exited"); last_cs = -1; break; } case MouseEvent.MOUSE_DRAGGED: { cs += DRAG; pa("mouse dragged"); break; } }; pa("cs: " + cs); Object pair = callers.get(""+cs); if (pair == null) return; if (!(pair instanceof Pair)) { throw new Error("NOT PAIR!"); } String key = (String)((Pair)pair).first; Object ev_h = ev_handlers.get(key); pa("foo: "+ev_h); if (ev_h instanceof EventObj) ((EventObj)ev_h).event(ev, ( (Object[]) ((Pair)pair).second)); } public interface EventObj { public void event(MouseEvent ev, Object[] obs); } }