📄 dragtree.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.Vector;import java.util.Enumeration;import java.util.Hashtable;import org.emboss.jemboss.soap.PrivateRequest;import org.emboss.jemboss.gui.ResultsMenuBar;import org.emboss.jemboss.gui.ShowResultSet;import org.emboss.jemboss.JembossParams;/**** Creates a local file tree manager for Jemboss. This acts as a drag * source and sink for files.**/public class DragTree extends JTree implements DragGestureListener, DragSourceListener, DropTargetListener, ActionListener, Autoscroll { /** jemboss properties */ private JembossParams mysettings; /** root directory */ private File root; /** store of directories that are opened */ private Vector openNode; /** file separator */ private String fs = new String(System.getProperty("file.separator")); /** popup menu */ private JPopupMenu popup; /** 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 rt root directory * @param f frame * @param mysettings jemboss properties * */ public DragTree(File rt, final JFrame f, final JembossParams mysettings) { this.mysettings = mysettings; this.root = rt; 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(root); 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 Alignment 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 MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent me) { if(me.getClickCount() == 2 && isFileSelection() && !me.isPopupTrigger()) { f.setCursor(cbusy); FileNode node = getSelectedNode(); String selected = node.getFile().getAbsolutePath(); showFilePane(selected,mysettings); f.setCursor(cdone); } } }; this.addMouseListener(mouseListener); addTreeExpansionListener(new TreeExpansionListener() { public void treeCollapsed(TreeExpansionEvent e){} public void treeExpanded(TreeExpansionEvent e) { TreePath path = e.getPath(); if(path != null) { FileNode node = (FileNode)path.getLastPathComponent(); if(!node.isExplored()) { f.setCursor(cbusy); exploreNode(node); f.setCursor(cdone); } } } }); } /** * * Popup menu actions * @param e action event * */ public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); final FileNode node = getSelectedNode(); if(source.getText().equals("Refresh")) { if(node == null) newRoot(root.getAbsolutePath()); else if(node.isLeaf()) refresh((FileNode)node.getParent()); else refresh(node); return; } if(node == null) { JOptionPane.showMessageDialog(null,"No file selected.", "Warning", JOptionPane.WARNING_MESSAGE); return; } final File f = node.getFile(); if(source.getText().equals("Jemboss Alignment Editor")) { org.emboss.jemboss.editor.AlignJFrame ajFrame = new org.emboss.jemboss.editor.AlignJFrame(f); ajFrame.setVisible(true); } else if(source.getText().equals("Text Editor")) showFilePane(f.getAbsolutePath(), mysettings); else if(source.getText().equals("New Folder...")) { String path = null; if(node.isLeaf()) path = f.getParent(); else path = f.getAbsolutePath(); String inputValue = JOptionPane.showInputDialog(null, "Folder Name","Create New Folder in "+path, JOptionPane.QUESTION_MESSAGE); if(inputValue != null && !inputValue.equals("") ) { String fullname = path+fs+inputValue; File dir = new File(fullname); if(dir.exists()) JOptionPane.showMessageDialog(null, fullname+" alread exists!", "Error", JOptionPane.ERROR_MESSAGE); else { if(dir.mkdir()) addObject(inputValue,path,node); else JOptionPane.showMessageDialog(null, "Cannot make the folder\n"+fullname, "Error", JOptionPane.ERROR_MESSAGE); } } } else if(source.getText().equals("Delete...")) { File fn[] = getSelectedFiles(); String names = ""; for(int i=0; i<fn.length;i++) names = names.concat(fn[i].getAbsolutePath()+"\n"); int n = JOptionPane.showConfirmDialog(null, "Delete \n"+names+"?", "Delete Files", JOptionPane.YES_NO_OPTION); FileNode nodes[] = getSelectedNodes(); if(n == JOptionPane.YES_OPTION) for(int i=0; i<nodes.length;i++) deleteFile(nodes[i]); } else if(source.getText().equals("De-select All")) clearSelection(); else if(isFileSelection() && source.getText().equals("Rename...")) { String inputValue = (String)JOptionPane.showInputDialog(null, "New File Name","Rename "+f.getName(), JOptionPane.QUESTION_MESSAGE,null,null,f.getName()); if(inputValue != null && !inputValue.equals("") ) { String path = f.getParent(); String fullname = path+fs+inputValue; File newFile = new File(fullname); try { renameFile(f,node,newFile.getCanonicalPath()); } catch(IOException ioe){} } } } /** * * Delete a file from the tree * @param node node to delete * */ public void deleteFile(final FileNode node) { File f = node.getFile(); if(f.delete()) { Runnable deleteFileFromTree = new Runnable() { public void run () { deleteObject(node); }; }; SwingUtilities.invokeLater(deleteFileFromTree); } else JOptionPane.showMessageDialog(null,"Cannot delete\n"+ f.getAbsolutePath(),"Warning", JOptionPane.ERROR_MESSAGE); } /** * * Method to rename a file and update the filenode's. * @param oldFile file to rename * @param oldNode filenode to be removed * @param newFullName name of the new file * */ private void renameFile(final File oldFile, final FileNode oldNode, String newFullName) { final File fnew = new File(newFullName); if(fnew.exists()) JOptionPane.showMessageDialog(null, newFullName+" alread exists!", "Warning", JOptionPane.ERROR_MESSAGE); else { if(oldFile.renameTo(fnew)) { Runnable renameFileInTree = new Runnable() { public void run () { addObject(fnew.getName(),fnew.getParent(),oldNode); deleteObject(oldNode); }; }; SwingUtilities.invokeLater(renameFileInTree); } else JOptionPane.showMessageDialog(null, "Cannot rename \n"+oldFile.getAbsolutePath()+ "\nto\n"+fnew.getAbsolutePath(), "Rename Error", JOptionPane.ERROR_MESSAGE); } return; } /** * * Define a directory root for the file tree * @param newRoot directory to use as the root for * the tree. * */ public void newRoot(String newRoot) { root = new File(newRoot); DefaultTreeModel model = (DefaultTreeModel)getModel(); model = createTreeModel(root); setModel(model); } /** * * Refresh * @param FileNode node to refresh * */ public void refresh(FileNode node) { node.reExplore(); DefaultTreeModel model = (DefaultTreeModel)getModel(); model.nodeStructureChanged(node); } /** * * Get FileNode of selected node * @return node that is currently selected * */ public FileNode getSelectedNode() { TreePath path = getLeadSelectionPath(); if(path == null) return null; FileNode node = (FileNode)path.getLastPathComponent(); return node; } /** * * Get FileNodes of selected nodes * @return node that is currently selected * */ public FileNode[] getSelectedNodes() { TreePath path[] = getSelectionPaths(); if(path == null) return null; int numberSelected = path.length; FileNode nodes[] = new FileNode[numberSelected]; for(int i=0;i<numberSelected;i++) nodes[i] = (FileNode)path[i].getLastPathComponent(); return nodes; } /** * * Get selected files * @return node that is currently selected * */ public File[] getSelectedFiles() { FileNode[] fn = getSelectedNodes(); int numberSelected = fn.length; File files[] = new File[numberSelected]; for(int i=0;i<numberSelected;i++) files[i] = fn[i].getFile(); return files; } /** * * Return true if selected node is a file * @return true is a file is selected, false if * a directory is selected * */ public boolean isFileSelection() { TreePath path = getLeadSelectionPath(); if(path == null) return false; FileNode node = (FileNode)path.getLastPathComponent(); return node.isLeaf(); } /** * * Make the given directory the root and create a new * DefaultTreeModel. * @param root root directory * @param tree model with the root node set * to the given directory * */ private DefaultTreeModel createTreeModel(File root) { FileNode rootNode = new FileNode(root); rootNode.explore(); openNode = new Vector(); openNode.add(rootNode); return new DefaultTreeModel(rootNode); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -