package eg.ui.filetree;

import java.awt.event.ActionListener;

import java.awt.Color;
import java.awt.Component;
import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.UIManager;

import javax.swing.border.LineBorder;

import javax.swing.filechooser.FileSystemView;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import java.io.File;

//--Eadgyth--/
import eg.Constants;
import eg.ui.IconFiles;
import eg.utils.UIComponents;

/**
 * Defines the panel which contains another JPanel which a JTree for
 * a file system can be added to and a toolbar.
 */
public class TreePanel {

   private final JPanel content         = new JPanel(new BorderLayout());
   private final JPanel holdTreePnl = new JPanel(new BorderLayout());
   private final JScrollPane scroll = new JScrollPane(
         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

   private final JButton upBt = new JButton(UIManager.getIcon(
         "FileChooser.upFolderIcon"));

   private final JButton renewBt = new JButton(IconFiles.REFRESH_ICON);
   private final JButton closeBt = new JButton(IconFiles.CLOSE_ICON);

   public TreePanel() {
      initTreePanel();
   }

   /**
    * Gets this JPanel which holds the tree and the toolbar
    *
    * @return  the JPanel
    */
   public JPanel content() {
      return content;
   }

   /**
    * Adds the specified <code>JTree</code>
    *
    * @param tree  the JTree
    */
   public void setTree(JTree tree) {
      holdTreePnl.add(tree);
      tree.setRootVisible(true);
      //tree.setFont(Constants.VERDANA_PLAIN_8);
      tree.setBorder(new LineBorder(Color.WHITE, 5));
      tree.setCellRenderer(new TreeRenderer());
      tree.setToggleClickCount(0);
      renewBt.setEnabled(true);
   }

   /**
    * Sets the listener for folder up actions
    *
    * @param al  the <code>ActionListener</code>
    */
   public void setFolderUpAction(ActionListener al) {
      upBt.addActionListener(al);
   }

   /**
    * Sets the listener for actions to renew the tree
    *
    * @param al  the <code>ActionListener</code>
    */
   public void setRenewTreeAction(ActionListener al) {
      renewBt.addActionListener(al);
   }

   /**
    * Sets the listener for actions to close the file view panel
    *
    * @param al  the <code>ActionListener</code>
    */
   public void setCloseAct(ActionListener al) {
      closeBt.addActionListener(al);
   }

   /**
    * Sets the boolean that specifies if the button for folder
    * up actions is enabled (true) or disabled
    *
    * @param b  the boolen value
    */
   public void enableFolderUpAct(boolean b) {
      upBt.setEnabled(b);
   }

   //
   //--private--/
   //

   private void initTreePanel() {
      scroll.setBorder(Constants.MATTE_TOP);
      scroll.setViewportView(holdTreePnl);
      scroll.getVerticalScrollBar().setUnitIncrement(10);
      content.add(toolbar(), BorderLayout.NORTH);
      content.add(scroll, BorderLayout.CENTER);
      content.setBorder(Constants.GRAY_BORDER);
      renewBt.setEnabled(false);
      enableFolderUpAct(false);
   }

   private JToolBar toolbar() {
      JButton[] bts = new JButton[] {
         upBt, renewBt, closeBt
      };
      String[] tooltips = new String[] {
         "Folder up",
         "Update tree",
         "Close the project explorer",
      };
      return UIComponents.lastBtRightToolbar(bts, tooltips);
   }

   private class TreeRenderer extends DefaultTreeCellRenderer {

      private final FileSystemView fsv = FileSystemView.getFileSystemView();

      @Override
      public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean sel, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
              row, hasFocus);

        if (value instanceof DefaultMutableTreeNode) {
           value = ((DefaultMutableTreeNode) value).getUserObject();
           if (value instanceof File) {
              File f = (File) value;
              setIcon(fsv.getSystemIcon(f));
              setText(f.getName());
           }
        }
        return this;
      }
   }
}

@@ -22,47 +22,8 @@
 e;%0A%0A
-import java.awt.event.ActionListener;%0A%0A
 impo
@@ -99,16 +99,55 @@
 ayout;%0A%0A
+import java.awt.event.ActionListener;%0A%0A
 import j
@@ -494,16 +494,60 @@
 enderer;
+%0Aimport javax.swing.tree.TreeSelectionModel;
 %0A%0Aimport
@@ -698,87 +698,49 @@
 ns a
-nother JPanel which a JTree for%0A * a file system can be added to and a toolbar.
+ tool bar and panel for adding%0A * a JTree
 %0A */
@@ -798,24 +798,16 @@
 content 
-        
 = new JP
@@ -864,19 +864,16 @@
 holdTree
-Pnl
  = new J
@@ -1340,25 +1340,16 @@
     init
-TreePanel
 ();%0A   %7D
@@ -1390,38 +1390,68 @@
 ich 
-holds the tre
+contains the toolbar and th
 e 
+p
 an
-d the toolbar
+el for%0A    * adding the tree
 %0A   
@@ -1677,37 +1677,8 @@
 ) %7B%0A
-      holdTreePnl.add(tree);%0A
     
@@ -1710,57 +1710,8 @@
 e);%0A
-      //tree.setFont(Constants.VERDANA_PLAIN_8);%0A
     
@@ -1839,24 +1839,161 @@
 ckCount(0);%0A
+      tree.getSelectionModel().setSelectionMode(%0A            TreeSelectionModel.SINGLE_TREE_SELECTION);%0A%0A      tree.setFocusable(false);%0A
       renewB
@@ -2008,24 +2008,50 @@
 bled(true);%0A
+      holdTree.add(tree);%0A
    %7D%0A%0A   /**
@@ -2716,42 +2716,27 @@
   * 
-Sets the boolean that specifies if
+Enables or disables
  the
@@ -2753,22 +2753,16 @@
 r folder
-%0A    *
  up acti
@@ -2768,76 +2768,62 @@
 ions
- is enabled (true) or disabled%0A    *%0A    * @param b  the boolen valu
+%0A    *%0A    * @param b  true to enable, false to disabl
 e%0A  
@@ -2956,25 +2956,16 @@
 oid init
-TreePanel
 () %7B%0A   
@@ -3047,11 +3047,8 @@
 Tree
-Pnl
 );%0A 
@@ -3597,24 +3597,13 @@
 nts.
-lastBtRightToolb
+toolB
 ar(b

make only one node in file tree selecable and make file tree unfocusable