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