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

📄 awtfileselector.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if(useAcceptAllFilter && !filters.contains(acceptAllFilter)) {
            filters.insertElementAt(acceptAllFilter, 0);
        }
        else if(!useAcceptAllFilter && filters.contains(acceptAllFilter)) {
            filters.removeElement(acceptAllFilter);
        }
        rebuildFilterSelect();
    }
    
    public void addFileFilter(FileFilter filter) {
        filters.addElement(filter);
        rebuildFilterSelect();
    }
    
    private void rebuildFilterSelect() {
        filterSelect.removeAll();
        for(Enumeration e = filters.elements(); e.hasMoreElements(); ) {
            FileFilter f = (FileFilter)e.nextElement();
            filterSelect.add(f.getDescription());
        }
    }

    private void rebuildLookIn() {
        File dir = cwd;
        String lastParentPath = null;
        lookIn.removeAll();
        System.out.println("Rebuilding look in menu, staring at " + cwd);
        while (dir != null && dir.exists()) {            
            lookIn.add(dir.getAbsolutePath());
            String parentPath = dir.getParent();
            System.out.println("Parent = " + parentPath);
            dir = parentPath == null ? null : new File(parentPath);
        }
    }
    
    private void gotoParent() {

        String newPath = cwd.getAbsolutePath();
        System.out.println("Going to parent - current path is " + newPath);
        if (newPath.endsWith(File.separator)) {
            System.out.println("Stripping trailing /");
            newPath = newPath.substring(0, newPath.length() - 1);
        }
        int idx = newPath.lastIndexOf(File.separator);
        if (idx != -1) {
            newPath = newPath.substring(0, idx + 1);
            System.out.println("Found /, new path is " + newPath);
            cwd = new File(newPath);
            path.setText("");
            refresh();
        }
        else {
            System.out.println("No /s");
        }
    }

    private void selectFile() {
        if (path.getText().equals("..")) {
            gotoParent();
        } else {
            File f = new File(path.getText());
            if (!f.isAbsolute())
                f = new File(cwd, path.getText());
            if (f.exists()) {
                if (f.isFile()) {
                    cwd = new File(f.getParent());
                    path.setText(f.getName());
                } else {
                    cwd = f;
                    path.setText("");
                }
                refresh();
            } else {
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }

    public File[] getSelectedFiles() {
        int[] sel = files.getSelectedIndexes();
        File[] f = new File[sel.length];
        for (int i = 0; i < sel.length; i++) {
            f[i] = new File(cwd, files.getItem(sel[i]).toString());
        }
        return f;
    }

    public File getSelectedFile() {
        File f = new File(path.getText());
        if (f.isAbsolute())
            if (type == 1 && !f.isDirectory())
                return cwd;
            else
                return f;
        if (type == 1 && !f.isDirectory())
            return cwd;
        else
            return new File(cwd, path.getText());
    }

    public void refresh() {
        String l[] = cwd.list();
        rebuildLookIn();
        files.removeAll();
        Vector v = new Vector();
        if (cwd.getParent() != null)
            files.add("..");
        for (int i = 0; l != null && i < l.length; i++) {
            if (showHiddenFiles == null || showHiddenFiles.getState() && l[i].startsWith(".") || !l[i].startsWith(".")) {
                if(filters.size() == 0) {
                    v.addElement(l[i]);
                }
                else {
                    for(Enumeration e = filters.elements(); e.hasMoreElements(); ) {
                        FileFilter filter = (FileFilter)e.nextElement();
                        if(filter.getDescription().equals(filterSelect.getSelectedItem())) {
                            File f = new File(cwd, l[i]);
                            if(f.isDirectory() || filter.accept(f)) {
                                v.addElement(l[i]);
                                break;
                            }                            
                        }
                    }
                }
            }
        }
        Util.sort(v, StringComparator.getDefaultInstance());
        for (Enumeration e = v.elements(); e.hasMoreElements();) {
            files.addItem(e.nextElement().toString());
        }
        files.deselect(files.getSelectedIndex());
        doLayout();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == home) {
            cwd = new File(System.getProperty("user.home"));
            refresh();
        }
        else if (e.getSource() == parent) {
            gotoParent();
        }
        else if (e.getSource() == remove) {
            File f = getSelectedFile();
            Option choice = OptionDialog.prompt(this, OptionDialog.WARNING, "Confirm remove", "Are you sure you want to delete\n"
                            + f.getPath() + "?", OptionDialog.CHOICES_YES_NO);
            if (choice == OptionDialog.CHOICE_YES)
                if (!f.delete()) {
                    OptionDialog.error(this, "Error", "Failed to remove file.");
                } else {
                    refresh();
                    path.setText("");
                }
        } else if (e.getSource() == newFolder)
            newFolder();
        else
            selectFile();
    }

    private void newFolder() {
        String name = OptionDialog.promptForText(this, "New folder", "", null, ' ', "Name");
        if (name != null) {
            File f = new File(cwd, name);
            try {
                if (!f.mkdir())
                    throw new IOException("Could not create directory. ");
                refresh();
            } catch (IOException ioe) {
                OptionDialog.error(this, "Error", ioe.getMessage());
            }
        }
    }

    public void setAllowMultipleSelection(boolean allowMultipleSelection) {
        files.setMultipleMode(allowMultipleSelection);
    }

    public void itemStateChanged(ItemEvent e) {
        String sel = files.getSelectedItem();
        if (sel != null && sel.equals("..")) {
            String parent = getWorkingDirectory().getParent();
            path.setText(parent);
        }
        path.setText(sel != null ? sel : "");
    }

    public Option showDialog(Component parent, String title) {
        Option choice = OptionDialog.prompt(parent, OptionDialog.UNCATEGORISED, title, this, OptionDialog.CHOICES_OK_CANCEL, null,  showHiddenFiles);
        return choice;
    }

    public File getWorkingDirectory() {
        return cwd;
    }

    /**
     * @param filter
     */
    public void setSelectedFileFilter(FileFilter filter) {
        int idx = filters.indexOf(filter);
        if(idx != -1) {
            filterSelect.select(idx);
        }
    }

    /**
     * @param cwd2
     */
    public void setWorkingDirectory(File cwd) {
        this.cwd = cwd;
        refresh();        
    }

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

⌨️ 快捷键说明

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