1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.zmpp.debug;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.swing.event.TreeModelEvent;
29 import javax.swing.event.TreeModelListener;
30 import javax.swing.tree.TreeModel;
31 import javax.swing.tree.TreePath;
32
33 import org.zmpp.vm.Machine;
34 import org.zmpp.vm.ObjectTree;
35 import org.zmpp.vm.ZObject;
36 import org.zmpp.vmutil.ZsciiString;
37
38 public class ObjectTreeModel implements TreeModel {
39
40 public class ObjWrapper {
41
42 public int id;
43 public ZObject obj;
44 public String toString() {
45
46 ZsciiString str = new ZsciiString(machine.getMemoryAccess(), obj.getPropertiesDescriptionAddress());
47 return "[" + id + "] " + str.toString();
48 }
49 }
50
51 private List<ObjWrapper> toplevel;
52 private ObjectTree objectTree;
53 private List<TreeModelListener> listeners;
54 Machine machine;
55
56 public ObjectTreeModel(Machine machine, ObjectTree objectTree) {
57
58 this.objectTree = objectTree;
59 listeners = new ArrayList<TreeModelListener>();
60 this.machine = machine;
61 }
62
63 public ObjWrapper createObjWrapper() {
64
65 return new ObjWrapper();
66 }
67
68 public void setToplevelObjects(List<ObjWrapper> objs) {
69
70 toplevel = objs;
71 Object[] path = { "Root" };
72 TreeModelEvent event = new TreeModelEvent("Root", path);
73
74 for (TreeModelListener l : listeners) {
75
76 l.treeStructureChanged(event);
77 }
78 }
79
80 public void addTreeModelListener(TreeModelListener l) {
81
82 listeners.add(l);
83 }
84 public void removeTreeModelListener(TreeModelListener l) {
85
86 listeners.remove(l);
87 }
88
89 public int getIndexOfChild(Object parent, Object child) {
90
91 if (parent.equals("Root")) {
92
93 return toplevel.indexOf(child);
94 } else {
95
96
97
98 }
99 return 0;
100 }
101
102 public Object getChild(Object parent, int index) {
103
104 if (parent.equals("Root")) {
105
106 return toplevel.get(index);
107 } else {
108
109 ZObject p = ((ObjWrapper) parent).obj;
110 int cid = p.getChild();
111 ZObject child = objectTree.getObject(cid);
112
113 for (int i = 0; i < index; i++) {
114
115 cid = objectTree.getObject(cid).getSibling();
116 child = objectTree.getObject(cid);
117 }
118 ObjWrapper result = new ObjWrapper();
119 result.id = cid;
120 result.obj = child;
121 return result;
122 }
123 }
124
125 public int getChildCount(Object parent) {
126
127 if (parent.equals("Root")) {
128
129 return (toplevel != null) ? toplevel.size() : 0;
130 } else {
131
132 ZObject p = ((ObjWrapper) parent).obj;
133 int count = 0;
134 int ci = p.getChild();
135 while (ci > 0) {
136
137 ci = objectTree.getObject(ci).getSibling();
138 count++;
139 }
140 return count;
141 }
142 }
143
144 public Object getRoot() {
145
146 return "Root";
147 }
148
149 public boolean isLeaf(Object node) {
150
151 if (node.equals("Root")) {
152 return false;
153 } else {
154
155 ObjWrapper obj = (ObjWrapper) node;
156 return (obj.obj.getChild() == 0);
157 }
158 }
159
160 public void valueForPathChanged(TreePath path, Object newValue) {
161
162 }
163 }