View Javadoc

1   /*
2    * $Id: ObjectTreeFrame.java,v 1.1 2005/10/17 01:28:18 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.awt.BorderLayout;
26  import java.awt.FlowLayout;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import javax.swing.JButton;
33  import javax.swing.JFrame;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JTree;
37  
38  import org.zmpp.debug.ObjectTreeModel.ObjWrapper;
39  import org.zmpp.vm.Machine3;
40  import org.zmpp.vm.ObjectTree;
41  import org.zmpp.vm.ZObject;
42  
43  public class ObjectTreeFrame extends JFrame implements ActionListener {
44  
45    private ObjectTreeModel treeModel;
46    private ObjectTree objectTree;
47    private static final long serialVersionUID = 1L;
48    
49    public ObjectTreeFrame(Machine3 machine) {
50      super("Object tree");
51      
52      treeModel = new ObjectTreeModel(machine, machine.getObjectTree());
53      JTree tree = new JTree(treeModel);
54      this.objectTree = machine.getObjectTree();
55      JScrollPane spane = new JScrollPane(tree);
56      JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
57      JButton okButton = new JButton("Ok");
58      buttonPanel.add(okButton);
59      okButton.addActionListener(this);
60      getContentPane().add(spane, BorderLayout.CENTER);
61      getContentPane().add(buttonPanel, BorderLayout.SOUTH);
62      update();
63    }
64    
65    public void update() {
66  
67      int numObjects = objectTree.getNumObjects();
68      List<ObjWrapper> rootObjs = new ArrayList<ObjWrapper>();
69      
70      for (int i = 1; i <= numObjects; i++) {
71        
72        ZObject obj = objectTree.getObject((short) i);
73        if (obj.getParent() == 0) {
74          
75          ObjWrapper tlnode = treeModel.createObjWrapper();
76          tlnode.id = i;
77          tlnode.obj = obj;
78          rootObjs.add(tlnode);
79        }
80      }
81      treeModel.setToplevelObjects(rootObjs);
82    }
83    
84    public void actionPerformed(ActionEvent e) {
85      
86      System.out.println("update");
87      update();
88    }
89  }