TreeModel mod = BasicTreeUI.this.tree.getModel(); TreePath start = BasicTreeUI.this.tree.getLeadSelectionPath(); Object last = null; if (start != null) last = start.getLastPathComponent();  if (last != null) {  if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_KP_DOWN) { Object next = BasicTreeUI.this.getNextVisibleNode(last); if (next != null) { TreePath newPath = new TreePath(getPathToRoot(next, 0)); BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, newPath); if (e.isControlDown()) tree.setLeadSelectionPath(newPath); else if (!mod.isLeaf(next) && e.isShiftDown()) BasicTreeUI.this.tree.expandPath(newPath); } }  else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_KP_UP) { Object prev = BasicTreeUI.this.getPreviousVisibleNode(last);  if (prev != null) { TreePath newPath = new TreePath(getPathToRoot(prev, 0)); BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, new TreePath(getPathToRoot(prev, 0))); if (e.isControlDown()) tree.setLeadSelectionPath(newPath); else if (!mod.isLeaf(prev) && e.isShiftDown()) BasicTreeUI.this.tree.expandPath(newPath); } }  else if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_KP_LEFT) { TreePath path = new TreePath(getPathToRoot(last, 0)); Object p = getParent(mod.getRoot(), last);  if (!mod.isLeaf(last) && BasicTreeUI.this.tree.isExpanded(path)) BasicTreeUI.this.tree.collapsePath(path); else if (p != null) BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, new TreePath(getPathToRoot(p, 0))); }  else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_KP_RIGHT) { TreePath path = new TreePath(getPathToRoot(last, 0));  if (!mod.isLeaf(last) && BasicTreeUI.this.tree.isCollapsed(path)) BasicTreeUI.this.tree.expandPath(path); else { Object next = BasicTreeUI.this.getNextVisibleNode(last);  if (next != null) BasicTreeUI.this.selectPath( BasicTreeUI.this.tree, new TreePath( getPathToRoot( next, 0))); } }  else if (e.getKeyCode() == KeyEvent.VK_ENTER) { TreePath path = new TreePath(getPathToRoot(last, 0)); if (!mod.isLeaf(last)) { if (BasicTreeUI.this.tree.isExpanded(path)) BasicTreeUI.this.tree.collapsePath(path); else BasicTreeUI.this.tree.expandPath(path); } } }
		public void keyPressed(KeyEvent e)		{      TreeModel mod = BasicTreeUI.this.tree.getModel();         TreePath start = BasicTreeUI.this.tree.getLeadSelectionPath();      Object last = null;         if (start != null)        last = start.getLastPathComponent();         if (last != null)         {            // DOWN, KP_DOWN            if (e.getKeyCode() == KeyEvent.VK_DOWN                  || e.getKeyCode() == KeyEvent.VK_KP_DOWN)            {              Object next = BasicTreeUI.this.getNextVisibleNode(last);               if (next != null)               {                  TreePath newPath = new TreePath(getPathToRoot(next, 0));                  BasicTreeUI.this.selectPath(BasicTreeUI.this.tree, newPath);                  if (e.isControlDown())                     tree.setLeadSelectionPath(newPath);                  else if (!mod.isLeaf(next) && e.isShiftDown())                     BasicTreeUI.this.tree.expandPath(newPath);               }            }            // UP, KP_UP            else if (e.getKeyCode() == KeyEvent.VK_UP                  || e.getKeyCode() == KeyEvent.VK_KP_UP)            {              Object prev = BasicTreeUI.this.getPreviousVisibleNode(last);                        if (prev != null)               {                  TreePath newPath = new TreePath(getPathToRoot(prev, 0));               BasicTreeUI.this.selectPath(BasicTreeUI.this.tree,                                              new TreePath(getPathToRoot(prev,                                                                         0)));                  if (e.isControlDown())                     tree.setLeadSelectionPath(newPath);                  else if (!mod.isLeaf(prev) && e.isShiftDown())                     BasicTreeUI.this.tree.expandPath(newPath);            }            }            // LEFT, KP_LEFT            else if (e.getKeyCode() == KeyEvent.VK_LEFT                  || e.getKeyCode() == KeyEvent.VK_KP_LEFT)            {              TreePath path = new TreePath(getPathToRoot(last, 0));              Object p = getParent(mod.getRoot(), last);                             if (!mod.isLeaf(last) && BasicTreeUI.this.tree.isExpanded(path))                  BasicTreeUI.this.tree.collapsePath(path);               else if (p != null)                  BasicTreeUI.this.selectPath(BasicTreeUI.this.tree,                                            new TreePath(getPathToRoot(p, 0)));            }            // RIGHT, KP_RIGHT            else if (e.getKeyCode() == KeyEvent.VK_RIGHT                  || e.getKeyCode() == KeyEvent.VK_KP_RIGHT)            {              TreePath path = new TreePath(getPathToRoot(last, 0));                 if (!mod.isLeaf(last) && BasicTreeUI.this.tree.isCollapsed(path))                  BasicTreeUI.this.tree.expandPath(path);               else               {                  Object next = BasicTreeUI.this.getNextVisibleNode(last);                  if (next != null)                    BasicTreeUI.this.selectPath(                                                BasicTreeUI.this.tree,                                                new TreePath(                                                             getPathToRoot(                                                                           next,                                                                           0)));               }            }            // Enter            else if (e.getKeyCode() == KeyEvent.VK_ENTER)            {              TreePath path = new TreePath(getPathToRoot(last, 0));              if (!mod.isLeaf(last))               {                  if (BasicTreeUI.this.tree.isExpanded(path))                     BasicTreeUI.this.tree.collapsePath(path);                  else                     BasicTreeUI.this.tree.expandPath(path);               }            }         }		}