git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268442 13f79535-47bb-0310-9956-ffa450edef68master
@@ -66,124 +66,6 @@ import java.util.*; | |||
* @version $Revision$ | |||
* @author Simeon Fitch | |||
*/ | |||
public abstract class ACSTreeNodeElement extends ACSElement | |||
implements TreeNode { | |||
public abstract class ACSTreeNodeElement extends ACSElement { | |||
/** Cache of TreeNode only children. */ | |||
private List _treeNodeCache = null; | |||
/** | |||
* Get the cache of TreeNode only children. | |||
* | |||
* @return List of TreeNodes that are children. | |||
*/ | |||
private List getCache() { | |||
if(_treeNodeCache == null) { | |||
_treeNodeCache = new ArrayList(); | |||
// XXX this crazy casting is to get around an | |||
// inconsistency between jikes and javac whereby | |||
// the call without this cast when compiled with | |||
// jikes causes an IllegalAccessException | |||
// because the implementation of getLength() and | |||
// item() are actually in a package only class | |||
// in the Sun implementation classes. | |||
int len = ((NodeList)this).getLength(); | |||
for(int i = 0; i < len; i++) { | |||
Object n = ((NodeList)this).item(i); | |||
if(n instanceof TreeNode) { | |||
_treeNodeCache.add(n); | |||
} | |||
} | |||
} | |||
return _treeNodeCache; | |||
} | |||
/** | |||
* Returns the child <code>TreeNode</code> at index | |||
* <code>childIndex</code>. | |||
*/ | |||
public TreeNode getChildAt(int childIndex) { | |||
List nodes = getCache(); | |||
return (TreeNode) nodes.get(childIndex); | |||
} | |||
/** | |||
* Returns the number of children <code>TreeNode</code>s the receiver | |||
* contains. | |||
*/ | |||
public int getChildCount() { | |||
List nodes = getCache(); | |||
return nodes.size(); | |||
} | |||
/** | |||
* Returns the parent <code>TreeNode</code> of the receiver. | |||
*/ | |||
public TreeNode getParent() { | |||
// XXX this barfs becase a different "getParent()" is in Node | |||
// interface. Need to fix... | |||
return (TreeNode) getParent(); | |||
} | |||
/** | |||
* Returns the index of <code>node</code> in the receivers children. | |||
* If the receiver does not contain <code>node</code>, -1 will be | |||
* returned. | |||
*/ | |||
public int getIndex(TreeNode node) { | |||
List nodes = getCache(); | |||
return nodes.indexOf(node); | |||
} | |||
/** | |||
* Returns true if the receiver allows children. | |||
*/ | |||
public boolean getAllowsChildren() { | |||
return true; | |||
} | |||
/** | |||
* Returns true if the receiver is a leaf. | |||
*/ | |||
public boolean isLeaf() { | |||
List nodes = getCache(); | |||
return nodes.size() <= 0; | |||
} | |||
/** | |||
* Returns the children of the reciever as an Enumeration. | |||
*/ | |||
public Enumeration children() { | |||
return new NodeEnum(); | |||
} | |||
/** Internal iterator for the child nodes. */ | |||
private class NodeEnum implements Enumeration { | |||
/** Current child index. */ | |||
private int _index = 0; | |||
/** | |||
* Determine if there are more elements to visit. | |||
* | |||
* @return True if nextElement() can be called, false otherwise. | |||
*/ | |||
public boolean hasMoreElements() { | |||
List nodes = getCache(); | |||
return _index < nodes.size(); | |||
} | |||
/** | |||
* Get the next element. hasMoreElements() must currently return true. | |||
* | |||
* @return Next element | |||
*/ | |||
public Object nextElement() { | |||
List nodes = getCache(); | |||
return nodes.get(_index++); | |||
} | |||
} | |||
} |
@@ -54,9 +54,13 @@ | |||
package org.apache.tools.ant.gui.acs; | |||
import javax.swing.tree.DefaultTreeModel; | |||
import javax.swing.tree.*; | |||
import javax.swing.event.TreeModelListener; | |||
import javax.swing.event.TreeModelEvent; | |||
import org.w3c.dom.Node; | |||
import org.w3c.dom.NodeList; | |||
import org.apache.tools.ant.gui.acs.ACSProjectElement; | |||
import java.util.*; | |||
/** | |||
* Provides a tree model view of the Project class. XXX This | |||
@@ -64,8 +68,153 @@ import org.apache.tools.ant.gui.acs.ACSProjectElement; | |||
* | |||
* @version $Revision$ | |||
* @author Simeon H.K. Fitch */ | |||
public class ElementTreeModel extends DefaultTreeModel { | |||
public class ElementTreeModel implements TreeModel { | |||
/** Root of the tree. */ | |||
private ACSProjectElement _root = null; | |||
/** List of listeners. */ | |||
private List _listeners = new ArrayList(); | |||
public ElementTreeModel(ACSProjectElement root) { | |||
super(root); | |||
_root = root; | |||
} | |||
/** | |||
* Returns the root of the tree. Returns null only if the tree has | |||
* no nodes. | |||
* | |||
* @return the root of the tree | |||
*/ | |||
public Object getRoot() { | |||
return _root; | |||
} | |||
/** | |||
* Gets the set of children that this tree model is interested in. | |||
* | |||
* @param parent Parent to extract children from. | |||
*/ | |||
private List getChildren(Node parent) { | |||
NodeList children = parent.getChildNodes(); | |||
int length = children.getLength(); | |||
List retval = new ArrayList(length); | |||
for(int i = 0; i < length; i++) { | |||
// XXX This is where we will eventually add dynamic filtering | |||
// capabilities. | |||
Node n = children.item(i); | |||
if(n instanceof ACSTreeNodeElement) { | |||
retval.add(n); | |||
} | |||
} | |||
return retval; | |||
} | |||
/** | |||
* Returns the child of <I>parent</I> at index <I>index</I> in the parent's | |||
* child array. <I>parent</I> must be a node previously obtained from | |||
* this data source. This should not return null if <i>index</i> | |||
* is a valid index for <i>parent</i> (that is <i>index</i> >= 0 && | |||
* <i>index</i> < getChildCount(<i>parent</i>)). | |||
* | |||
* @param parent a node in the tree, obtained from this data source | |||
* @return the child of <I>parent</I> at index <I>index</I> | |||
*/ | |||
public Object getChild(Object parent, int index) { | |||
if(parent instanceof Node) { | |||
Node n = (Node) parent; | |||
return getChildren(n).get(index); | |||
} | |||
else { | |||
return null; | |||
} | |||
} | |||
/** | |||
* Returns the number of children of <I>parent</I>. Returns 0 if the node | |||
* is a leaf or if it has no children. <I>parent</I> must be a node | |||
* previously obtained from this data source. | |||
* | |||
* @param parent a node in the tree, obtained from this data source | |||
* @return the number of children of the node <I>parent</I> | |||
*/ | |||
public int getChildCount(Object parent) { | |||
if(parent instanceof Node) { | |||
Node n = (Node) parent; | |||
return getChildren(n).size(); | |||
} | |||
else { | |||
return 0; | |||
} | |||
} | |||
/** | |||
* Returns true if <I>node</I> is a leaf. It is possible for this method | |||
* to return false even if <I>node</I> has no children. A directory in a | |||
* filesystem, for example, may contain no files; the node representing | |||
* the directory is not a leaf, but it also has no children. | |||
* | |||
* @param node a node in the tree, obtained from this data source | |||
* @return true if <I>node</I> is a leaf | |||
*/ | |||
public boolean isLeaf(Object node) { | |||
if(node instanceof Node) { | |||
Node n = (Node) node; | |||
return getChildren(n).size() == 0; | |||
} | |||
else { | |||
return true; | |||
} | |||
} | |||
/** | |||
* Returns the index of child in parent. | |||
*/ | |||
public int getIndexOfChild(Object parent, Object child) { | |||
if(parent instanceof Node && child instanceof Node) { | |||
Node n = (Node) parent; | |||
List children = getChildren(n); | |||
int count = children.size(); | |||
for(int i = 0; i < count; i++) { | |||
if(children.get(i) == child) return i; | |||
} | |||
} | |||
return -1; | |||
} | |||
/** | |||
* Messaged when the user has altered the value for the item identified | |||
* by <I>path</I> to <I>newValue</I>. If <I>newValue</I> signifies | |||
* a truly new value the model should post a treeNodesChanged | |||
* event. | |||
* | |||
* @param path path to the node that the user has altered. | |||
* @param newValue the new value from the TreeCellEditor. | |||
*/ | |||
public void valueForPathChanged(TreePath path, Object newValue) { | |||
} | |||
/** | |||
* Adds a listener for the TreeModelEvent posted after the tree changes. | |||
* | |||
* @see #removeTreeModelListener | |||
* @param l the listener to add | |||
*/ | |||
public void addTreeModelListener(TreeModelListener l) { | |||
_listeners.add(l); | |||
} | |||
/** | |||
* Removes a listener previously added with <B>addTreeModelListener()</B>. | |||
* | |||
* @see #addTreeModelListener | |||
* @param l the listener to remove | |||
*/ | |||
public void removeTreeModelListener(TreeModelListener l) { | |||
_listeners.remove(l); | |||
} | |||
} |
@@ -76,12 +76,10 @@ public class NewProjectCmd extends AbstractCommand { | |||
super(context); | |||
} | |||
/** | |||
* Display a dialog asking the user to select a file to open. | |||
* If one is selected then an event is posted requesting the open | |||
* operation be completed. | |||
* | |||
*/ | |||
/** | |||
* Create a new project and make it active. | |||
* | |||
*/ | |||
public void run() { | |||
ACSProjectElement project = | |||
getContext().getProjectManager().createNew(); | |||
@@ -0,0 +1,95 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.gui.command; | |||
import org.apache.tools.ant.gui.core.AppContext; | |||
import org.apache.tools.ant.gui.event.TargetSelectionEvent; | |||
import org.apache.tools.ant.gui.acs.*; | |||
/** | |||
* Command for creating a new target. | |||
* | |||
* @version $Revision$ | |||
* @author Simeon Fitch | |||
*/ | |||
public class NewTargetCmd extends AbstractCommand { | |||
/** New project count for this session. Used to create default names, | |||
* numbered as a convenience. */ | |||
private static int _count = 1; | |||
/** | |||
* Standard ctor. | |||
* | |||
* @param context Application context. | |||
*/ | |||
public NewTargetCmd(AppContext context) { | |||
super(context); | |||
} | |||
/** | |||
* Create a new target and make it active. | |||
* | |||
*/ | |||
public void run() { | |||
ACSProjectElement project = getContext().getSelectionManager(). | |||
getSelectedProject(); | |||
ACSTargetElement retval = | |||
ACSFactory.getInstance().createTarget(project); | |||
retval.setName(getContext().getResources(). | |||
getString(getClass(), "defName") + " " + _count++); | |||
getContext().getEventBus().postEvent( | |||
new TargetSelectionEvent( | |||
getContext(), new ACSElement[] { retval })); | |||
} | |||
} |
@@ -82,7 +82,8 @@ public class ElementTreeCellRenderer extends DefaultTreeCellRenderer { | |||
try { | |||
BeanInfo info = Introspector.getBeanInfo(value.getClass()); | |||
Image icon = info.getIcon(BeanInfo.ICON_COLOR_16x16); | |||
setIcon(new ImageIcon(icon)); | |||
setIcon(icon == null ? null : new ImageIcon(icon)); | |||
if(value instanceof ACSElement) { | |||
setText(((ACSElement)value).getDisplayName()); | |||
} | |||
@@ -111,6 +111,7 @@ newTarget.shortDescription=Create a new target | |||
newTarget.icon=new-target.gif | |||
newTarget.enabled=false | |||
newTarget.separator=true | |||
newTarget.command=org.apache.tools.ant.gui.command.NewTargetCmd | |||
newTarget.enableOn=\ | |||
org.apache.tools.ant.gui.event.ProjectSelectedEvent, \ | |||
org.apache.tools.ant.gui.event.NewProjectEvent | |||
@@ -118,6 +119,7 @@ newTarget.disableOn=\ | |||
org.apache.tools.ant.gui.event.TargetSelectionEvent, \ | |||
org.apache.tools.ant.gui.event.TaskSelectionEvent, \ | |||
org.apache.tools.ant.gui.event.PropertySelectionEvent, \ | |||
org.apache.tools.ant.gui.event.ProjectClosedEvent, \ | |||
org.apache.tools.ant.gui.event.NullSelectionEvent | |||
newTask.name=New Task | |||
@@ -143,4 +145,5 @@ newProperty.enableOn=\ | |||
org.apache.tools.ant.gui.event.TaskSelectionEvent | |||
newProperty.disableOn=\ | |||
org.apache.tools.ant.gui.event.PropertySelectionEvent, \ | |||
org.apache.tools.ant.gui.event.ProjectClosedEvent, \ | |||
org.apache.tools.ant.gui.event.NullSelectionEvent |
@@ -93,6 +93,7 @@ org.apache.tools.ant.gui.acs.ACSTaskElementBeanInfo.icon=task.gif | |||
org.apache.tools.ant.gui.command.NewProjectCmd.defName=New Project | |||
org.apache.tools.ant.gui.command.NewTargetCmd.defName=New Target | |||
#---------------------------------------------------------------------------- | |||
# About Description (NB: this is only a temporary approach). | |||