You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by me...@apache.org on 2001/01/12 20:02:22 UTC

cvs commit: jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs ACSFactory.java ElementTreeModel.java

metasim     01/01/12 11:02:22

  Modified:    src/antidote/org/apache/tools/ant/gui/acs ACSFactory.java
                        ElementTreeModel.java
  Log:
  Added ability to add new targets.
  
  Revision  Changes    Path
  1.7       +20 -1     jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSFactory.java
  
  Index: ACSFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSFactory.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ACSFactory.java	2001/01/11 19:24:30	1.6
  +++ ACSFactory.java	2001/01/12 19:02:20	1.7
  @@ -70,7 +70,7 @@
   /**
    * Factory for loading Ant Construction set elements.
    * 
  - * @version $Revision: 1.6 $ 
  + * @version $Revision: 1.7 $ 
    * @author Simeon Fitch 
    */
   public class ACSFactory {
  @@ -201,9 +201,28 @@
       public ACSTargetElement createTarget(ACSProjectElement project) {
           ACSTargetElement retval = (ACSTargetElement) project.
               getOwnerDocument().createElement("target");
  +        indent(project, 1);
           project.appendChild(retval);
           return retval;
       }
  +
  +    /** 
  +     * Insert a new line and indentation at the end of the given
  +     * node in preparation for a new element being added.
  +     * 
  +     * @param node Node to append indent to.
  +     * @param level Indentation level.
  +     */
  +    private void indent(ACSElement node, int level) {
  +        StringBuffer buf = new StringBuffer("\n");
  +        for(int i = 0; i < level; i++) {
  +            buf.append("    ");
  +        }
  +
  +        Text text = node.getOwnerDocument().createTextNode(buf.toString());
  +        node.appendChild(text);
  +    }
  +
   
   	/** 
   	 * Test code
  
  
  
  1.4       +95 -3     jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ElementTreeModel.java
  
  Index: ElementTreeModel.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ElementTreeModel.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ElementTreeModel.java	2001/01/11 22:30:05	1.3
  +++ ElementTreeModel.java	2001/01/12 19:02:21	1.4
  @@ -59,14 +59,14 @@
   import javax.swing.event.TreeModelEvent;
   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
  -import org.apache.tools.ant.gui.acs.ACSProjectElement;
  +import org.apache.tools.ant.gui.acs.*;
   import java.util.*;
   
   /**
    * Provides a tree model view of the Project class. XXX This
    * is a major hack right now that needs to be cleaned up.
    *
  - * @version $Revision: 1.3 $ 
  + * @version $Revision: 1.4 $ 
    * @author Simeon H.K. Fitch */
   public class ElementTreeModel implements TreeModel {
       /** Root of the tree. */
  @@ -90,6 +90,9 @@
   
       /** 
        * Gets the set of children that this tree model is interested in.
  +     * NB: This is <b>really</b> inefficient, but may not be an issue given
  +     * the number of times it is ultimately called. A profiler definately needs
  +     * to be applied here.
        * 
        * @param parent Parent to extract children from.
        */
  @@ -123,7 +126,8 @@
       public Object getChild(Object parent, int index) {
           if(parent instanceof Node) {
               Node n = (Node) parent;
  -            return getChildren(n).get(index);
  +            List children = getChildren(n);
  +            return children.get(index);
           }
           else {
               return null;
  @@ -194,6 +198,8 @@
         * @param newValue the new value from the TreeCellEditor.
         */
       public void valueForPathChanged(TreePath path, Object newValue) {
  +        // XXX What should the implementation be here?
  +        fireNodeChanged((Node) path.getLastPathComponent());
       }
   
   
  @@ -217,4 +223,90 @@
           _listeners.remove(l);
       }
   
  +    /** 
  +     * Get the list of nodes from the root to the
  +     * given node.
  +     * 
  +     * @param startNode Node to get path for.
  +     */
  +    public Node[] getPathToRoot(Node startNode) {
  +        return getPathToRoot(startNode, 0);
  +    }
  +
  +    /** 
  +     * A recursive method for generating a list of nodes defining
  +     * the path from the given node to the root.
  +     * 
  +     * @param node Node to get path for.
  +     * @param depth The number of calls taken towards the root.
  +     */
  +    private Node[] getPathToRoot(Node node, int depth) {
  +        Node[] retval = null;
  +
  +        depth++;
  +        if(node == _root || node.getParentNode() == null) {
  +            retval = new Node[depth];
  +        }
  +        else {
  +            retval = getPathToRoot(node.getParentNode(), depth);
  +        }
  +
  +        retval[retval.length - depth] = node;
  +        return retval;
  +    }
  +
  +
  +    /** 
  +     * Fire a node change event.
  +     * 
  +     * @param node Node that changed.
  +     */
  +    public void fireNodeChanged(Node node) {
  +        TreeModelEvent event = new TreeModelEvent(this, getPathToRoot(node));
  +
  +        // XXX This doen't support modifying the list during dispatch...
  +        Iterator it = _listeners.iterator();
  +        while(it.hasNext()) {
  +            TreeModelListener l = (TreeModelListener) it.next();
  +            l.treeNodesChanged(event);
  +        }
  +    }
  +
  +    /** 
  +     * Fire a node change event.
  +     * 
  +     * @param node Node that changed.
  +     */
  +    public void fireNodeAdded(Node node) {
  +        Node parent = node.getParentNode();
  +        TreeModelEvent event = null;
  +        if(parent == null) {
  +            event = new TreeModelEvent(this, getPathToRoot(node));
  +        }
  +        else {
  +            Node[] path = getPathToRoot(parent);
  +            int[] indicies = null;
  +            Node[] children = new Node[] { node };
  +
  +            // XXX Right now we assume that the node was added at the end.
  +            // This may not be the case in the future.
  +            if(parent.getLastChild() == node) {
  +                List filteredChildren = getChildren(parent);
  +                indicies = new int[] { filteredChildren.indexOf(node) };
  +            }
  +            else {
  +                throw new UnsupportedOperationException(
  +                    "Haven't implemented non-append notification yet.");
  +            }
  +
  +            event = new TreeModelEvent(this, path, indicies, children);
  +        }
  +
  +        // XXX This doen't support modifying the list during dispatch...
  +        Iterator it = _listeners.iterator();
  +        while(it.hasNext()) {
  +            TreeModelListener l = (TreeModelListener) it.next();
  +            l.treeNodesInserted(event);
  +        }
  +    }
   }