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

📄 dragtree.java

📁 emboss的linux版本的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**  *  * Adding a file (or directory) to the file tree manager.  * This looks to see if the directory has already been opened  * and updates the filetree if it has.  * @param child        new child to add in  * @param path         path to where child is to be added  * @param node         node to add child to  *  */  public DefaultMutableTreeNode addObject(String child,                            String path, FileNode node)  {    DefaultTreeModel model = (DefaultTreeModel)getModel();    if(node == null)    {      node = getNode(path);      if(node==null)        return null;    }    FileNode parentNode = getNode(path);    File newleaf = new File(path + fs + child);    FileNode childNode = null;    if(parentNode.isExplored())    {      childNode = new FileNode(newleaf);      int index = getAnIndex(parentNode,child);      if(index > -1)        model.insertNodeInto(childNode, parentNode, index);    }    else if(parentNode.isDirectory())    {      exploreNode(parentNode);      childNode = getNode(path + fs + child);    }    // Make sure the user can see the new node.    this.scrollPathToVisible(new TreePath(childNode.getPath()));    return childNode;  }  /**  *  * Delete a node from the JTree  * @param node         node for deletion  *  */  public void deleteObject(FileNode node)  {    DefaultTreeModel model =(DefaultTreeModel)getModel();    FileNode parentNode = getNode(node.getFile().getParent());    model.removeNodeFromParent(node);  }  /**  *  * Explore a directory node  * @param dirNode      direcory node to display  *  */  public void exploreNode(FileNode dirNode)  {    DefaultTreeModel model = (DefaultTreeModel)getModel();    dirNode.explore();    openNode.add(dirNode);    model.nodeStructureChanged(dirNode);  }  /**  *  * Gets the node from the existing explored nodes and their  * children.  * @param path         path to a file or directory  * @return             corresponding node if the directory or  *                     file is visible otherwise returns null.  *  */  private FileNode getNode(String path)  {    Enumeration en = openNode.elements();    while(en.hasMoreElements())    {      FileNode node = (FileNode)en.nextElement();      String nodeName = node.getFile().getAbsolutePath();      if(nodeName.equals(path))        return node;    }// check children of explored nodes    en = openNode.elements();    while(en.hasMoreElements())    {      FileNode child = getChildNode((FileNode)en.nextElement(),path);      if(child != null)        return child;    }    return null;  }  /**  *  * Gets the child node of a parent node  * @param parent       parent node  * @param childName    name of child  * @return the child node  *  */  private FileNode getChildNode(FileNode parent, String childName)  {    for(Enumeration children = parent.children(); children.hasMoreElements() ;)    {      FileNode childNode = (FileNode)children.nextElement();      String nodeName = childNode.getFile().getAbsolutePath();      if(childName.equals(nodeName))        return childNode;    }    return null;  }  /**  *  * Finds a new index for adding a new file to the file manager.  * @param parentNode   parent directory node  * @param child        new child node  * @return             index of the child in the directory  *  */  private int getAnIndex(FileNode parentNode, String child)  {    //find the index for the child    int num = parentNode.getChildCount();    int childIndex = num;    for(int i=0;i<num;i++)    {      String nodeName =            ((FileNode)parentNode.getChildAt(i)).getFile().getName();      if(nodeName.compareTo(child) > 0)      {        childIndex = i;        break;      }      else if (nodeName.compareTo(child) == 0)  //file already exists      {        childIndex = -1;        break;      }    }    return childIndex;  }  /**  *  * Read a file into a byte array.  * @param filename     file name  * @return             byte[] contents of file  *  */  protected static byte[] readByteFile(String filename)  {    File fn = new File(filename);    byte[] b = null;    try    {      long s = fn.length();      if(s == 0)        return b;      b = new byte[(int)s];      FileInputStream fi = new FileInputStream(fn);      fi.read(b);      fi.close();    }    catch (IOException ioe)    {      System.out.println("Cannot read file: " + filename);    }    return b;  }  /**  *  * Opens a JFrame with the file contents displayed.  * @param filename     file name to display  * @param mysettings   jemboss properties  *  */  public static void showFilePane(String filename, JembossParams mysettings)  {    Hashtable contents = new Hashtable();    contents.put(filename,readByteFile(filename));    ShowResultSet srs = new ShowResultSet(contents,mysettings);    srs.setTitle("Local File");  }////////////////////// DRAG AND DROP////////////////////// drag source  public void dragGestureRecognized(DragGestureEvent e)   {    // ignore if mouse popup trigger    InputEvent ie = e.getTriggerEvent();    if(ie instanceof MouseEvent)       if(((MouseEvent)ie).isPopupTrigger())         return;        // drag only files     if(isFileSelection())      e.startDrag(DragSource.DefaultCopyDrop,     // cursor                 (Transferable)getSelectedNode(), // transferable data                                       this);     // drag source listener  }  public void dragDropEnd(DragSourceDropEvent e) {}  public void dragEnter(DragSourceDragEvent e) {}  public void dragExit(DragSourceEvent e) {}  public void dragOver(DragSourceDragEvent e) {}  public void dropActionChanged(DragSourceDragEvent e) {}// drop sink  public void dragEnter(DropTargetDragEvent e)  {    if(e.isDataFlavorSupported(RemoteFileNode.REMOTEFILENODE) ||       e.isDataFlavorSupported(FileNode.FILENODE) )      e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);  }  public void drop(DropTargetDropEvent e)  {    Transferable t = e.getTransferable();    final FileNode dropNode = getSelectedNode();    if(dropNode == null)    {      e.rejectDrop();      return;    }    //local drop    if(t.isDataFlavorSupported(FileNode.FILENODE))    {       try       {         FileNode fn = (FileNode)t.getTransferData(FileNode.FILENODE);         fn = getNode(fn.getFile().getAbsolutePath());         if (dropNode.isLeaf())         {           e.rejectDrop();           return;         }                 String dropDir = dropNode.getFile().getAbsolutePath();         String newFullName = dropDir+fs+fn.toString();         renameFile(fn.getFile(),fn,newFullName);         }       catch(Exception ufe){}            }    //remote drop    else if(t.isDataFlavorSupported(RemoteFileNode.REMOTEFILENODE))    {      try      {        final RemoteFileNode fn =           (RemoteFileNode)t.getTransferData(RemoteFileNode.REMOTEFILENODE);        File dropDest = null;        String dropDir = null;        if (dropNode.isLeaf())         {          FileNode pn = (FileNode)dropNode.getParent();          dropDir = pn.getFile().getAbsolutePath();          dropDest = new File(dropDir,fn.getFile());        }         else         {          dropDir = dropNode.getFile().getAbsolutePath();          dropDest = new File(dropDir,fn.getFile());        }        try        {          setCursor(cbusy);                                //check we want to & can save          FileSave fsave = new FileSave(dropDest);           if(fsave.doWrite())          {            Vector params = new Vector();            params.addElement("fileroot=" + fn.getRootDir());            params.addElement(fn.getFullName());            PrivateRequest gReq = new PrivateRequest(mysettings,                                "EmbreoFile","get_file",params);            fsave.fileSaving(gReq.getHash().get("contents"));            if(fsave.writeOK() && !fsave.fileExists())            {              final String ndropDir = dropDir;              Runnable updateTheTree = new Runnable()               {                public void run ()                 {                   addObject(fn.getFile(),ndropDir,dropNode);                 };              };              SwingUtilities.invokeLater(updateTheTree);            }          }          setCursor(cdone);        }         catch (Exception exp)         {          setCursor(cdone);          System.out.println("DragTree: caught exception");        }        e.getDropTargetContext().dropComplete(true);         }      catch (Exception exp)      {        e.rejectDrop();        return;      }    }    else    {      e.rejectDrop();      return;    }  }  /**  *  * When a suitable DataFlavor is offered over a remote file  * node the node is highlighted/selected and the drag  * accepted. Otherwise the drag is rejected.  *  */  public void dragOver(DropTargetDragEvent e)  {    if (e.isDataFlavorSupported(RemoteFileNode.REMOTEFILENODE))    {      Point ploc = e.getLocation();      TreePath ePath = getPathForLocation(ploc.x,ploc.y);      if (ePath == null)        e.rejectDrag();      else      {        setSelectionPath(ePath);        e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);      }    }    else if(e.isDataFlavorSupported(FileNode.FILENODE))    {      Point ploc = e.getLocation();      TreePath ePath = getPathForLocation(ploc.x,ploc.y);      if (ePath == null)      {        e.rejectDrag();        return;      }      FileNode node = (FileNode)ePath.getLastPathComponent();      if(!node.isDirectory())        e.rejectDrag();      else       {        setSelectionPath(ePath);        e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);      }    }    else      e.rejectDrag();        return;  }  public void dropActionChanged(DropTargetDragEvent e) {}  public void dragExit(DropTargetEvent e){}////////////////////// AUTO SCROLLING //////////////////////  /**  *  * Handles the auto scrolling of the JTree.  * @param location The location of the mouse.  *  */  public void autoscroll( Point location )  {    int top = 0, left = 0, bottom = 0, right = 0;    Dimension size = getSize();    Rectangle rect = getVisibleRect();    int bottomEdge = rect.y + rect.height;    int rightEdge = rect.x + rect.width;    if( location.y - rect.y < AUTOSCROLL_MARGIN && rect.y > 0 )       top = AUTOSCROLL_MARGIN;    if( location.x - rect.x < AUTOSCROLL_MARGIN && rect.x > 0 )      left = AUTOSCROLL_MARGIN;    if( bottomEdge - location.y < AUTOSCROLL_MARGIN && bottomEdge < size.height )      bottom = AUTOSCROLL_MARGIN;    if( rightEdge - location.x < AUTOSCROLL_MARGIN && rightEdge < size.width )       right = AUTOSCROLL_MARGIN;    rect.x += right - left;    rect.y += bottom - top;    scrollRectToVisible( rect );  }  /**  *  * Gets the insets used for the autoscroll.  * @return The insets.  *  */  public Insets getAutoscrollInsets()  {    Dimension size = getSize();    Rectangle rect = getVisibleRect();    autoscrollInsets.top = rect.y + AUTOSCROLL_MARGIN;    autoscrollInsets.left = rect.x + AUTOSCROLL_MARGIN;    autoscrollInsets.bottom = size.height - (rect.y+rect.height) + AUTOSCROLL_MARGIN;    autoscrollInsets.right  = size.width - (rect.x+rect.width) + AUTOSCROLL_MARGIN;    return autoscrollInsets;  }  /**  *  * Popup menu listener  *  */  class PopupListener extends MouseAdapter  {    public void mousePressed(MouseEvent e)    {      maybeShowPopup(e);    }    public void mouseReleased(MouseEvent e)    {      maybeShowPopup(e);    }    private void maybeShowPopup(MouseEvent e)    {      if(e.isPopupTrigger())        popup.show(e.getComponent(),                e.getX(), e.getY());    }  }}

⌨️ 快捷键说明

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