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

📄 fileexplorer.java

📁 java GUI编程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        for (int i = 0; i < count; i++) {
            Object oneChild = jTreeDisk.getModel().getChild(parentTreeNode, i);

            if (oneChild instanceof MyTreeNode) {
                File file = (File) ((MyTreeNode) oneChild).getUserObject();

                if (file.equals(selectedSubDir)) {
                    selectedTreeNode = (MyTreeNode) oneChild;
                    break;
                }
            }
        }

        TreePath newPath = new TreePath(selectedTreeNode.getPath());

        if (jTreeDisk.isExpanded(newPath)) {
            // if the new path is already expanded, just select it.
            jTreeDisk.setSelectionPath(newPath);
            jTreeDisk.scrollPathToVisible(newPath);
        } else {
            jTreeDisk.expandPath(newPath);
        }
    }

    /**
     * Expands the tree to the given path. 
     */
    private void expandPaths(JTree tree, List paths) {
        Iterator iter = paths.iterator();

        if (!iter.hasNext()) {
            return;
        }

        MyTreeNode parentNode = (MyTreeNode) tree.getModel().getRoot();

        if (!parentNode.isExplored()) {
            parentNode.explore();
        }

        // ===
        // For Windows "My Computer" node only.
        // ===
        // Ignore the root node "My Computer", since the path for this node
        // is not in the path list of the expanded node.
        File parentFile = (File) ((MyTreeNode) parentNode).getUserObject();

        if (parentFile.equals(new File(FileExplorer.MY_COMPUTER_FOLDER_PATH))) {
            int count = jTreeDisk.getModel().getChildCount(parentNode);
            boolean pathNotFound = true;

            for (int i = 0; i < count; i++) {
                Object oneChild = jTreeDisk.getModel().getChild(parentNode, i);
                String onePath = ((MyTreeNode) oneChild).toString();

                if (onePath.equalsIgnoreCase((String) iter.next())) {
                    parentNode = (MyTreeNode) oneChild;
                    pathNotFound = false;
                    break;
                }
            }
        } else {
            if (!parentFile.equals((String) iter.next())) {
                return;
            }
        }

        boolean pathNotFound = false;

        while (iter.hasNext() && !pathNotFound) {
            if (!parentNode.isExplored()) {
                parentNode.explore();
            }

            String nextPath = (String) iter.next();

            pathNotFound = true;
            int count = jTreeDisk.getModel().getChildCount(parentNode);

            for (int i = 0; i < count; i++) {
                Object oneChild = jTreeDisk.getModel().getChild(parentNode, i);
                String onePath = ((MyTreeNode) oneChild).toString();

                if (onePath.equalsIgnoreCase(nextPath)) {
                    parentNode = (MyTreeNode) oneChild;
                    pathNotFound = false;
                    break;
                }
            }
        }

        if (pathNotFound) {
            return;
        } else {
            selectedTreeNode = parentNode;
            TreePath newPath = new TreePath(selectedTreeNode.getPath());

            if (jTreeDisk.isExpanded(newPath)) {
                // if the new path is already expanded, just select it.
                jTreeDisk.setSelectionPath(newPath);
                jTreeDisk.scrollPathToVisible(newPath);
            } else {
                jTreeDisk.expandPath(newPath);
            }
        }
    }

    /**
     * Explores the specified directory by expanding the right tree to it path,
     * and display it's subdirectories and files in the right table.
     */
    private void exploreDirectory(File selectedDir) {
        // First parse the given directory path into separate path names/fields.
        List paths = new ArrayList();
        String selectedAbsPath = selectedDir.getAbsolutePath();
        int beginIndex = 0;
        int endIndex = selectedAbsPath.indexOf(File.separator);

        // For the first path name, attach the path separator.
        // For Windows, it should be like 'C:\', for Unix, it should be like '/'.
        paths.add(selectedAbsPath.substring(beginIndex, endIndex + 1));
        beginIndex = endIndex + 1;
        endIndex = selectedAbsPath.indexOf(File.separator, beginIndex);
        while (endIndex != -1) {
            // For other path names, do not attach the path separator.
            paths.add(selectedAbsPath.substring(beginIndex, endIndex));
            beginIndex = endIndex + 1;
            endIndex = selectedAbsPath.indexOf(File.separator, beginIndex);
        }
        String lastPath = selectedAbsPath.substring(beginIndex,
                selectedAbsPath.length());

        if ((lastPath != null) && (lastPath.length() != 0)) {
            paths.add(lastPath);
        }

        expandPaths(jTreeDisk, paths);
    }

    /**
     * Updates the status info including the address text field and status bar.
     *
     * It should be called in below cases:
     *   the selected node in the left tree changes;
     *   click on one item in the right file table;
     *   the current status changes from STATUS_FILEEXPLORER to "WebBrowse".
     */
    private void updateStatusInfo() {
        File selectedDir = (File) selectedTreeNode.getUserObject();

        if (selectedDir.equals(new File(FileExplorer.MY_COMPUTER_FOLDER_PATH))) {
            // ===
            // For Windows "My Computer" node only.
            // ===
            if (selectedFile == null) {
                jAddressTextField.setText("");
                statusBar.lblObject.setText(selectedTreeNode.getChildrenCount()
                        + " object(s)");
            } else {
                jAddressTextField.setText(selectedFile.getPath());
                statusBar.lblObject.setText("1 object(s) selected");
            }

            statusBar.lblSize.setText("0 Bytes");
        } else {
            if (selectedFile == null) {
                jAddressTextField.setText(selectedDir.getPath());

                statusBar.lblObject.setText(selectedTreeNode.getChildrenCount()
                        + " object(s)");
                statusBar.lblSize.setText(MyUtility.length2KB(selectedTreeNode.getSize()));
            } else {
                jAddressTextField.setText(selectedFile.getPath());

                statusBar.lblObject.setText("1 object(s) selected");
                statusBar.lblSize.setText(MyUtility.length2KB(selectedFile.length()));
            }
        }
    }

    void jTable_maybePopUpMenu(MouseEvent e) {
        if (e.isPopupTrigger() == false || selectedFile == null) {
            return;
        }

        if (!selectedFile.isDirectory()) {
            // For a selected file, all the menu items are visible.
            // Check the availability of the menu items.
            jDesktopPopupMenu.removeAll();
            jDesktopPopupMenu.add(jMenuItemOpen);
            jDesktopPopupMenu.add(jMenuItemEdit);
            jDesktopPopupMenu.add(jMenuItemPrint);
            jDesktopPopupMenu.addSeparator();
            jDesktopPopupMenu.add(jMenuItemBrowse);
            jDesktopPopupMenu.addSeparator();
            jDesktopPopupMenu.add(jMenuItemMail);

            if (Desktop.isEditable(selectedFile)) {
                jMenuItemEdit.setEnabled(true);
            } else {
                jMenuItemEdit.setEnabled(false);
            }

            if (Desktop.isPrintable(selectedFile)) {
                jMenuItemPrint.setEnabled(true);
            } else {
                jMenuItemPrint.setEnabled(false);
            }

            jDesktopPopupMenu.show((Component) jTable, e.getX(), e.getY());
        } else {
            // For a selected directory, only "Open", "Browse" and "Browser in New
            // Window" items are visible.
            jDesktopPopupMenu.removeAll();
            jDesktopPopupMenu.add(jMenuItemOpen);
            jDesktopPopupMenu.addSeparator();
            jDesktopPopupMenu.add(jMenuItemBrowse);

            jDesktopPopupMenu.show((Component) jTable, e.getX(), e.getY());
        }
    }

    void jTable_mouseClicked(MouseEvent e) {
        int curRow = jTable.rowAtPoint(new Point(e.getX(), e.getY()));

        if (curRow == -1) {
            selectedFile = null;
            updateStatusInfo();
            jTable.clearSelection();
            return;
        }

        if (SwingUtilities.isRightMouseButton(e)) {
            // Enable right click selection. Left click selection is auto-enabled,
            // and doesn't need to be enabled specifically.
            curRow = jTable.rowAtPoint(new Point(e.getX(), e.getY()));
            jTable.clearSelection();
            jTable.addRowSelectionInterval(curRow, curRow);
        }

        ListSelectionModel lsm = jTable.getSelectionModel();
        int selectedRow = lsm.getMinSelectionIndex();

        DiskObject selectedObject = (DiskObject) tableModel.getValueAt(selectedRow,
                0);
        File selectedDir = (File) selectedTreeNode.getUserObject();

        if (selectedDir.equals(new File(FileExplorer.MY_COMPUTER_FOLDER_PATH))) {
            // ===
            // For Windows "My Computer" node only.
            // ===
            selectedFile = new File(selectedObject.name);
        } else {
            selectedFile = new File(selectedDir.toString() + File.separator
                    + selectedObject.name);
        }

        // Update the address text field and status bar.
        updateStatusInfo();

        if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
            // For a left and double click, open the file or directory.
            // Notice!!!. Below code is duplicate of
            // void jMenuItemOpen_actionPerformed(ActionEvent e) {}
            // Popup the context menu.
            if (!selectedFile.isDirectory()) {
                try {
                    Desktop.open(selectedFile);
                } catch (DesktopException de) {
                    JOptionPane.showMessageDialog(this, de.getMessage(), "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } else {
                // The selection is a directory, open it.
                exploreDirectory(selectedTreeNode, selectedFile);
            }
        }
    }

    void jMenuItemOpen_actionPerformed(ActionEvent e) {
        if (!selectedFile.isDirectory()) {
            try {
                Desktop.open(selectedFile);
            } catch (DesktopException de) {
                JOptionPane.showMessageDialog(this, de.getMessage(), "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            // The selection is a directory, open it.
            exploreDirectory(selectedTreeNode, selectedFile);
        }
    }

    void jMenuItemEdit_actionPerformed(ActionEvent e) {
        try {
            Desktop.edit(selectedFile);
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.getMessage(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void jMenuItemPrint_actionPerformed(ActionEvent e) {
        try {
            Desktop.print(selectedFile);
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.getMessage(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void jMenuItemBrowse_actionPerformed(ActionEvent e) {
        URL fileURL = null;

        try {
            fileURL = selectedFile.toURL();
        } catch (MalformedURLException mue) {
            JOptionPane.showMessageDialog(this, mue.getMessage(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        }

        try {
            Desktop.browse(fileURL);
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.getMessage(), "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void jMenuItemMail_actionPerformed(ActionEvent e) {
        Message msg = new Message();

⌨️ 快捷键说明

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