Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / debugger / command / ArrayModel.java
1 package jde.debugger.gui;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Comparator;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.TreeSet;
9 import javax.swing.tree.DefaultTreeModel;
10 import javax.swing.tree.MutableTreeNode;
11 import javax.swing.tree.TreeModel;
12 import javax.swing.tree.TreeNode;
13
14 import com.sun.jdi.ArrayReference;
15 import com.sun.jdi.ArrayType;
16 import com.sun.jdi.Field;
17 import com.sun.jdi.ReferenceType;
18 import com.sun.jdi.StringReference;
19 import com.sun.jdi.Value;
20 import jde.debugger.JDEException;
21
22
23
24 /** A TreeNode for array references.
25  * @author <a href="mailto:udalrich@carolingia.org">Troy Daniels</a>
26  * @since 2.3.2
27  * @version $Revision: 1.1 $
28  */
29 class ArrayModel extends ReferenceModel {
30   /**  Constructor
31    */
32   ArrayModel(ArrayReference value, DefaultTreeModel treeModel)  {
33     super();
34     if (null == value)
35       throw new IllegalArgumentException("Null value in constructor.");
36     if (null == treeModel)
37       throw new IllegalArgumentException("Null tree model in constructor.");
38     m_value = value;
39     m_treeModel = treeModel;
40   }
41
42
43   /** Get the number of children.
44    * @return The number of fields that we should be displaying.
45    */
46   int getChildCount() {
47     return m_value.length();
48   }
49   /** Returns if the node allows children */
50   boolean getAllowsChildren() { return true; }
51
52   /** Create a child at the given index.
53    * @return A tree node for the child at the given index */
54   MutableTreeNode createChildAt(int index) throws JDEException {
55     String name = getIndexName(index);
56     ArrayType arrayType = (ArrayType) m_value.type();
57     MutableTreeNode node =
58         LVTreeNode.makeTreeNode(name,
59                                 arrayType.componentTypeName(),
60                                 m_value.getValue(index),
61                                 m_treeModel);
62     return node;
63   }
64
65   /** Get a string to represent the value of the variable */
66   String getValue() {
67       return m_value.type().name() + "[" + m_value.length() + "]";
68   }
69
70   private static String getIndexName(int index) {
71     return "[" + index + "]";
72   }
73
74   /** Update the values in the children
75    * @param children The array of old child values
76    */
77   void updateChildren(TreeNode[] children) {
78     // For the children that already exist, set the new value.
79     TreeNode reloadParent = null;
80     for (int index = 0;
81          (index < children.length) &&
82            (index < m_value.length());
83          ++index) {
84       LVTreeNode child = asLVTreeNode(children[index]);
85       if ((null != child) &&
86           (child.getName().equals(getIndexName(index))))
87         child.setValue(m_value.getValue(index));
88       else {
89                 // Different node.  Reset it
90         if ((null != children[index]) &&
91             (null != children[index].getParent()))
92           reloadParent = children[index].getParent();
93         children[index] = null;
94       }
95     } // for each child
96
97     // Reload the model if we had significant changes
98     if (null != reloadParent)
99       m_treeModel.reload(reloadParent);
100   }
101
102   private final ArrayReference m_value;
103   private final DefaultTreeModel m_treeModel;
104 }