2012 lines · 73.4 KB · Java
RawDownload
            this.tf = tf;            tf.setName("Table.editor");            tf.addFocusListener(editorFocusListener);        }         public Component getTableCellEditorComponent(JTable table, Object value,                                                     boolean isSelected, int row, int column) {            Component comp = super.getTableCellEditorComponent(table, value,                    isSelected, row, column);            if (value instanceof File) {                tf.setText(getFileChooser().getName((File) value));                tf.selectAll();            }            return comp;        }    }      class DetailsTableCellRenderer extends DefaultTableCellRenderer {        JFileChooser chooser;        DateFormat df;         DetailsTableCellRenderer(JFileChooser chooser) {            this.chooser = chooser;            df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,                                                chooser.getLocale());        }         public void setBounds(int x, int y, int width, int height) {        if (getHorizontalAlignment() == SwingConstants.LEADING &&                    !fullRowSelection) {                // Restrict width to actual text                width = Math.min(width, this.getPreferredSize().width+4);            } else {                x -= 4;            }            super.setBounds(x, y, width, height);        }          public Insets getInsets(Insets i) {            // Provide some space between columns            i = super.getInsets(i);            i.left  += 4;            i.right += 4;            return i;        }         public Component getTableCellRendererComponent(JTable table, Object value,                              boolean isSelected, boolean hasFocus, int row, int column) {             if ((table.convertColumnIndexToModel(column) != COLUMN_FILENAME ||                    (listViewWindowsStyle && !table.isFocusOwner())) &&                    !fullRowSelection) {                isSelected = false;            }             super.getTableCellRendererComponent(table, value, isSelected,                                                       hasFocus, row, column);             setIcon(null);             int modelColumn = table.convertColumnIndexToModel(column);            ShellFolderColumnInfo columnInfo = detailsTableModel.getColumns()[modelColumn];             Integer alignment = columnInfo.getAlignment();            if (alignment == null) {                alignment = (value instanceof Number)                        ? SwingConstants.RIGHT                        : SwingConstants.LEADING;            }             setHorizontalAlignment(alignment);             // formatting cell text            // TODO: it's rather a temporary trick, to be revised            String text;             if (value == null) {                text = "";             } else if (value instanceof File) {                File file = (File)value;                text = chooser.getName(file);                Icon icon = chooser.getIcon(file);                setIcon(icon);             } else if (value instanceof Long) {                long len = ((Long) value) / 1024L;                if (listViewWindowsStyle) {                    text = MessageFormat.format(kiloByteString, len + 1);                } else if (len < 1024L) {                    text = MessageFormat.format(kiloByteString, (len == 0L) ? 1L : len);                } else {                    len /= 1024L;                    if (len < 1024L) {                        text = MessageFormat.format(megaByteString, len);                    } else {                        len /= 1024L;                        text = MessageFormat.format(gigaByteString, len);                    }                }             } else if (value instanceof Date) {                text = df.format((Date)value);             } else {                text = value.toString();            }             setText(text);             return this;        }    }     public JPanel createDetailsView() {        final JFileChooser chooser = getFileChooser();         JPanel p = new JPanel(new BorderLayout());         final JTable detailsTable = new JTable(getDetailsTableModel()) {            // Handle Escape key events here            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {                if (e.getKeyCode() == KeyEvent.VK_ESCAPE && getCellEditor() == null) {                    // We are not editing, forward to filechooser.                    chooser.dispatchEvent(e);                    return true;                }                return super.processKeyBinding(ks, e, condition, pressed);            }             public void tableChanged(TableModelEvent e) {                super.tableChanged(e);                 if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {                    // update header with possibly changed column set                    updateDetailsColumnModel(this);                }            }        };         detailsTable.setRowSorter(getRowSorter());        detailsTable.setAutoCreateColumnsFromModel(false);        detailsTable.setComponentOrientation(chooser.getComponentOrientation());        detailsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);        detailsTable.setShowGrid(false);        detailsTable.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);        detailsTable.addKeyListener(detailsKeyListener);         Font font = list.getFont();        detailsTable.setFont(font);        detailsTable.setIntercellSpacing(new Dimension(0, 0));         TableCellRenderer headerRenderer =                new AlignableTableHeaderRenderer(detailsTable.getTableHeader().getDefaultRenderer());        detailsTable.getTableHeader().setDefaultRenderer(headerRenderer);        TableCellRenderer cellRenderer = new DetailsTableCellRenderer(chooser);        detailsTable.setDefaultRenderer(Object.class, cellRenderer);         // So that drag can be started on a mouse press        detailsTable.getColumnModel().getSelectionModel().            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);         detailsTable.addMouseListener(getMouseHandler());        // No need to addListSelectionListener because selections are forwarded        // to our JList.         // 4835633 : tell BasicTableUI that this is a file list        detailsTable.putClientProperty("Table.isFileList", Boolean.TRUE);         if (listViewWindowsStyle) {            detailsTable.addFocusListener(repaintListener);        }         // TAB/SHIFT-TAB should transfer focus and ENTER should select an item.        // We don't want them to navigate within the table        ActionMap am = SwingUtilities.getUIActionMap(detailsTable);        am.remove("selectNextRowCell");        am.remove("selectPreviousRowCell");        am.remove("selectNextColumnCell");        am.remove("selectPreviousColumnCell");        detailsTable.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,                     null);        detailsTable.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,                     null);         JScrollPane scrollpane = new JScrollPane(detailsTable);        scrollpane.setComponentOrientation(chooser.getComponentOrientation());        LookAndFeel.installColors(scrollpane.getViewport(), "Table.background", "Table.foreground");         // Adjust width of first column so the table fills the viewport when        // first displayed (temporary listener).        scrollpane.addComponentListener(new ComponentAdapter() {            public void componentResized(ComponentEvent e) {                JScrollPane sp = (JScrollPane)e.getComponent();                fixNameColumnWidth(sp.getViewport().getSize().width);                sp.removeComponentListener(this);            }        });         // 4835633.        // If the mouse is pressed in the area below the Details view table, the        // event is not dispatched to the Table MouseListener but to the        // scrollpane.  Listen for that here so we can clear the selection.        scrollpane.addMouseListener(new MouseAdapter() {            public void mousePressed(MouseEvent e) {                JScrollPane jsp = ((JScrollPane)e.getComponent());                JTable table = (JTable)jsp.getViewport().getView();                 if (!e.isShiftDown() || table.getSelectionModel().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) {                    clearSelection();                    TableCellEditor tce = table.getCellEditor();                    if (tce != null) {                        tce.stopCellEditing();                    }                }            }        });         detailsTable.setForeground(list.getForeground());        detailsTable.setBackground(list.getBackground());         if (listViewBorder != null) {            scrollpane.setBorder(listViewBorder);        }        p.add(scrollpane, BorderLayout.CENTER);         detailsTableModel.fireTableStructureChanged();         return p;    } // createDetailsView     private class AlignableTableHeaderRenderer implements TableCellRenderer {        TableCellRenderer wrappedRenderer;         public AlignableTableHeaderRenderer(TableCellRenderer wrappedRenderer) {            this.wrappedRenderer = wrappedRenderer;        }         public Component getTableCellRendererComponent(                                JTable table, Object value, boolean isSelected,                                boolean hasFocus, int row, int column) {             Component c = wrappedRenderer.getTableCellRendererComponent(                                table, value, isSelected, hasFocus, row, column);             int modelColumn = table.convertColumnIndexToModel(column);            ShellFolderColumnInfo columnInfo = detailsTableModel.getColumns()[modelColumn];             Integer alignment = columnInfo.getAlignment();            if (alignment == null) {                alignment = SwingConstants.CENTER;            }            if (c instanceof JLabel) {                ((JLabel) c).setHorizontalAlignment(alignment);            }             return c;        }    }     private void fixNameColumnWidth(int viewWidth) {        TableColumn nameCol = detailsTable.getColumnModel().getColumn(COLUMN_FILENAME);        int tableWidth = detailsTable.getPreferredSize().width;         if (tableWidth < viewWidth) {            nameCol.setPreferredWidth(nameCol.getPreferredWidth() + viewWidth - tableWidth);        }    }     private class DelayedSelectionUpdater implements Runnable {        File editFile;         DelayedSelectionUpdater() {            this(null);        }         DelayedSelectionUpdater(File editFile) {            this.editFile = editFile;            if (isShowing()) {                SwingUtilities.invokeLater(this);            }        }         public void run() {            setFileSelected();            if (editFile != null) {                editFileName(getRowSorter().convertRowIndexToView(                        getModel().indexOf(editFile)));                editFile = null;            }        }    }      /**     * Creates a selection listener for the list of files and directories.     *     * @return a <code>ListSelectionListener</code>     */    public ListSelectionListener createListSelectionListener() {        return fileChooserUIAccessor.createListSelectionListener();    }     int lastIndex = -1;    File editFile = null;     private int getEditIndex() {        return lastIndex;    }     private void setEditIndex(int i) {        lastIndex = i;    }     private void resetEditIndex() {        lastIndex = -1;    }     private void cancelEdit() {        if (editFile != null) {            editFile = null;            list.remove(editCell);            repaint();        } else if (detailsTable != null && detailsTable.isEditing()) {            detailsTable.getCellEditor().cancelCellEditing();        }    }     JTextField editCell = null;     /**     * @param index visual index of the file to be edited     */    private void editFileName(int index) {        JFileChooser chooser = getFileChooser();        File currentDirectory = chooser.getCurrentDirectory();         if (readOnly || !canWrite(currentDirectory)) {            return;        }         ensureIndexIsVisible(index);        switch (viewType) {          case VIEWTYPE_LIST:            editFile = (File)getModel().getElementAt(getRowSorter().convertRowIndexToModel(index));            Rectangle r = list.getCellBounds(index, index);            if (editCell == null) {                editCell = new JTextField();                editCell.setName("Tree.cellEditor");                editCell.addActionListener(new EditActionListener());                editCell.addFocusListener(editorFocusListener);                editCell.setNextFocusableComponent(list);            }            list.add(editCell);            editCell.setText(chooser.getName(editFile));            ComponentOrientation orientation = list.getComponentOrientation();            editCell.setComponentOrientation(orientation);             Icon icon = chooser.getIcon(editFile);             // PENDING - grab padding (4) below from defaults table.            int editX = icon == null ? 20 : icon.getIconWidth() + 4;             if (orientation.isLeftToRight()) {                editCell.setBounds(editX + r.x, r.y, r.width - editX, r.height);            } else {                editCell.setBounds(r.x, r.y, r.width - editX, r.height);            }            editCell.requestFocus();            editCell.selectAll();            break;           case VIEWTYPE_DETAILS:            detailsTable.editCellAt(index, COLUMN_FILENAME);            break;        }    }      class EditActionListener implements ActionListener {        public void actionPerformed(ActionEvent e) {            applyEdit();        }    }     private void applyEdit() {        if (editFile != null && editFile.exists()) {            JFileChooser chooser = getFileChooser();            String oldDisplayName = chooser.getName(editFile);            String oldFileName = editFile.getName();            String newDisplayName = editCell.getText().trim();            String newFileName;             if (!newDisplayName.equals(oldDisplayName)) {                newFileName = newDisplayName;                //Check if extension is hidden from user                int i1 = oldFileName.length();                int i2 = oldDisplayName.length();                if (i1 > i2 && oldFileName.charAt(i2) == '.') {                    newFileName = newDisplayName + oldFileName.substring(i2);                }                 // rename                FileSystemView fsv = chooser.getFileSystemView();                File f2 = fsv.createFileObject(editFile.getParentFile(), newFileName);                if (f2.exists()) {                    JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorFileExistsText, oldFileName),                            renameErrorTitleText, JOptionPane.ERROR_MESSAGE);                } else {                    if (getModel().renameFile(editFile, f2)) {                        if (fsv.isParent(chooser.getCurrentDirectory(), f2)) {                            if (chooser.isMultiSelectionEnabled()) {                                chooser.setSelectedFiles(new File[]{f2});                            } else {                                chooser.setSelectedFile(f2);                            }                        } else {                            //Could be because of delay in updating Desktop folder                            //chooser.setSelectedFile(null);                        }                    } else {                        JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorText, oldFileName),                                renameErrorTitleText, JOptionPane.ERROR_MESSAGE);                    }                }            }        }        if (detailsTable != null && detailsTable.isEditing()) {            detailsTable.getCellEditor().stopCellEditing();        }        cancelEdit();    }     protected Action newFolderAction;     public Action getNewFolderAction() {        if (!readOnly && newFolderAction == null) {            newFolderAction = new AbstractAction(newFolderActionLabelText) {                private Action basicNewFolderAction;                 // Initializer                {                    putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_NEW_FOLDER);                     File currentDirectory = getFileChooser().getCurrentDirectory();                    if (currentDirectory != null) {                        setEnabled(canWrite(currentDirectory));                    }                }                 public void actionPerformed(ActionEvent ev) {                    if (basicNewFolderAction == null) {                        basicNewFolderAction = fileChooserUIAccessor.getNewFolderAction();                    }                    JFileChooser fc = getFileChooser();                    File oldFile = fc.getSelectedFile();                    basicNewFolderAction.actionPerformed(ev);                    File newFile = fc.getSelectedFile();                    if (newFile != null && !newFile.equals(oldFile) && newFile.isDirectory()) {                        newFolderFile = newFile;                    }                }            };        }        return newFolderAction;    }     protected class FileRenderer extends DefaultListCellRenderer  {         public Component getListCellRendererComponent(JList list, Object value,                                                      int index, boolean isSelected,                                                      boolean cellHasFocus) {             if (listViewWindowsStyle && !list.isFocusOwner()) {                isSelected = false;            }             super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);            File file = (File) value;            String fileName = getFileChooser().getName(file);            setText(fileName);            setFont(list.getFont());             Icon icon = getFileChooser().getIcon(file);            if (icon != null) {                setIcon(icon);            } else {                if (getFileChooser().getFileSystemView().isTraversable(file)) {                    setText(fileName+File.separator);                }            }             return this;        }    }      void setFileSelected() {

Lines 1001–1500 of 2012. Previous lines