/**
 * 
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package frame;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.FileChooserUI;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
//import javax.swing.filechooser.FileSystemView;

import filesystem.Extensions;
import filesystem.FSInfo;
import filesystem.FSUtil;

/**
 * JPanel customized for remote connections
 * and acts as the content pane and workspace
 * for visual file browsing on remote server
 * 
 * @author 
 * @version
 */

@SuppressWarnings("serial")
public class SlideBrowserFTPPanel extends JPanel {
	//Class variables
	private ArrayList<FTPNodeItem> nodeList;
	private ArrayList<FTPNodeItem> selectedNodeList;
	private int selectedNodeIndex;
	private JPopupMenu backgroundPopUp;
	private JPopupMenu nodePopUp;
	private SlideBrowser sb;
	private boolean showHitBox;
	private boolean showHiddenFiles;
	private MouseInputListener mouse;
	private Point rClickPoint;
	private Point initialClick;
	private Extensions ext;
	private FTPClient client;
	private int timeoutCounter;
	private int reply;
	private String replyString;
	private JMenuItem expandNodeMenuItem;
	private JMenuItem foldNodeMenuItem;
	private JMenuItem openMenuItem;
	private JMenuItem makeDirMenuItem;
	private JMenuItem deleteMenuItem;
	private JMenuItem copyMenuItem;
	private JMenuItem cutMenuItem;
	private JMenuItem pasteMenuItem;
	private JMenuItem renameMenuItem;
	String eol = System.getProperty("line.separator");
	
	/**
	 * Default constructor for the panel. Takes in the frame
	 * that made/added the panel in order to call methods from
	 * other frames
	 * 
	 * @param Initial file to make first root node
	 * @param frame = root frame that contains the panel
	 */
	public SlideBrowserFTPPanel(String[] details, SlideBrowser frame) {
		//Ability to call methods in frame
		//Is this already possible without the parameter?
		sb = frame;
		
		//Focus the panel
		setFocusable(true);
		
		//Set background
		setBackground(Color.GRAY);
		
		//Make FTP client
		client = new FTPClient();
		
		//Attempt connection
		boolean error = false;
		
		//ArrayList for the nodes of this panel
		nodeList = new ArrayList<FTPNodeItem>();
		
		ext = new Extensions();
		
		try 
		{
		   client.connect(details[1]);
		   replyString = client.getReplyString();

		   // After connection attempt, you should check the reply code to verify
		   // success.
		   reply = client.getReplyCode();
		      
		   client.login(details[3], details[4]);
		      
		   //Get files in parent directory
		   FTPFile[] list = client.listFiles();
		   FTPNodeItem tNode;
		   for(int i = 0; i < list.length; i++)
		   {
		     nodeList.add(new FTPNodeItem(list[i]));
		     //TODO temporary square view; use until we have a 
			 //setting variable to determine how to space new node items
			 int rowOffset = 40;
			 int columnOffset = 40;
			 int counter = 0;
			 tNode = new FTPNodeItem(list[i]);
				
			 nodeList.add(tNode);
			 int tIndex = nodeList.indexOf(tNode);
			
			 //Set hbsize and position
			 nodeList.get(tIndex).sethbSize(32);
			 nodeList.get(tIndex).setPosition(nodeList.get(tIndex).getX()+columnOffset, nodeList.get(tIndex).getY()+rowOffset);
		     if(list[i].isDirectory())
		     {
		    	 nodeList.get(tIndex).setIcon(getImage("/resources/32x32/folder.png"));
		     }
		     else
			 {
			 	 String name = nodeList.get(i).getFile().getName();
			 	 String path = ext.getExtensionIcon(name);
			 	 nodeList.get(i).setIcon(getImage(path));
			 }
		     
		     //TODO increases temporary offset
			 if(counter == 9)
			 {
				 columnOffset = 40;
				 rowOffset += 40;
				 counter = 0;
			 }
			 else
			 {
			 	 counter++;
			 	 columnOffset += 40;
				 rowOffset += 20;
			 }
		   }

		   if(!FTPReply.isPositiveCompletion(reply)) {
			 JOptionPane.showMessageDialog(sb, new JLabel(client.getRemoteAddress() + " refused connection" + eol +
					 "Server Error Code: " + reply + eol + 
				   "Server Error String: " + replyString), 
				   "Connection Refused", JOptionPane.WARNING_MESSAGE);
			 sb.removeTab();
		   }
		} 
		catch(IOException e) 
		{
		   error = true;
		   //Something happened, remove tab and
		   //disconnect
		   JOptionPane.showMessageDialog(sb, new JLabel("Server Error Code: " + reply + eol + 
				   "Server Error String: " + replyString), 
				   "Network Error", JOptionPane.WARNING_MESSAGE);
		   sb.removeTab();
		}
		
		//Add MouseListener mouseAction
		mouse = new mouseFTPAction(this);
		addMouseListener(mouse);
		addMouseMotionListener(mouse);
		
		//Make popup menu for background context menu
		backgroundPopUp = new JPopupMenu();
		
		//Add item "Remove Tab"
		JMenuItem menuRemoveTab = new JMenuItem("Remove Tab", new ImageIcon(getClass().getResource("/resources/16x16/tab_delete.png")));
		menuRemoveTab.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sb.removeTab();
			}
		});
		//Add everything to background popup menu
		backgroundPopUp.add(menuRemoveTab);
		
		//Make popup menu for node context menu
		nodePopUp = new JPopupMenu();
		
		//Add item "Expand node"
		expandNodeMenuItem = new JMenuItem("Expand node", new ImageIcon(getClass().getResource("/resources/16x16/folders_explorer.png")));
		expandNodeMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				expandNode(selectedNodeList, selectedNodeIndex);
			}
		});
		expandNodeMenuItem.setVisible(true);
		
		//Add item "Fold node"
		foldNodeMenuItem = new JMenuItem("Fold node", new ImageIcon(getClass().getResource("/resources/16x16/folders.png")));
		foldNodeMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				foldNode(selectedNodeList, selectedNodeIndex);
			}
		});
		foldNodeMenuItem.setVisible(false);
		
		//Add item "Delete"
		deleteMenuItem = new JMenuItem("Delete", new ImageIcon(getClass().getResource("/resources/16x16/delete.png")));
		deleteMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//sb.terminalMessage("Deleting File: " + selectedNodeList.get(selectedNodeIndex).getFile().getAbsolutePath());
				//FSUtil.getInstance().delete(selectedNodeList.get(selectedNodeIndex).getFile());
				//TODO: does delete files properly but does update the nodeList
				repaint();
				//openFile(selectedNodeList, selectedNodeIndex);
			}
		});
		deleteMenuItem.setVisible(false);
		
		//Add item "Cut"
		cutMenuItem = new JMenuItem("Cut", new ImageIcon(getClass().getResource("/resources/16x16/cut.png")));
		cutMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//sb.terminalMessage("Cut File: " + selectedNodeList.get(selectedNodeIndex).getFile().getAbsolutePath());
				//FSUtil.getInstance().cut(selectedNodeList.get(selectedNodeIndex).getFile());
			}
		});
		cutMenuItem.setVisible(false);
		
		//Add item "Copy"
		copyMenuItem = new JMenuItem("Copy", new ImageIcon(getClass().getResource("/resources/16x16/copying_and_distribution.png")));
		copyMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//sb.terminalMessage("Copy File: " + selectedNodeList.get(selectedNodeIndex).getFile().getAbsolutePath());
				//FSUtil.getInstance().copy(selectedNodeList.get(selectedNodeIndex).getFile());
			}
		});
		copyMenuItem.setVisible(false);
		
		//Add item "Paste"
		pasteMenuItem = new JMenuItem("Paste", new ImageIcon(getClass().getResource("/resources/16x16/paste_plain.png")));
		pasteMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//sb.terminalMessage("Paste File: " + selectedNodeList.get(selectedNodeIndex).getFile().getAbsolutePath());
				//FSUtil.getInstance().paste(selectedNodeList.get(selectedNodeIndex).getFile());
				//TODO also need to fold/expand cut directory if cut command used.
				foldNode(selectedNodeList, selectedNodeIndex);
				expandNode(selectedNodeList, selectedNodeIndex);
				repaint();
			}
		});
		pasteMenuItem.setVisible(false);
		
		//Add item "Rename"
		renameMenuItem = new JMenuItem("Rename", new ImageIcon(getClass().getResource("/resources/16x16/textfield_rename.png")));
		renameMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sb.terminalMessage("Rename File: " + selectedNodeList.get(selectedNodeIndex).getFile().getName() + 
						" @" + client.getRemoteAddress() + ":" + client.getRemotePort());
				new renameFTPWindow(selectedNodeList.get(selectedNodeIndex).getFile(), client);
				foldNode(selectedNodeList, selectedNodeIndex);
				expandNode(selectedNodeList, selectedNodeIndex);
				repaint();
			}
		});
		renameMenuItem.setVisible(false);
		
		//Add item "Make Directory"
		makeDirMenuItem = new JMenuItem("Make Directory", new ImageIcon(getClass().getResource("/resources/16x16/folder_add.png")));
		makeDirMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				makeDir(selectedNodeList, selectedNodeIndex);
			}
		});
		
		//Add item "Remove node"
		//TODO make a different icon
		JMenuItem removeNodeMenuItem = new JMenuItem("Remove node", new ImageIcon(getClass().getResource("/resources/16x16/folder_delete.png")));
		removeNodeMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				removeNode(selectedNodeList, selectedNodeIndex);
			}
		});
		
		//TODO properly implement
		//Add everything to node menu
		nodePopUp.add(expandNodeMenuItem);
		nodePopUp.add(foldNodeMenuItem);
		nodePopUp.add(removeNodeMenuItem);
		nodePopUp.addSeparator();
		nodePopUp.add(makeDirMenuItem);
		//nodePopUp.add(cutMenuItem);
		//nodePopUp.add(copyMenuItem);
		//nodePopUp.add(pasteMenuItem);
		//nodePopUp.add(renameMenuItem);
		//nodePopUp.addSeparator();
		nodePopUp.add(deleteMenuItem);
		
		//Instantiate other values:
		showHitBox = false;
		showHiddenFiles = false;
		rClickPoint = new Point();
		selectedNodeList = new ArrayList<FTPNodeItem>();
		selectedNodeIndex = 0;
		ext = new Extensions();
		ext.addExtensionList(new File((getClass().getResource("/resources/photosExt.txt")).getPath()), "Photos");
		ext.addExtensionList(new File((getClass().getResource("/resources/musicExt.txt")).getPath()), "Music");
		ext.addExtensionList(new File((getClass().getResource("/resources/videoExt.txt")).getPath()), "Video");
		ext.addExtensionList(new File((getClass().getResource("/resources/documentsExt.txt")).getPath()), "Documents");
		timeoutCounter = 0;
		
		//Add Timer
		Timer timer = new Timer(10, new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Additional timer stuff?
				//Update graphics
				repaint();
				
				//Send NoOP every 200 milliseconds to server to prevent timeout
				//Standard server timeout is 900 milliseconds?
				if(timeoutCounter == 20)
				{
					try {
						client.sendNoOp();
					} catch (IOException e1) {
						//Something happened, remove tab and
						//disconnect
						JOptionPane.showMessageDialog(sb, new JLabel("Network Error"), 
								"There has been an error with the connection. " +
								"You have been disconnected.", JOptionPane.WARNING_MESSAGE);
						sb.removeTab();
					}
					timeoutCounter = 0;
				}
				else
					timeoutCounter++;
			}
		});
		//Start timer
		timer.start();
	}
	
	/**
	 * Returns the FTPClient connection
	 * 
	 * @return FTPClient object in use by this panel
	 */
	public FTPClient getClient(){
		return client;
	}
	
	protected void makeDir(ArrayList<FTPNodeItem> selectedNodeList,
			int selectedNodeIndex) 
	{
		//TODO make it do remote directory
		FTPNodeItem parent = selectedNodeList.get(selectedNodeIndex);
		if(!parent.getFile().isDirectory())
		{
			selectedNodeList = parent.getParentList();
			selectedNodeIndex = parent.getParentIndex();
			parent = selectedNodeList.get(selectedNodeIndex);
		}
		String dirname = JOptionPane.showInputDialog(null, "New Directory", "Name of directory", JOptionPane.QUESTION_MESSAGE);
		String path = parent.getFile().getName();
		path += "\\" + dirname;
		new File(path).mkdir();
		
		//Temporary method of showing new directory via
		//refreshing directory node by folding and expanding
		foldNode(selectedNodeList, selectedNodeIndex);
		expandNode(selectedNodeList, selectedNodeIndex);
	}

	/**
	 * Custom paintComponent method to draw all components within
	 * this panel
	 * 
	 * @param Graphics component to draw
	 */
	@Override
	protected void paintComponent(Graphics g) {
		//Set Graphics2d for additional graphical painting
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
		
		//If the user does not want the hidden files shown. Paint the following way.
		//Paint Nodes and selected attributes
		for(int i = 0; i < nodeList.size(); i++)
		{
			//Draw child node lines, draw them first so that the lines are behind icons
			ArrayList<FTPNodeItem> childNodes = nodeList.get(i).getChildNodes();
			if(childNodes.size() > 0)
			{
				for(int x = 0; x < childNodes.size(); x++)
				{
					//The actual drawing of the line.
					g.drawLine(nodeList.get(i).getX()+((nodeList.get(i).gethbSize())/2),
								nodeList.get(i).getY()+((nodeList.get(i).gethbSize())/2),
									childNodes.get(x).getX()+((childNodes.get(x).gethbSize())/2),
										childNodes.get(x).getY()+((childNodes.get(x).gethbSize())/2));
					
					//Set the chileNodes line instance variable to specific coordinates
					//because g.drawLine does not return a modifiable line, it simply draws one.
					//The line object will be used to find where it intersects with a hitbox. 
					Line2D childLine = childNodes.get(x).getLine();							
					childLine.setLine((double)nodeList.get(i).getX()+((nodeList.get(i).gethbSize())/2),
							(double)nodeList.get(i).getY()+((nodeList.get(i).gethbSize())/2),
								(double)childNodes.get(x).getX()+ ((childNodes.get(x).gethbSize())/2),
									(double)childNodes.get(x).getY() + ((childNodes.get(x).gethbSize())/2)); //The HitBox's (x,y) position is referring to the top
																	   //left corner of the HitBox.
					CalculateArrowHead theArrowHead = new CalculateArrowHead(childNodes.get(x).getHitBox(), 
												childNodes.get(x).getLine(), 130, 15);
					childNodes.get(x).setArrowHead(theArrowHead.makeArrow());
					g.fillPolygon(childNodes.get(x).getArrowHead());
					g.drawPolygon(childNodes.get(x).getArrowHead());
				}
			}
		
			//Draw node image
			//TODO figure out what observer is for
			g.drawImage(nodeList.get(i).getIcon(),
					nodeList.get(i).getX(),
						nodeList.get(i).getY(), 
							nodeList.get(i).gethbSize(), 
								nodeList.get(i).gethbSize(), this);
		
			//Draw hit box
			if(showHitBox){
				g2d.draw(nodeList.get(i).getHitBox());
			}
		
			//Draw file/directory name label
			g.drawString(nodeList.get(i).getFile().getName(),
							nodeList.get(i).getX(), 
								nodeList.get(i).getY()+(nodeList.get(i).gethbSize())+12);
		}	
	}
	
	
	/**
	 * Update position of a selected Node to the new position using
	 * point given. Acts for dragging with mouse
	 * 
	 * @return Point to where Node needs to be moved to
	 */
	public void updatePosition(Point p) {
		int index = 0;
		ArrayList<FTPNodeItem> list = nodeList;
		boolean selected = false;
		for(int i = 0; i < nodeList.size(); i++)
		{
			if(nodeList.get(i).isSelected())
			{	
				index = i;
				selected = true;
				break;
			}
		}
		if(selected)
		{
			//Boundary + Collision Checker
			//TODO fix invisible wall error
		
			//TODO non-hard-coded way
			//Rectangle bounds = (Rectangle) this.getBounds().clone();
			//Rectangle bounds = new Rectangle(0, 0, 2000, 2000);
			//bounds.grow(-5, -5);
			boolean collision = false;
			//TODO fix collision detection
			//possibly use separating axis test
			/*Rectangle newHitBox = (Rectangle) nodeList.get(index).getHitBox().clone();
			Rectangle newHitBoxX = new Rectangle((int)(newHitBox.getX()+p.getX()), (int)newHitBox.getY(), nodeList.get(index).gethbSize(), nodeList.get(index).gethbSize());
			Rectangle newHitBoxY = new Rectangle(((int)newHitBox.getX()), (int)(newHitBox.getY()+p.getY()), nodeList.get(index).gethbSize(), nodeList.get(index).gethbSize());
		
			for(int i = 0; i < nodeList.size(); i++)
			{
				if(i != index)
				{
					if(nodeList.get(i).getHitBox().intersects(newHitBoxX) || nodeList.get(i).getHitBox().intersects(newHitBoxY))
					{
						collision = true;
					}
				}
			}*/
			//TODO fix bounds for hit detection
			if(!collision){
				//if(bounds.contains(nodeList.get(index).getX()+(int)p.getX(),10))
				//{
					list.get(index).setPosition((int)p.getX(), list.get(index).getY());
					//}
					//if(bounds.contains(10, nodeList.get(index).getY()+(int)p.getY()))
					//{
					list.get(index).setPosition(list.get(index).getX(), (int)p.getY());
					//}
			}
		}
		else
		{
			JScrollPane scroll = sb.getScrollPane();
			JScrollBar vScroll = scroll.getVerticalScrollBar();
			JScrollBar hScroll = scroll.getHorizontalScrollBar();
			int x = (int) (p.getX() - initialClick.getX());
			int y = (int) (p.getY() - initialClick.getY());
			vScroll.setValue(vScroll.getValue() - (int) (y / 15));
			hScroll.setValue(hScroll.getValue() - (int) (x / 15));
		}
	}
	
	/**
	 * Check if a point is within a node's hit box
	 * If true, the node is considered selected
	 * Else, the node is unselected
	 * For use to see if a node is clicked on
	 * 
	 * @param Point of where the mouse clicked
	 */
	public void checkNodeClick(Point p){
		initialClick = p;
		for(int i = 0; i < nodeList.size(); i++)
		{
			if(nodeList.get(i).getHitBox().contains(p))
			{
				nodeList.get(i).selectNode();
			}
			else
			{
				nodeList.get(i).unselectNode();
			}							
		}
	}
	
	/**
	 * Check if a node has been double clicked
	 * If true, the node is expanded
	 * 
	 * @param Point of where the mouse clicked
	 */
	public void checkNodeDoubleClick(Point p) {
		for(int i = 0; i < nodeList.size(); i++)
		{
			if(nodeList.get(i).getHitBox().contains(p))
			{
				if(nodeList.get(i).isExpanded())
				{
					foldNode(nodeList, i);
				}
				else
				{
					expandNode(nodeList, i);
				}
			}							
		}
	}
	
	/**
	 * Removes a node and its entire child tree from the workspace
	 * 
	 * @param ArrayList that contains node to be folded
	 * @param int index of the Node in the given ArrayList
	 */
	private void removeNode(ArrayList<FTPNodeItem> list, int index)
	{
		//Remove all child nodes recursively
		removeAllChildNodes(list, index);
		
		//Remove node from associated parent node if it has one
		if(list.get(index).getParentIndex() != -1)
		{
			FTPNodeItem parent = list.get(index).getParentList().get(list.get(index).getParentIndex());
			parent.removeChildNode(list.get(index));
		}
		
		//Remove the node itself last
		list.remove(index);
	}
	

	/**
	 * Removes all child nodes from the given list
	 * and recursively goes through child node list
	 * to remove the entire tree below given node
	 * 
	 * @param ArrayList that contains node to be removed
	 * @param int index of the Node in the given ArrayList
	 */
	private void removeAllChildNodes(ArrayList<FTPNodeItem> list, int index)
	{
		ArrayList<FTPNodeItem> childNodes = list.get(index).getChildNodes();
		if(childNodes.size() > 0)
		{
			for(int i = 0; i < childNodes.size(); i++)
			{
				if(childNodes.get(i).getChildNodes().size() > 0)
				{
					removeAllChildNodes(childNodes, i);
				}
				//Fold the node
				foldNode(childNodes, i);
			}
		}
		//Once all others have been folded, fold the given node
		foldNode(list, index);
	}
	
	/**
	 * Folds a node by grabbing a list of all child nodes
	 * within the Node Item's directory and removing them from the
	 * frame's list of nodes then removes the child nodes
	 * 
	 * @param ArrayList that contains node to be folded
	 * @param int index of the Node in the given ArrayList
	 */
	private void foldNode(ArrayList<FTPNodeItem> list, int index) {
		if(list.get(index).getFile().isDirectory())
		{
			//Set expand to false so that it can be expanded
			list.get(index).setExpanded(false);
			
			ArrayList<FTPNodeItem> childNodes = list.get(index).getChildNodes();
			for(int i = 0; i < childNodes.size(); i++)
			{
				if(childNodes.get(i).getChildNodes().size() > 0)
				{
					foldNode(childNodes, i);
				}
				else
				{
					nodeList.remove(childNodes.get(i));
				}
			}
			list.get(index).clearChildNodes();
		}
	}

	/**
	 * Retrieves image resource from within jar/project
	 * Just specify the path of the image resource to get
	 * 
	 * @return String fName is the path to the image resource
	 */
	private Image getImage(String fName){
		URL url = getClass().getResource(fName);
		ImageIcon icon = new ImageIcon(url);
		return icon.getImage();
	}

	/**
	 * Determines popup menu to display based on context
	 * of mouse click location
	 * 
	 * @param Component to draw on
	 * @param Point at which to display popup
	 */
	public void showPopUp(Component e, Point p){
		boolean node = false;
		for(int i = 0; i < nodeList.size(); i++)
		{
			if(nodeList.get(i).getHitBox().contains(p))
			{
				setRClickPoint(p);
				node = true;
				//deleteMenuItem.setVisible(true);
				//cutMenuItem.setVisible(true);
				//copyMenuItem.setVisible(true);
				//pasteMenuItem.setVisible(true);
				//renameMenuItem.setVisible(true);
				//pasteMenuItem.setEnabled(false);
				if(nodeList.get(i).getFile().isDirectory())
				{
					if(!FSUtil.getInstance().isClipBoardEmpty())
						pasteMenuItem.setEnabled(true);
					//openMenuItem.setVisible(false);
					if(nodeList.get(i).isExpanded())
					{
						expandNodeMenuItem.setVisible(false);
						foldNodeMenuItem.setVisible(true);
					}
					else
					{
						expandNodeMenuItem.setVisible(true);
						foldNodeMenuItem.setVisible(false);
					}
				}
				else
				{
					expandNodeMenuItem.setVisible(false);
					foldNodeMenuItem.setVisible(false);
					//openMenuItem.setVisible(true);
				}
				nodePopUp.show(e, (int)p.getX(), (int)p.getY());
				selectedNodeIndex = i;
				selectedNodeList = nodeList;
				break;
			}
		}
		if(!node)
		{
			setRClickPoint(p);
			backgroundPopUp.show(e, (int)p.getX(), (int)p.getY());
		}
	}
	
	/**
	 * Returns an array list of all nodes within the panel
	 * 
	 * @return Entire Node List within this panel
	 */
	public ArrayList<FTPNodeItem> getNodeList(){
		return nodeList;
	}
	
	/**
	 * Toggles boolean for displaying hit boxes
	 * 
	 */
	public void toggleShowHitBoxes(){
		if(showHitBox)
		{
			showHitBox = false;
		}
		else showHitBox = true;
	}

	/**
	 * Toggles boolean for displaying hidden files
	 * 
	 */
	public void toggleShowHiddenFiles() {
		if(showHiddenFiles)
		{
			showHiddenFiles = false;
		}
		else showHiddenFiles = true;
	}
	
	/**
	 * Expands a node by grabbing a list of all files/directories
	 * within a Node Item's directory and associates all new 
	 * NodeItem objects as child nodes of the expanded node as
	 * well as adds NodeItems to the total nodeList for the frame
	 * 
	 * @param ArrayList that contains node to be expanded
	 * @param int index of the Node in the given ArrayList
	 */
	private void expandNode(ArrayList<FTPNodeItem> list, int index) {
		//Only expand for directories
		if(list.get(index).getFile().isDirectory())
		{
			//Only expand once
			list.get(index).setExpanded(true);
			
			//Attempt to get list of files
			FTPFile[] fList = new FTPFile[0];
			boolean error = false;
			try {
				fList = client.listFiles(list.get(index).getFile().getName());
			}
			catch (FTPConnectionClosedException e1)
			{
				error = true;
				replyString = client.getReplyString();
				reply = client.getReplyCode();
				JOptionPane.showMessageDialog(sb, new JLabel("Connection Closed"), 
						"Your connection has been closed." + eol + 
						"Server Error Code: " + reply + eol + 
						"Server Error String: " + replyString, JOptionPane.WARNING_MESSAGE);
				sb.removeTab();
			}
			catch (IOException e) {
				error = true;
				replyString = client.getReplyString();
				reply = client.getReplyCode();
				JOptionPane.showMessageDialog(sb, new JLabel("I/O Error"), 
						"An I/O Error has occured." + eol + 
						"Server Error Code: " + reply + eol + 
						"Server Error String: " + replyString, JOptionPane.WARNING_MESSAGE);
			}
			//So long as there is no error, follow through
			if(!error)
			{
				//TODO temporary square view; use until we have a 
				//setting variable to determine how to space new node items
				int rowOffset = 40;
				int columnOffset = 40;
				int counter = 0;
			
				FTPNodeItem tNode;			
				for(int i = 0; i < fList.length; i++){
					tNode = new FTPNodeItem(fList[i]);
				
					nodeList.add(tNode);
					int tIndex = nodeList.indexOf(tNode);
			
					//Set hbsize and position
					nodeList.get(tIndex).sethbSize(32);
					nodeList.get(tIndex).setPosition(list.get(index).getX()+columnOffset, list.get(index).getY()+rowOffset);
			
					//Set icon
					if(nodeList.get(tIndex).getFile().isDirectory())
					{
						nodeList.get(tIndex).setIcon(getImage("/resources/32x32/folder.png"));
					}
					else
					{
						String name = nodeList.get(tIndex).getFile().getName();
						String path = ext.getExtensionIcon(name);
						nodeList.get(tIndex).setIcon(getImage(path));
					}
				
					//Add child node and set parent
					list.get(index).addChildNode(nodeList.get(tIndex));
					nodeList.get(tIndex).setParentNode(nodeList, index);
				
					//TODO increases temporary offset
					if(counter == 9)
					{
						columnOffset = 40;
						rowOffset += 40;
						counter = 0;
					}
					else
					{
						counter++;
						columnOffset += 40;
						rowOffset += 20;
					}
				}
			}
		}
		else
		{
			FTPFile temp = list.get(index).getFile();
			sb.terminalMessage("Double clicked: " + temp.getName());
		}
	}

	/**
	 * Sets point where a right clicked occured
	 * 
	 * @param Point where click occured
	 */
	public void setRClickPoint(Point p) {
		rClickPoint = p;
	}
}