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

📄 fileviewimpl.java

📁 Java资源管理器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package mvc;

import gui.*;
import basic.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.io.File;
import java.util.Collection;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.border.*;
import javax.swing.JPopupMenu;
import javax.swing.BorderFactory;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.util.*;
/*
 * Created on 2005-5-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FileViewImpl extends JFrame implements FileView,ActionListener {
    private FileModel model;
    private FileControllerImpl control;
    private JTable table;
    private JTree tree;
    private FileTBModel tableModel;
    private Collection roots;
    private DefaultTreeModel treeModel;
    private JLabel addressLabel;
    private JTextField addressTextField;
    private JButton upBtn;
    private JButton addFolderBtn;
    private JButton addFileBtn;
    private JButton deleteBtn;    
    private JButton exitBtn;
    private File currentFile;
    private TNode currentNode;
    private TreePath curPath;
    private boolean isTable = false;
    private JPopupMenu popMenu;
    private JMenuItem addFolderMenu;
    private JMenuItem addFileMenu;
    private JMenuItem deleteMenu;
    
    /**
     * The tree mouse selection event processor
     * @author George Wen
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    private class TreeSelectionProcesser implements TreeSelectionListener{
		public void valueChanged(TreeSelectionEvent e){
			try{
			    curPath = tree.getSelectionPath();
			    currentNode = (TNode)e.getPath().getLastPathComponent();		        
			    currentFile = currentNode.getFile();
				tree.scrollPathToVisible(curPath);
			    addressTextField.setText(currentFile.getAbsolutePath());
			    isTable = false;

			    if(!currentFile.getName().equals("My Computer")){
			        Collection files = control.handleGetAll(currentNode.getFile());
			        tableModel.addAllFiles(files);
			        upBtn.setEnabled(true);
			        addFolderBtn.setEnabled(true);
			        addFileBtn.setEnabled(true);
			        addFolderMenu.setEnabled(true);
			        addFileMenu.setEnabled(true);
			        if(!((TNode)currentNode.getParent()).isRoot()){
			            deleteBtn.setEnabled(true);
			            deleteMenu.setEnabled(true);
			        }
			        else{
			            deleteBtn.setEnabled(false);
			            deleteMenu.setEnabled(false);
			        }
			    }
			    else{
			        upBtn.setEnabled(false);
			        addFolderBtn.setEnabled(false);
			        addFileBtn.setEnabled(false);
			        deleteBtn.setEnabled(false);
			        addFolderMenu.setEnabled(false);
			        addFileMenu.setEnabled(false);
			        deleteMenu.setEnabled(false);
			        currentNode = (TNode)treeModel.getRoot();
			        tableModel.addAllFiles(control.handleGetRootFiles());
			    }
			}
			catch(Exception ee){
	            JOptionPane.showMessageDialog(null,"Read file error!","Error",JOptionPane.ERROR_MESSAGE);
			    ee.printStackTrace();
			}
		}
	}
    
    /**
     * The tree will be expand event processor
     * @author George Wen
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
	private class TreeWillExpandProcessor implements TreeWillExpandListener{		
	    public void treeWillExpand(TreeExpansionEvent e){
			try{
			    currentNode = (TNode)e.getPath().getLastPathComponent();
			    if(!currentNode.getName().equals("My Computer")){
				    Collection directories = control.handleGetAllDirectories(currentNode.getFile());
				    appendNodes(currentNode,directories);
				    treeModel.nodeStructureChanged(currentNode);
			    }
			}
			catch(Exception ee){
	            JOptionPane.showMessageDialog(null,"Read file error!","Error",JOptionPane.ERROR_MESSAGE);
	            ee.printStackTrace();
			}
		}
		
		public void treeWillCollapse(TreeExpansionEvent ec){
		} 
	}
    
	/**
	 * This is the table mouse click event processor
	 * @author George Wen
	 *
	 * TODO To change the template for this generated type comment go to
	 * Window - Preferences - Java - Code Style - Code Templates
	 */
    private class mouseClickedProcessor extends MouseAdapter {
        public void mouseClicked(MouseEvent e){  
            int clickedIndex = table.getSelectedRow();
            if(clickedIndex >=0){
                isTable = true;
                currentFile = tableModel.getFile(clickedIndex);
                addressTextField.setText(currentFile.getAbsolutePath()); 
                if(!currentNode.getFile().getName().equals("My Computer")){
                    deleteBtn.setEnabled(true);
                    deleteMenu.setEnabled(true);
                }
            }
            if(e.getClickCount()>=2){
                if(currentFile.isDirectory()){
                    try{
    			        upBtn.setEnabled(true);
    			        if(!tree.isExpanded(curPath))
    			            tree.expandPath(curPath);
    			        if(currentNode.getChildCount()>0){
    			            for(int i=0;i<currentNode.getChildCount();i++){
    			                TNode temp = (TNode)currentNode.getChildAt(i);
    			                if(temp.getFile().getPath().equalsIgnoreCase(currentFile.getPath())){
    			                    TreeNode[] nodes = treeModel.getPathToRoot(temp);
    			        			curPath = new TreePath(nodes);
    			        			tree.setSelectionPath(curPath);
    			                    break;
    			                }
    			            }
    			        }
                    }
                    catch(Exception ee){
                        ee.printStackTrace();
                    }
                }
            }
            else if(e.getButton() == e.BUTTON3){
                popMenu.show(table,e.getX(),e.getY());
            }
        }
    }    
    
    public FileViewImpl(FileModel model){
        super("Resources Explore...");
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        upBtn = new JButton(new ImageIcon("icons\\up.png"));upBtn.setEnabled(false);
        upBtn.setPreferredSize(new Dimension(20,18));
        addressLabel = new JLabel("Address: ");
        addressTextField = new JTextField();
        addressTextField.setForeground(Color.BLUE);
        addFolderBtn = new JButton("Add Folder");addFolderBtn.setEnabled(false);
        addFileBtn = new JButton("Add File");addFileBtn.setEnabled(false);
        deleteBtn = new JButton("Delete Me");deleteBtn.setEnabled(false);
        exitBtn = new JButton("Exit");
        popMenu = new JPopupMenu("PopMenu");
        addFolderMenu = new JMenuItem("AddFolder");addFolderMenu.setEnabled(false);popMenu.add(addFolderMenu);
        addFileMenu = new JMenuItem("AddFile");addFileMenu.setEnabled(false);popMenu.add(addFileMenu);
        deleteMenu = new JMenuItem("Delete");deleteMenu.setEnabled(false);popMenu.add(deleteMenu);
        
        try{
            this.model = model;
            model.addViews(this);
            control = new FileControllerImpl(this,model);
            roots = control.handleGetRootNodes();//获得要加到根节点的磁盘盘符数组
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
        /**
         * Tree的初始化和相关配置,TNode为树的Node类,根节点为root,roots为要加到根节点上的磁盘盘符
         * appendNodes为添加节点的方法,调用时传入父节点node,和要添加到该节点的节点数组Collection 类形
         */
        TNode root = new TNode("My Computer");
        currentNode = root;
        Vector tn = (Vector)roots;
        if(tn.size()>0){
            for(int i=0;i<tn.size();i++){
                TNode tmp = (TNode)tn.get(i);
                tmp.add(new DefaultMutableTreeNode(""));
                root.add(tmp);
            }
        }

        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);
        tree.setRowHeight(20);
        TreeSelectionModel sm = tree.getSelectionModel();
        sm.setSelectionMode(1);
        tree.setSelectionModel(sm);
        DefaultTreeCellRenderer treeRender = (DefaultTreeCellRenderer)tree.getCellRenderer();
        treeRender.setLeafIcon(new ImageIcon("icons\\foldercolse.png"));
        treeRender.setClosedIcon(new ImageIcon("icons\\foldercolse.png"));
        treeRender.setOpenIcon(new ImageIcon("icons\\folderopen.png"));
        treeRender.setBackgroundNonSelectionColor(Color.white);
        treeRender.setBackgroundSelectionColor(Color.ORANGE);
        treeRender.setBorderSelectionColor(Color.pink);
        treeRender.setTextNonSelectionColor(Color.black);
        treeRender.setTextSelectionColor(Color.blue);
		tree.putClientProperty("JTree.lineStyle","Horizontal");
        
		/**
		 * Table的相关初始化配置,初始化的时候Table中都初始化与TREE一样的内容,调用tableModel的addAll方法
		 * 把盘符信息显示在Table中,同时调用setColumnWidth方法来设置第0例和第4例的优先例宽,然后调用table.getColumnModel()
		 * 方法获得Table的例模型从而获得指定例名,最后调用setCellRenderer方法给获得的该例指定一个TblCellRenderer这样就可以在
		 * 给指定的例画上图标什么的
		 */
        tableModel = new FileTBModel();
        try{
            tableModel.addAllFiles(control.handleGetRootFiles());
        }
        catch(Exception e){
            e.printStackTrace();
        }
        table = new JTable(tableModel);
        table.setShowGrid(false);
        setColumnWidth(0,300);
        setColumnWidth(3,150);
        table.setSelectionBackground(Color.ORANGE);
        table.setSelectionForeground(Color.blue);
        table.setRowHeight(20);
		TableColumnModel columnModel = table.getColumnModel();
		TableColumn columnName = columnModel.getColumn(0);
		columnName.setCellRenderer(new TblCellRenderer());
        
		/**

⌨️ 快捷键说明

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