java 资源浏览器的图形化组件

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

资源浏览器的图形化组件

FileList.java

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class FileList extends JList<Object> {
    class FileListModel implements ListModel<Object> {
        private FolderNode node;
        private final static char ALL = 'A';
        char fileType = ALL;

        public void setNode(FolderNode node) {
            this.node = node;
        }

        public FolderNode getElementAt(int index) {
            if (node != null)
                return node.getChild(fileType, index);
            else
                return null;
        }

        public int getSize() {
            if (node != null)
                return node.getChildCount(fileType);
            else
                return 0;
        }

        public void addListDataListener(ListDataListener l) {
        }

        public void removeListDataListener(ListDataListener l) {
        }
    }

    class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
        private static final long serialVersionUID = 1L;

        public MyCellRenderer() {
            setOpaque(true);
        }

        public Component getListCellRendererComponent(JList<?> list,
                Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            FolderNode node = (FolderNode) value;
            setIcon(node.getIcon());
            setText(value.toString());
            setBackground(isSelected ? Color.BLUE.darker().darker()
                    : Color.WHITE);
            setForeground(isSelected ? Color.WHITE : Color.BLACK);
            return this;
        }
    }

    FileListModel dataModel;
    static final long serialVersionUID = 10;

    public FileList() {
        dataModel = new FileListModel();
        setModel(dataModel);
        this.setCellRenderer(new MyCellRenderer());
    }

    public void fireTreeSelectionChanged(FolderNode node) {
        dataModel.setNode(node);
        updateUI();
    }
}

I_fileSystem.java

import javax.swing.*;

public interface I_fileSystem {

    final public static char DIRECTORY = 'D';

    final public static char FILE = 'F';

    final public static char ALL = 'A';

    public Icon getIcon();

    public I_fileSystem getChild(char fileType, int index);
    public String getFullPath(); 
    public int getChildCount(char fileType);

    public boolean isLeaf(char fileType);

    public int getIndexOfChild(char fileType, Object child);

}

FileTree.java

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class FileTree extends JTree {
    class FolderRenderer extends DefaultTreeCellRenderer {

        private static final long serialVersionUID = 1L;

        public Component getTreeCellRendererComponent(JTree tree, Object value,

        boolean sel, boolean expanded, boolean leaf, int row,

        boolean hasFocus) {

            I_fileSystem node = (I_fileSystem) value;

            Icon icon = node.getIcon();

            setLeafIcon(icon);

            setOpenIcon(icon);

            setClosedIcon(icon);

            return super.getTreeCellRendererComponent(tree, value, sel,
                    expanded,

                    leaf, row, hasFocus);

        }

    }
    class FileSystemModel implements TreeModel {

        I_fileSystem theRoot;
        public FileSystemModel(I_fileSystem fs) {

            theRoot = fs;

        }

        public Object getRoot() {

            return theRoot;

        }

        public Object getChild(Object parent, int index) {

            return ((I_fileSystem) parent).getChild(I_fileSystem.DIRECTORY, index);

        }

        public int getChildCount(Object parent) {

            return ((I_fileSystem) parent).getChildCount(I_fileSystem.DIRECTORY);

        }

        public boolean isLeaf(Object node) {

            return ((I_fileSystem) node).isLeaf(I_fileSystem.DIRECTORY);

        }

        public int getIndexOfChild(Object parent, Object child) {

            return ((I_fileSystem) parent).getIndexOfChild(I_fileSystem.DIRECTORY, child);

        }

        public void valueForPathChanged(TreePath path, Object newValue) {

        }

        public void addTreeModelListener(TreeModelListener l) {

        }

        public void removeTreeModelListener(TreeModelListener l) {

        }

    }
    static final long serialVersionUID = 0;

    private FileList theList;

    public FileTree(FileList list) {

        theList = list;
        setModel(new FileSystemModel(new FolderNode()));
        this.setCellRenderer(new FolderRenderer());
        addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent tse) {
            }
        });
        this.setSelectionRow(0);

    }

    public void fireValueChanged(TreeSelectionEvent tse) {
        TreePath tp = tse.getNewLeadSelectionPath();
        theList.fireTreeSelectionChanged((FolderNode) tp.getLastPathComponent());
    }

    public void fireTreeCollapsed(TreePath path) {
        super.fireTreeCollapsed(path);
        TreePath curpath = getSelectionPath();
        if (path.isDescendant(curpath))
            setSelectionPath(path);
    }

    public void fireTreeWillExpand(TreePath path) {
        System.out.println("Path will expand is " + path);
    }

    public void fireTreeWillCollapse(TreePath path) {
        System.out.println("Path will collapse is " + path);
    }

    class ExpansionListener implements TreeExpansionListener {
        FileTree tree;

        public ExpansionListener(FileTree ft) {
            tree = ft;
        }

        public void treeCollapsed(TreeExpansionEvent tee) {
        }

        public void treeExpanded(TreeExpansionEvent tee) {
        }

    }

}

FolderNode.java

import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.*;

/**
 * 
 * A data model for a JTree. This model explorer windows file system directly.
 * 
 * 
 * 
 * <p>
 * 
 * Perhaps there is a fatal bug with this design. For speed, each of instances
 * 
 * of this model contains file objects of subdirectory, up to now, there isn't
 * 
 * any method to release them until program be end. I'm afraid that the memory
 * 
 * would be full of if the file system is large enough and JVM memery size
 * 
 * setted too small.
 * 
 * 
 * 
 * <p>
 * 
 * I won't pay more attention to solve it. it isn't goal of current a exercise.
 * 
 * 
 * 
 * @author Jason
 */
public class FolderNode implements I_fileSystem {
    private static FileSystemView fsView;

    private static boolean showHiden = true;;

    private File theFile;

    private Vector<File> all = new Vector<File>();

    private Vector<File> folder = new Vector<File>();

    /**
     * 
     * set that whether apply hiden file.
     * 
     * 
     * 
     * @param ifshow
     */

    public void setShowHiden(boolean ifshow) {

        showHiden = ifshow;

    }

    public Icon getIcon() {

        return fsView.getSystemIcon(theFile);

    }

    public String toString() {

        // return fsView.

        return fsView.getSystemDisplayName(theFile);

    }

    /**
     * 
     * create a root node. by default, it should be the DeskTop in window file
     * 
     * system.
     * 
     * 
     */

    public FolderNode() {

        fsView = FileSystemView.getFileSystemView();

        theFile = fsView.getHomeDirectory();

        prepareChildren();

    }

    private void prepareChildren() {

        File[] files = fsView.getFiles(theFile, showHiden);

        for (int i = 0; i < files.length; i++) {

            all.add(files[i]);

            if (files[i].isDirectory()

            && !files[i].toString().toLowerCase().endsWith(".lnk")) {

                folder.add(files[i]);

            }

        }

    }

    private FolderNode(File file) {

        theFile = file;

        prepareChildren();

    }

    public FolderNode getChild(char fileType, int index) {

        if (I_fileSystem.DIRECTORY == fileType) {

            return new FolderNode(folder.get(index));

        } else if (I_fileSystem.ALL == fileType) {

            return new FolderNode(all.get(index));

        } else if (I_fileSystem.FILE == fileType) {

            return null;

        } else {

            return null;

        }

    }

    public int getChildCount(char fileType) {

        if (I_fileSystem.DIRECTORY == fileType) {

            return folder.size();

        } else if (I_fileSystem.ALL == fileType) {

            return all.size();

        } else if (I_fileSystem.FILE == fileType) {

            return -1;

        } else {

            return -1;

        }

    }

    public boolean isLeaf(char fileType) {

        if (I_fileSystem.DIRECTORY == fileType) {

            return folder.size() == 0;

        } else if (I_fileSystem.ALL == fileType) {

            return all.size() == 0;

        } else if (I_fileSystem.FILE == fileType) {

            return true;

        } else {

            return true;

        }

    }

    public int getIndexOfChild(char fileType, Object child) {

        if (child instanceof FolderNode) {

            if (I_fileSystem.DIRECTORY == fileType) {

                return folder.indexOf(((FolderNode) child).theFile);

            } else if (I_fileSystem.ALL == fileType) {

                return all.indexOf(((FolderNode) child).theFile);

            } else if (I_fileSystem.FILE == fileType) {

                return -1;

            } else {

                return -1;

            }

        } else {

            return -1;

        }

    }

    @Override
    public String getFullPath() {
        return theFile.getAbsolutePath();
    }

}

JExplorer.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;

public class JExplorer extends JFrame {
    private static final long serialVersionUID = 0l;
    private static final int LEFT_WIDTH = 200;
    private static final int RIGHT_WIDTH = 300;
    private static final int WINDOW_HEIGHT = 300;

    private JPopupMenu jmTree;
    private JPopupMenu jmList;
    private FileTree tree;
    private FileList list;
    public JExplorer() {
        super("资源浏览器");
        setPreferredSize(new Dimension(800, 600));
        setLayout(new BorderLayout());
        list = new FileList();
        tree = new FileTree(list);
        jmTree = new JPopupMenu();
        JMenuItem jmiCopyPathName = new JMenuItem("复制目录全路径");
        jmiCopyPathName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Clipboard clipboard = Toolkit.getDefaultToolkit()
                        .getSystemClipboard();
                Transferable tText = new StringSelection(((I_fileSystem) tree
                        .getSelectionPath().getLastPathComponent())
                        .getFullPath());
                clipboard.setContents(tText, null);
            }
        });
        jmTree.add(jmiCopyPathName);
        tree.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 3 && tree.getSelectionCount() > 0)
                    jmTree.show(tree, e.getX(), e.getY());
            }
        });
        tree.setDoubleBuffered(true);
        jmList = new JPopupMenu();
        JMenuItem jmiCopyFileName = new JMenuItem("复制文件系统对象全路径");
        jmiCopyFileName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Clipboard clipboard = Toolkit.getDefaultToolkit()
                        .getSystemClipboard();
                Transferable tText = new StringSelection(((I_fileSystem)list.getSelectedValue()).getFullPath());
                clipboard.setContents(tText, null);
            }
        });
        jmList.add(jmiCopyFileName);
        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 3 && tree.getSelectionCount() > 0)
                    jmList.show(list, e.getX(), e.getY());
            }
        });
        list.setDoubleBuffered(true);
        JScrollPane treeView = new JScrollPane(tree);
        treeView.setPreferredSize(new Dimension(LEFT_WIDTH, WINDOW_HEIGHT));
        JScrollPane listView = new JScrollPane(list);
        listView.setPreferredSize(new Dimension(RIGHT_WIDTH, WINDOW_HEIGHT));
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,
                listView);
        pane.setDividerLocation(300);
        pane.setDividerSize(4);
        add(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int left = (screen.width - getWidth()) / 2;
        int top = (screen.height - getHeight()) / 2;
        setLocation(left, top);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JExplorer();
    }
}

标签: isp seo

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:js实现按钮的滑动效果

下一篇:统计项目源码行数的Java代码