jdk/src/share/classes/sun/swing/FilePane.java
author alexp
Wed, 02 Sep 2009 17:47:19 +0400
changeset 3749 d0ecbd7075b1
parent 3346 1c65be97eaa2
child 5506 202f599c92aa
permissions -rw-r--r--
6797139: JButton title is truncating for some strings irrespective of preferred size. Reviewed-by: peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3346
1c65be97eaa2 6460525: javax/swing/JFileChooser/6396844/TwentyThousandTest.java times out
rupashka
parents: 2658
diff changeset
     2
 * Copyright 2003-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.text.DateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
2489
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
    36
import java.util.concurrent.Callable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.awt.shell.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <b>WARNING:</b> This class is an implementation detail and is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * public so that it can be used by two packages. You should NOT consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * this public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This component is intended to be used in a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * javax.swing.plaf.basic.BasicFileChooserUI. It realies heavily on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * implementation of BasicFileChooserUI, and is intended to be API compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * with earlier implementations of MetalFileChooserUI and WindowsFileChooserUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Leif Samuelsson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class FilePane extends JPanel implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Constants for actions. These are used for the actions' ACTION_COMMAND_KEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // and as keys in the action maps for FilePane and the corresponding UI classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public final static String ACTION_APPROVE_SELECTION = "approveSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public final static String ACTION_CANCEL            = "cancelSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public final static String ACTION_EDIT_FILE_NAME    = "editFileName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public final static String ACTION_REFRESH           = "refresh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public final static String ACTION_CHANGE_TO_PARENT_DIRECTORY = "Go Up";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public final static String ACTION_NEW_FOLDER        = "New Folder";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public final static String ACTION_VIEW_LIST         = "viewTypeList";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public final static String ACTION_VIEW_DETAILS      = "viewTypeDetails";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Action[] actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // "enums" for setViewType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public  static final int VIEWTYPE_LIST     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public  static final int VIEWTYPE_DETAILS  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int VIEWTYPE_COUNT    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private int viewType = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private JPanel[] viewPanels = new JPanel[VIEWTYPE_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private JPanel currentViewPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private String[] viewTypeActionNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private JPopupMenu contextMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private JMenu viewMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private String viewMenuLabelText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private String refreshActionLabelText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private String newFolderActionLabelText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String kiloByteString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String megaByteString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private String gigaByteString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private String renameErrorTitleText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private String renameErrorText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private String renameErrorFileExistsText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final Cursor waitCursor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final KeyListener detailsKeyListener = new KeyAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        private final long timeFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        private final StringBuilder typedString = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        private long lastTime = 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            Long l = (Long) UIManager.get("Table.timeFactor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            timeFactor = (l != null) ? l : 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * Moves the keyboard focus to the first element whose prefix matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * the sequence of alphanumeric keys pressed by the user with delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * less than value of <code>timeFactor</code>. Subsequent same key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * presses move the keyboard focus to the next object that starts with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * the same letter until another key is pressed, then it is treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * as the prefix with appropriate number of the same letters followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * by first typed another letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            BasicDirectoryModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int rowCount = model.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (detailsTable == null || rowCount == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    e.isAltDown() || e.isControlDown() || e.isMetaDown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            InputMap inputMap = detailsTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            KeyStroke key = KeyStroke.getKeyStrokeForEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (inputMap != null && inputMap.get(key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            int startIndex = detailsTable.getSelectionModel().getLeadSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (startIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (startIndex >= rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                startIndex = rowCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            char c = e.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            long time = e.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (time - lastTime < timeFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (typedString.length() == 1 && typedString.charAt(0) == c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // Subsequent same key presses move the keyboard focus to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // object that starts with the same letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    typedString.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                typedString.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                typedString.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            lastTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (startIndex >= rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // Find next file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int index = getNextMatch(startIndex, rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (index < 0 && startIndex > 0) { // wrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                index = getNextMatch(0, startIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                detailsTable.getSelectionModel().setSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                Rectangle cellRect = detailsTable.getCellRect(index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        detailsTable.convertColumnIndexToView(COLUMN_FILENAME), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                detailsTable.scrollRectToVisible(cellRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        private int getNextMatch(int startIndex, int finishIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            BasicDirectoryModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            JFileChooser fileChooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            DetailsTableRowSorter rowSorter = getRowSorter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            String prefix = typedString.toString().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // Search element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            for (int index = startIndex; index <= finishIndex; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                File file = (File) model.getElementAt(rowSorter.convertRowIndexToModel(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                String fileName = fileChooser.getName(file).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (fileName.startsWith(prefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private FocusListener editorFocusListener = new FocusAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (! e.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static FocusListener repaintListener = new FocusListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public void focusGained(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            repaintSelection(fe.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public void focusLost(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            repaintSelection(fe.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        private void repaintSelection(Object source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (source instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                repaintListSelection((JList)source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } else if (source instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                repaintTableSelection((JTable)source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        private void repaintListSelection(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int[] indices = list.getSelectedIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            for (int i : indices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                Rectangle bounds = list.getCellBounds(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                list.repaint(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        private void repaintTableSelection(JTable table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            int minRow = table.getSelectionModel().getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            int maxRow = table.getSelectionModel().getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (minRow == -1 || maxRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            int col0 = table.convertColumnIndexToView(COLUMN_FILENAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            Rectangle first = table.getCellRect(minRow, col0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            Rectangle last = table.getCellRect(maxRow, col0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Rectangle dirty = first.union(last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            table.repaint(dirty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private boolean smallIconsView = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private Border  listViewBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private Color   listViewBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private boolean listViewWindowsStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private boolean readOnly;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
   267
    private boolean fullRowSelection = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private ListSelectionModel listSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private JList list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private JTable detailsTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private static final int COLUMN_FILENAME = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // Provides a way to recognize a newly created folder, so it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // be selected when it appears in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private File newFolderFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    // Used for accessing methods in the corresponding UI class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private FileChooserUIAccessor fileChooserUIAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private DetailsTableModel detailsTableModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private DetailsTableRowSorter rowSorter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public FilePane(FileChooserUIAccessor fileChooserUIAccessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        super(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this.fileChooserUIAccessor = fileChooserUIAccessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        createActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public void uninstallUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (getModel() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            getModel().removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    protected JFileChooser getFileChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return fileChooserUIAccessor.getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    protected BasicDirectoryModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return fileChooserUIAccessor.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public int getViewType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return viewType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public void setViewType(int viewType) {
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   312
        if (viewType == this.viewType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   315
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   316
        int oldValue = this.viewType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        this.viewType = viewType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   319
        JPanel createdViewPanel = null;
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   320
        Component newFocusOwner = null;
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   321
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        switch (viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
          case VIEWTYPE_LIST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (viewPanels[viewType] == null) {
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   325
                createdViewPanel = fileChooserUIAccessor.createList();
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   326
                if (createdViewPanel == null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   327
                    createdViewPanel = createList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   329
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   330
                list = (JList) findChildComponent(createdViewPanel, JList.class);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   331
                if (listSelectionModel == null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   332
                    listSelectionModel = list.getSelectionModel();
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   333
                    if (detailsTable != null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   334
                        detailsTable.setSelectionModel(listSelectionModel);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   335
                    }
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   336
                } else {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   337
                    list.setSelectionModel(listSelectionModel);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   338
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            list.setLayoutOrientation(JList.VERTICAL_WRAP);
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   341
            newFocusOwner = list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
          case VIEWTYPE_DETAILS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (viewPanels[viewType] == null) {
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   346
                createdViewPanel = fileChooserUIAccessor.createDetailsView();
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   347
                if (createdViewPanel == null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   348
                    createdViewPanel = createDetailsView();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   350
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   351
                detailsTable = (JTable) findChildComponent(createdViewPanel, JTable.class);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   352
                detailsTable.setRowHeight(Math.max(detailsTable.getFont().getSize() + 4, 16 + 1));
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   353
                if (listSelectionModel != null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   354
                    detailsTable.setSelectionModel(listSelectionModel);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   355
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   357
            newFocusOwner = detailsTable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   360
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   361
        if (createdViewPanel != null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   362
            viewPanels[viewType] = createdViewPanel;
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   363
            recursivelySetInheritsPopupMenu(createdViewPanel, true);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   364
        }
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   365
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   366
        boolean isFocusOwner = false;
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   367
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   368
        if (currentViewPanel != null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   369
            Component owner = DefaultKeyboardFocusManager.
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   370
                    getCurrentKeyboardFocusManager().getPermanentFocusOwner();
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   371
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   372
            isFocusOwner = owner == detailsTable || owner == list;
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   373
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   374
            remove(currentViewPanel);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   375
        }
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   376
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        currentViewPanel = viewPanels[viewType];
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   378
        add(currentViewPanel, BorderLayout.CENTER);
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   379
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   380
        if (isFocusOwner && newFocusOwner != null) {
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   381
            newFocusOwner.requestFocusInWindow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
1305
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   383
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   384
        revalidate();
e6f0b298de50 6554743: JFileChooser dn't close after pressing escape key after changing the views
rupashka
parents: 1301
diff changeset
   385
        repaint();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        updateViewMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        firePropertyChange("viewType", oldValue, viewType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    class ViewTypeAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        private int viewType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        ViewTypeAction(int viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            super(viewTypeActionNames[viewType]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            this.viewType = viewType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            String cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            switch (viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                case VIEWTYPE_LIST:    cmd = ACTION_VIEW_LIST;    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                case VIEWTYPE_DETAILS: cmd = ACTION_VIEW_DETAILS; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                default:               cmd = (String)getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            putValue(Action.ACTION_COMMAND_KEY, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            setViewType(viewType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public Action getViewTypeAction(int viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return new ViewTypeAction(viewType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private static void recursivelySetInheritsPopupMenu(Container container, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (container instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            ((JComponent)container).setInheritsPopupMenu(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int n = container.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            recursivelySetInheritsPopupMenu((Container)container.getComponent(i), b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Locale l = getFileChooser().getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        listViewBorder       = UIManager.getBorder("FileChooser.listViewBorder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        listViewBackground   = UIManager.getColor("FileChooser.listViewBackground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        listViewWindowsStyle = UIManager.getBoolean("FileChooser.listViewWindowsStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        readOnly             = UIManager.getBoolean("FileChooser.readOnly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        // TODO: On windows, get the following localized strings from the OS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        viewMenuLabelText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        UIManager.getString("FileChooser.viewMenuLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        refreshActionLabelText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        UIManager.getString("FileChooser.refreshActionLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        newFolderActionLabelText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        UIManager.getString("FileChooser.newFolderActionLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        viewTypeActionNames = new String[VIEWTYPE_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        viewTypeActionNames[VIEWTYPE_LIST] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        UIManager.getString("FileChooser.listViewActionLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        viewTypeActionNames[VIEWTYPE_DETAILS] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        UIManager.getString("FileChooser.detailsViewActionLabelText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        kiloByteString = UIManager.getString("FileChooser.fileSizeKiloBytes", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        megaByteString = UIManager.getString("FileChooser.fileSizeMegaBytes", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        gigaByteString = UIManager.getString("FileChooser.fileSizeGigaBytes", l);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
   451
        fullRowSelection = UIManager.getBoolean("FileView.fullRowSelection");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        renameErrorTitleText = UIManager.getString("FileChooser.renameErrorTitleText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        renameErrorText = UIManager.getString("FileChooser.renameErrorText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        renameErrorFileExistsText = UIManager.getString("FileChooser.renameErrorFileExistsText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Fetches the command list for the FilePane. These commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * are useful for binding to events, such as in a keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @return the command list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public Action[] getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            class FilePaneAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                FilePaneAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    this(name, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                FilePaneAction(String name, String cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    putValue(Action.ACTION_COMMAND_KEY, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    String cmd = (String)getValue(Action.ACTION_COMMAND_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    if (cmd == ACTION_CANCEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        if (editFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                           cancelEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                           getFileChooser().cancelSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    } else if (cmd == ACTION_EDIT_FILE_NAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        int index = listSelectionModel.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        if (index >= 0 && editFile == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                            (!fc.isMultiSelectionEnabled() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                             fc.getSelectedFiles().length <= 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                            editFileName(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    } else if (cmd == ACTION_REFRESH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        getFileChooser().rescanCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    String cmd = (String)getValue(Action.ACTION_COMMAND_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    if (cmd == ACTION_CANCEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        return getFileChooser().isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    } else if (cmd == ACTION_EDIT_FILE_NAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        return !readOnly && getFileChooser().isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            ArrayList<Action> actionList = new ArrayList<Action>(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            Action action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            actionList.add(new FilePaneAction(ACTION_CANCEL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            actionList.add(new FilePaneAction(ACTION_EDIT_FILE_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            actionList.add(new FilePaneAction(refreshActionLabelText, ACTION_REFRESH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            action = fileChooserUIAccessor.getApproveSelectionAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                actionList.add(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            action = fileChooserUIAccessor.getChangeToParentDirectoryAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                actionList.add(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            action = getNewFolderAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                actionList.add(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            action = getViewTypeAction(VIEWTYPE_LIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                actionList.add(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            action = getViewTypeAction(VIEWTYPE_DETAILS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                actionList.add(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            actions = actionList.toArray(new Action[actionList.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    protected void createActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        addActionsToMap(super.getActionMap(), getActions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public static void addActionsToMap(ActionMap map, Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (map != null && actions != null) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   551
            for (Action a : actions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                String cmd = (String)a.getValue(Action.ACTION_COMMAND_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (cmd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    cmd = (String)a.getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                map.put(cmd, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    private void updateListRowCount(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (smallIconsView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            list.setVisibleRowCount(getModel().getSize() / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            list.setVisibleRowCount(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public JPanel createList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        JPanel p = new JPanel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        final JFileChooser fileChooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        final JList list = new JList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                ListModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                int max = model.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                if (prefix == null || startIndex < 0 || startIndex >= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                // start search from the next element before/after the selected element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                boolean backwards = (bias == Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                for (int i = startIndex; backwards ? i >= 0 : i < max; i += (backwards ?  -1 : 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    String filename = fileChooser.getName((File)model.getElementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    if (filename.regionMatches(true, 0, prefix, 0, prefix.length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        list.setCellRenderer(new FileRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        list.setLayoutOrientation(JList.VERTICAL_WRAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // 4835633 : tell BasicListUI that this is a file list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        list.putClientProperty("List.isFileList", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (listViewWindowsStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            list.addFocusListener(repaintListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        updateListRowCount(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        getModel().addListDataListener(new ListDataListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            public void intervalAdded(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                updateListRowCount(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            public void intervalRemoved(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                updateListRowCount(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            public void contentsChanged(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                updateListRowCount(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        getModel().addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (fileChooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        list.setModel(new SortableListModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        list.addListSelectionListener(createListSelectionListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        list.addMouseListener(getMouseHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        JScrollPane scrollpane = new JScrollPane(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (listViewBackground != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            list.setBackground(listViewBackground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (listViewBorder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            scrollpane.setBorder(listViewBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        p.add(scrollpane, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * This model allows for sorting JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    private class SortableListModel extends AbstractListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            implements TableModelListener, RowSorterListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        public SortableListModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            getDetailsTableModel().addTableModelListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            getRowSorter().addRowSorterListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        public Object getElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // JList doesn't support RowSorter so far, so we put it into the list model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return getModel().getElementAt(getRowSorter().convertRowIndexToModel(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        public void tableChanged(TableModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            fireContentsChanged(this, 0, getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        public void sorterChanged(RowSorterEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            fireContentsChanged(this, 0, getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private DetailsTableModel getDetailsTableModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if(detailsTableModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            detailsTableModel = new DetailsTableModel(getFileChooser());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        return detailsTableModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    class DetailsTableModel extends AbstractTableModel implements ListDataListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        JFileChooser chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        BasicDirectoryModel directoryModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        ShellFolderColumnInfo[] columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        int[] columnMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        DetailsTableModel(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            this.chooser = fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            directoryModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            directoryModel.addListDataListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            updateColumnInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        void updateColumnInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            File dir = chooser.getCurrentDirectory();
1842
824c7f885a64 6776856: Code with useShellFolder field shuold be simplify
rupashka
parents: 1840
diff changeset
   694
            if (dir != null && usesShellFolder(chooser)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    dir = ShellFolder.getShellFolder(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    // Leave dir without changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            ShellFolderColumnInfo[] allColumns = ShellFolder.getFolderColumns(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            ArrayList<ShellFolderColumnInfo> visibleColumns =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    new ArrayList<ShellFolderColumnInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            columnMap = new int[allColumns.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            for (int i = 0; i < allColumns.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                ShellFolderColumnInfo column = allColumns[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                if (column.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    columnMap[visibleColumns.size()] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    visibleColumns.add(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            columns = new ShellFolderColumnInfo[visibleColumns.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            visibleColumns.toArray(columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            columnMap = Arrays.copyOf(columnMap, columns.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   719
            List<? extends RowSorter.SortKey> sortKeys =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    (rowSorter == null) ? null : rowSorter.getSortKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            fireTableStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            restoreSortKeys(sortKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   725
        private void restoreSortKeys(List<? extends RowSorter.SortKey> sortKeys) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (sortKeys != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                // check if preserved sortKeys are valid for this folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                for (int i = 0; i < sortKeys.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    RowSorter.SortKey sortKey = sortKeys.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    if (sortKey.getColumn() >= columns.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        sortKeys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                if (sortKeys != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    rowSorter.setSortKeys(sortKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return directoryModel.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        public int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            return columns.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        public Object getValueAt(int row, int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            // Note: It is very important to avoid getting info on drives, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            // this will trigger "No disk in A:" and similar dialogs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            // Use (f.exists() && !chooser.getFileSystemView().isFileSystemRoot(f)) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            // determine if it is safe to call methods directly on f.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return getFileColumnValue((File)directoryModel.getElementAt(row), col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        private Object getFileColumnValue(File f, int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return (col == COLUMN_FILENAME)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    ? f // always return the file itself for the 1st column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    : ShellFolder.getFolderColumnValue(f, columnMap[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        public void setValueAt(Object value, int row, int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            if (col == COLUMN_FILENAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                File f = (File)getValueAt(row, col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    String oldDisplayName = chooser.getName(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    String oldFileName = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    String newDisplayName = ((String)value).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    String newFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    if (!newDisplayName.equals(oldDisplayName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        newFileName = newDisplayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        //Check if extension is hidden from user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        int i1 = oldFileName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        int i2 = oldDisplayName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        if (i1 > i2 && oldFileName.charAt(i2) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                            newFileName = newDisplayName + oldFileName.substring(i2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        // rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        FileSystemView fsv = chooser.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        File f2 = fsv.createFileObject(f.getParentFile(), newFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                        if (f2.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                            JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorFileExistsText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                    oldFileName), renameErrorTitleText, JOptionPane.ERROR_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                            if (FilePane.this.getModel().renameFile(f, f2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                                if (fsv.isParent(chooser.getCurrentDirectory(), f2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                    if (chooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                        chooser.setSelectedFiles(new File[]{f2});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                        chooser.setSelectedFile(f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                    // Could be because of delay in updating Desktop folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                    // chooser.setSelectedFile(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorText, oldFileName),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                        renameErrorTitleText, JOptionPane.ERROR_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        public boolean isCellEditable(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            File currentDirectory = getFileChooser().getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            return (!readOnly && column == COLUMN_FILENAME && canWrite(currentDirectory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        public void contentsChanged(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            // Update the selection after the model has been updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            new DelayedSelectionUpdater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            fireTableDataChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        public void intervalAdded(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            int i0 = e.getIndex0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            int i1 = e.getIndex1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (i0 == i1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                File file = (File)getModel().getElementAt(i0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                if (file.equals(newFolderFile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    new DelayedSelectionUpdater(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    newFolderFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            fireTableRowsInserted(e.getIndex0(), e.getIndex1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        public void intervalRemoved(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            fireTableRowsDeleted(e.getIndex0(), e.getIndex1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        public ShellFolderColumnInfo[] getColumns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return columns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    private void updateDetailsColumnModel(JTable table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (table != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            ShellFolderColumnInfo[] columns = detailsTableModel.getColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            TableColumnModel columnModel = new DefaultTableColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            for (int i = 0; i < columns.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                ShellFolderColumnInfo dataItem = columns[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                TableColumn column = new TableColumn(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                String title = dataItem.getTitle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                if (title != null && title.startsWith("FileChooser.") && title.endsWith("HeaderText")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    // the column must have a string resource that we try to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    String uiTitle = UIManager.getString(title, table.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    if (uiTitle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                        title = uiTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                column.setHeaderValue(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                Integer width = dataItem.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                if (width != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    column.setPreferredWidth(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    // otherwise we let JTable to decide the actual width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                columnModel.addColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // Install cell editor for editing file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if (!readOnly && columnModel.getColumnCount() > COLUMN_FILENAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                columnModel.getColumn(COLUMN_FILENAME).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        setCellEditor(getDetailsTableCellEditor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            table.setColumnModel(columnModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    private DetailsTableRowSorter getRowSorter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (rowSorter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            rowSorter = new DetailsTableRowSorter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        return rowSorter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   890
    private class DetailsTableRowSorter extends TableRowSorter<TableModel> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        public DetailsTableRowSorter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            setModelWrapper(new SorterModelWrapper());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        public void updateComparators(ShellFolderColumnInfo [] columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            for (int i = 0; i < columns.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                Comparator c = columns[i].getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    c = new DirectoriesFirstComparatorWrapper(i, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                setComparator(i, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
2489
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   905
        @Override
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   906
        public void sort() {
3346
1c65be97eaa2 6460525: javax/swing/JFileChooser/6396844/TwentyThousandTest.java times out
rupashka
parents: 2658
diff changeset
   907
            ShellFolder.invoke(new Callable<Void>() {
1c65be97eaa2 6460525: javax/swing/JFileChooser/6396844/TwentyThousandTest.java times out
rupashka
parents: 2658
diff changeset
   908
                public Void call() {
2489
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   909
                    DetailsTableRowSorter.super.sort();
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   910
                    return null;
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   911
                }
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   912
            });
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   913
        }
5052722686e2 6491795: COM should be initialized for Shell API calls in ShellFolder2.cpp
rupashka
parents: 1842
diff changeset
   914
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        public void modelStructureChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            super.modelStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            updateComparators(detailsTableModel.getColumns());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   920
        private class SorterModelWrapper extends ModelWrapper<TableModel, Integer> {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   921
            public TableModel getModel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                return getDetailsTableModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            public int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                return getDetailsTableModel().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                return getDetailsTableModel().getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            public Object getValueAt(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                return FilePane.this.getModel().getElementAt(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   937
            public Integer getIdentifier(int row) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * This class sorts directories before files, comparing directory to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * directory and file to file using the wrapped comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    private class DirectoriesFirstComparatorWrapper implements Comparator<File> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        private Comparator comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        private int column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        public DirectoriesFirstComparatorWrapper(int column, Comparator comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            this.column = column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        public int compare(File f1, File f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (f1 != null && f2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                boolean traversable1 = getFileChooser().isTraversable(f1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                boolean traversable2 = getFileChooser().isTraversable(f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                // directories go first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                if (traversable1 && !traversable2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                if (!traversable1 && traversable2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            if (detailsTableModel.getColumns()[column].isCompareByColumn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                return comparator.compare(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        getDetailsTableModel().getFileColumnValue(f1, column),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        getDetailsTableModel().getFileColumnValue(f2, column)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            // For this column we need to pass the file itself (not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            // column value) to the comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            return comparator.compare(f1, f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    private DetailsTableCellEditor tableCellEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    private DetailsTableCellEditor getDetailsTableCellEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (tableCellEditor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            tableCellEditor = new DetailsTableCellEditor(new JTextField());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        return tableCellEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    private class DetailsTableCellEditor extends DefaultCellEditor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        private final JTextField tf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        public DetailsTableCellEditor(JTextField tf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            super(tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            this.tf = tf;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
   995
            tf.setName("Table.editor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            tf.addFocusListener(editorFocusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        public Component getTableCellEditorComponent(JTable table, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                                                     boolean isSelected, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            Component comp = super.getTableCellEditorComponent(table, value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    isSelected, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            if (value instanceof File) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                tf.setText(getFileChooser().getName((File) value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                tf.selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    class DetailsTableCellRenderer extends DefaultTableCellRenderer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        JFileChooser chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        DateFormat df;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        DetailsTableCellRenderer(JFileChooser chooser) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            this.chooser = chooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                                                chooser.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        public void setBounds(int x, int y, int width, int height) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1023
        if (getHorizontalAlignment() == SwingConstants.LEADING &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1024
                    !fullRowSelection) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                // Restrict width to actual text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                width = Math.min(width, this.getPreferredSize().width+4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                x -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            super.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        public Insets getInsets(Insets i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            // Provide some space between columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            i = super.getInsets(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            i.left  += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            i.right += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        public Component getTableCellRendererComponent(JTable table, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                              boolean isSelected, boolean hasFocus, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1045
            if ((table.convertColumnIndexToModel(column) != COLUMN_FILENAME ||
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1046
                    (listViewWindowsStyle && !table.isFocusOwner())) &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1047
                    !fullRowSelection) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                isSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            super.getTableCellRendererComponent(table, value, isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                                                       hasFocus, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            setIcon(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            int modelColumn = table.convertColumnIndexToModel(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            ShellFolderColumnInfo columnInfo = detailsTableModel.getColumns()[modelColumn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            Integer alignment = columnInfo.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            if (alignment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                alignment = (value instanceof Number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        ? SwingConstants.RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        : SwingConstants.LEADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            setHorizontalAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            // formatting cell text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // TODO: it's rather a temporary trick, to be revised
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            } else if (value instanceof File) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                File file = (File)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                text = chooser.getName(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                Icon icon = chooser.getIcon(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                setIcon(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            } else if (value instanceof Long) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                long len = ((Long) value) / 1024L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                if (listViewWindowsStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    text = MessageFormat.format(kiloByteString, len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                } else if (len < 1024L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    text = MessageFormat.format(kiloByteString, (len == 0L) ? 1L : len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    len /= 1024L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    if (len < 1024L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                        text = MessageFormat.format(megaByteString, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        len /= 1024L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        text = MessageFormat.format(gigaByteString, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            } else if (value instanceof Date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                text = df.format((Date)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                text = value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    public JPanel createDetailsView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        final JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        JPanel p = new JPanel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        final JTable detailsTable = new JTable(getDetailsTableModel()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            // Handle Escape key events here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE && getCellEditor() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    // We are not editing, forward to filechooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    chooser.dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                return super.processKeyBinding(ks, e, condition, pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            public void tableChanged(TableModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                super.tableChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    // update header with possibly changed column set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    updateDetailsColumnModel(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        detailsTable.setRowSorter(getRowSorter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        detailsTable.setAutoCreateColumnsFromModel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        detailsTable.setComponentOrientation(chooser.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        detailsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        detailsTable.setShowGrid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        detailsTable.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        detailsTable.addKeyListener(detailsKeyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        Font font = list.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        detailsTable.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        detailsTable.setIntercellSpacing(new Dimension(0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        TableCellRenderer headerRenderer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                new AlignableTableHeaderRenderer(detailsTable.getTableHeader().getDefaultRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        detailsTable.getTableHeader().setDefaultRenderer(headerRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        TableCellRenderer cellRenderer = new DetailsTableCellRenderer(chooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        detailsTable.setDefaultRenderer(Object.class, cellRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        // So that drag can be started on a mouse press
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        detailsTable.getColumnModel().getSelectionModel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        detailsTable.addMouseListener(getMouseHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        // No need to addListSelectionListener because selections are forwarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        // to our JList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        // 4835633 : tell BasicTableUI that this is a file list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        detailsTable.putClientProperty("Table.isFileList", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (listViewWindowsStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            detailsTable.addFocusListener(repaintListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        // TAB/SHIFT-TAB should transfer focus and ENTER should select an item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        // We don't want them to navigate within the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        ActionMap am = SwingUtilities.getUIActionMap(detailsTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        am.remove("selectNextRowCell");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        am.remove("selectPreviousRowCell");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        am.remove("selectNextColumnCell");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        am.remove("selectPreviousColumnCell");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        detailsTable.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                     null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        detailsTable.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                     null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        JScrollPane scrollpane = new JScrollPane(detailsTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        scrollpane.setComponentOrientation(chooser.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        LookAndFeel.installColors(scrollpane.getViewport(), "Table.background", "Table.foreground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        // Adjust width of first column so the table fills the viewport when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // first displayed (temporary listener).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        scrollpane.addComponentListener(new ComponentAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            public void componentResized(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                JScrollPane sp = (JScrollPane)e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                fixNameColumnWidth(sp.getViewport().getSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                sp.removeComponentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // 4835633.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        // If the mouse is pressed in the area below the Details view table, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        // event is not dispatched to the Table MouseListener but to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        // scrollpane.  Listen for that here so we can clear the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        scrollpane.addMouseListener(new MouseAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                JScrollPane jsp = ((JScrollPane)e.getComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                JTable table = (JTable)jsp.getViewport().getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                if (!e.isShiftDown() || table.getSelectionModel().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                    TableCellEditor tce = table.getCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    if (tce != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                        tce.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        detailsTable.setForeground(list.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        detailsTable.setBackground(list.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        if (listViewBorder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            scrollpane.setBorder(listViewBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        p.add(scrollpane, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        detailsTableModel.fireTableStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    } // createDetailsView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    private class AlignableTableHeaderRenderer implements TableCellRenderer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        TableCellRenderer wrappedRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        public AlignableTableHeaderRenderer(TableCellRenderer wrappedRenderer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            this.wrappedRenderer = wrappedRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        public Component getTableCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                                JTable table, Object value, boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                                boolean hasFocus, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            Component c = wrappedRenderer.getTableCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                                table, value, isSelected, hasFocus, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            int modelColumn = table.convertColumnIndexToModel(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            ShellFolderColumnInfo columnInfo = detailsTableModel.getColumns()[modelColumn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            Integer alignment = columnInfo.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            if (alignment == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                alignment = SwingConstants.CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            if (c instanceof JLabel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                ((JLabel) c).setHorizontalAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    private void fixNameColumnWidth(int viewWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        TableColumn nameCol = detailsTable.getColumnModel().getColumn(COLUMN_FILENAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        int tableWidth = detailsTable.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        if (tableWidth < viewWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            nameCol.setPreferredWidth(nameCol.getPreferredWidth() + viewWidth - tableWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    private class DelayedSelectionUpdater implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        File editFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        DelayedSelectionUpdater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        DelayedSelectionUpdater(File editFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            this.editFile = editFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                SwingUtilities.invokeLater(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            setFileSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            if (editFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                editFileName(getRowSorter().convertRowIndexToView(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                        getModel().indexOf(editFile)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                editFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * Creates a selection listener for the list of files and directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @return a <code>ListSelectionListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    public ListSelectionListener createListSelectionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        return fileChooserUIAccessor.createListSelectionListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    int lastIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    File editFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    private int getEditIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        return lastIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    private void setEditIndex(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        lastIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    private void resetEditIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        lastIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    private void cancelEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        if (editFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            editFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            list.remove(editCell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        } else if (detailsTable != null && detailsTable.isEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            detailsTable.getCellEditor().cancelCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    JTextField editCell = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * @param index visual index of the file to be edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    private void editFileName(int index) {
464
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1330
        JFileChooser chooser = getFileChooser();
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1331
        File currentDirectory = chooser.getCurrentDirectory();
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1332
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        if (readOnly || !canWrite(currentDirectory)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        ensureIndexIsVisible(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        switch (viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
          case VIEWTYPE_LIST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            editFile = (File)getModel().getElementAt(getRowSorter().convertRowIndexToModel(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            Rectangle r = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            if (editCell == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                editCell = new JTextField();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1344
                editCell.setName("Tree.cellEditor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                editCell.addActionListener(new EditActionListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                editCell.addFocusListener(editorFocusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                editCell.setNextFocusableComponent(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            list.add(editCell);
464
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1350
            editCell.setText(chooser.getName(editFile));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            ComponentOrientation orientation = list.getComponentOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            editCell.setComponentOrientation(orientation);
464
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1353
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1354
            Icon icon = chooser.getIcon(editFile);
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1355
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1356
            // PENDING - grab padding (4) below from defaults table.
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1357
            int editX = icon == null ? 20 : icon.getIconWidth() + 4;
9c414b00e007 6635277: Incorrect text seen when creating a new folder, when selection is on the image file in JFileChooser
rupashka
parents: 2
diff changeset
  1358
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            if (orientation.isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                editCell.setBounds(editX + r.x, r.y, r.width - editX, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                editCell.setBounds(r.x, r.y, r.width - editX, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            editCell.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            editCell.selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
          case VIEWTYPE_DETAILS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            detailsTable.editCellAt(index, COLUMN_FILENAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    class EditActionListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    private void applyEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        if (editFile != null && editFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            String oldDisplayName = chooser.getName(editFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            String oldFileName = editFile.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            String newDisplayName = editCell.getText().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            String newFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            if (!newDisplayName.equals(oldDisplayName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                newFileName = newDisplayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                //Check if extension is hidden from user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                int i1 = oldFileName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                int i2 = oldDisplayName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                if (i1 > i2 && oldFileName.charAt(i2) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                    newFileName = newDisplayName + oldFileName.substring(i2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                // rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                FileSystemView fsv = chooser.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                File f2 = fsv.createFileObject(editFile.getParentFile(), newFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                if (f2.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorFileExistsText, oldFileName),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                            renameErrorTitleText, JOptionPane.ERROR_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    if (getModel().renameFile(editFile, f2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                        if (fsv.isParent(chooser.getCurrentDirectory(), f2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                            if (chooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                chooser.setSelectedFiles(new File[]{f2});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                                chooser.setSelectedFile(f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                            //Could be because of delay in updating Desktop folder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                            //chooser.setSelectedFile(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                        JOptionPane.showMessageDialog(chooser, MessageFormat.format(renameErrorText, oldFileName),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                                renameErrorTitleText, JOptionPane.ERROR_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        if (detailsTable != null && detailsTable.isEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            detailsTable.getCellEditor().stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        cancelEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    protected Action newFolderAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    public Action getNewFolderAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        if (!readOnly && newFolderAction == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            newFolderAction = new AbstractAction(newFolderActionLabelText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                private Action basicNewFolderAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                // Initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                    putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_NEW_FOLDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                    File currentDirectory = getFileChooser().getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    if (currentDirectory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                        setEnabled(canWrite(currentDirectory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                public void actionPerformed(ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    if (basicNewFolderAction == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                        basicNewFolderAction = fileChooserUIAccessor.getNewFolderAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                    JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                    File oldFile = fc.getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                    basicNewFolderAction.actionPerformed(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    File newFile = fc.getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                    if (newFile != null && !newFile.equals(oldFile) && newFile.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                        newFolderFile = newFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        return newFolderAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    protected class FileRenderer extends DefaultListCellRenderer  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        public Component getListCellRendererComponent(JList list, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                                                      int index, boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                                                      boolean cellHasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            if (listViewWindowsStyle && !list.isFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                isSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            File file = (File) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            String fileName = getFileChooser().getName(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            setText(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            setFont(list.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            Icon icon = getFileChooser().getIcon(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                setIcon(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                if (getFileChooser().getFileSystemView().isTraversable(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                    setText(fileName+File.separator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    void setFileSelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        if (getFileChooser().isMultiSelectionEnabled() && !isDirectorySelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            File[] files = getFileChooser().getSelectedFiles(); // Should be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            Object[] selectedObjects = list.getSelectedValues(); // Are actually selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            listSelectionModel.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                int lead = listSelectionModel.getLeadSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                int anchor = listSelectionModel.getAnchorSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                Arrays.sort(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                Arrays.sort(selectedObjects);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                int shouldIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                int actuallyIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                // Remove files that shouldn't be selected and add files which should be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                // Note: Assume files are already sorted in compareTo order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                while (shouldIndex < files.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                       actuallyIndex < selectedObjects.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    int comparison = files[shouldIndex].compareTo((File)selectedObjects[actuallyIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    if (comparison < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                        doSelectFile(files[shouldIndex++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    } else if (comparison > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                        doDeselectFile(selectedObjects[actuallyIndex++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                        // Do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                        shouldIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                        actuallyIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                while (shouldIndex < files.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    doSelectFile(files[shouldIndex++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                while (actuallyIndex < selectedObjects.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    doDeselectFile(selectedObjects[actuallyIndex++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                // restore the anchor and lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                if (listSelectionModel instanceof DefaultListSelectionModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    ((DefaultListSelectionModel)listSelectionModel).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                        moveLeadSelectionIndex(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    listSelectionModel.setAnchorSelectionIndex(anchor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                listSelectionModel.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            File f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            if (isDirectorySelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                f = getDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                f = chooser.getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (f != null && (i = getModel().indexOf(f)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                int viewIndex = getRowSorter().convertRowIndexToView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                listSelectionModel.setSelectionInterval(viewIndex, viewIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                ensureIndexIsVisible(viewIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    private void doSelectFile(File fileToSelect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        int index = getModel().indexOf(fileToSelect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        // could be missed in the current directory if it changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            index = getRowSorter().convertRowIndexToView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            listSelectionModel.addSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    private void doDeselectFile(Object fileToDeselect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        int index = getRowSorter().convertRowIndexToView(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                                getModel().indexOf(fileToDeselect));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        listSelectionModel.removeSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    /* The following methods are used by the PropertyChange Listener */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    private void doSelectedFileChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        File f = (File) e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        if (f != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            && ((fc.isFileSelectionEnabled() && !f.isDirectory())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                || (f.isDirectory() && fc.isDirectorySelectionEnabled()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            setFileSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    private void doSelectedFilesChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        File[] files = (File[]) e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        if (files != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            && files.length > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            setFileSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    private void doDirectoryChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        getDetailsTableModel().updateColumnInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        FileSystemView fsv = fc.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        resetEditIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        ensureIndexIsVisible(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        File currentDirectory = fc.getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        if (currentDirectory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            if (!readOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                getNewFolderAction().setEnabled(canWrite(currentDirectory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            fileChooserUIAccessor.getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        if (list != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            list.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    private void doFilterChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        resetEditIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    private void doFileSelectionModeChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        resetEditIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    private void doMultiSelectionChanged(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        if (getFileChooser().isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            getFileChooser().setSelectedFiles(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * Listen for filechooser property changes, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * the selected file changing, or the type of the dialog changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            if (viewType == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                setViewType(VIEWTYPE_LIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        String s = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        if (s.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            doSelectedFileChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        } else if (s.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            doSelectedFilesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        } else if (s.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            doDirectoryChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        } else if (s.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            doFilterChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        } else if (s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            doFileSelectionModeChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        } else if (s.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            doMultiSelectionChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        } else if (s.equals(JFileChooser.CANCEL_SELECTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            applyEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        } else if (s.equals("busy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            setCursor((Boolean)e.getNewValue() ? waitCursor : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        } else if (s.equals("componentOrientation")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            ComponentOrientation o = (ComponentOrientation)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            JFileChooser cc = (JFileChooser)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            if (o != e.getOldValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                cc.applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            if (detailsTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                detailsTable.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                detailsTable.getParent().getParent().setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    private void ensureIndexIsVisible(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            if (list != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                list.ensureIndexIsVisible(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            if (detailsTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                detailsTable.scrollRectToVisible(detailsTable.getCellRect(i, COLUMN_FILENAME, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    public void ensureFileIsVisible(JFileChooser fc, File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        int modelIndex = getModel().indexOf(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        if (modelIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            ensureIndexIsVisible(getRowSorter().convertRowIndexToView(modelIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    public void rescanCurrentDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        getModel().validateFileCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        if (listSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            listSelectionModel.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            if (listSelectionModel instanceof DefaultListSelectionModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                ((DefaultListSelectionModel)listSelectionModel).moveLeadSelectionIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                listSelectionModel.setAnchorSelectionIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    public JMenu getViewMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        if (viewMenu == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            viewMenu = new JMenu(viewMenuLabelText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            ButtonGroup viewButtonGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            for (int i = 0; i < VIEWTYPE_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                JRadioButtonMenuItem mi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                    new JRadioButtonMenuItem(new ViewTypeAction(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                viewButtonGroup.add(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                viewMenu.add(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            updateViewMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        return viewMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    private void updateViewMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        if (viewMenu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            Component[] comps = viewMenu.getMenuComponents();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1735
            for (Component comp : comps) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1736
                if (comp instanceof JRadioButtonMenuItem) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1737
                    JRadioButtonMenuItem mi = (JRadioButtonMenuItem) comp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                    if (((ViewTypeAction)mi.getAction()).viewType == viewType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                        mi.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    public JPopupMenu getComponentPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        JPopupMenu popupMenu = getFileChooser().getComponentPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        if (popupMenu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            return popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        JMenu viewMenu = getViewMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        if (contextMenu == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            contextMenu = new JPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            if (viewMenu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                contextMenu.add(viewMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                if (listViewWindowsStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                    contextMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            ActionMap actionMap = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            Action refreshAction   = actionMap.get(ACTION_REFRESH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            Action newFolderAction = actionMap.get(ACTION_NEW_FOLDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            if (refreshAction != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                contextMenu.add(refreshAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                if (listViewWindowsStyle && newFolderAction != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                    contextMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            if (newFolderAction != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                contextMenu.add(newFolderAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        if (viewMenu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            viewMenu.getPopupMenu().setInvoker(viewMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        return contextMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
    protected Handler getMouseHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    private class Handler implements MouseListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        private MouseListener doubleClickListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        public void mouseClicked(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            JComponent source = (JComponent)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            if (source instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            } else if (source instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                JTable table = (JTable)source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                Point p = evt.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                index = table.rowAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1804
                boolean pointOutsidePrefSize =
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1805
                        SwingUtilities2.pointOutsidePrefSize(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1806
                            table, index, table.columnAtPoint(p), p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2489
diff changeset
  1808
                if (pointOutsidePrefSize && !fullRowSelection) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                // Translate point from table to list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                if (index >= 0 && list != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                    listSelectionModel.isSelectedIndex(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                    // Make a new event with the list as source, placing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                    // click in the corresponding list cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                    Rectangle r = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                    evt = new MouseEvent(list, evt.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                                         evt.getWhen(), evt.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                                         r.x + 1, r.y + r.height/2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                         evt.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                         evt.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                                         evt.getClickCount(), evt.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                                         evt.getButton());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            if (index >= 0 && SwingUtilities.isLeftMouseButton(evt)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                // For single click, we handle editing file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                if (evt.getClickCount() == 1 && source instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    if ((!fc.isMultiSelectionEnabled() || fc.getSelectedFiles().length <= 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                        && index >= 0 && listSelectionModel.isSelectedIndex(index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                        && getEditIndex() == index && editFile == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                        editFileName(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                        if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                            setEditIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                            resetEditIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                } else if (evt.getClickCount() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                    // on double click (open or drill down one directory) be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    // sure to clear the edit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                    resetEditIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            // Forward event to Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if (getDoubleClickListener() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                getDoubleClickListener().mouseClicked(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        public void mouseEntered(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            JComponent source = (JComponent)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            if (source instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                JTable table = (JTable)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                TransferHandler th1 = getFileChooser().getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                TransferHandler th2 = table.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                if (th1 != th2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    table.setTransferHandler(th1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                boolean dragEnabled = getFileChooser().getDragEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                if (dragEnabled != table.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    table.setDragEnabled(dragEnabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            } else if (source instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                // Forward event to Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                if (getDoubleClickListener() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    getDoubleClickListener().mouseEntered(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        public void mouseExited(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            if (evt.getSource() instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                // Forward event to Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                if (getDoubleClickListener() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                    getDoubleClickListener().mouseExited(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        public void mousePressed(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            if (evt.getSource() instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                // Forward event to Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                if (getDoubleClickListener() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                    getDoubleClickListener().mousePressed(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        public void mouseReleased(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            if (evt.getSource() instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                // Forward event to Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                if (getDoubleClickListener() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                    getDoubleClickListener().mouseReleased(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        private MouseListener getDoubleClickListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            // Lazy creation of Basic's listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            if (doubleClickListener == null && list != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                doubleClickListener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                    fileChooserUIAccessor.createDoubleClickListener(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            return doubleClickListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * Property to remember whether a directory is currently selected in the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * @return <code>true</code> iff a directory is currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    protected boolean isDirectorySelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        return fileChooserUIAccessor.isDirectorySelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * Property to remember the directory that is currently selected in the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * @return the value of the <code>directory</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * @see javax.swing.plaf.basic.BasicFileChooserUI#setDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    protected File getDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        return fileChooserUIAccessor.getDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    private Component findChildComponent(Container container, Class cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        int n = container.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            Component comp = container.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            if (cls.isInstance(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            } else if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                Component c = findChildComponent((Container)comp, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                    return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    public boolean canWrite(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        // Return false for non FileSystem files or if file doesn't exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        if (f instanceof ShellFolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            return ((ShellFolder) f).isFileSystem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        } else {
1842
824c7f885a64 6776856: Code with useShellFolder field shuold be simplify
rupashka
parents: 1840
diff changeset
  1966
            if (usesShellFolder(getFileChooser())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                    return ShellFolder.getShellFolder(f).isFileSystem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                } catch (FileNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    // File doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                // Ordinary file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
1840
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1980
    /**
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1981
     * Returns true if specified FileChooser should use ShellFolder
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1982
     */
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1983
    public static boolean usesShellFolder(JFileChooser chooser) {
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1984
        Boolean prop = (Boolean) chooser.getClientProperty("FileChooser.useShellFolder");
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1985
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1986
        return prop == null ? chooser.getFileSystemView().equals(FileSystemView.getFileSystemView())
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1987
                : prop.booleanValue();
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1988
    }
984ba55ac827 6698013: JFileChooser can no longer navigate non-local file systems.
rupashka
parents: 1305
diff changeset
  1989
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    // This interface is used to access methods in the FileChooserUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    // that are not public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    public interface FileChooserUIAccessor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        public JFileChooser getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        public BasicDirectoryModel getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        public JPanel createList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        public JPanel createDetailsView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        public boolean isDirectorySelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        public File getDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        public Action getApproveSelectionAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        public Action getChangeToParentDirectoryAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        public Action getNewFolderAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        public MouseListener createDoubleClickListener(JList list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        public ListSelectionListener createListSelectionListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
}