summaryrefslogtreecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/ActMgr.java
blob: a2ad23be581b8b156b3b4566ff3ccf1d4df9ae52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.macaroon.piztor;

import java.util.*;

import android.annotation.SuppressLint;

@SuppressLint("UseSparseArrays")
public class ActMgr {
	final static int Create = -1;
	// event
	PiztorAct act;
	ActStatus nowStatus;
	HashMap<ActStatus, HashMap<Integer, ActStatus>> mp;
	AppMgr appMgr;
	ActMgr(AppMgr appMgr, PiztorAct act, ActStatus nowStatus, ActStatus[] r) {
		this.appMgr = appMgr;  
		this.act = act;
		this.nowStatus = nowStatus;
		nowStatus.enter(Create);
		mp = new HashMap<ActStatus, HashMap<Integer, ActStatus>>();
		for (int i = 0; i < r.length; i++) {
			mp.put(r[i], new HashMap<Integer, ActStatus>());
		}
	}

	void trigger(int event) {
		System.out.println(act.id + " : " + event);
		if (mp.get(nowStatus).containsKey(event)) {
			System.out.println("status + " + event);
			nowStatus.leave(event);
			nowStatus = mp.get(nowStatus).get(event);
			nowStatus.enter(event);
		} else if (appMgr.mp.get(act.getClass()).containsKey(event)) {
			appMgr.trigger(event);
		} else {
			System.out.println("can not trigger the event at " + act.id + " : "
					+ event);
		}
	}

	void add(ActStatus a, int event, ActStatus b) {
		if (mp.containsKey(a)) {
			HashMap<Integer, ActStatus> h = mp.get(a);
			h.put(event, b);
			mp.put(a, h);
		} else {
			HashMap<Integer, ActStatus> h = new HashMap<Integer, ActStatus>();
			h.put(event, b);
			mp.put(a, h);
		}
	}
}

abstract class ActStatus {
	abstract void enter(int e);

	abstract void leave(int e);
}

class EmptyStatus extends ActStatus {
	@Override
	void enter(int e) {
	}

	@Override
	void leave(int e) {
	}

}