-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathObjectTreeModel.java
More file actions
94 lines (81 loc) · 2.19 KB
/
Copy pathObjectTreeModel.java
File metadata and controls
94 lines (81 loc) · 2.19 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package treeModel;
import java.lang.reflect.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.tree.*;
/**
* This tree model describes the tree structure of a Java object. Children are the objects that are
* stored in instance variables.
*/
public class ObjectTreeModel implements TreeModel
{
private Variable root;
private EventListenerList listenerList = new EventListenerList();
/**
* Constructs an empty tree.
*/
public ObjectTreeModel()
{
root = null;
}
/**
* Sets the root to a given variable.
* @param v the variable that is being described by this tree
*/
public void setRoot(Variable v)
{
Variable oldRoot = v;
root = v;
fireTreeStructureChanged(oldRoot);
}
public Object getRoot()
{
return root;
}
public int getChildCount(Object parent)
{
return ((Variable) parent).getFields().size();
}
public Object getChild(Object parent, int index)
{
ArrayList<Field> fields = ((Variable) parent).getFields();
Field f = (Field) fields.get(index);
Object parentValue = ((Variable) parent).getValue();
try
{
return new Variable(f.getType(), f.getName(), f.get(parentValue));
}
catch (IllegalAccessException e)
{
return null;
}
}
public int getIndexOfChild(Object parent, Object child)
{
int n = getChildCount(parent);
for (int i = 0; i < n; i++)
if (getChild(parent, i).equals(child)) return i;
return -1;
}
public boolean isLeaf(Object node)
{
return getChildCount(node) == 0;
}
public void valueForPathChanged(TreePath path, Object newValue)
{
}
public void addTreeModelListener(TreeModelListener l)
{
listenerList.add(TreeModelListener.class, l);
}
public void removeTreeModelListener(TreeModelListener l)
{
listenerList.remove(TreeModelListener.class, l);
}
protected void fireTreeStructureChanged(Object oldRoot)
{
TreeModelEvent event = new TreeModelEvent(this, new Object[] { oldRoot });
for (TreeModelListener l : listenerList.getListeners(TreeModelListener.class))
l.treeStructureChanged(event);
}
}