2012 lines · 73.4 KB · Java
RawDownload
        if (getFileChooser().isMultiSelectionEnabled() && !isDirectorySelected()) {            File[] files = getFileChooser().getSelectedFiles(); // Should be selected            Object[] selectedObjects = list.getSelectedValues(); // Are actually selected             listSelectionModel.setValueIsAdjusting(true);            try {                int lead = listSelectionModel.getLeadSelectionIndex();                int anchor = listSelectionModel.getAnchorSelectionIndex();                 Arrays.sort(files);                Arrays.sort(selectedObjects);                 int shouldIndex = 0;                int actuallyIndex = 0;                 // Remove files that shouldn't be selected and add files which should be selected                // Note: Assume files are already sorted in compareTo order.                while (shouldIndex < files.length &&                       actuallyIndex < selectedObjects.length) {                    int comparison = files[shouldIndex].compareTo((File)selectedObjects[actuallyIndex]);                    if (comparison < 0) {                        doSelectFile(files[shouldIndex++]);                    } else if (comparison > 0) {                        doDeselectFile(selectedObjects[actuallyIndex++]);                    } else {                        // Do nothing                        shouldIndex++;                        actuallyIndex++;                    }                 }                 while (shouldIndex < files.length) {                    doSelectFile(files[shouldIndex++]);                }                 while (actuallyIndex < selectedObjects.length) {                    doDeselectFile(selectedObjects[actuallyIndex++]);                }                 // restore the anchor and lead                if (listSelectionModel instanceof DefaultListSelectionModel) {                    ((DefaultListSelectionModel)listSelectionModel).                        moveLeadSelectionIndex(lead);                    listSelectionModel.setAnchorSelectionIndex(anchor);                }            } finally {                listSelectionModel.setValueIsAdjusting(false);            }        } else {            JFileChooser chooser = getFileChooser();            File f;            if (isDirectorySelected()) {                f = getDirectory();            } else {                f = chooser.getSelectedFile();            }            int i;            if (f != null && (i = getModel().indexOf(f)) >= 0) {                int viewIndex = getRowSorter().convertRowIndexToView(i);                listSelectionModel.setSelectionInterval(viewIndex, viewIndex);                ensureIndexIsVisible(viewIndex);            } else {                clearSelection();            }        }    }     private void doSelectFile(File fileToSelect) {        int index = getModel().indexOf(fileToSelect);        // could be missed in the current directory if it changed        if (index >= 0) {            index = getRowSorter().convertRowIndexToView(index);            listSelectionModel.addSelectionInterval(index, index);        }    }     private void doDeselectFile(Object fileToDeselect) {        int index = getRowSorter().convertRowIndexToView(                                getModel().indexOf(fileToDeselect));        listSelectionModel.removeSelectionInterval(index, index);    }     /* The following methods are used by the PropertyChange Listener */     private void doSelectedFileChanged(PropertyChangeEvent e) {        applyEdit();        File f = (File) e.getNewValue();        JFileChooser fc = getFileChooser();        if (f != null            && ((fc.isFileSelectionEnabled() && !f.isDirectory())                || (f.isDirectory() && fc.isDirectorySelectionEnabled()))) {             setFileSelected();        }    }     private void doSelectedFilesChanged(PropertyChangeEvent e) {        applyEdit();        File[] files = (File[]) e.getNewValue();        JFileChooser fc = getFileChooser();        if (files != null            && files.length > 0            && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) {            setFileSelected();        }    }     private void doDirectoryChanged(PropertyChangeEvent e) {        getDetailsTableModel().updateColumnInfo();         JFileChooser fc = getFileChooser();        FileSystemView fsv = fc.getFileSystemView();         applyEdit();        resetEditIndex();        ensureIndexIsVisible(0);        File currentDirectory = fc.getCurrentDirectory();        if (currentDirectory != null) {            if (!readOnly) {                getNewFolderAction().setEnabled(canWrite(currentDirectory));            }            fileChooserUIAccessor.getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory));        }        if (list != null) {            list.clearSelection();        }    }     private void doFilterChanged(PropertyChangeEvent e) {        applyEdit();        resetEditIndex();        clearSelection();    }     private void doFileSelectionModeChanged(PropertyChangeEvent e) {        applyEdit();        resetEditIndex();        clearSelection();    }     private void doMultiSelectionChanged(PropertyChangeEvent e) {        if (getFileChooser().isMultiSelectionEnabled()) {            listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);        } else {            listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);            clearSelection();            getFileChooser().setSelectedFiles(null);        }    }     /*     * Listen for filechooser property changes, such as     * the selected file changing, or the type of the dialog changing.     */    public void propertyChange(PropertyChangeEvent e) {            if (viewType == -1) {                setViewType(VIEWTYPE_LIST);            }         String s = e.getPropertyName();        if (s.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {            doSelectedFileChanged(e);        } else if (s.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {            doSelectedFilesChanged(e);        } else if (s.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {            doDirectoryChanged(e);        } else if (s.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) {            doFilterChanged(e);        } else if (s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {            doFileSelectionModeChanged(e);        } else if (s.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) {            doMultiSelectionChanged(e);        } else if (s.equals(JFileChooser.CANCEL_SELECTION)) {            applyEdit();        } else if (s.equals("busy")) {            setCursor((Boolean)e.getNewValue() ? waitCursor : null);        } else if (s.equals("componentOrientation")) {            ComponentOrientation o = (ComponentOrientation)e.getNewValue();            JFileChooser cc = (JFileChooser)e.getSource();            if (o != e.getOldValue()) {                cc.applyComponentOrientation(o);            }            if (detailsTable != null) {                detailsTable.setComponentOrientation(o);                detailsTable.getParent().getParent().setComponentOrientation(o);            }        }    }     private void ensureIndexIsVisible(int i) {        if (i >= 0) {            if (list != null) {                list.ensureIndexIsVisible(i);            }            if (detailsTable != null) {                detailsTable.scrollRectToVisible(detailsTable.getCellRect(i, COLUMN_FILENAME, true));            }        }    }     public void ensureFileIsVisible(JFileChooser fc, File f) {        int modelIndex = getModel().indexOf(f);        if (modelIndex >= 0) {            ensureIndexIsVisible(getRowSorter().convertRowIndexToView(modelIndex));        }    }     public void rescanCurrentDirectory() {        getModel().validateFileCache();    }     public void clearSelection() {        if (listSelectionModel != null) {            listSelectionModel.clearSelection();            if (listSelectionModel instanceof DefaultListSelectionModel) {                ((DefaultListSelectionModel)listSelectionModel).moveLeadSelectionIndex(0);                listSelectionModel.setAnchorSelectionIndex(0);            }        }    }     public JMenu getViewMenu() {        if (viewMenu == null) {            viewMenu = new JMenu(viewMenuLabelText);            ButtonGroup viewButtonGroup = new ButtonGroup();             for (int i = 0; i < VIEWTYPE_COUNT; i++) {                JRadioButtonMenuItem mi =                    new JRadioButtonMenuItem(new ViewTypeAction(i));                viewButtonGroup.add(mi);                viewMenu.add(mi);            }            updateViewMenu();        }        return viewMenu;    }     private void updateViewMenu() {        if (viewMenu != null) {            Component[] comps = viewMenu.getMenuComponents();            for (Component comp : comps) {                if (comp instanceof JRadioButtonMenuItem) {                    JRadioButtonMenuItem mi = (JRadioButtonMenuItem) comp;                    if (((ViewTypeAction)mi.getAction()).viewType == viewType) {                        mi.setSelected(true);                    }                }            }        }    }     public JPopupMenu getComponentPopupMenu() {        JPopupMenu popupMenu = getFileChooser().getComponentPopupMenu();        if (popupMenu != null) {            return popupMenu;        }         JMenu viewMenu = getViewMenu();        if (contextMenu == null) {            contextMenu = new JPopupMenu();            if (viewMenu != null) {                contextMenu.add(viewMenu);                if (listViewWindowsStyle) {                    contextMenu.addSeparator();                }            }            ActionMap actionMap = getActionMap();            Action refreshAction   = actionMap.get(ACTION_REFRESH);            Action newFolderAction = actionMap.get(ACTION_NEW_FOLDER);            if (refreshAction != null) {                contextMenu.add(refreshAction);                if (listViewWindowsStyle && newFolderAction != null) {                    contextMenu.addSeparator();                }            }            if (newFolderAction != null) {                contextMenu.add(newFolderAction);            }        }        if (viewMenu != null) {            viewMenu.getPopupMenu().setInvoker(viewMenu);        }        return contextMenu;    }      private Handler handler;     protected Handler getMouseHandler() {        if (handler == null) {            handler = new Handler();        }        return handler;    }     private class Handler implements MouseListener {        private MouseListener doubleClickListener;         public void mouseClicked(MouseEvent evt) {            JComponent source = (JComponent)evt.getSource();             int index;            if (source instanceof JList) {                index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint());            } else if (source instanceof JTable) {                JTable table = (JTable)source;                Point p = evt.getPoint();                index = table.rowAtPoint(p);                 boolean pointOutsidePrefSize =                        SwingUtilities2.pointOutsidePrefSize(                            table, index, table.columnAtPoint(p), p);                 if (pointOutsidePrefSize && !fullRowSelection) {                    return;                }                 // Translate point from table to list                if (index >= 0 && list != null &&                    listSelectionModel.isSelectedIndex(index)) {                     // Make a new event with the list as source, placing the                    // click in the corresponding list cell.                    Rectangle r = list.getCellBounds(index, index);                    evt = new MouseEvent(list, evt.getID(),                                         evt.getWhen(), evt.getModifiers(),                                         r.x + 1, r.y + r.height/2,                                         evt.getXOnScreen(),                                         evt.getYOnScreen(),                                         evt.getClickCount(), evt.isPopupTrigger(),                                         evt.getButton());                }            } else {                return;            }             if (index >= 0 && SwingUtilities.isLeftMouseButton(evt)) {                JFileChooser fc = getFileChooser();                 // For single click, we handle editing file name                if (evt.getClickCount() == 1 && source instanceof JList) {                    if ((!fc.isMultiSelectionEnabled() || fc.getSelectedFiles().length <= 1)                        && index >= 0 && listSelectionModel.isSelectedIndex(index)                        && getEditIndex() == index && editFile == null) {                         editFileName(index);                    } else {                        if (index >= 0) {                            setEditIndex(index);                        } else {                            resetEditIndex();                        }                    }                } else if (evt.getClickCount() == 2) {                    // on double click (open or drill down one directory) be                    // sure to clear the edit index                    resetEditIndex();                }            }             // Forward event to Basic            if (getDoubleClickListener() != null) {                getDoubleClickListener().mouseClicked(evt);            }        }         public void mouseEntered(MouseEvent evt) {            JComponent source = (JComponent)evt.getSource();            if (source instanceof JTable) {                JTable table = (JTable)evt.getSource();                 TransferHandler th1 = getFileChooser().getTransferHandler();                TransferHandler th2 = table.getTransferHandler();                if (th1 != th2) {                    table.setTransferHandler(th1);                }                 boolean dragEnabled = getFileChooser().getDragEnabled();                if (dragEnabled != table.getDragEnabled()) {                    table.setDragEnabled(dragEnabled);                }            } else if (source instanceof JList) {                // Forward event to Basic                if (getDoubleClickListener() != null) {                    getDoubleClickListener().mouseEntered(evt);                }            }        }         public void mouseExited(MouseEvent evt) {            if (evt.getSource() instanceof JList) {                // Forward event to Basic                if (getDoubleClickListener() != null) {                    getDoubleClickListener().mouseExited(evt);                }            }        }         public void mousePressed(MouseEvent evt) {            if (evt.getSource() instanceof JList) {                // Forward event to Basic                if (getDoubleClickListener() != null) {                    getDoubleClickListener().mousePressed(evt);                }            }        }         public void mouseReleased(MouseEvent evt) {            if (evt.getSource() instanceof JList) {                // Forward event to Basic                if (getDoubleClickListener() != null) {                    getDoubleClickListener().mouseReleased(evt);                }            }        }         private MouseListener getDoubleClickListener() {            // Lazy creation of Basic's listener            if (doubleClickListener == null && list != null) {                doubleClickListener =                    fileChooserUIAccessor.createDoubleClickListener(list);            }            return doubleClickListener;        }    }     /**     * Property to remember whether a directory is currently selected in the UI.     *     * @return <code>true</code> iff a directory is currently selected.     */    protected boolean isDirectorySelected() {        return fileChooserUIAccessor.isDirectorySelected();    }      /**     * Property to remember the directory that is currently selected in the UI.     *     * @return the value of the <code>directory</code> property     * @see javax.swing.plaf.basic.BasicFileChooserUI#setDirectory     */    protected File getDirectory() {        return fileChooserUIAccessor.getDirectory();    }     private Component findChildComponent(Container container, Class cls) {        int n = container.getComponentCount();        for (int i = 0; i < n; i++) {            Component comp = container.getComponent(i);            if (cls.isInstance(comp)) {                return comp;            } else if (comp instanceof Container) {                Component c = findChildComponent((Container)comp, cls);                if (c != null) {                    return c;                }            }        }        return null;    }     public boolean canWrite(File f) {        // Return false for non FileSystem files or if file doesn't exist.        if (!f.exists()) {            return false;        }         if (f instanceof ShellFolder) {            return ((ShellFolder) f).isFileSystem();        } else {            if (usesShellFolder(getFileChooser())) {                try {                    return ShellFolder.getShellFolder(f).isFileSystem();                } catch (FileNotFoundException ex) {                    // File doesn't exist                    return false;                }            } else {                // Ordinary file                return true;            }        }    }     /**     * Returns true if specified FileChooser should use ShellFolder     */    public static boolean usesShellFolder(JFileChooser chooser) {        Boolean prop = (Boolean) chooser.getClientProperty("FileChooser.useShellFolder");         return prop == null ? chooser.getFileSystemView().equals(FileSystemView.getFileSystemView())                : prop.booleanValue();    }     // This interface is used to access methods in the FileChooserUI    // that are not public.    public interface FileChooserUIAccessor {        public JFileChooser getFileChooser();

Lines 1501–2000 of 2012. Previous lines