View Javadoc

1   /*
2    * $Id: ObjectTreeModel.java,v 1.2 2005/10/24 16:04:43 weiju Exp $
3    * 
4    * Created on 15.10.2005
5    * Copyright 2005 by Wei-ju Wu
6    *
7    * This file is part of The Z-machine Preservation Project (ZMPP).
8    *
9    * ZMPP is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   *
14   * ZMPP is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with ZMPP; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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        //ObjWrapper p = (ObjWrapper) parent; 
97        //ObjWrapper c = (ObjWrapper) child; 
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 }