/** Gets the value for the given node at the given column. */ public Object getValueAt(Object node, int col) { DefaultMutableTreeNode tree_node = (DefaultMutableTreeNode) node; Object value = tree_node.getUserObject(); switch (col) { // Name case 0: return value; // Value case 1: if (value instanceof ConfigElement) { return null; } else if (value instanceof PropertyDefinition) { // Only provide comma delimited editing for simple types if (((PropertyDefinition) value).getType() != ConfigElement.class) { StringBuffer buffer = new StringBuffer(); for (Enumeration e = tree_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode child_node = (DefaultMutableTreeNode) e.nextElement(); buffer.append(child_node.getUserObject()); if (e.hasMoreElements()) { buffer.append(", "); } } return buffer.toString(); } return null; } return value; } return null; }
/** * This gets called whenever one of the ConfigElements we are editing removes a property value. */ public void propertyValueRemoved(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Get the node containing the property description under the source // ConfigElement node for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { // The newly removed property value must be a child to this node System.out.println("Removing child " + idx + " from node: " + node.getUserObject()); DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(idx); // If the child is an embedded element, stop listening to it if (child.getUserObject() instanceof ConfigElement) { ConfigElement removed_elt = (ConfigElement) child.getUserObject(); removed_elt.removeConfigElementListener(this); } // Physically remove the child from the tree removeNodeFromParent(child); } } }
/** * This gets called whenever one of the ConfigElements we are editing adds a new property value. */ public void propertyValueAdded(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Get the node containing the property description under the source // ConfigElement node for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { // The newly inserted property value must be added as a child to // this node if (prop_def.getType() != ConfigElement.class) { DefaultMutableTreeNode new_child = new DefaultMutableTreeNode(evt.getValue()); insertNodeInto(new_child, node, idx); } else { // Embedded elements are handled specially in that all of their // respective child properties and such also need to be added to // the tree at this time. addEmbeddedElement(node, (ConfigElement) evt.getValue(), idx); } } } }
/** Clears the data in the model. */ private void clear() { // Stop listening to all config elements in the tree List nodes = getNodesOfClass(ConfigElement.class); for (Iterator itr = nodes.iterator(); itr.hasNext(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) itr.next(); ConfigElement elt = (ConfigElement) node.getUserObject(); elt.removeConfigElementListener(mElementListener); } // Clear out all the old nodes from the tree. ((DefaultMutableTreeNode) getRoot()).removeAllChildren(); }
/** Indicates whether the value for the given node at the given column is editable. */ public boolean isCellEditable(Object node, int col) { // The tree column has to be editable so it can get the events if (col == 0) { return true; } DefaultMutableTreeNode tree_node = (DefaultMutableTreeNode) node; Object value = tree_node.getUserObject(); // config elements nodes are not editable if (value instanceof ConfigElement) { return false; } else { return true; } }
/** * Gets a list of the nodes in this model that contain an object of the given class starting with * the given node. * * @param cls the class to search for * @param node the node whose subtree will be searched * @return a list of matching DefaultMutableTreeNodes */ public List getNodesOfClass(Class cls, DefaultMutableTreeNode node) { List matches = new ArrayList(); // Check if the current node matches if (cls.isInstance(node.getUserObject())) { matches.add(node); } // Recurse to the children of the current node for (Enumeration e = node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement(); matches.addAll(getNodesOfClass(cls, child)); } return matches; }
/** * This gets called whenever one of the ConfigElements we are editing has the values of a * property get reordered. */ public void propertyValueOrderChanged(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Get the node containing the property description under the source // ConfigElement node. for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { int start_index = Math.min(evt.getIndex0(), evt.getIndex1()); int end_index = Math.max(evt.getIndex0(), evt.getIndex1()); List removed_children = new ArrayList(); for (int c = start_index; c <= end_index; ++c) { removed_children.add(getChild(node, c)); } for (Iterator c = removed_children.iterator(); c.hasNext(); ) { removeNodeFromParent((MutableTreeNode) c.next()); } String prop_token = prop_def.getToken(); for (int v = start_index; v <= end_index; ++v) { if (prop_def.getType() != ConfigElement.class) { // Create a new node for the reordered property value. DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(src.getProperty(prop_token, v)); // Add the new node into the tree. insertNodeInto(new_node, node, v); } else { // Embedded elements are handled specially in that all of // their respective child properties and such also need to // be added to the tree at this time. ConfigElement cur_value = (ConfigElement) src.getProperty(prop_token, v); addEmbeddedElement(node, cur_value, v); } } } } }
/** Gets the node for the given object */ private DefaultMutableTreeNode getNodeFor(Object obj, DefaultMutableTreeNode node) { // System.out.println("getNodeFor() node: " + node.getUserObject()); // Check if we found a match if (obj.equals(node.getUserObject())) { return node; } // Check all children of the current node for (Enumeration e = node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement(); DefaultMutableTreeNode result = getNodeFor(obj, child); if (result != null) { return result; } } // Didn't find anything :( return null; }
/** * This gets called whenever one of the ConfigElements we are editing has one of its property * values change. */ public void propertyValueChanged(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Multi-valued properties and embedded elements are treated specially if ((prop_def.getPropertyValueDefinitionCount() > 1) || (prop_def.isVariable()) || (prop_def.getType() == ConfigElement.class)) { // Look for the property definition node for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(idx); fireTreeNodesChanged( this, new Object[] {getPathToRoot(node)}, new int[] {node.getIndex(child)}, new Object[] {child}); } } } // Property value is not an embedded element else { // Take into account the extra two rows at the top of the table if (elt_node == getRoot()) { idx += 2; } fireTreeNodesChanged( this, new Object[] {getPathToRoot(elt_node)}, new int[] {idx}, new Object[] {elt_node.getChildAt(idx)}); } }
/** Sets the value for the given node at the given column to the given value. */ public void setValueAt(Object value, Object node, int col) { DefaultMutableTreeNode tree_node = (DefaultMutableTreeNode) node; Object node_value = tree_node.getUserObject(); switch (col) { // Name (not supported) case 0: break; // Value case 1: DefaultMutableTreeNode parent_node = (DefaultMutableTreeNode) tree_node.getParent(); // First child of root is always the element name if (parent_node == getRoot() && parent_node.getIndex((TreeNode) node) == 0) { ConfigElement elt = (ConfigElement) parent_node.getUserObject(); elt.setName((String) value); tree_node.setUserObject(value); } else if (node_value instanceof PropertyDefinition) { // Hey, we're editing a property definition. If it's type is not a // configuration element, we're probably editing a summary list of // the valuesof the children. if (((PropertyDefinition) node_value).getType() != ConfigElement.class) { ConfigElement elt = (ConfigElement) parent_node.getUserObject(); PropertyDefinition prop_def = (PropertyDefinition) node_value; StringTokenizer tokenizer = new StringTokenizer((String) value, ", "); int idx = 0; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); // Make sure we don't overrun the property values if ((idx >= prop_def.getPropertyValueDefinitionCount()) && (!prop_def.isVariable())) { break; } // Convert the value to the appropriate type Object new_value = null; Class type = prop_def.getType(); if (type == Boolean.class) { new_value = new Boolean(token); } else if (type == Integer.class) { new_value = new Integer(token); } else if (type == Float.class) { new_value = new Float(token); } else if (type == String.class) { new_value = new String(token); } else if (type == ConfigElementPointer.class) { new_value = new ConfigElementPointer(token); } setProperty(new_value, elt, prop_def.getToken(), idx); // Get the node for the current property value and update it if (idx < tree_node.getChildCount()) { DefaultMutableTreeNode child_node = (DefaultMutableTreeNode) tree_node.getChildAt(idx); child_node.setUserObject(new_value); } else { // Insert the new property DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_value); tree_node.add(new_node); fireTreeNodesInserted( this, new Object[] {getPathToRoot(tree_node)}, new int[] {tree_node.getIndex(new_node)}, new Object[] {new_node}); } ++idx; } } } else { // Parent is a ConfigElement ... must be a single-valued property if (parent_node.getUserObject() instanceof ConfigElement) { ConfigElement elt = (ConfigElement) parent_node.getUserObject(); int desc_idx = parent_node.getIndex(tree_node); // If the parent is the root, take into account the extra name // and type nodes if (parent_node == getRoot()) { desc_idx -= 2; } PropertyDefinition prop_def = (PropertyDefinition) elt.getDefinition().getPropertyDefinitions().get(desc_idx); setProperty(value, elt, prop_def.getToken(), 0); tree_node.setUserObject(value); } else { // Parent must be a PropertyDefinition PropertyDefinition prop_def = (PropertyDefinition) parent_node.getUserObject(); int value_idx = parent_node.getIndex(tree_node); DefaultMutableTreeNode elt_node = (DefaultMutableTreeNode) parent_node.getParent(); ConfigElement elt = (ConfigElement) elt_node.getUserObject(); setProperty(value, elt, prop_def.getToken(), value_idx); tree_node.setUserObject(value); } } fireTreeNodesChanged( this, new Object[] {getPathToRoot(parent_node)}, new int[] {parent_node.getIndex(tree_node)}, new Object[] {tree_node}); break; default: throw new IllegalArgumentException("Invalid column: " + col); } }