⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.java

📁 eclipse平台的CDT项目3.0版本的源代码
💻 JAVA
字号:
//Title:        Curriculum Development Tool
//Copyright:    Copyright (c) 2001
//Author:       David Bradford - dbrad@medstat.med.utah.edu
//Company:      Knowledge Weavers - http://medstat.med.utah.edu/kw/
//File:         projects/tree/Node.java
//Description:  An abstract node for a project tree.

package cdt.projects.tree.nodes;

import java.util.*;
import javax.swing.tree.*;
import java.io.OutputStream;
import java.awt.datatransfer.*;
import java.io.*;

import cdt.projects.Project;

/**
 * Leaf in a <code>ProjectTree</code>.
 *
 * @author David Bradford<BR>
 * 		   Brad Schaefer (<A HREF="mailto:schaefer@medstat.med.utah.edu">schaefer@medstat.med.utah.edu</A>)
 * @version 2.1
 */
public abstract class Node implements MutableTreeNode, Transferable, Serializable {

	/** A DataFlavor for our nodes */
	public static final DataFlavor NODE_FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType, "Node");
	/** The transferable DataFlavors of this object */
	private static final DataFlavor[] flavors = { NODE_FLAVOR } ;

	/** name of node. */
	private String name;
	/** file of node. */
	private String file;
	/** the parent of this node */
	private Node parent = null;

	//Node methods

	/**
	 * Gets the parent of this node.
	 *
	 * @return parent node, or null if it's the root node.
	 */
	public Node getNodeParent() {
		return parent;
	}

	/**
	 * Sets the parent of this node.
	 *
	 * @param parent The parent of this node.
	 */
	protected void setParent(Node parent) {
		this.parent = parent;
	}

	/**
	 * Gets a list of the Children of this <code>Node</code>.
	 *
	 * @return List of Children.
	 */
	public LinkedList getChildren() {
		System.out.println("getChildren called in Node class");
		return null;
	}

	/**
	 * Gets a list of the Siblings of this <code>Node</code>. This function
	 * will return a list of the siblings in the order the parent has them
	 * stored as children.  The list will also include this <code>Node</code>.
	 *
	 * @return List of Siblings.  Returns null if it's the root node
	 */
	public LinkedList getSiblings() {
		if(getNodeParent() == null) {
			return null;
		}
		return getNodeParent().getChildren();
	}

	/**
	 * Gets the name of this node.
	 *
	 * @return Name of this node.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name of this node.
	 *
	 * @param name Name of this node.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the filename of this node.
	 *
	 * @return Filename of this node.
	 */
	public String getFile() {
		return file;
	}

    /**
     * Sets the filename of this node.
     *
     * @param name Filename of this node.
     */
    public void setFile(String name) {
        this.file = name;
    }

	/**
	 * Gets the relative filepath of this node.
	 *
	 * @return Relative path of this node
	 */
	public String getFilePath() {
		if(parent != null) {
			if(parent.getNodeParent() != null) {
				return parent.getFilePath() + "\\" + this.getFile();
			} else {
				return this.getFile();
			}
		}
		return "";
	}

	/**
	 * Gets the data in this node.
	 *
	 * @return Data contained in this node.
	 */
	public abstract String getData();

	/**
	 * Sets the data in this node.
	 *
	 * @param s String to change the data to.
	 */
	public abstract void setData(String s);

    /**
     * Adds a node above this item.
     *
     * @param above node to add above.
     */
    public void addNodeAbove(Node above) {
        LinkedList list = getSiblings();
        final int place = list.indexOf(this);
        if(-1 != place) {
            list.add(place, above);
            above.setParent(getNodeParent());
        }
    }

    /**
     * Adds a node below this item.
     *
     * @param below node to add below.
     */
    public void addNodeBelow(Node below) {
        LinkedList list = getSiblings();
        final int place = list.indexOf(this);
        if(-1 != place) {
            list.add(place+1, below);
            below.setParent(getNodeParent());
        }
    }

	/**
	 * Inserts a node as a child to this <code>Node</code>.
	 *
	 * @param into Node to insert.
	 */
	public abstract void addNodeInto(Node into);

    /**
     * Deletes this node.
     */
    public void removeNode() {
        LinkedList list = getSiblings();
        list.remove(this);
    }

	/**
	 * Creates a deep copy of this <code>Node</code>.
	 *
	 * @return Copy of this node.
	 */
	public abstract Node copy();

    /**
     * Returns whether a node is visible in the tree or not
     *
     * @return Visiblity of Node.
     */
    public abstract boolean isVisible();

    /**
     * Returns a linked list of the visible children
     *
     * @return LinkedList of visible children
     */
    public LinkedList getVisibleChildren() {
        LinkedList children = getChildren();
        Iterator iter = children.iterator();
        LinkedList l = new LinkedList();
        while(iter.hasNext()) {
            Node n = (Node)iter.next();
            if(n.isVisible()) {
                l.add(n);
            }
        }
        return l;
    }

    /**
	 * Writes the project file to an output stream.
	 *
	 * @param out OutputStream to write information to.
	 * @param dir Represents the directory of the node.
	 */
	public abstract void writeProject(OutputStream out, String dir);

	//TreeNode methods
	/**
	 * Gets this nodes children.
	 *
	 * @return children of this node.
	 */
	public Enumeration children() {
		return new LinkedListEnum(getVisibleChildren());
	}

	/**
	 * Gets whether this node allows children or not.
	 *
	 * @return if this node allows children.
	 */
	public abstract boolean getAllowsChildren();

	/**
	 * Gets a child under this node.
	 *
	 * @param childIndex index of child to get.
	 * @return child under node.
	 */
	public TreeNode getChildAt(int childIndex) {
		if(getAllowsChildren() && getVisibleChildren() != null) {
			try {
				return (TreeNode)getVisibleChildren().get(childIndex);
			} catch (Exception e) {
                e.printStackTrace();
				return null;
			}
		}
		return null;
	}

	/**
	 * Gets the number of children.
	 *
	 * @return number of children.
	 */
	public int getChildCount() {
		return getVisibleChildren().size();
	}

	/**
	 * gets the index of a child.
	 *
	 * @param node node to get the index of.
	 * @return index of child.
	 */
	public int getIndex(TreeNode node) {
		return getVisibleChildren().indexOf(node);
	}

	/**
	 * Gets the parent of this node.
	 *
	 * @return parent of this node.
	 */
	public TreeNode getParent() {
		return getNodeParent();
	}

	/**
	 * Gets whether this is a leaf or not.
	 *
	 * @return whether this is a leaf.
	 */
	public boolean isLeaf() {
		return !getAllowsChildren();
	}

	/**
	 * Gets the name of this node.
	 *
	 * @return name of this node.
	 */
	public String toString() {
		return name;
	}

	/**
	 * A class that is a linked List enumeration.
	 */
	private static class LinkedListEnum implements Enumeration {
		/** an iterator so we can cheat with the enumeration. */
        private final Iterator iter;

		/**
		 * Create the enumeration.
		 *
		 * @param list list to create the enumeration for.
		 */
		public LinkedListEnum(LinkedList list) {
			this.iter = list.iterator();
		}

		/**
		 * whether the enumeration has more elements.
		 *
		 * @return whether the enumeration has more elements.
		 */
		public boolean hasMoreElements() {
			return iter.hasNext();
		}

		/**
		 * Gets the next element in the enumeration.
		 *
		 * @return the next element.
		 */
		public Object nextElement() {
			return iter.next();
		}
	}

	//Transeferable functions
	/**
	 * Gets the info about this node.
	 *
	 * @param flavor to be transfered.
     * @throws UnsupportedFlavorException
	 * @return this object.
	 */
	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
		if(flavor == NODE_FLAVOR) {
			return this;
		} else {
			throw new UnsupportedFlavorException(flavor) ;
		}
	}

	/**
	 * Gets the data flavors supported by this class.
	 *
	 * @return DataFlavors that this object is able to transfer.
	 */
	public DataFlavor[] getTransferDataFlavors() {
		return this.flavors;
	}

    /**
     * Says whether a data flavor is supported.
     *
     * @param flavor The DataFlavor to test.
     * @return true if it's a supported DataFlavor, false otherwise.
     */
    public boolean isDataFlavorSupported(DataFlavor flavor) {
		return Arrays.asList(flavors).contains(flavor);
    }


	/**
	 * Called when a node is dropped into this node.
	 *
	 * @param n node being dropped into this node.
	 */
	public abstract void dropInto(Node n);

    /**
	 * Checks if a filename is already in the project or not.
	 *
	 * @param info filename to check.
	 * @return filename that can be used.
	 */
    public static String foldername(String info) {
        String newString = new String();
        for(int i=0;i<info.length() && newString.length()<=8;i++) {
            if(isAlpha(info.charAt(i))) {
                newString = newString + info.charAt(i);
            }
        }
        if(newString.charAt(0)>='0' && newString.charAt(0) <='9') {
            newString = "a"+newString;
        }

        return checkString(newString.toLowerCase(), 0, info);
    }

	/**
	 * Checks if a string is already used in the project.
	 *
	 * @param old old filename.
	 * @param tries number of filenames tried.
	 * @param name of tree node.
	 * @return filename that can be used.
	 */
    private static String checkString(String old, int tries, String name) {
        if(Project.currentProject.getTree().nodes.containsKey(old)) {
            return getNewString(old, tries+1, name);
        } else {
            Project.currentProject.getTree().nodes.put(old, name);
            return old;
        }
    }

	/**
	 * Gets a string that can be used in the project for a filename.
	 *
	 * @param old old filename.
	 * @param tries number of filenames tried.
	 * @param name name of tree node.
	 * @return filename that can be used.
	 */
    private static String getNewString(String old, int tries, String name) {
        if(Project.currentProject.getTree().nodes.containsKey(old+tries)) {
            return getNewString(old, tries+1, name);
        } else {
            Project.currentProject.getTree().nodes.put(old+tries, name);
            return old+tries;
        }
    }

	/**
	 * Checks if a character is a letter or a number.
	 *
	 * @param letter character to check.
	 * @return whether the character is a letter or a number.
	 */
    private static boolean isAlpha(char letter) {
        if(letter >='a' && letter <='z') {
            return true;
        }
        if(letter >='A' && letter <='Z') {
            return true;
        }
        if(letter >='0' && letter <='9') {
            return true;
        }
        return false;
    }


	/**
	 * Gets a flat list of nodes starting from this node.
	 *
     * @param dir The directory used to store the path information for the
     *      {@link cdt.projects.tree.nodes.Node nodes} returned.
	 * @return LinkedList of nodes.
	 */
	public static LinkedList getFlatTreeList(Node root, StringBuffer dir) {
		LinkedList children = root.getChildren();
		LinkedList ret = new LinkedList();

		if(null == children) { return ret; }
		
		Iterator iter = children.iterator();
		Node n;

		while(iter.hasNext()) {
			n = (Node)iter.next();

            if(n instanceof Wizard) { continue; }
			if(n instanceof Hold) {
				ret.addAll(n.getFlatTreeList(n, new StringBuffer(dir.toString()).append(n.getFile()).append(File.separatorChar)));
			} else {
				Node foo = n.copy();
				foo.setFile((new StringBuffer(dir.toString())).append(foo.getFile()).toString());
				ret.add(foo);
			}
		}
		
		return ret;
	}

    /**
     * Finds a child {@link cdt.projects.tree.nodes.Node Node} inside
     * the Node passed to this function.
     *
     * @param folder The folder Node to look inside for a node (or null).
     * @param childName The name of the Node to find inside of the folder.
     * @return The found node, or null if the Node cannot be found.  Also returns
     *      null if the folder passed is null.
     */
    public static Node findChildNode(Node folder, String childName) {
        Node ret = null;
        if(null != folder) {
            Iterator iter = folder.getChildren().iterator();
            while(iter.hasNext()) {
                Node n = (Node)iter.next();
                if(n.getName().equals(childName)) {
                    ret = n;
                    break;
                }
            }
        }
        return ret;
    }


    ///////////////////////////////
    // MutableTreeNode functions //
    ///////////////////////////////
    public void insert(MutableTreeNode child, int index) {
        addNodeInto((Node)child);
    }
    
    public void remove(int index) {
        ((Node)getChildAt(index)).removeNode();
    }
    
    public void remove(MutableTreeNode node) {
        ((Node)node).removeNode();
    }
    
    public void removeFromParent() {
        if(null != parent) {
            parent.remove(this);
            parent = null;
        }
    }
    
    public void setParent(MutableTreeNode newParent) {
        setParent((Node)newParent);
    }
    
    public void setUserObject(Object object) {
        if(object instanceof String) {
            if(!object.equals("")) {
                if(getNodeParent() == null && this instanceof Hold && null != Project.currentProject) {
                    Project.currentProject.rename((String)object);
                }
                setName((String)object);
            }
        } else {
            // ???
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -