jdk/src/share/classes/javax/swing/plaf/basic/BasicFileChooserUI.java
author rupashka
Wed, 27 Aug 2008 20:49:35 +0400
changeset 1302 ac8b63707542
parent 1299 027d966d5658
child 1840 984ba55ac827
permissions -rw-r--r--
6351074: JFileChooser removes leading space in filename Summary: Removed trimming of leading spaces in filename Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 680
diff changeset
     2
 * Copyright 1998-2008 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.filechooser.FileFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.datatransfer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
    39
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.shell.ShellFolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Basic L&F implementation of a FileChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class BasicFileChooserUI extends FileChooserUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /* FileView icons */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected Icon directoryIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected Icon fileIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected Icon computerIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected Icon hardDriveIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected Icon floppyDriveIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected Icon newFolderIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected Icon upFolderIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected Icon homeFolderIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected Icon listViewIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Icon detailsViewIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Icon viewMenuIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected int saveButtonMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected int openButtonMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected int cancelButtonMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected int updateButtonMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected int helpButtonMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The mnemonic keycode used for the approve button when a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * is selected and the current selection mode is FILES_ONLY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected int directoryOpenButtonMnemonic = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected String saveButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected String openButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected String cancelButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected String updateButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected String helpButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The label text displayed on the approve button when a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * is selected and the current selection mode is FILES_ONLY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected String directoryOpenButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private String openDialogTitleText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private String saveDialogTitleText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected String saveButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected String openButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    protected String cancelButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected String updateButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected String helpButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The tooltip text displayed on the approve button when a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * is selected and the current selection mode is FILES_ONLY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected String directoryOpenButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Some generic FileChooser functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private Action approveSelectionAction = new ApproveSelectionAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private Action cancelSelectionAction = new CancelSelectionAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private Action updateAction = new UpdateAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Action newFolderAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private Action goHomeAction = new GoHomeAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private Action changeToParentDirectoryAction = new ChangeToParentDirectoryAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private String newFolderErrorSeparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private String newFolderErrorText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private String newFolderParentDoesntExistTitleText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private String newFolderParentDoesntExistText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private String fileDescriptionText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private String directoryDescriptionText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private JFileChooser filechooser = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private boolean directorySelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private File directory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private PropertyChangeListener propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private FileFilter actualFileFilter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private GlobFilter globFilter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private BasicDirectoryModel model = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private BasicFileView fileView = new BasicFileView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private boolean usesSingleFilePane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean readOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // The accessoryPanel is a container to place the JFileChooser accessory component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private JPanel accessoryPanel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public BasicFileChooserUI(JFileChooser b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        accessoryPanel = new JPanel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        filechooser = (JFileChooser) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        createModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        clearIconCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        installDefaults(filechooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        installComponents(filechooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        installListeners(filechooser);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        filechooser.applyComponentOrientation(filechooser.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void uninstallUI(JComponent c) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   163
        uninstallListeners(filechooser);
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   164
        uninstallComponents(filechooser);
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   165
        uninstallDefaults(filechooser);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if(accessoryPanel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            accessoryPanel.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        accessoryPanel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        getFileChooser().removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void installComponents(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void uninstallComponents(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected void installListeners(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        propertyChangeListener = createPropertyChangeListener(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if(propertyChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            fc.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        fc.addPropertyChangeListener(getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        InputMap inputMap = getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                        WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        SwingUtilities.replaceUIInputMap(fc, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        ActionMap actionMap = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        SwingUtilities.replaceUIActionMap(fc, actionMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return (InputMap)DefaultLookup.get(getFileChooser(), this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    "FileChooser.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    ActionMap getActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return createActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    ActionMap createActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        ActionMap map = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Action refreshAction = new UIAction(FilePane.ACTION_REFRESH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                getFileChooser().rescanCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        map.put(FilePane.ACTION_APPROVE_SELECTION, getApproveSelectionAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        map.put(FilePane.ACTION_CANCEL, getCancelSelectionAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        map.put(FilePane.ACTION_REFRESH, refreshAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        map.put(FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                getChangeToParentDirectoryAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected void uninstallListeners(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if(propertyChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            fc.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        fc.removePropertyChangeListener(getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        SwingUtilities.replaceUIInputMap(fc, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        SwingUtilities.replaceUIActionMap(fc, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    protected void installDefaults(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        installIcons(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        installStrings(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        usesSingleFilePane = UIManager.getBoolean("FileChooser.usesSingleFilePane");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        readOnly           = UIManager.getBoolean("FileChooser.readOnly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        TransferHandler th = fc.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (th == null || th instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            fc.setTransferHandler(defaultTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        LookAndFeel.installProperty(fc, "opaque", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    protected void installIcons(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        directoryIcon    = UIManager.getIcon("FileView.directoryIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        fileIcon         = UIManager.getIcon("FileView.fileIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        computerIcon     = UIManager.getIcon("FileView.computerIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        hardDriveIcon    = UIManager.getIcon("FileView.hardDriveIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        floppyDriveIcon  = UIManager.getIcon("FileView.floppyDriveIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        newFolderIcon    = UIManager.getIcon("FileChooser.newFolderIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        upFolderIcon     = UIManager.getIcon("FileChooser.upFolderIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        homeFolderIcon   = UIManager.getIcon("FileChooser.homeFolderIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        detailsViewIcon  = UIManager.getIcon("FileChooser.detailsViewIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        listViewIcon     = UIManager.getIcon("FileChooser.listViewIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        viewMenuIcon     = UIManager.getIcon("FileChooser.viewMenuIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    protected void installStrings(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Locale l = fc.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        newFolderParentDoesntExistTitleText = UIManager.getString("FileChooser.newFolderParentDoesntExistTitleText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        newFolderParentDoesntExistText = UIManager.getString("FileChooser.newFolderParentDoesntExistText", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        saveButtonText   = UIManager.getString("FileChooser.saveButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        openButtonText   = UIManager.getString("FileChooser.openButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        cancelButtonText = UIManager.getString("FileChooser.cancelButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        updateButtonText = UIManager.getString("FileChooser.updateButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        helpButtonText   = UIManager.getString("FileChooser.helpButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        directoryOpenButtonText = UIManager.getString("FileChooser.directoryOpenButtonText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        saveButtonMnemonic   = getMnemonic("FileChooser.saveButtonMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        openButtonMnemonic   = getMnemonic("FileChooser.openButtonMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        updateButtonMnemonic = getMnemonic("FileChooser.updateButtonMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        helpButtonMnemonic   = getMnemonic("FileChooser.helpButtonMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        directoryOpenButtonMnemonic = getMnemonic("FileChooser.directoryOpenButtonMnemonic", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        saveButtonToolTipText   = UIManager.getString("FileChooser.saveButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        openButtonToolTipText   = UIManager.getString("FileChooser.openButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        helpButtonToolTipText   = UIManager.getString("FileChooser.helpButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        directoryOpenButtonToolTipText = UIManager.getString("FileChooser.directoryOpenButtonToolTipText",l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    protected void uninstallDefaults(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        uninstallIcons(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        uninstallStrings(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (fc.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            fc.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    protected void uninstallIcons(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        directoryIcon    = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        fileIcon         = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        computerIcon     = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        hardDriveIcon    = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        floppyDriveIcon  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        newFolderIcon    = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        upFolderIcon     = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        homeFolderIcon   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        detailsViewIcon  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        listViewIcon     = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        viewMenuIcon     = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    protected void uninstallStrings(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        saveButtonText   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        openButtonText   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        cancelButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        updateButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        helpButtonText   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        directoryOpenButtonText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        saveButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        openButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        cancelButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        updateButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        helpButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        directoryOpenButtonToolTipText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected void createModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            model.invalidateFileCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        model = new BasicDirectoryModel(getFileChooser());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public BasicDirectoryModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public String getFileName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public String getDirectoryName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public void setFileName(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void setDirectoryName(String dirname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public void rescanCurrentDirectory(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void ensureFileIsVisible(JFileChooser fc, File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public JFileChooser getFileChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return filechooser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public JPanel getAccessoryPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return accessoryPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    protected JButton getApproveButton(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
680
eaff686e34f7 5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents: 2
diff changeset
   388
    public JButton getDefaultButton(JFileChooser fc) {
eaff686e34f7 5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents: 2
diff changeset
   389
        return getApproveButton(fc);
eaff686e34f7 5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents: 2
diff changeset
   390
    }
eaff686e34f7 5035693: "Open" button should be a default one in JFileChooser under Windows XP LAF
rupashka
parents: 2
diff changeset
   391
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public String getApproveButtonToolTipText(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        String tooltipText = fc.getApproveButtonToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if(tooltipText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return tooltipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if(fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return openButtonToolTipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else if(fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return saveButtonToolTipText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return null;
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 clearIconCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        fileView.clearIconCache();
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
    // ********************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    // ************ Create Listeners **************
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 Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    protected MouseListener createDoubleClickListener(JFileChooser fc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                                      JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return new Handler(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public ListSelectionListener createListSelectionListener(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private class Handler implements MouseListener, ListSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        JList list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        Handler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        Handler(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            this.list = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        public void mouseClicked(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // Note: we can't depend on evt.getSource() because of backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // compatability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            if (list != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                SwingUtilities.isLeftMouseButton(evt) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                (evt.getClickCount()%2 == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                int index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    File f = (File)list.getModel().getElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        // Strip trailing ".."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        f = ShellFolder.getNormalizedFile(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        // That's ok, we'll use f as is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    if(getFileChooser().isTraversable(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        list.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        changeDirectory(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        getFileChooser().approveSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        public void mouseEntered(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (list != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                TransferHandler th1 = getFileChooser().getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                TransferHandler th2 = list.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (th1 != th2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    list.setTransferHandler(th1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (getFileChooser().getDragEnabled() != list.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    list.setDragEnabled(getFileChooser().getDragEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        public void mouseExited(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        public void mousePressed(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public void mouseReleased(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        public void valueChanged(ListSelectionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if(!evt.getValueIsAdjusting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                FileSystemView fsv = chooser.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                JList list = (JList)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                int fsm = chooser.getFileSelectionMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                boolean useSetDirectory = usesSingleFilePane &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                          (fsm == JFileChooser.FILES_ONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (chooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    File[] files = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    Object[] objects = list.getSelectedValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    if (objects != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        if (objects.length == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            && ((File)objects[0]).isDirectory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            && chooser.isTraversable(((File)objects[0]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            && (useSetDirectory || !fsv.isFileSystem(((File)objects[0])))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                            setDirectorySelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            setDirectory(((File)objects[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        } else {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   510
                            ArrayList<File> fList = new ArrayList<File>(objects.length);
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   511
                            for (Object object : objects) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   512
                                File f = (File) object;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                boolean isDir = f.isDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                if ((chooser.isFileSelectionEnabled() && !isDir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                    || (chooser.isDirectorySelectionEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                        && fsv.isFileSystem(f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                        && isDir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                    fList.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            if (fList.size() > 0) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
   522
                                files = fList.toArray(new File[fList.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                            setDirectorySelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    chooser.setSelectedFiles(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    File file = (File)list.getSelectedValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    if (file != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        && file.isDirectory()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        && chooser.isTraversable(file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        && (useSetDirectory || !fsv.isFileSystem(file))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        setDirectorySelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        setDirectory(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        if (usesSingleFilePane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                            chooser.setSelectedFile(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        setDirectorySelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        if (file != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                            chooser.setSelectedFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    }
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    protected class DoubleClickListener extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // NOTE: This class exists only for backward compatability. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        public  DoubleClickListener(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            handler = new Handler(list);
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
         * The JList used for representing the files is created by subclasses, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * selection is monitored in this class.  The TransferHandler installed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * JFileChooser is also installed in the file list as it is used as the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * transfer source.  The list is updated on a mouse enter to reflect the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * data transfer state of the file chooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            handler.mouseEntered(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            handler.mouseClicked(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    protected class SelectionListener implements ListSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // NOTE: This class exists only for backward compatability. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            getHandler().valueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Property to remember whether a directory is currently selected in the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @return <code>true</code> iff a directory is currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    protected boolean isDirectorySelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return directorySelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Property to remember whether a directory is currently selected in the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * This is normally called by the UI on a selection event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @param b iff a directory is currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    protected void setDirectorySelected(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        directorySelected = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Property to remember the directory that is currently selected in the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return the value of the <code>directory</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @see #setDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    protected File getDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Property to remember the directory that is currently selected in the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * This is normally called by the UI on a selection event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @param f the <code>File</code> object representing the directory that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *          currently selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    protected void setDirectory(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        directory = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * Returns the mnemonic for the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private int getMnemonic(String key, Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return SwingUtilities2.getUIDefaultsInt(key, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    // *******************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // ************ FileChooser UI PLAF methods **************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    // *******************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Returns the default accept all file filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public FileFilter getAcceptAllFileFilter(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return acceptAllFileFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public FileView getFileView(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return fileView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Returns the title of this dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public String getDialogTitle(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        String dialogTitle = fc.getDialogTitle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (dialogTitle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return dialogTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return openDialogTitleText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return saveDialogTitleText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return getApproveButtonText(fc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public int getApproveButtonMnemonic(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        int mnemonic = fc.getApproveButtonMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (mnemonic > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return openButtonMnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            return saveButtonMnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public String getApproveButtonText(JFileChooser fc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        String buttonText = fc.getApproveButtonText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (buttonText != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            return buttonText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            return openButtonText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return saveButtonText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    // *****************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    // ***** Directory Actions *****
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    // *****************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    public Action getNewFolderAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (newFolderAction == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            newFolderAction = new NewFolderAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            // Note: Don't return null for readOnly, it might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            // break older apps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (readOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                newFolderAction.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return newFolderAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public Action getGoHomeAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return goHomeAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public Action getChangeToParentDirectoryAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        return changeToParentDirectoryAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public Action getApproveSelectionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return approveSelectionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public Action getCancelSelectionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        return cancelSelectionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public Action getUpdateAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        return updateAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Creates a new folder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    protected class NewFolderAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        protected NewFolderAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            super(FilePane.ACTION_NEW_FOLDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (readOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            File currentDirectory = fc.getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if (!currentDirectory.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                JOptionPane.showMessageDialog(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    fc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    newFolderParentDoesntExistText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    newFolderParentDoesntExistTitleText, JOptionPane.WARNING_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            File newFolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                newFolder = fc.getFileSystemView().createNewFolder(currentDirectory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                if (fc.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    fc.setSelectedFiles(new File[] { newFolder });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    fc.setSelectedFile(newFolder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                JOptionPane.showMessageDialog(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    fc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    newFolderErrorText + newFolderErrorSeparator + exc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    newFolderErrorText, JOptionPane.ERROR_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            fc.rescanCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Acts on the "home" key event or equivalent event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    protected class GoHomeAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        protected GoHomeAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            super("Go Home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            changeDirectory(fc.getFileSystemView().getHomeDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    protected class ChangeToParentDirectoryAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        protected ChangeToParentDirectoryAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            super("Go Up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            if (focusOwner == null || !(focusOwner instanceof javax.swing.text.JTextComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                getFileChooser().changeToParentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Responds to an Open or Save request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    protected class ApproveSelectionAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        protected ApproveSelectionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            super(FilePane.ACTION_APPROVE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (isDirectorySelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                File dir = getDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if (dir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        // Strip trailing ".."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        dir = ShellFolder.getNormalizedFile(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        // Ok, use f as is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    changeDirectory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            String filename = getFileName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            FileSystemView fs = chooser.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            File dir = chooser.getCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            if (filename != null) {
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   833
                // Remove whitespaces from end of filename
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   834
                int i = filename.length() - 1;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   835
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   836
                while (i >=0 && filename.charAt(i) <= ' ') {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   837
                    i--;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   838
                }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   839
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   840
                filename = filename.substring(0, i + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   843
            if (filename == null || filename.length() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                // no file selected, multiple selection off, therefore cancel the approve action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                resetGlobFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            File selectedFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            File[] selectedFiles = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   852
            // Unix: Resolve '~' to user's home directory
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   853
            if (File.separatorChar == '/') {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   854
                if (filename.startsWith("~/")) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   855
                    filename = System.getProperty("user.home") + filename.substring(1);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   856
                } else if (filename.equals("~")) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   857
                    filename = System.getProperty("user.home");
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   858
                }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   859
            }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   860
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   861
            if (chooser.isMultiSelectionEnabled() && filename.length() > 1 &&
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   862
                    filename.charAt(0) == '"' && filename.charAt(filename.length() - 1) == '"') {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   863
                List<File> fList = new ArrayList<File>();
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   864
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   865
                String[] files = filename.substring(1, filename.length() - 1).split("\" \"");
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   866
                // Optimize searching files by names in "children" array
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   867
                Arrays.sort(files);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   868
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   869
                File[] children = null;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   870
                int childIndex = 0;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   871
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   872
                for (String str : files) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   873
                    File file = fs.createFileObject(str);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   874
                    if (!file.isAbsolute()) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   875
                        if (children == null) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   876
                            children = fs.getFiles(dir, false);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   877
                            Arrays.sort(children);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   878
                        }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   879
                        for (int k = 0; k < children.length; k++) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   880
                            int l = (childIndex + k) % children.length;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   881
                            if (children[l].getName().equals(str)) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   882
                                file = children[l];
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   883
                                childIndex = l + 1;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   884
                                break;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   885
                            }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   886
                        }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   887
                    }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   888
                    fList.add(file);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   889
                }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   890
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   891
                if (!fList.isEmpty()) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   892
                    selectedFiles = fList.toArray(new File[fList.size()]);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   893
                }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   894
                resetGlobFilter();
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   895
            } else {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   896
                selectedFile = fs.createFileObject(filename);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   897
                if (!selectedFile.isAbsolute()) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   898
                    selectedFile = fs.getChild(dir, filename);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   899
                }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   900
                // check for wildcard pattern
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   901
                FileFilter currentFilter = chooser.getFileFilter();
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   902
                if (!selectedFile.exists() && isGlobPattern(filename)) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   903
                    changeDirectory(selectedFile.getParentFile());
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   904
                    if (globFilter == null) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   905
                        globFilter = new GlobFilter();
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   906
                    }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   907
                    try {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   908
                        globFilter.setPattern(selectedFile.getName());
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   909
                        if (!(currentFilter instanceof GlobFilter)) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   910
                            actualFileFilter = currentFilter;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   911
                        }
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   912
                        chooser.setFileFilter(null);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   913
                        chooser.setFileFilter(globFilter);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   914
                        return;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   915
                    } catch (PatternSyntaxException pse) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   916
                        // Not a valid glob pattern. Abandon filter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   920
                resetGlobFilter();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   922
                // Check for directory change action
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   923
                boolean isDir = (selectedFile != null && selectedFile.isDirectory());
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   924
                boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile));
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   925
                boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled();
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   926
                boolean isFileSelEnabled = chooser.isFileSelectionEnabled();
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   927
                boolean isCtrl = (e != null && (e.getModifiers() & ActionEvent.CTRL_MASK) != 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   929
                if (isDir && isTrav && (isCtrl || !isDirSelEnabled)) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   930
                    changeDirectory(selectedFile);
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   931
                    return;
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   932
                } else if ((isDir || !isFileSelEnabled)
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   933
                        && (!isDir || !isDirSelEnabled)
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   934
                        && (!isDirSelEnabled || selectedFile.exists())) {
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   935
                    selectedFile = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
1302
ac8b63707542 6351074: JFileChooser removes leading space in filename
rupashka
parents: 1299
diff changeset
   938
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            if (selectedFiles != null || selectedFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                if (selectedFiles != null || chooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    if (selectedFiles == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                        selectedFiles = new File[] { selectedFile };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    chooser.setSelectedFiles(selectedFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    // Do it again. This is a fix for bug 4949273 to force the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    // selected value in case the ListSelectionModel clears it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    // for non-existing file names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    chooser.setSelectedFiles(selectedFiles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    chooser.setSelectedFile(selectedFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                chooser.approveSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                if (chooser.isMultiSelectionEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    chooser.setSelectedFiles(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    chooser.setSelectedFile(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                chooser.cancelSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    private void resetGlobFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (actualFileFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            JFileChooser chooser = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            FileFilter currentFilter = chooser.getFileFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            if (currentFilter != null && currentFilter.equals(globFilter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                chooser.setFileFilter(actualFileFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                chooser.removeChoosableFileFilter(globFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            actualFileFilter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    private static boolean isGlobPattern(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        return ((File.separatorChar == '\\' && (filename.indexOf('*') >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                                                  || filename.indexOf('?') >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                || (File.separatorChar == '/' && (filename.indexOf('*') >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                                                  || filename.indexOf('?') >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                                                  || filename.indexOf('[') >= 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    /* A file filter which accepts file patterns containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * the special wildcards *? on Windows and *?[] on Unix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    class GlobFilter extends FileFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        Pattern pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        String globPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        public void setPattern(String globPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            char[] gPat = globPattern.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            char[] rPat = new char[gPat.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            boolean isWin32 = (File.separatorChar == '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            boolean inBrackets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            this.globPattern = globPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            if (isWin32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                // On windows, a pattern ending with *.* is equal to ending with *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                int len = gPat.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                if (globPattern.endsWith("*.*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                    len -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    switch(gPat[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                      case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                        rPat[j++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        rPat[j++] = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                      case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                        rPat[j++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                      case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                        rPat[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                        rPat[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                      default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        if ("+()^$.{}[]".indexOf(gPat[i]) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                            rPat[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                        rPat[j++] = gPat[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                for (int i = 0; i < gPat.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    switch(gPat[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                      case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                        if (!inBrackets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                            rPat[j++] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        rPat[j++] = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                      case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                        rPat[j++] = inBrackets ? '?' : '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                      case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                        inBrackets = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        rPat[j++] = gPat[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                        if (i < gPat.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                            switch (gPat[i+1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                              case '!':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                              case '^':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                rPat[j++] = '^';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                              case ']':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                                rPat[j++] = gPat[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                      case ']':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        rPat[j++] = gPat[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        inBrackets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                      case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                        if (i == 0 && gPat.length > 1 && gPat[1] == '~') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                            rPat[j++] = gPat[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                            rPat[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                            if (i < gPat.length - 1 && "*?[]".indexOf(gPat[i+1]) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                                rPat[j++] = gPat[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                                rPat[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                      default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                        //if ("+()|^$.{}<>".indexOf(gPat[i]) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                        if (!Character.isLetterOrDigit(gPat[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                            rPat[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        rPat[j++] = gPat[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            this.pattern = Pattern.compile(new String(rPat, 0, j), Pattern.CASE_INSENSITIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        public boolean accept(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            return pattern.matcher(f.getName()).matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return globPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Responds to a cancel request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    protected class CancelSelectionAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            getFileChooser().cancelSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * Rescans the files in the current directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    protected class UpdateAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            fc.rescanCurrentDirectory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    private void changeDirectory(File dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        JFileChooser fc = getFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        // Traverse shortcuts on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if (dir != null && File.separatorChar == '\\' && dir.getPath().endsWith(".lnk")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                File linkedTo = ShellFolder.getShellFolder(dir).getLinkLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                if (linkedTo != null && fc.isTraversable(linkedTo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    dir = linkedTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            } catch (FileNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        fc.setCurrentDirectory(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            fc.getFileSystemView().isFileSystem(dir)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            setFileName(dir.getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    // *****************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    // ***** default AcceptAll file filter *****
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    // *****************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    protected class AcceptAllFileFilter extends FileFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        public AcceptAllFileFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        public boolean accept(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            return UIManager.getString("FileChooser.acceptAllFileFilterText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    // ***********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    // * FileView operations *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    // ***********************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    protected class BasicFileView extends FileView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        /* FileView type descriptions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        // PENDING(jeff) - pass in the icon cache size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        protected Hashtable<File,Icon> iconCache = new Hashtable<File,Icon>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        public BasicFileView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        public void clearIconCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            iconCache = new Hashtable<File,Icon>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        public String getName(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            // Note: Returns display name rather than file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            String fileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            if(f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                fileName = getFileChooser().getFileSystemView().getSystemDisplayName(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            return fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        public String getDescription(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        public String getTypeDescription(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            String type = getFileChooser().getFileSystemView().getSystemTypeDescription(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    type = directoryDescriptionText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    type = fileDescriptionText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        public Icon getCachedIcon(File f) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
  1216
            return iconCache.get(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        public void cacheIcon(File f, Icon i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            if(f == null || i == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            iconCache.put(f, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        public Icon getIcon(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            Icon icon = getCachedIcon(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            if(icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            icon = fileIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                FileSystemView fsv = getFileChooser().getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                if (fsv.isFloppyDrive(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    icon = floppyDriveIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                } else if (fsv.isDrive(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    icon = hardDriveIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                } else if (fsv.isComputerNode(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                    icon = computerIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                } else if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    icon = directoryIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            cacheIcon(f, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        public Boolean isHidden(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            String name = f.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if(name != null && name.charAt(0) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    private static final TransferHandler defaultTransferHandler = new FileTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * Data transfer support for the file chooser.  Since files are currently presented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * as a list, the list support is reused with the added flavor of DataFlavor.javaFileListFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    static class FileTransferHandler extends TransferHandler implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         * Create a Transferable to use as the source for a data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         * @param c  The component holding the data to be transfered.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         * @return  The representation of the data to be transfered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        protected Transferable createTransferable(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            Object[] values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            if (c instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                values = ((JList)c).getSelectedValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            } else if (c instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                JTable table = (JTable)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                int[] rows = table.getSelectedRows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                if (rows != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    values = new Object[rows.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    for (int i=0; i<rows.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                        values[i] = table.getValueAt(rows[i], 0);
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
            if (values == null || values.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            StringBuffer plainBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            StringBuffer htmlBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            htmlBuf.append("<html>\n<body>\n<ul>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
  1299
            for (Object obj : values) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                String val = ((obj == null) ? "" : obj.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                plainBuf.append(val + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                htmlBuf.append("  <li>" + val + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            // remove the last newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            plainBuf.deleteCharAt(plainBuf.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            htmlBuf.append("</ul>\n</body>\n</html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            return COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        static class FileTransferable extends BasicTransferable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            Object[] fileData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            FileTransferable(String plainData, String htmlData, Object[] fileData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                super(plainData, htmlData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                this.fileData = fileData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
             * Best format of the file chooser is DataFlavor.javaFileListFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            protected DataFlavor[] getRicherFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                DataFlavor[] flavors = new DataFlavor[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                flavors[0] = DataFlavor.javaFileListFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
             * The only richer format supported is the file list flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            protected Object getRicherData(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                if (DataFlavor.javaFileListFlavor.equals(flavor)) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
  1339
                    ArrayList<Object> files = new ArrayList<Object>();
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
  1340
                    for (Object file : this.fileData) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 680
diff changeset
  1341
                        files.add(file);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    return files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
}