📄 remotedragtree.java
字号:
/********************************************************************** This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Library General Public* License as published by the Free Software Foundation; either* version 2 of the License, or (at your option) any later version.** This library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Library General Public License for more details.** You should have received a copy of the GNU Library General Public* License along with this library; if not, write to the* Free Software Foundation, Inc., 59 Temple Place - Suite 330,* Boston, MA 02111-1307, USA.** @author: Copyright (C) Tim Carver*********************************************************************/package org.emboss.jemboss.gui.filetree;import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.awt.dnd.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.tree.*;import java.io.*;import java.util.*;import org.emboss.jemboss.gui.ShowResultSet;import org.emboss.jemboss.gui.ResultsMenuBar;import org.emboss.jemboss.soap.*;import org.emboss.jemboss.JembossParams;/**** Creates a remote file tree which is a drag source & sink**/public class RemoteDragTree extends JTree implements DragGestureListener, DragSourceListener, DropTargetListener, ActionListener, Autoscroll { /** jemboss properties */ private JembossParams mysettings; /** remote directory roots */ private static FileRoots froots; /** popup menu */ private JPopupMenu popup; /** file separator */ private String fs = new String(System.getProperty("file.separator")); /** line separator */ private String ls = new String(System.getProperty("line.separator")); /** store of directories that are opened */ private Vector openNode; /** busy cursor */ private Cursor cbusy = new Cursor(Cursor.WAIT_CURSOR); /** done cursor */ private Cursor cdone = new Cursor(Cursor.DEFAULT_CURSOR); /** AutoScroll margin */ private static final int AUTOSCROLL_MARGIN = 45; /** used by AutoScroll method */ private Insets autoscrollInsets = new Insets( 0, 0, 0, 0 ); /** * * @param mysettings jemboss properties * @param froots remote directory roots * */ public RemoteDragTree(final JembossParams mysettings, FileRoots froots) { this.mysettings = mysettings; this.froots = froots; DragSource dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer( this, // component where drag originates DnDConstants.ACTION_COPY_OR_MOVE, // actions this); // drag gesture recognizer setDropTarget(new DropTarget(this,this)); DefaultTreeModel model = createTreeModel(" "); setModel(model); createTreeModelListener(); this.getSelectionModel().setSelectionMode (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);// popup menu addMouseListener(new PopupListener()); popup = new JPopupMenu(); JMenuItem menuItem = new JMenuItem("Refresh"); menuItem.addActionListener(this); popup.add(menuItem); popup.add(new JSeparator());//open menu JMenu openMenu = new JMenu("Open With"); popup.add(openMenu); menuItem = new JMenuItem("Jemboss Aligmnment Editor"); menuItem.addActionListener(this); openMenu.add(menuItem); menuItem = new JMenuItem("Text Editor"); menuItem.addActionListener(this); openMenu.add(menuItem); menuItem = new JMenuItem("Rename..."); menuItem.addActionListener(this); popup.add(menuItem); menuItem = new JMenuItem("New Folder..."); menuItem.addActionListener(this); popup.add(menuItem); menuItem = new JMenuItem("Delete..."); menuItem.addActionListener(this); popup.add(menuItem); popup.add(new JSeparator()); menuItem = new JMenuItem("De-select All"); menuItem.addActionListener(this); popup.add(menuItem); //Listen for when a file is selected addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent me) { if(me.getClickCount() == 2 && isFileSelection()) { setCursor(cbusy); RemoteFileNode node = (RemoteFileNode)getLastSelectedPathComponent(); if(node==null) return; if(node.isLeaf()) showFilePane(node.getFullName(),mysettings); setCursor(cdone); } } public void mousePressed(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public void mouseReleased(MouseEvent me){} }); addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent e) { TreePath path = e.getPath(); if(path != null) { setCursor(cbusy); RemoteFileNode node = (RemoteFileNode)path.getLastPathComponent(); if(!node.isExplored()) exploreNode(node); setCursor(cdone); } } public void treeCollapsed(TreeExpansionEvent e){} }); } /** * * This is used to refresh the file manager * */ public void refreshRoot() { DefaultTreeModel model = (DefaultTreeModel)getModel(); model = createTreeModel(" "); setModel(model); } /** * * Popup menu actions * @param e action event * */ public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); final RemoteFileNode node = getSelectedNode(); if(node == null) { JOptionPane.showMessageDialog(null,"No file selected.", "Warning", JOptionPane.WARNING_MESSAGE); return; } final String fn = node.getFullName(); final String parent = node.getPathName(); String rootPath = node.getRootDir(); RemoteFileNode pn = node; if(source.getText().equals("Refresh")) { refreshRoot(); } else if(source.getText().equals("Jemboss Aligmnment Editor")) { Vector params = new Vector(); String options= "fileroot=" + froots.getCurrentRoot(); params.addElement(options); params.addElement(fn); try { PrivateRequest gReq = new PrivateRequest(mysettings,"EmbreoFile", "get_file",params); org.emboss.jemboss.editor.AlignJFrame ajFrame = new org.emboss.jemboss.editor.AlignJFrame( (String)gReq.getHash().get("contents"),fn); ajFrame.setVisible(true); } catch(JembossSoapException eae) { System.out.println("RemoteDragTree :: JembossSoapException "+fn); } } else if(source.getText().equals("Text Editor")) showFilePane(fn, mysettings); else if(source.getText().equals("New Folder...")) { final String inputValue = JOptionPane.showInputDialog(null, "Folder Name","Create New Folder in", JOptionPane.QUESTION_MESSAGE); String dropDest = null; if(node.isLeaf()) { pn = (RemoteFileNode)node.getParent(); dropDest = pn.getFullName() + "/" + inputValue; //assumes unix file sep.! } else dropDest = node.getFullName() + "/" + inputValue; String newNode = pn.getServerName(); if(!newNode.endsWith("/")) newNode = newNode.concat("/"); newNode = newNode.concat(inputValue); if(nodeExists(pn,newNode)) return; if(inputValue != null && !inputValue.equals("") ) { final RemoteFileNode pnn = pn; Vector params = new Vector(); params.addElement("fileroot=" + rootPath); params.addElement(dropDest); try { setCursor(cbusy); PrivateRequest gReq = new PrivateRequest(mysettings, "EmbreoFile","mkdir",params); setCursor(cdone); Runnable addDirToTree = new Runnable() { public void run () { addObject(pnn,inputValue,true); }; }; SwingUtilities.invokeLater(addDirToTree); } catch (JembossSoapException jse) { setCursor(cdone); } } } else if(source.getText().equals("Delete...")) { RemoteFileNode nodes[] = getSelectedNodes(); String sname = ""; for(int i=0;i<nodes.length;i++) sname = sname.concat(nodes[i].getServerName()+ls); int n = JOptionPane.showConfirmDialog(null, "Delete"+ls+sname+"?", "Delete "+sname, JOptionPane.YES_NO_OPTION); if(n == JOptionPane.YES_OPTION) for(int i=0;i<nodes.length;i++) deleteNode(nodes[i]); } else if(source.getText().equals("De-select All")) clearSelection(); else if(source.getText().equals("Rename...")) { if(node.isLeaf()) { String inputValue = (String)JOptionPane.showInputDialog(null, "New File Name","Rename "+fn, JOptionPane.QUESTION_MESSAGE,null,null,fn); pn = (RemoteFileNode)node.getParent(); if(inputValue != null && !inputValue.equals("") ) { String newfile = null; if(parent.endsWith("/")) newfile = parent+inputValue; else newfile = parent+"/"+inputValue; String dir = ((RemoteFileNode)node.getParent()).getFullName(); if(inputValue.indexOf("/") > 0) { int index = inputValue.lastIndexOf("/"); dir = inputValue.substring(0,index); } RemoteFileNode parentNode = getNode(dir); if(!nodeExists(parentNode,newfile)) rename(rootPath,fn,parent,inputValue,node,parentNode); } } } } /** * * Delete a node (file or directory) from the tree * and from the server * @param node node to remove * */ private void deleteNode(final RemoteFileNode node) { setCursor(cbusy); String rootPath = node.getRootDir(); String dropDest = node.getFullName(); Vector params = new Vector(); params.addElement("fileroot=" + rootPath); params.addElement(dropDest); if(node.isLeaf()) // file deletion { try { PrivateRequest gReq = new PrivateRequest(mysettings, "EmbreoFile","delFile",params); Runnable deleteFileFromTree = new Runnable() { public void run () { deleteObject(node); }; }; SwingUtilities.invokeLater(deleteFileFromTree); } catch (JembossSoapException jse) { setCursor(cdone); } } else // directory deletion { try { FileList efl = new FileList(mysettings,rootPath,dropDest); Vector children = efl.fileVector(); if(children.size() > 0) JOptionPane.showMessageDialog(null,"Cannot delete"+ls+ node.getServerName()+ ls+"this directory is not empty","Warning", JOptionPane.ERROR_MESSAGE); else { PrivateRequest gReq = new PrivateRequest(mysettings, "EmbreoFile","delDir",params); Runnable deleteFileFromTree = new Runnable() { public void run () { deleteObject(node); }; }; SwingUtilities.invokeLater(deleteFileFromTree); } } catch (JembossSoapException jse) { setCursor(cdone); } } setCursor(cdone); } /** * * Explore a directory node * @param dirNode direcory node to display * */ public void exploreNode(RemoteFileNode dirNode) { DefaultTreeModel model = (DefaultTreeModel)getModel(); dirNode.explore(); openNode.add(dirNode); model.nodeStructureChanged(dirNode); } /** * * Test if a child node exists * @param parentNode parent node * @param child child to test for * */ public boolean nodeExists(RemoteFileNode parentNode,String child) { RemoteFileNode childNode = getChildNode(parentNode,child); if(childNode != null) { String ls = System.getProperty("line.separator"); JOptionPane.showMessageDialog(null, child+ls+" already exists!", "File Exists", JOptionPane.ERROR_MESSAGE); return true; } return false; } /** * * Rename a node from the tree * @param rootPath root path * @param fullname full name of node to rename * @param pathToNewFile path to new file * @param newfile new file name * @param node node to rename * */ private void rename(String rootPath, final String fullname, String pathToNewFile, final String newfile, final RemoteFileNode node, final RemoteFileNode parentNode) { Vector params = new Vector(); params.addElement("fileroot=" + rootPath); params.addElement(fullname); params.addElement(pathToNewFile+"/"+newfile); try { setCursor(cbusy); PrivateRequest gReq = new PrivateRequest(mysettings, "EmbreoFile","rename",params); setCursor(cdone); Runnable deleteFileFromTree = new Runnable() { public void run () { addObject(parentNode,newfile,false); deleteObject(node); }; }; SwingUtilities.invokeLater(deleteFileFromTree); } catch (JembossSoapException jse) { setCursor(cdone); } } /** * * 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 parentNode parent node * @param child file to add to the tree * @param ldir true if child is a directory * */ public void addObject(RemoteFileNode parentNode,String child, boolean ldir) { DefaultTreeModel model = (DefaultTreeModel)getModel(); if(parentNode == null) return; String path = parentNode.getFullName(); //create new file node if(path.equals(" ")) path = ""; if(child.indexOf("/") > -1) child = child.substring(child.lastIndexOf("/")+1); RemoteFileNode childNode = null; if(!parentNode.isExplored()) { exploreNode(parentNode); childNode = getNode(parentNode.getServerName() + "/" + child); } else { childNode = new RemoteFileNode(mysettings,froots, child,null,path,ldir);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -