📄 remotedragtree.java
字号:
//find the index for the child int num = parentNode.getChildCount(); int childIndex = num; for(int i=0;i<num;i++) { String nodeName = ((RemoteFileNode)parentNode.getChildAt(i)).getFile(); if(nodeName.compareTo(child) > 0) { childIndex = i; break; } else if(nodeName.compareTo(child) == 0) //file already exists { childIndex = -1; break; } } if(childIndex != -1) model.insertNodeInto(childNode,parentNode,childIndex); } // Make sure the user can see the new node. this.scrollPathToVisible(new TreePath(childNode.getPath())); return; } /** * * Delete a node from the tree * @param node node to remove * */ public void deleteObject(RemoteFileNode node) { RemoteFileNode parentNode = (RemoteFileNode)node.getParent(); DefaultTreeModel model = (DefaultTreeModel)getModel(); model.removeNodeFromParent(node);// model.nodeStructureChanged(parentNode); return; } /** * * Get the contents of a local file as a byte array * @param name local file name * @return contents of file * */ public byte[] getLocalFile(File name) { byte[] b = null; try { long s = name.length(); b = new byte[(int)s]; FileInputStream fi = new FileInputStream(name); fi.read(b); fi.close(); } catch (IOException ioe) { System.out.println("Cannot read file: " + name); } return b; } /** * * Get RemoteFileNode of selected node * @return node that is currently selected * */ public RemoteFileNode getSelectedNode() { TreePath path = getLeadSelectionPath(); if(path == null) return null; RemoteFileNode node = (RemoteFileNode)path.getLastPathComponent(); return node; } /** * * Get RemoteFileNodes of selected nodes * @return node that is currently selected * */ public RemoteFileNode[] getSelectedNodes() { TreePath path[] = getSelectionPaths(); if(path == null) return null; int numberSelected = path.length; RemoteFileNode nodes[] = new RemoteFileNode[numberSelected]; for(int i=0;i<numberSelected;i++) nodes[i] = (RemoteFileNode)path[i].getLastPathComponent(); return nodes; } /** * * Determine if selected node is a file * @return true if the selected node is a file * */ public boolean isFileSelection() { TreePath path = getLeadSelectionPath(); if(path == null) return false; RemoteFileNode node = (RemoteFileNode)path.getLastPathComponent(); return !node.isDirectory(); } /** * * Get the selected node file name * @return file name * */ public String getFilename() { TreePath path = getLeadSelectionPath(); RemoteFileNode node = (RemoteFileNode)path.getLastPathComponent(); return node.getServerName(); } /** * * Creates the tree model for the given root * @param root root to create model for * @return tree model * */ private DefaultTreeModel createTreeModel(String root) { setCursor(cbusy); RemoteFileNode rootNode = new RemoteFileNode(mysettings,froots, root,null,null); rootNode.explore(); openNode = new Vector(); openNode.add(rootNode); setCursor(cdone); return new DefaultTreeModel(rootNode); } /** * * 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 RemoteFileNode getNode(String path) { Enumeration en = openNode.elements(); while(en.hasMoreElements()) { RemoteFileNode node = (RemoteFileNode)en.nextElement(); String nodeName = node.getFullName(); if(nodeName.equals(path)) return node; }// check children of explored nodes en = openNode.elements(); while(en.hasMoreElements()) { RemoteFileNode child = getChildNode((RemoteFileNode)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 RemoteFileNode getChildNode(RemoteFileNode parent, String childName) { for(Enumeration children = parent.children(); children.hasMoreElements() ;) { RemoteFileNode childNode = (RemoteFileNode)children.nextElement(); String nodeName = childNode.getServerName(); if(childName.equals(nodeName)) return childNode; } return null; } /** * * Opens a JFrame with the file contents displayed. * @param filename file name * @param mysettings jemboss properties * */ public static void showFilePane(String filename, JembossParams mysettings) { try { Vector params = new Vector(); String options= "fileroot=" + froots.getCurrentRoot(); params.addElement(options); params.addElement(filename); PrivateRequest gReq = new PrivateRequest(mysettings,"EmbreoFile", "get_file",params); Hashtable hfile = new Hashtable(); Object contents = gReq.getHash().get("contents"); if(contents instanceof String) hfile.put(filename, ((String)contents).getBytes() ); else hfile.put(filename, (byte[])contents); ShowResultSet srs = new ShowResultSet(hfile,mysettings); srs.setTitle("Remote File"); } catch(JembossSoapException eae) { } }////////////////////// DRAG AND DROP//////////////////// 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 }// Source 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) {}// Target public void dragEnter(DropTargetDragEvent e) { if(e.isDataFlavorSupported(FileNode.FILENODE) || e.isDataFlavorSupported(RemoteFileNode.REMOTEFILENODE)) e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } public void drop(DropTargetDropEvent e) { Transferable t = e.getTransferable(); if(t.isDataFlavorSupported(RemoteFileNode.REMOTEFILENODE)) { try { Point ploc = e.getLocation(); TreePath dropPath = getPathForLocation(ploc.x,ploc.y); if(dropPath != null) { RemoteFileNode fn = (RemoteFileNode)t.getTransferData(RemoteFileNode.REMOTEFILENODE); fn = getNode(fn.getServerName()); // need to get the instance of // this directly to manipulate tree String dropDest = null; RemoteFileNode fdropPath = (RemoteFileNode)dropPath.getLastPathComponent(); String dropRoot = fdropPath.getRootDir(); String dropFile = null; if(fdropPath.getFile().equals(" ")) dropFile = fn.getFile(); else dropFile = fdropPath.getFile()+"/"+fn.getFile(); if(!nodeExists(fdropPath,fdropPath.getServerName()+fn.getFile())) rename(fn.getRootDir(),fn.getFullName(), fdropPath.getPathName(), dropFile, fn, fdropPath); } } catch(Exception ex){} } else if(t.isDataFlavorSupported(FileNode.FILENODE)) { try { Point ploc = e.getLocation(); TreePath dropPath = getPathForLocation(ploc.x,ploc.y); if (dropPath != null) { FileNode fn = (FileNode)t.getTransferData(FileNode.FILENODE); File lfn = fn.getFile(); String dropDest = null; RemoteFileNode fdropPath = (RemoteFileNode)dropPath.getLastPathComponent(); String dropRoot = fdropPath.getRootDir(); RemoteFileNode pn = fdropPath; if(fdropPath.isLeaf()) { pn = (RemoteFileNode)fdropPath.getParent(); dropDest = pn.getFullName() + "/" + lfn.getName(); //assumes unix file sep.! } else dropDest = fdropPath.getFullName()+ "/" + lfn.getName(); if(!nodeExists(pn,pn.getServerName()+lfn.getName())) { try { Vector params = new Vector(); byte[] fileData = getLocalFile(lfn); params.addElement("fileroot=" + dropRoot); params.addElement(dropDest); params.addElement(fileData); setCursor(cbusy); PrivateRequest gReq = new PrivateRequest(mysettings,"EmbreoFile", "put_file",params); setCursor(cdone); //add file to remote file tree RemoteFileNode parentNode = fdropPath; if(parentNode.isLeaf()) parentNode = (RemoteFileNode)fdropPath.getParent(); else parentNode = fdropPath; if(parentNode.isExplored()) addObject(parentNode,lfn.getName(),false); else { exploreNode(parentNode); RemoteFileNode childNode = getNode(parentNode.getServerName() + "/" + lfn.getName()); scrollPathToVisible(new TreePath(childNode.getPath())); } } catch (Exception exp) { setCursor(cdone); System.out.println("RemoteDragTree: caught exception " + dropRoot + " Destination: " + dropDest + " Local File " + lfn.toString()); } } else e.rejectDrop(); } } catch (Exception ex) { } } else e.rejectDrop(); } /** * * 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(FileNode.FILENODE)) { 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(RemoteFileNode.REMOTEFILENODE)) { Point ploc = e.getLocation(); TreePath ePath = getPathForLocation(ploc.x,ploc.y); if (ePath == null) { e.rejectDrag(); return; } RemoteFileNode node = (RemoteFileNode)ePath.getLastPathComponent(); if(!node.isDirectory()) e.rejectDrag(); else { setSelectionPath(ePath); e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } } else e.rejectDrag(); } 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 + -