1 package org.catacomb.interlish.content;
2
3 import org.catacomb.be.ChangeEvent;
4 import org.catacomb.interlish.content.BasicTouchTime;
5 import org.catacomb.interlish.structure.*;
6 import org.catacomb.report.E;
7
8
9 import java.util.ArrayList;
10
11 public class PrimitiveValue implements Value, TouchTimed, ParentReporter {
12
13
14 private BasicTouchTime touchTime;
15
16 private ArrayList<ValueWatcher> watchers;
17 private ArrayList<UseWatcher> useWatchers;
18
19 private ChildListener childListener;
20 String reportableID;
21
22 private boolean able;
23
24 private boolean reportingUse;
25
26
27 public PrimitiveValue() {
28 touchTime = new BasicTouchTime();
29 able = true;
30 }
31
32 public PrimitiveValue(boolean b) {
33 touchTime = new BasicTouchTime();
34 able = b;
35 }
36
37
38 public void setAble(boolean b) {
39 if (b != able) {
40 able = b;
41 reportValueChange(this);
42 }
43 }
44
45 public boolean isAble() {
46 return able;
47 }
48
49 public void setChildListener(ChildListener cl, String srcid) {
50 childListener = cl;
51 reportableID = srcid;
52 }
53
54
55 public void removeChildListener() {
56 childListener = null;
57 }
58
59
60 public void addValueWatcher(ValueWatcher vw) {
61 if (watchers == null) {
62 watchers = new ArrayList<ValueWatcher>();
63 }
64 watchers.add(vw);
65 }
66
67 public void removeValueWatcher(ValueWatcher vw) {
68 if (watchers == null) {
69 E.warning("cant remove - not present");
70 } else {
71 if (watchers.contains(vw)) {
72 watchers.remove(vw);
73
74 } else {
75 E.warning("attempted to remove a watcher that isnt in the list");
76 }
77 }
78 }
79
80
81
82 public void addUseWatcher(UseWatcher vw) {
83 if (useWatchers == null) {
84 useWatchers = new ArrayList<UseWatcher>();
85 }
86 useWatchers.add(vw);
87 }
88
89 public void removeUseWatcher(UseWatcher vw) {
90 if (useWatchers == null) {
91 E.warning("cant remove - not present");
92 } else {
93 if (useWatchers.contains(vw)) {
94 useWatchers.remove(vw);
95
96 } else {
97 E.warning("attempted to remove a watcher that isnt in the list");
98 }
99 }
100 }
101
102
103
104
105
106 public BasicTouchTime getTouchTime() {
107 return touchTime;
108 }
109
110
111 public void reportValueChange(Object src) {
112 if (watchers != null) {
113 for (ValueWatcher w : watchers) {
114 w.valueChangedBy(this, src);
115 }
116 }
117 valueChanged();
118 }
119
120
121 public void reportUse(Object src) {
122 if (!reportingUse) {
123 reportingUse = true;
124 if (useWatchers != null) {
125 for (UseWatcher w : useWatchers) {
126 w.usedBy(this, src);
127 }
128 }
129 reportingUse = false;
130 }
131 }
132
133
134 public void logChange() {
135 touchTime.now();
136 }
137
138 public boolean changedSince(TouchTimed tt) {
139 return (touchTime.isAfter(tt.getTouchTime()));
140 }
141
142
143
144 public void editCompleted() {
145 touchTime.now();
146
147
148 }
149
150 public void valueChanged() {
151 if (childListener != null) {
152 childListener.childChanged(new ChangeEvent(reportableID, this));
153 }
154 }
155
156 }