CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2009
    Posts
    2

    HELP PLEASE - Trees and JTree

    I have to use a tree structure to store bookmarks and history in the java tabbed web browser i must create :blink: This is what i have done so far

    Code:
    /**This class is the basis of the tree directory which will be used
     * to represent bookmarks and the browser history.
     * 
     * BrowserMutableTreeNode implements MutableTreeNode, therefore inheriting
     * its methods and those of TreeNode, since MutableTreeNode extends TreeNode.
     * 
     * 	@author CL WILLIAMS 15173534
     */
    
    package cs214.trees;
    
    import javax.swing.tree.MutableTreeNode;
    import javax.swing.tree.TreeNode;
    import java.util.*;
    
    public class BrowserMutableTreeNode implements MutableTreeNode {
    	
    	/** This is a user defined object of any type.*/
    	
    	private Object userObject;
    	
    	/** This determines whether the node can have children or not.*/
    	
    	private boolean allowsChildren = true;
    	
    	/**This is the parent node.*/
    	
    	protected MutableTreeNode parent;
    	
    	/**The children are kept in an ArrayList of MutableTreeNodes.*/
    	
    	protected List <MutableTreeNode> children = new ArrayList<MutableTreeNode>();
    	
    	public BrowserMutableTreeNode(){
    		BrowserMutableTreeNode rootNode = new BrowserMutableTreeNode();
    	}
    	
    	/**This constructor creates a BrowserMutableTreeNode which receives
    	 * an object as parameter.
    	 * 
    	 * @param o
    	 * 		The user defined object to be stored in the node.
    	 */
    	
    	public BrowserMutableTreeNode (Object o){
    		//BrowserMutableTreeNode node = new BrowserMutableTreeNode(o);
    		this.userObject = o;
    	//makeNodes(rootNode);
    	}
    	
    	/** This constructor creates a BrowserMutableTreeNode which receives an object 
    	 * boolean as parameters.
    	 * 
    	 * @param o
    	 * 		The user defined object to be stored in the node.
    	 * 
    	 * @param allowsChildren
    	 * 		A boolean which returns true if the node is allowed to have
    	 * 		children.
    	 */		
    	
    	public BrowserMutableTreeNode (Object o, boolean allowsChildren){
    	//BrowserMutableTreeNode node = new BrowserMutableTreeNode(o,allowsChildren);
    	this.allowsChildren = allowsChildren;
    	}
    
    	/** This method inserts a new child into the ArrayList at the given index.
    	 * 
    	 * @param newChild
    	 * 		The new node which will be inserted.
    	 * 
    	 * @param index
    	 * 		The given index.
    	 * 
    	 */
    	public void insert (MutableTreeNode newChild, int index) throws
    		ArrayIndexOutOfBoundsException {
    	   
    		if (children == null){
    		   children = new ArrayList<MutableTreeNode>();
    	   }
    	
    		if(index < 0 || index > children.size()){
    			throw new ArrayIndexOutOfBoundsException("You cannot access" +
    					"that index.");
    			
    		}
    		children.add(index,newChild);
    		newChild.setParent(this);
    	//newChild = new BrowserMutableTreeNode(o,true);   
    	   
       }
    
    	/** This method removes a node from the tree.
    	 * 
    	 */
    	
    	public void remove (MutableTreeNode node){
    		
    		if(children.remove(node)){
    			node.setParent(null);
    		}
    	}
    	
    	/**
    	 * 
    	 * Removes the node at the given index.
    	 * 
    	 * @param index
    	 * 		The given index
    	 * 
    	 * @throws ArrayIndexOutOfBoundsException
    	 * 		This occurs when an illegal index is accessed.		
    	 */
    	
    	public void remove (int index) throws ArrayIndexOutOfBoundsException{
    		
    		if (index < 0 || index > children.size()){
    			throw new ArrayIndexOutOfBoundsException("You cannot access that index.");
    		}
    		MutableTreeNode child  = children.remove(index);
    		child.setParent(null);
    		
    	}
    	
    	
    	/** 
    	 * 
    	 * This method sets the user defined object as the object of this node.
    	 * 
    	 * @param o
    	 * 		The user defined object.
    	 * 
    	 */
    	
    	public void setUserObject(Object o){
    		this.userObject = o;
    	}
    	
    	/**
    	 * 
    	 *  This method removes the current child from its parent.
    	 * 
    	 */
    	public void removeFromParent(){
    		
    		if (parent == null){
    			return;
    		}
    		parent.remove(this);
    		
    	}
    	
    	/** This method sets the parent node of a child.
    	 * 
    	 * @param newParent
    	 * 		The node which will be the parent.
    	 */
    	
    	public void setParent(MutableTreeNode newParent){
    		
    		this.parent = newParent;
    		
    	}
    	
    	/** This method gets the index of a given node.
    	 * 
    	 * @param node
    	 * 		The given node
    	 * 
    	 * @return children.indexOf(node)
    	 * 		The index of the given node
    	 */
    	
    	public int getIndex(TreeNode node){
    		 
    		return children.indexOf(node);
    	    	
    	  }
    
    	/** This method checks if the current node is allowed to have children.
    	 * 
    	 * @return allowsChildren
    	 * 		Returns true if the node is allowed to have children.
    	 */
    	
        public boolean getAllowsChildren(){
        	
        	return allowsChildren;
        }
        
        /** This method returns the child at the given index.
         * 
         * @param childIndex
         * 		The index of the child
         * 
         * @return
         * 		The child at the given index
         * 
         * @throws ArrayIndexOutOfBoundsException
         * 		This occurs when an illegal index is accessed.
         * 		
         *
         */
        public TreeNode getChildAt(int childIndex) throws ArrayIndexOutOfBoundsException{
        	
        	if (childIndex < 0 || childIndex > children.size()) {
        		throw new ArrayIndexOutOfBoundsException("You cannot access that index.");
        	}
        	return children.get(childIndex);
        }
        
        /** This method has no parameters. It simply returns the parent of the current
         * node.
         * 
         * @return parent
         * 		The parent of the node which executed this method call.
         */
        
        public TreeNode getParent(){
        	
        	return parent;
        	
        }
        
        /** This method returns the number of children currently in the array.
         * 
         * @return children.size()
         * 		The size of the ArrayList, i.e. the number of children.
         */
        
        public int getChildCount(){
        	
        	if (children == null){
        		return 0;
        	}
        	
        	return children.size();
        }
        
        /** This method returns true if the child is a leaf node.
         * 
         * @return children.isEmpty();
         * 		Returns true if the child is not null and is a leaf node.
         * 
         */
        
        public boolean isLeaf(){
        	
        	return children.isEmpty();
        	
        }
        
        /**
         * 
         *  This Enumeration class returns an enumeration over the children of the
         * 	ArrayList.
         * 
         * @throws NoSuchElementException
         * 		This occurs when there are no more elements in the tree.
         * @return e
         * 		The enumeration over the list.
         * 
         */
        
        public Enumeration<MutableTreeNode> children() throws NoSuchElementException {
    		/*Vector<MutableTreeNode> temp = new Vector<MutableTreeNode>();
    		for(Enumeration<MutableTreeNode> e = temp.elements();e.hasMoreElements();)
    		return temp;*/
        	Enumeration<MutableTreeNode> e = Collections.enumeration(children);
        	while(e.hasMoreElements()){
        		e.nextElement();
        	}
        	if(e.nextElement() == null){
        		throw new NoSuchElementException("There are no more elements" +
        				"to add to the enumeration.");
        	}
        	return e;
    	}
        
        
        @Override
        
        /**This overrides the toString() method.
         * 
         * @return children().toString()
         * 		The string representation of the Enumeration over the ArrayList.
         *
         */
        
        public String toString(){
        	return children().toString();
        }
    	    
    }
    ive attached the question in a pdf file. Im supposed to test if this works by creating a gui. For the bookmark GUI i have:

    Code:
    package cs214.tests;
    
    import javax.swing.JTree;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.UIManager;
    import javax.swing.tree.MutableTreeNode;
    import javax.swing.JEditorPane;
    import javax.swing.UIManager;
    import javax.swing.JTextField;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeSelectionEvent;
    
    import java.net.URL;
    
    import java.awt.event.*;
    
    import cs214.trees.BrowserMutableTreeNode;
    
    public class Bookmark implements TreeSelectionListener{
    	
    	private JTree bookmarkTree;
    	//private URL bookmarkURL;
    	private BrowserMutableTreeNode newNode;
    	private MutableTreeNode node;
    	//private String bookmarkName;
    	private JTextField currentSelection;
    	
    	private class BookmarkInfo {
    		
    		public String bookmarkTitle;
    		public String description;
    		public URL bookmarkURL;
    		public String urlName;
    		
    		public BookmarkInfo(String title, String desc, String url){
    			bookmarkTitle = title;
    			description = desc;
    			urlName = url;
    			
    			if (bookmarkURL == null) {
    				System.err.println("You cannot have a null url.");
    				
    			}
    			
    			
    		}
    		
    		public String toString(){
    			return bookmarkTitle;
    		}
    		
    	}
    	
    	public void makeNewNodes(BrowserMutableTreeNode top) {
    		BrowserMutableTreeNode topNode = null;
    		BrowserMutableTreeNode bookmark = null;
    		
    		topNode = new BrowserMutableTreeNode("Bookmarks");
    		top.insert(topNode, 0);
    		
    		bookmark = new BrowserMutableTreeNode(new BookmarkInfo("Google","The Google Search Engine","www.google.com"));
    		topNode.insert(bookmark,0);
    		
    		
    		
    	}
    	
    	public void valueChanged(TreeSelectionEvent event) {
    	    MutableTreeNode node = (MutableTreeNode)bookmarkTree.getLastSelectedPathComponent();
    		
    		currentSelection.setText
    	      ("Current Selection: " +
    	       bookmarkTree.getLastSelectedPathComponent().toString());
    	  }
    
    	
    	private static void createAndShowGUI() {
            /*if (useSystemLookAndFeel) {
                try {
                    UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    System.err.println("Couldn't use system look and feel.");
                }
            }*/
    
            //Create and set up the window.
            JFrame frame = new JFrame("TreeDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            //Add content to the window.
            bookmarkTree = new JTree(newNode);
            bookmarkTree.addTreeSelectionListener(this);
            frame.add(Bookmark);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
        	  
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
        	
        	new Bookmark();
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    		
    		//BrowserMutableTreeNode topNode = new BrowserMutableTreeNode("Bookmarks");
    		
    		
    		
    		
    	}
    for the folder GUI i have:

    Code:
    /** This is the test file to create a folder tree.
     * 
     * @author CL WILLIAMS 15173534
     * 
     */
    
    package cs214.tests;
    
    import javax.swing.JRootPane;
    import javax.swing.JTree;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.tree.MutableTreeNode;
    
    import java.awt.event.*;
    
    import cs214.trees.BrowserMutableTreeNode;
    
    public class Folder {
    	
    	private  static JTree folderTree;
    	String folderName;
    	private BrowserMutableTreeNode newNode;
    	private MutableTreeNode node;
    	
    	public static void main(String[] args){
    		
    		BrowserMutableTreeNode topFolder = new BrowserMutableTreeNode("Root Folder",true);
    		BrowserMutableTreeNode topFolderDescription = new BrowserMutableTreeNode("This is the root folder.");
    		BrowserMutableTreeNode Folder1 = new BrowserMutableTreeNode("Folder 1",true);
    		BrowserMutableTreeNode Folder1Description = new BrowserMutableTreeNode("This is the 1st folder in the root folder.");
    		BrowserMutableTreeNode folder2 = new BrowserMutableTreeNode("Folder 2",true);
    		BrowserMutableTreeNode folder2Description = new BrowserMutableTreeNode("This is the 2nd folder.");
    		BrowserMutableTreeNode folder3 = new BrowserMutableTreeNode("Folder 3",true);
    		BrowserMutableTreeNode folder3Description = new BrowserMutableTreeNode("1st Folder in Folder 2");
    		
    	//	folderTree = new JTree("Root Folder");
    		JFrame frame = new JFrame("Bookmark JTree");
    		JPanel panel = new JPanel();
    		
    		folderTree = new JTree(topFolder);
    		topFolder.insert(topFolderDescription,0);
    		topFolder.insert(Folder1,1);
    		Folder1.insert(Folder1Description, 2);
    		
    		
    		panel.add(folderTree);
    		//panel.add(button);
    		frame.add(panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setUndecorated(true);
    		frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    		frame.setSize(200,300);
    		frame.setVisible(true);
    		
    		
    		
    	}
    }
    So all i need to know is if this tree structure actually works. Please can anyone help asap? thank you

    i also have to extend the structure so that new bookmarks etc can be added and saved to disk izzy:
    Attached Images Attached Images
    Last edited by tecna_enchantix; April 18th, 2009 at 10:17 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured