1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.datatransfer.*;
  30 import java.awt.dnd.*;
  31 import java.awt.event.*;
  32 import java.beans.*;
  33 import java.io.File;
  34 import java.net.URI;
  35 import java.text.DateFormat;
  36 import java.util.*;
  37 
  38 import javax.swing.*;
  39 import javax.swing.border.Border;
  40 import javax.swing.event.*;
  41 import javax.swing.filechooser.*;
  42 import javax.swing.plaf.*;
  43 import javax.swing.table.*;
  44 
  45 import sun.swing.SwingUtilities2;
  46 
  47 public class AquaFileChooserUI extends FileChooserUI {
  48     /* FileView icons */
  49     protected Icon directoryIcon = null;
  50     protected Icon fileIcon = null;
  51     protected Icon computerIcon = null;
  52     protected Icon hardDriveIcon = null;
  53     protected Icon floppyDriveIcon = null;
  54 
  55     protected Icon upFolderIcon = null;
  56     protected Icon homeFolderIcon = null;
  57     protected Icon listViewIcon = null;
  58     protected Icon detailsViewIcon = null;
  59 
  60     protected int saveButtonMnemonic = 0;
  61     protected int openButtonMnemonic = 0;
  62     protected int cancelButtonMnemonic = 0;
  63     protected int updateButtonMnemonic = 0;
  64     protected int helpButtonMnemonic = 0;
  65     protected int chooseButtonMnemonic = 0;
  66 
  67     private String saveTitleText = null;
  68     private String openTitleText = null;
  69     String newFolderTitleText = null;
  70 
  71     protected String saveButtonText = null;
  72     protected String openButtonText = null;
  73     protected String cancelButtonText = null;
  74     protected String updateButtonText = null;
  75     protected String helpButtonText = null;
  76     protected String newFolderButtonText = null;
  77     protected String chooseButtonText = null;
  78 
  79     //private String newFolderErrorSeparator = null;
  80     String newFolderErrorText = null;
  81     String newFolderExistsErrorText = null;
  82     protected String fileDescriptionText = null;
  83     protected String directoryDescriptionText = null;
  84 
  85     protected String saveButtonToolTipText = null;
  86     protected String openButtonToolTipText = null;
  87     protected String cancelButtonToolTipText = null;
  88     protected String updateButtonToolTipText = null;
  89     protected String helpButtonToolTipText = null;
  90     protected String chooseItemButtonToolTipText = null; // Choose anything
  91     protected String chooseFolderButtonToolTipText = null; // Choose folder
  92     protected String directoryComboBoxToolTipText = null;
  93     protected String filenameTextFieldToolTipText = null;
  94     protected String filterComboBoxToolTipText = null;
  95     protected String openDirectoryButtonToolTipText = null;
  96 
  97     protected String cancelOpenButtonToolTipText = null;
  98     protected String cancelSaveButtonToolTipText = null;
  99     protected String cancelChooseButtonToolTipText = null;
 100     protected String cancelNewFolderButtonToolTipText = null;
 101 
 102     protected String desktopName = null;
 103     String newFolderDialogPrompt = null;
 104     String newFolderDefaultName = null;
 105     private String newFileDefaultName = null;
 106     String createButtonText = null;
 107 
 108     JFileChooser filechooser = null;
 109 
 110     private MouseListener doubleClickListener = null;
 111     private PropertyChangeListener propertyChangeListener = null;
 112     private AncestorListener ancestorListener = null;
 113     private DropTarget dragAndDropTarget = null;
 114 
 115     private final AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter();
 116 
 117     private AquaFileSystemModel model;
 118 
 119     final AquaFileView fileView = new AquaFileView(this);
 120 
 121     boolean selectionInProgress = false;
 122 
 123     // The accessoryPanel is a container to place the JFileChooser accessory component
 124     private JPanel accessoryPanel = null;
 125 
 126     //
 127     // ComponentUI Interface Implementation methods
 128     //
 129     public static ComponentUI createUI(final JComponent c) {
 130         return new AquaFileChooserUI((JFileChooser)c);
 131     }
 132 
 133     public AquaFileChooserUI(final JFileChooser filechooser) {
 134         super();
 135     }
 136 
 137     public void installUI(final JComponent c) {
 138         accessoryPanel = new JPanel(new BorderLayout());
 139         filechooser = (JFileChooser)c;
 140 
 141         createModel();
 142 
 143         installDefaults(filechooser);
 144         installComponents(filechooser);
 145         installListeners(filechooser);
 146 
 147         AquaUtils.enforceComponentOrientation(filechooser, ComponentOrientation.getOrientation(Locale.getDefault()));
 148     }
 149 
 150     public void uninstallUI(final JComponent c) {
 151         uninstallListeners(filechooser);
 152         uninstallComponents(filechooser);
 153         uninstallDefaults(filechooser);
 154 
 155         if (accessoryPanel != null) {
 156             accessoryPanel.removeAll();
 157         }
 158 
 159         accessoryPanel = null;
 160         getFileChooser().removeAll();
 161     }
 162 
 163     protected void installListeners(final JFileChooser fc) {
 164         doubleClickListener = createDoubleClickListener(fc, fFileList);
 165         fFileList.addMouseListener(doubleClickListener);
 166 
 167         propertyChangeListener = createPropertyChangeListener(fc);
 168         if (propertyChangeListener != null) {
 169             fc.addPropertyChangeListener(propertyChangeListener);
 170         }
 171         if (model != null) fc.addPropertyChangeListener(model);
 172 
 173         ancestorListener = new AncestorListener(){
 174             public void ancestorAdded(final AncestorEvent e) {
 175                 // Request defaultness for the appropriate button based on mode
 176                 setFocusForMode(getFileChooser());
 177                 // Request defaultness for the appropriate button based on mode
 178                 setDefaultButtonForMode(getFileChooser());
 179             }
 180 
 181             public void ancestorRemoved(final AncestorEvent e) {
 182             }
 183 
 184             public void ancestorMoved(final AncestorEvent e) {
 185             }
 186         };
 187         fc.addAncestorListener(ancestorListener);
 188 
 189         fc.registerKeyboardAction(new CancelSelectionAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
 190         dragAndDropTarget = new DropTarget(fc, DnDConstants.ACTION_COPY, new DnDHandler(), true);
 191         fc.setDropTarget(dragAndDropTarget);
 192     }
 193 
 194     protected void uninstallListeners(final JFileChooser fc) {
 195         if (propertyChangeListener != null) {
 196             fc.removePropertyChangeListener(propertyChangeListener);
 197         }
 198         fFileList.removeMouseListener(doubleClickListener);
 199         fc.removePropertyChangeListener(model);
 200         fc.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
 201         fc.removeAncestorListener(ancestorListener);
 202         fc.setDropTarget(null);
 203         ancestorListener = null;
 204     }
 205 
 206     protected void installDefaults(final JFileChooser fc) {
 207         installIcons(fc);
 208         installStrings(fc);
 209         setPackageIsTraversable(fc.getClientProperty(PACKAGE_TRAVERSABLE_PROPERTY));
 210         setApplicationIsTraversable(fc.getClientProperty(APPLICATION_TRAVERSABLE_PROPERTY));
 211     }
 212 
 213     protected void installIcons(final JFileChooser fc) {
 214         directoryIcon = UIManager.getIcon("FileView.directoryIcon");
 215         fileIcon = UIManager.getIcon("FileView.fileIcon");
 216         computerIcon = UIManager.getIcon("FileView.computerIcon");
 217         hardDriveIcon = UIManager.getIcon("FileView.hardDriveIcon");
 218     }
 219 
 220     String getString(final String uiKey, final String fallback) {
 221         final String result = UIManager.getString(uiKey);
 222         return (result == null ? fallback : result);
 223     }
 224 
 225     protected void installStrings(final JFileChooser fc) {
 226         // Exist in basic.properties (though we might want to override)
 227         fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText");
 228         directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText");
 229         newFolderErrorText = getString("FileChooser.newFolderErrorText", "Error occurred during folder creation");
 230 
 231         saveButtonText = UIManager.getString("FileChooser.saveButtonText");
 232         openButtonText = UIManager.getString("FileChooser.openButtonText");
 233         cancelButtonText = UIManager.getString("FileChooser.cancelButtonText");
 234         updateButtonText = UIManager.getString("FileChooser.updateButtonText");
 235         helpButtonText = UIManager.getString("FileChooser.helpButtonText");
 236 
 237         saveButtonMnemonic = UIManager.getInt("FileChooser.saveButtonMnemonic");
 238         openButtonMnemonic = UIManager.getInt("FileChooser.openButtonMnemonic");
 239         cancelButtonMnemonic = UIManager.getInt("FileChooser.cancelButtonMnemonic");
 240         updateButtonMnemonic = UIManager.getInt("FileChooser.updateButtonMnemonic");
 241         helpButtonMnemonic = UIManager.getInt("FileChooser.helpButtonMnemonic");
 242         chooseButtonMnemonic = UIManager.getInt("FileChooser.chooseButtonMnemonic");
 243 
 244         saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText");
 245         openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText");
 246         cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText");
 247         updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText");
 248         helpButtonToolTipText = UIManager.getString("FileChooser.helpButtonToolTipText");
 249 
 250         // Mac-specific, but fallback to basic if it's missing
 251         saveTitleText = getString("FileChooser.saveTitleText", saveButtonText);
 252         openTitleText = getString("FileChooser.openTitleText", openButtonText);
 253 
 254         // Mac-specific, required
 255         newFolderExistsErrorText = getString("FileChooser.newFolderExistsErrorText", "That name is already taken");
 256         chooseButtonText = getString("FileChooser.chooseButtonText", "Choose");
 257         newFolderButtonText = getString("FileChooser.newFolderButtonText", "New");
 258         newFolderTitleText = getString("FileChooser.newFolderTitleText", "New Folder");
 259 
 260         if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
 261             fileNameLabelText = getString("FileChooser.saveDialogFileNameLabelText", "Save As:");
 262         } else {
 263             fileNameLabelText = getString("FileChooser.fileNameLabelText", "Name:");
 264         }
 265 
 266         filesOfTypeLabelText = getString("FileChooser.filesOfTypeLabelText", "Format:");
 267 
 268         desktopName = getString("FileChooser.desktopName", "Desktop");
 269         newFolderDialogPrompt = getString("FileChooser.newFolderPromptText", "Name of new folder:");
 270         newFolderDefaultName = getString("FileChooser.untitledFolderName", "untitled folder");
 271         newFileDefaultName = getString("FileChooser.untitledFileName", "untitled");
 272         createButtonText = getString("FileChooser.createButtonText", "Create");
 273 
 274         fColumnNames[1] = getString("FileChooser.byDateText", "Date Modified");
 275         fColumnNames[0] = getString("FileChooser.byNameText", "Name");
 276 
 277         // Mac-specific, optional
 278         chooseItemButtonToolTipText = UIManager.getString("FileChooser.chooseItemButtonToolTipText");
 279         chooseFolderButtonToolTipText = UIManager.getString("FileChooser.chooseFolderButtonToolTipText");
 280         openDirectoryButtonToolTipText = UIManager.getString("FileChooser.openDirectoryButtonToolTipText");
 281 
 282         directoryComboBoxToolTipText = UIManager.getString("FileChooser.directoryComboBoxToolTipText");
 283         filenameTextFieldToolTipText = UIManager.getString("FileChooser.filenameTextFieldToolTipText");
 284         filterComboBoxToolTipText = UIManager.getString("FileChooser.filterComboBoxToolTipText");
 285 
 286         cancelOpenButtonToolTipText = UIManager.getString("FileChooser.cancelOpenButtonToolTipText");
 287         cancelSaveButtonToolTipText = UIManager.getString("FileChooser.cancelSaveButtonToolTipText");
 288         cancelChooseButtonToolTipText = UIManager.getString("FileChooser.cancelChooseButtonToolTipText");
 289         cancelNewFolderButtonToolTipText = UIManager.getString("FileChooser.cancelNewFolderButtonToolTipText");
 290 
 291         newFolderTitleText = UIManager.getString("FileChooser.newFolderTitleText");
 292         newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText");
 293         newFolderAccessibleName = getString("FileChooser.newFolderAccessibleName", newFolderTitleText);
 294     }
 295 
 296     protected void uninstallDefaults(final JFileChooser fc) {
 297         uninstallIcons(fc);
 298         uninstallStrings(fc);
 299     }
 300 
 301     protected void uninstallIcons(final JFileChooser fc) {
 302         directoryIcon = null;
 303         fileIcon = null;
 304         computerIcon = null;
 305         hardDriveIcon = null;
 306         floppyDriveIcon = null;
 307 
 308         upFolderIcon = null;
 309         homeFolderIcon = null;
 310         detailsViewIcon = null;
 311         listViewIcon = null;
 312     }
 313 
 314     protected void uninstallStrings(final JFileChooser fc) {
 315         saveTitleText = null;
 316         openTitleText = null;
 317         newFolderTitleText = null;
 318 
 319         saveButtonText = null;
 320         openButtonText = null;
 321         cancelButtonText = null;
 322         updateButtonText = null;
 323         helpButtonText = null;
 324         newFolderButtonText = null;
 325         chooseButtonText = null;
 326 
 327         cancelOpenButtonToolTipText = null;
 328         cancelSaveButtonToolTipText = null;
 329         cancelChooseButtonToolTipText = null;
 330         cancelNewFolderButtonToolTipText = null;
 331 
 332         saveButtonToolTipText = null;
 333         openButtonToolTipText = null;
 334         cancelButtonToolTipText = null;
 335         updateButtonToolTipText = null;
 336         helpButtonToolTipText = null;
 337         chooseItemButtonToolTipText = null;
 338         chooseFolderButtonToolTipText = null;
 339         openDirectoryButtonToolTipText = null;
 340         directoryComboBoxToolTipText = null;
 341         filenameTextFieldToolTipText = null;
 342         filterComboBoxToolTipText = null;
 343 
 344         newFolderDefaultName = null;
 345         newFileDefaultName = null;
 346 
 347         desktopName = null;
 348     }
 349 
 350     protected void createModel() {
 351     }
 352 
 353     AquaFileSystemModel getModel() {
 354         return model;
 355     }
 356 
 357     /*
 358      * Listen for filechooser property changes, such as
 359      * the selected file changing, or the type of the dialog changing.
 360      */
 361     // Taken almost verbatim from Metal
 362     protected PropertyChangeListener createPropertyChangeListener(final JFileChooser fc) {
 363         return new PropertyChangeListener(){
 364             public void propertyChange(final PropertyChangeEvent e) {
 365                 final String prop = e.getPropertyName();
 366                 if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
 367                     final File f = (File)e.getNewValue();
 368                     if (f != null) {
 369                         // Select the file in the list if the selected file didn't change as
 370                         // a result of a list click.
 371                         if (!selectionInProgress && getModel().contains(f)) {
 372                             fFileList.setSelectedIndex(getModel().indexOf(f));
 373                         }
 374 
 375                         // [3643835] Need to populate the text field here.  No-op on Open dialogs
 376                         // Note that this was removed for 3514735, but should not have been.
 377                         if (!f.isDirectory()) {
 378                             setFileName(getFileChooser().getName(f));
 379                         }
 380                     }
 381                     updateButtonState(getFileChooser());
 382                 } else if (prop.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {
 383                     JFileChooser fileChooser = getFileChooser();
 384                     if (!fileChooser.isDirectorySelectionEnabled()) {
 385                         final File[] files = (File[]) e.getNewValue();
 386                         if (files != null) {
 387                             for (int selectedRow : fFileList.getSelectedRows()) {
 388                                 File file = (File) fFileList.getValueAt(selectedRow, 0);
 389                                 if (fileChooser.isTraversable(file)) {
 390                                     fFileList.removeSelectedIndex(selectedRow);
 391                                 }
 392                             }
 393                         }
 394                     }
 395                 } else if (prop.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
 396                     fFileList.clearSelection();
 397                     final File currentDirectory = getFileChooser().getCurrentDirectory();
 398                     if (currentDirectory != null) {
 399                         fDirectoryComboBoxModel.addItem(currentDirectory);
 400                         // Enable the newFolder action if the current directory
 401                         // is writable.
 402                         // PENDING(jeff) - broken - fix
 403                         getAction(kNewFolder).setEnabled(currentDirectory.canWrite());
 404                     }
 405                     updateButtonState(getFileChooser());
 406                 } else if (prop.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
 407                     fFileList.clearSelection();
 408                     setBottomPanelForMode(getFileChooser()); // Also updates approve button
 409                 } else if (prop == JFileChooser.ACCESSORY_CHANGED_PROPERTY) {
 410                     if (getAccessoryPanel() != null) {
 411                         if (e.getOldValue() != null) {
 412                             getAccessoryPanel().remove((JComponent)e.getOldValue());
 413                         }
 414                         final JComponent accessory = (JComponent)e.getNewValue();
 415                         if (accessory != null) {
 416                             getAccessoryPanel().add(accessory, BorderLayout.CENTER);
 417                         }
 418                     }
 419                 } else if (prop == JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY) {
 420                     updateApproveButton(getFileChooser());
 421                     getFileChooser().invalidate();
 422                 } else if (prop == JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY) {
 423                     if (getFileChooser().getDialogType() == JFileChooser.SAVE_DIALOG) {
 424                         fileNameLabelText = getString("FileChooser.saveDialogFileNameLabelText", "Save As:");
 425                     } else {
 426                         fileNameLabelText = getString("FileChooser.fileNameLabelText", "Name:");
 427                     }
 428                     fTextFieldLabel.setText(fileNameLabelText);
 429 
 430                     // Mac doesn't show the text field or "new folder" button in 'Open' dialogs
 431                     setBottomPanelForMode(getFileChooser()); // Also updates approve button
 432                 } else if (prop.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY)) {
 433                     getApproveButton(getFileChooser()).setMnemonic(getApproveButtonMnemonic(getFileChooser()));
 434                 } else if (prop.equals(PACKAGE_TRAVERSABLE_PROPERTY)) {
 435                     setPackageIsTraversable(e.getNewValue());
 436                 } else if (prop.equals(APPLICATION_TRAVERSABLE_PROPERTY)) {
 437                     setApplicationIsTraversable(e.getNewValue());
 438                 } else if (prop.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY)) {
 439                     if (getFileChooser().isMultiSelectionEnabled()) {
 440                         fFileList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
 441                     } else {
 442                         fFileList.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 443                     }
 444                 } else if (prop.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) {
 445                     doControlButtonsChanged(e);
 446                 }
 447             }
 448         };
 449     }
 450 
 451     void setPackageIsTraversable(final Object o) {
 452         int newProp = -1;
 453         if (o != null && o instanceof String) newProp = parseTraversableProperty((String)o);
 454         if (newProp != -1) fPackageIsTraversable = newProp;
 455         else fPackageIsTraversable = sGlobalPackageIsTraversable;
 456     }
 457 
 458     void setApplicationIsTraversable(final Object o) {
 459         int newProp = -1;
 460         if (o != null && o instanceof String) newProp = parseTraversableProperty((String)o);
 461         if (newProp != -1) fApplicationIsTraversable = newProp;
 462         else fApplicationIsTraversable = sGlobalApplicationIsTraversable;
 463     }
 464 
 465     void doControlButtonsChanged(final PropertyChangeEvent e) {
 466         if (getFileChooser().getControlButtonsAreShown()) {
 467             fBottomPanel.add(fDirectoryPanelSpacer);
 468             fBottomPanel.add(fDirectoryPanel);
 469         } else {
 470             fBottomPanel.remove(fDirectoryPanelSpacer);
 471             fBottomPanel.remove(fDirectoryPanel);
 472         }
 473     }
 474 
 475     public String getFileName() {
 476         if (filenameTextField != null) { return filenameTextField.getText(); }
 477         return null;
 478     }
 479 
 480     public String getDirectoryName() {
 481         // PENDING(jeff) - get the name from the directory combobox
 482         return null;
 483     }
 484 
 485     public void setFileName(final String filename) {
 486         if (filenameTextField != null) {
 487             filenameTextField.setText(filename);
 488         }
 489     }
 490 
 491     public void setDirectoryName(final String dirname) {
 492         // PENDING(jeff) - set the name in the directory combobox
 493     }
 494 
 495     public void rescanCurrentDirectory(final JFileChooser fc) {
 496         getModel().invalidateFileCache();
 497         getModel().validateFileCache();
 498     }
 499 
 500     public void ensureFileIsVisible(final JFileChooser fc, final File f) {
 501         if (f == null) {
 502             fFileList.requestFocusInWindow();
 503             fFileList.ensureIndexIsVisible(-1);
 504             return;
 505         }
 506 
 507         getModel().runWhenDone(new Runnable() {
 508             public void run() {
 509                 fFileList.requestFocusInWindow();
 510                 fFileList.ensureIndexIsVisible(getModel().indexOf(f));
 511             }
 512         });
 513     }
 514 
 515     public JFileChooser getFileChooser() {
 516         return filechooser;
 517     }
 518 
 519     public JPanel getAccessoryPanel() {
 520         return accessoryPanel;
 521     }
 522 
 523     protected JButton getApproveButton(final JFileChooser fc) {
 524         return fApproveButton;
 525     }
 526 
 527     public int getApproveButtonMnemonic(final JFileChooser fc) {
 528         return fSubPanel.getApproveButtonMnemonic(fc);
 529     }
 530 
 531     public String getApproveButtonToolTipText(final JFileChooser fc) {
 532         return fSubPanel.getApproveButtonToolTipText(fc);
 533     }
 534 
 535     public String getApproveButtonText(final JFileChooser fc) {
 536         return fSubPanel.getApproveButtonText(fc);
 537     }
 538 
 539     protected String getCancelButtonToolTipText(final JFileChooser fc) {
 540         return fSubPanel.getCancelButtonToolTipText(fc);
 541     }
 542 
 543     // If the item's not selectable, it'll be visible but disabled in the list
 544     boolean isSelectableInList(final File f) {
 545         return fSubPanel.isSelectableInList(getFileChooser(), f);
 546     }
 547 
 548     // Is this a file that the JFileChooser wants?
 549     // Directories can be selected in the list regardless of mode
 550     boolean isSelectableForMode(final JFileChooser fc, final File f) {
 551         if (f == null) return false;
 552         final int mode = fc.getFileSelectionMode();
 553         if (mode == JFileChooser.FILES_AND_DIRECTORIES) return true;
 554         boolean traversable = fc.isTraversable(f);
 555         if (mode == JFileChooser.DIRECTORIES_ONLY) return traversable;
 556         return !traversable;
 557     }
 558 
 559     // ********************************************
 560     // ************ Create Listeners **************
 561     // ********************************************
 562 
 563     // From Basic
 564     public ListSelectionListener createListSelectionListener(final JFileChooser fc) {
 565         return new SelectionListener();
 566     }
 567 
 568     protected class SelectionListener implements ListSelectionListener {
 569         public void valueChanged(final ListSelectionEvent e) {
 570             if (e.getValueIsAdjusting()) return;
 571 
 572             File f = null;
 573             final int selectedRow = fFileList.getSelectedRow();
 574             final JFileChooser chooser = getFileChooser();
 575             boolean isSave = (chooser.getDialogType() == JFileChooser.SAVE_DIALOG);
 576             if (selectedRow >= 0) {
 577                 f = (File)fFileList.getValueAt(selectedRow, 0);
 578             }
 579 
 580             // Save dialog lists can't be multi select, because all we're selecting is the next folder to open
 581             selectionInProgress = true;
 582             if (!isSave && chooser.isMultiSelectionEnabled()) {
 583                 final int[] rows = fFileList.getSelectedRows();
 584                 int selectableCount = 0;
 585                 // Double-check that all the list selections are valid for this mode
 586                 // Directories can be selected in the list regardless of mode
 587                 if (rows.length > 0) {
 588                     for (final int element : rows) {
 589                         if (isSelectableForMode(chooser, (File)fFileList.getValueAt(element, 0))) selectableCount++;
 590                     }
 591                 }
 592                 if (selectableCount > 0) {
 593                     final File[] files = new File[selectableCount];
 594                     for (int i = 0, si = 0; i < rows.length; i++) {
 595                         f = (File)fFileList.getValueAt(rows[i], 0);
 596                         if (isSelectableForMode(chooser, f)) {
 597                             if (fileView.isAlias(f)) {
 598                                 f = fileView.resolveAlias(f);
 599                             }
 600                             files[si++] = f;
 601                         }
 602                     }
 603                     chooser.setSelectedFiles(files);
 604                 } else {
 605                     chooser.setSelectedFiles(null);
 606                 }
 607             } else {
 608                 chooser.setSelectedFiles(null);
 609                 chooser.setSelectedFile(f);
 610             }
 611             selectionInProgress = false;
 612         }
 613     }
 614 
 615     // When the Save textfield has the focus, the button should say "Save"
 616     // Otherwise, it depends on the list selection
 617     protected class SaveTextFocusListener implements FocusListener {
 618         public void focusGained(final FocusEvent e) {
 619             updateButtonState(getFileChooser());
 620         }
 621 
 622         // Do nothing, we might be losing focus due to window deactivation
 623         public void focusLost(final FocusEvent e) {
 624 
 625         }
 626     }
 627 
 628     // When the Save textfield is empty and the button says "Save", it should be disabled
 629     // Otherwise, it depends on the list selection
 630     protected class SaveTextDocumentListener implements DocumentListener {
 631         public void insertUpdate(final DocumentEvent e) {
 632             textChanged();
 633         }
 634 
 635         public void removeUpdate(final DocumentEvent e) {
 636             textChanged();
 637         }
 638 
 639         public void changedUpdate(final DocumentEvent e) {
 640 
 641         }
 642 
 643         void textChanged() {
 644             updateButtonState(getFileChooser());
 645         }
 646     }
 647 
 648     // Opens the File object if it's a traversable directory
 649     protected boolean openDirectory(final File f) {
 650         if (getFileChooser().isTraversable(f)) {
 651             fFileList.clearSelection();
 652             // Resolve any aliases
 653             final File original = fileView.resolveAlias(f);
 654             getFileChooser().setCurrentDirectory(original);
 655             updateButtonState(getFileChooser());
 656             return true;
 657         }
 658         return false;
 659     }
 660 
 661     // From Basic
 662     protected class DoubleClickListener extends MouseAdapter {
 663         JTableExtension list;
 664 
 665         public DoubleClickListener(final JTableExtension list) {
 666             this.list = list;
 667         }
 668 
 669         public void mouseClicked(final MouseEvent e) {
 670             if (e.getClickCount() != 2) return;
 671 
 672             final int index = list.locationToIndex(e.getPoint());
 673             if (index < 0) return;
 674 
 675             final File f = (File)((AquaFileSystemModel)list.getModel()).getElementAt(index);
 676             if (openDirectory(f)) return;
 677 
 678             if (!isSelectableInList(f)) return;
 679             getFileChooser().approveSelection();
 680         }
 681     }
 682 
 683     protected MouseListener createDoubleClickListener(final JFileChooser fc, final JTableExtension list) {
 684         return new DoubleClickListener(list);
 685     }
 686 
 687     // listens for drag events onto the JFileChooser and sets the selected file or directory
 688     class DnDHandler extends DropTargetAdapter {
 689         public void dragEnter(final DropTargetDragEvent dtde) {
 690             tryToAcceptDrag(dtde);
 691         }
 692 
 693         public void dragOver(final DropTargetDragEvent dtde) {
 694             tryToAcceptDrag(dtde);
 695         }
 696 
 697         public void dropActionChanged(final DropTargetDragEvent dtde) {
 698             tryToAcceptDrag(dtde);
 699         }
 700 
 701         public void drop(final DropTargetDropEvent dtde) {
 702             if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
 703                 handleFileDropEvent(dtde);
 704                 return;
 705             }
 706 
 707             if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
 708                 handleStringDropEvent(dtde);
 709                 return;
 710             }
 711         }
 712 
 713         protected void tryToAcceptDrag(final DropTargetDragEvent dtde) {
 714             if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
 715                 dtde.acceptDrag(DnDConstants.ACTION_COPY);
 716                 return;
 717             }
 718 
 719             dtde.rejectDrag();
 720         }
 721 
 722         protected void handleFileDropEvent(final DropTargetDropEvent dtde) {
 723             dtde.acceptDrop(dtde.getDropAction());
 724             final Transferable transferable = dtde.getTransferable();
 725 
 726             try {
 727                 final java.util.List<File> fileList = (java.util.List<File>)transferable.getTransferData(DataFlavor.javaFileListFlavor);
 728                 dropFiles(fileList.toArray(new File[fileList.size()]));
 729                 dtde.dropComplete(true);
 730             } catch (final Exception e) {
 731                 dtde.dropComplete(false);
 732             }
 733         }
 734 
 735         protected void handleStringDropEvent(final DropTargetDropEvent dtde) {
 736             dtde.acceptDrop(dtde.getDropAction());
 737             final Transferable transferable = dtde.getTransferable();
 738 
 739             final String stringData;
 740             try {
 741                 stringData = (String)transferable.getTransferData(DataFlavor.stringFlavor);
 742             } catch (final Exception e) {
 743                 dtde.dropComplete(false);
 744                 return;
 745             }
 746 
 747             try {
 748                 final File fileAsPath = new File(stringData);
 749                 if (fileAsPath.exists()) {
 750                     dropFiles(new File[] {fileAsPath});
 751                     dtde.dropComplete(true);
 752                     return;
 753                 }
 754             } catch (final Exception e) {
 755                 // try again
 756             }
 757 
 758             try {
 759                 final File fileAsURI = new File(new URI(stringData));
 760                 if (fileAsURI.exists()) {
 761                     dropFiles(new File[] {fileAsURI});
 762                     dtde.dropComplete(true);
 763                     return;
 764                 }
 765             } catch (final Exception e) {
 766                 // nothing more to do
 767             }
 768 
 769             dtde.dropComplete(false);
 770         }
 771 
 772         protected void dropFiles(final File[] files) {
 773             final JFileChooser jfc = getFileChooser();
 774 
 775             if (files.length == 1) {
 776                 if (files[0].isDirectory()) {
 777                     jfc.setCurrentDirectory(files[0]);
 778                     return;
 779                 }
 780 
 781                 if (!isSelectableForMode(jfc, files[0])) {
 782                     return;
 783                 }
 784             }
 785 
 786             jfc.setSelectedFiles(files);
 787             for (final File file : files) {
 788                 jfc.ensureFileIsVisible(file);
 789             }
 790             getModel().runWhenDone(new Runnable() {
 791                 public void run() {
 792                     final AquaFileSystemModel fileSystemModel = getModel();
 793                     for (final File element : files) {
 794                         final int index = fileSystemModel.indexOf(element);
 795                         if (index >= 0) fFileList.addRowSelectionInterval(index, index);
 796                     }
 797                 }
 798             });
 799         }
 800     }
 801 
 802     // FileChooser UI PLAF methods
 803 
 804     /**
 805      * Returns the default accept all file filter
 806      */
 807     public FileFilter getAcceptAllFileFilter(final JFileChooser fc) {
 808         return acceptAllFileFilter;
 809     }
 810 
 811     public FileView getFileView(final JFileChooser fc) {
 812         return fileView;
 813     }
 814 
 815     /**
 816      * Returns the title of this dialog
 817      */
 818     public String getDialogTitle(final JFileChooser fc) {
 819         if (fc.getDialogTitle() == null) {
 820             if (getFileChooser().getDialogType() == JFileChooser.OPEN_DIALOG) {
 821                 return openTitleText;
 822             } else if (getFileChooser().getDialogType() == JFileChooser.SAVE_DIALOG) { return saveTitleText; }
 823         }
 824         return fc.getDialogTitle();
 825     }
 826 
 827     // Utility to get the first selected item regardless of whether we're single or multi select
 828     File getFirstSelectedItem() {
 829         // Get the selected item
 830         File selectedFile = null;
 831         final int index = fFileList.getSelectedRow();
 832         if (index >= 0) {
 833             selectedFile = (File)((AquaFileSystemModel)fFileList.getModel()).getElementAt(index);
 834         }
 835         return selectedFile;
 836     }
 837 
 838     // Make a file from the filename
 839     File makeFile(final JFileChooser fc, final String filename) {
 840         File selectedFile = null;
 841         // whitespace is legal on Macs, even on beginning and end of filename
 842         if (filename != null && !filename.equals("")) {
 843             final FileSystemView fs = fc.getFileSystemView();
 844             selectedFile = fs.createFileObject(filename);
 845             if (!selectedFile.isAbsolute()) {
 846                 selectedFile = fs.createFileObject(fc.getCurrentDirectory(), filename);
 847             }
 848         }
 849         return selectedFile;
 850     }
 851 
 852     // Utility to tell if the textfield has anything in it
 853     boolean textfieldIsValid() {
 854         final String s = getFileName();
 855         return (s != null && !s.equals(""));
 856     }
 857 
 858     // Action to attach to the file list so we can override the default action
 859     // of the table for the return key, which is to select the next line.
 860     @SuppressWarnings("serial") // Superclass is not serializable across versions
 861     protected class DefaultButtonAction extends AbstractAction {
 862         public void actionPerformed(final ActionEvent e) {
 863             final JRootPane root = AquaFileChooserUI.this.getFileChooser().getRootPane();
 864             final JFileChooser fc = AquaFileChooserUI.this.getFileChooser();
 865             final JButton owner = root.getDefaultButton();
 866             if (owner != null && SwingUtilities.getRootPane(owner) == root && owner.isEnabled()) {
 867                 owner.doClick(20);
 868             } else if (!fc.getControlButtonsAreShown()) {
 869                 final JButton defaultButton = AquaFileChooserUI.this.fSubPanel.getDefaultButton(fc);
 870 
 871                 if (defaultButton != null) {
 872                     defaultButton.doClick(20);
 873                 }
 874             } else {
 875                 Toolkit.getDefaultToolkit().beep();
 876             }
 877         }
 878 
 879         public boolean isEnabled() {
 880             return true;
 881         }
 882     }
 883 
 884     /**
 885      * Creates a new folder.
 886      */
 887     @SuppressWarnings("serial") // Superclass is not serializable across versions
 888     protected class NewFolderAction extends AbstractAction {
 889         protected NewFolderAction() {
 890             super(newFolderAccessibleName);
 891         }
 892 
 893         // Muchlike showInputDialog, but we give it options instead of selectionValues
 894         private Object showNewFolderDialog(final Component parentComponent, final Object message, final String title, final int messageType, final Icon icon, final Object[] options, final Object initialSelectionValue) {
 895             final JOptionPane pane = new JOptionPane(message, messageType, JOptionPane.OK_CANCEL_OPTION, icon, options, null);
 896 
 897             pane.setWantsInput(true);
 898             pane.setInitialSelectionValue(initialSelectionValue);
 899 
 900             final JDialog dialog = pane.createDialog(parentComponent, title);
 901 
 902             pane.selectInitialValue();
 903             dialog.setVisible(true);
 904             dialog.dispose();
 905 
 906             final Object value = pane.getValue();
 907 
 908             if (value == null || value.equals(cancelButtonText)) {
 909                 return null;
 910             }
 911             return pane.getInputValue();
 912         }
 913 
 914         public void actionPerformed(final ActionEvent e) {
 915             final JFileChooser fc = getFileChooser();
 916             final File currentDirectory = fc.getCurrentDirectory();
 917             File newFolder = null;
 918             final String[] options = {createButtonText, cancelButtonText};
 919             final String filename = (String)showNewFolderDialog(fc, //parentComponent
 920                     newFolderDialogPrompt, // message
 921                     newFolderTitleText, // title
 922                     JOptionPane.PLAIN_MESSAGE, // messageType
 923                     null, // icon
 924                     options, // selectionValues
 925                     newFolderDefaultName); // initialSelectionValue
 926 
 927             if (filename != null) {
 928                 try {
 929                     newFolder = fc.getFileSystemView().createFileObject(currentDirectory, filename);
 930                     if (newFolder.exists()) {
 931                         JOptionPane.showMessageDialog(fc, newFolderExistsErrorText, "", JOptionPane.ERROR_MESSAGE);
 932                         return;
 933                     }
 934 
 935                     newFolder.mkdirs();
 936                 } catch(final Exception exc) {
 937                     JOptionPane.showMessageDialog(fc, newFolderErrorText, "", JOptionPane.ERROR_MESSAGE);
 938                     return;
 939                 }
 940 
 941                 openDirectory(newFolder);
 942             }
 943         }
 944     }
 945 
 946     /**
 947      * Responds to an Open, Save, or Choose request
 948      */
 949     @SuppressWarnings("serial") // Superclass is not serializable across versions
 950     protected class ApproveSelectionAction extends AbstractAction {
 951         public void actionPerformed(final ActionEvent e) {
 952             fSubPanel.approveSelection(getFileChooser());
 953         }
 954     }
 955 
 956     /**
 957      * Responds to an OpenDirectory request
 958      */
 959     @SuppressWarnings("serial") // Superclass is not serializable across versions
 960     protected class OpenSelectionAction extends AbstractAction {
 961         public void actionPerformed(final ActionEvent e) {
 962             final int index = fFileList.getSelectedRow();
 963             if (index >= 0) {
 964                 final File selectedFile = (File)((AquaFileSystemModel)fFileList.getModel()).getElementAt(index);
 965                 if (selectedFile != null) openDirectory(selectedFile);
 966             }
 967         }
 968     }
 969 
 970     /**
 971      * Responds to a cancel request.
 972      */
 973     @SuppressWarnings("serial") // Superclass is not serializable across versions
 974     protected class CancelSelectionAction extends AbstractAction {
 975         public void actionPerformed(final ActionEvent e) {
 976             getFileChooser().cancelSelection();
 977         }
 978 
 979         public boolean isEnabled() {
 980             return getFileChooser().isEnabled();
 981         }
 982     }
 983 
 984     /**
 985      * Rescans the files in the current directory
 986      */
 987     @SuppressWarnings("serial") // Superclass is not serializable across versions
 988     protected class UpdateAction extends AbstractAction {
 989         public void actionPerformed(final ActionEvent e) {
 990             final JFileChooser fc = getFileChooser();
 991             fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
 992             fc.rescanCurrentDirectory();
 993         }
 994     }
 995 
 996     // *****************************************
 997     // ***** default AcceptAll file filter *****
 998     // *****************************************
 999     protected class AcceptAllFileFilter extends FileFilter {
1000         public AcceptAllFileFilter() {
1001         }
1002 
1003         public boolean accept(final File f) {
1004             return true;
1005         }
1006 
1007         public String getDescription() {
1008             return UIManager.getString("FileChooser.acceptAllFileFilterText");
1009         }
1010     }
1011 
1012     // Penultimate superclass is JLabel
1013     @SuppressWarnings("serial") // Superclass is not serializable across versions
1014     protected class MacFCTableCellRenderer extends DefaultTableCellRenderer {
1015         boolean fIsSelected = false;
1016 
1017         public MacFCTableCellRenderer(final Font f) {
1018             super();
1019             setFont(f);
1020             setIconTextGap(10);
1021         }
1022 
1023         public Component getTableCellRendererComponent(final JTable list, final Object value, final boolean isSelected, final boolean cellHasFocus, final int index, final int col) {
1024             super.getTableCellRendererComponent(list, value, isSelected, false, index, col); // No focus border, thanks
1025             fIsSelected = isSelected;
1026             return this;
1027         }
1028 
1029         public boolean isSelected() {
1030             return fIsSelected && isEnabled();
1031         }
1032 
1033         protected String layoutCL(final JLabel label, final FontMetrics fontMetrics, final String text, final Icon icon, final Rectangle viewR, final Rectangle iconR, final Rectangle textR) {
1034             return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(), viewR, iconR, textR, label.getIconTextGap());
1035         }
1036 
1037         protected void paintComponent(final Graphics g) {
1038             final String text = getText();
1039             Icon icon = getIcon();
1040             if (icon != null && !isEnabled()) {
1041                 final Icon disabledIcon = getDisabledIcon();
1042                 if (disabledIcon != null) icon = disabledIcon;
1043             }
1044 
1045             if ((icon == null) && (text == null)) { return; }
1046 
1047             // from ComponentUI update
1048             g.setColor(getBackground());
1049             g.fillRect(0, 0, getWidth(), getHeight());
1050 
1051             // from BasicLabelUI paint
1052             final FontMetrics fm = g.getFontMetrics();
1053             Insets paintViewInsets = getInsets(null);
1054             paintViewInsets.left += 10;
1055 
1056             Rectangle paintViewR = new Rectangle(paintViewInsets.left, paintViewInsets.top, getWidth() - (paintViewInsets.left + paintViewInsets.right), getHeight() - (paintViewInsets.top + paintViewInsets.bottom));
1057 
1058             Rectangle paintIconR = new Rectangle();
1059             Rectangle paintTextR = new Rectangle();
1060 
1061             final String clippedText = layoutCL(this, fm, text, icon, paintViewR, paintIconR, paintTextR);
1062 
1063             if (icon != null) {
1064                 icon.paintIcon(this, g, paintIconR.x + 5, paintIconR.y);
1065             }
1066 
1067             if (text != null) {
1068                 final int textX = paintTextR.x;
1069                 final int textY = paintTextR.y + fm.getAscent() + 1;
1070                 if (isEnabled()) {
1071                     // Color background = fIsSelected ? getForeground() : getBackground();
1072                     final Color background = getBackground();
1073 
1074                     g.setColor(background);
1075                     g.fillRect(textX - 1, paintTextR.y, paintTextR.width + 2, fm.getAscent() + 2);
1076 
1077                     g.setColor(getForeground());
1078                     SwingUtilities2.drawString(filechooser, g, clippedText, textX, textY);
1079                 } else {
1080                     final Color background = getBackground();
1081                     g.setColor(background);
1082                     g.fillRect(textX - 1, paintTextR.y, paintTextR.width + 2, fm.getAscent() + 2);
1083 
1084                     g.setColor(background.brighter());
1085                     SwingUtilities2.drawString(filechooser, g, clippedText, textX, textY);
1086                     g.setColor(background.darker());
1087                     SwingUtilities2.drawString(filechooser, g, clippedText, textX + 1, textY + 1);
1088                 }
1089             }
1090         }
1091 
1092     }
1093 
1094     @SuppressWarnings("serial") // Superclass is not serializable across versions
1095     protected class FileRenderer extends MacFCTableCellRenderer {
1096         public FileRenderer(final Font f) {
1097             super(f);
1098         }
1099 
1100         public Component getTableCellRendererComponent(final JTable list, final Object value, final boolean isSelected, final boolean cellHasFocus, final int index, final int col) {
1101             super.getTableCellRendererComponent(list, value, isSelected, false, index, col); // No focus border, thanks
1102             final File file = (File)value;
1103             final JFileChooser fc = getFileChooser();
1104             setText(fc.getName(file));
1105             setIcon(fc.getIcon(file));
1106             setEnabled(isSelectableInList(file));
1107             return this;
1108         }
1109     }
1110 
1111     @SuppressWarnings("serial") // Superclass is not serializable across versions
1112     protected class DateRenderer extends MacFCTableCellRenderer {
1113         public DateRenderer(final Font f) {
1114             super(f);
1115         }
1116 
1117         public Component getTableCellRendererComponent(final JTable list, final Object value, final boolean isSelected, final boolean cellHasFocus, final int index, final int col) {
1118             super.getTableCellRendererComponent(list, value, isSelected, false, index, col);
1119             final File file = (File)fFileList.getValueAt(index, 0);
1120             setEnabled(isSelectableInList(file));
1121             final DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
1122             final Date date = (Date)value;
1123 
1124             if (date != null) {
1125                 setText(formatter.format(date));
1126             } else {
1127                 setText("");
1128             }
1129 
1130             return this;
1131         }
1132     }
1133 
1134     public Dimension getPreferredSize(final JComponent c) {
1135         return PREF_SIZE;
1136     }
1137 
1138     public Dimension getMinimumSize(final JComponent c) {
1139         return MIN_SIZE;
1140     }
1141 
1142     public Dimension getMaximumSize(final JComponent c) {
1143         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
1144     }
1145 
1146     @SuppressWarnings("serial") // anonymous class
1147     protected ListCellRenderer createDirectoryComboBoxRenderer(final JFileChooser fc) {
1148         return new AquaComboBoxRendererInternal(directoryComboBox) {
1149             public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
1150                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1151                 final File directory = (File)value;
1152                 if (directory == null) {
1153                     setText("");
1154                     return this;
1155                 }
1156 
1157                 final JFileChooser chooser = getFileChooser();
1158                 setText(chooser.getName(directory));
1159                 setIcon(chooser.getIcon(directory));
1160                 return this;
1161             }
1162         };
1163     }
1164 
1165     //
1166     // DataModel for DirectoryComboxbox
1167     //
1168     protected DirectoryComboBoxModel createDirectoryComboBoxModel(final JFileChooser fc) {
1169         return new DirectoryComboBoxModel();
1170     }
1171 
1172     /**
1173      * Data model for a type-face selection combo-box.
1174      */
1175     @SuppressWarnings("serial") // Superclass is not serializable across versions
1176     protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
1177         Vector<File> fDirectories = new Vector<File>();
1178         int topIndex = -1;
1179         int fPathCount = 0;
1180 
1181         File fSelectedDirectory = null;
1182 
1183         public DirectoryComboBoxModel() {
1184             super();
1185             // Add the current directory to the model, and make it the
1186             // selectedDirectory
1187             addItem(getFileChooser().getCurrentDirectory());
1188         }
1189 
1190         /**
1191          * Removes the selected directory, and clears out the
1192          * path file entries leading up to that directory.
1193          */
1194         private void removeSelectedDirectory() {
1195             fDirectories.removeAllElements();
1196             fPathCount = 0;
1197             fSelectedDirectory = null;
1198             // dump();
1199         }
1200 
1201         /**
1202          * Adds the directory to the model and sets it to be selected,
1203          * additionally clears out the previous selected directory and
1204          * the paths leading up to it, if any.
1205          */
1206         void addItem(final File directory) {
1207             if (directory == null) { return; }
1208             if (fSelectedDirectory != null) {
1209                 removeSelectedDirectory();
1210             }
1211 
1212             // create File instances of each directory leading up to the top
1213             File f = directory.getAbsoluteFile();
1214             final Vector<File> path = new Vector<File>(10);
1215             while (f.getParent() != null) {
1216                 path.addElement(f);
1217                 f = getFileChooser().getFileSystemView().createFileObject(f.getParent());
1218             };
1219 
1220             // Add root file (the desktop) to the model
1221             final File[] roots = getFileChooser().getFileSystemView().getRoots();
1222             for (final File element : roots) {
1223                 path.addElement(element);
1224             }
1225             fPathCount = path.size();
1226 
1227             // insert all the path fDirectories leading up to the
1228             // selected directory in reverse order (current directory at top)
1229             for (int i = 0; i < path.size(); i++) {
1230                 fDirectories.addElement(path.elementAt(i));
1231             }
1232 
1233             setSelectedItem(fDirectories.elementAt(0));
1234 
1235             // dump();
1236         }
1237 
1238         public void setSelectedItem(final Object selectedDirectory) {
1239             this.fSelectedDirectory = (File)selectedDirectory;
1240             fireContentsChanged(this, -1, -1);
1241         }
1242 
1243         public Object getSelectedItem() {
1244             return fSelectedDirectory;
1245         }
1246 
1247         public int getSize() {
1248             return fDirectories.size();
1249         }
1250 
1251         public Object getElementAt(final int index) {
1252             return fDirectories.elementAt(index);
1253         }
1254     }
1255 
1256     //
1257     // Renderer for Types ComboBox
1258     //
1259     @SuppressWarnings("serial") // anonymous class
1260     protected ListCellRenderer createFilterComboBoxRenderer() {
1261         return new AquaComboBoxRendererInternal(filterComboBox) {
1262             public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
1263                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
1264                 final FileFilter filter = (FileFilter)value;
1265                 if (filter != null) setText(filter.getDescription());
1266                 return this;
1267             }
1268         };
1269     }
1270 
1271     //
1272     // DataModel for Types Comboxbox
1273     //
1274     protected FilterComboBoxModel createFilterComboBoxModel() {
1275         return new FilterComboBoxModel();
1276     }
1277 
1278     /**
1279      * Data model for a type-face selection combo-box.
1280      */
1281     @SuppressWarnings("serial") // Superclass is not serializable across versions
1282     protected class FilterComboBoxModel extends AbstractListModel<FileFilter> implements ComboBoxModel<FileFilter>,
1283             PropertyChangeListener {
1284         protected FileFilter[] filters;
1285         protected FilterComboBoxModel() {
1286             super();
1287             filters = getFileChooser().getChoosableFileFilters();
1288         }
1289 
1290         public void propertyChange(PropertyChangeEvent e) {
1291             String prop = e.getPropertyName();
1292             if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
1293                 filters = (FileFilter[]) e.getNewValue();
1294                 fireContentsChanged(this, -1, -1);
1295             } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
1296                 fireContentsChanged(this, -1, -1);
1297             }
1298         }
1299 
1300         public void setSelectedItem(Object filter) {
1301             if(filter != null) {
1302                 getFileChooser().setFileFilter((FileFilter) filter);
1303                 fireContentsChanged(this, -1, -1);
1304             }
1305         }
1306 
1307         public Object getSelectedItem() {
1308             // Ensure that the current filter is in the list.
1309             // NOTE: we shouldnt' have to do this, since JFileChooser adds
1310             // the filter to the choosable filters list when the filter
1311             // is set. Lets be paranoid just in case someone overrides
1312             // setFileFilter in JFileChooser.
1313             FileFilter currentFilter = getFileChooser().getFileFilter();
1314             boolean found = false;
1315             if(currentFilter != null) {
1316                 for (FileFilter filter : filters) {
1317                     if (filter == currentFilter) {
1318                         found = true;
1319                     }
1320                 }
1321                 if(found == false) {
1322                     getFileChooser().addChoosableFileFilter(currentFilter);
1323                 }
1324             }
1325             return getFileChooser().getFileFilter();
1326         }
1327 
1328         public int getSize() {
1329             if(filters != null) {
1330                 return filters.length;
1331             } else {
1332                 return 0;
1333             }
1334         }
1335 
1336         public FileFilter getElementAt(int index) {
1337             if(index > getSize() - 1) {
1338                 // This shouldn't happen. Try to recover gracefully.
1339                 return getFileChooser().getFileFilter();
1340             }
1341             if(filters != null) {
1342                 return filters[index];
1343             } else {
1344                 return null;
1345             }
1346         }
1347     }
1348 
1349     /**
1350      * Acts when FilterComboBox has changed the selected item.
1351      */
1352     @SuppressWarnings("serial") // Superclass is not serializable across versions
1353     protected class FilterComboBoxAction extends AbstractAction {
1354         protected FilterComboBoxAction() {
1355             super("FilterComboBoxAction");
1356         }
1357 
1358         public void actionPerformed(final ActionEvent e) {
1359             getFileChooser().setFileFilter((FileFilter)filterComboBox.getSelectedItem());
1360         }
1361     }
1362 
1363     /**
1364      * Acts when DirectoryComboBox has changed the selected item.
1365      */
1366     @SuppressWarnings("serial") // Superclass is not serializable across versions
1367     protected class DirectoryComboBoxAction extends AbstractAction {
1368         protected DirectoryComboBoxAction() {
1369             super("DirectoryComboBoxAction");
1370         }
1371 
1372         public void actionPerformed(final ActionEvent e) {
1373             getFileChooser().setCurrentDirectory((File)directoryComboBox.getSelectedItem());
1374         }
1375     }
1376 
1377     // Sorting Table operations
1378     @SuppressWarnings("serial") // Superclass is not serializable across versions
1379     class JSortingTableHeader extends JTableHeader {
1380         public JSortingTableHeader(final TableColumnModel cm) {
1381             super(cm);
1382             setReorderingAllowed(true); // This causes mousePress to call setDraggedColumn
1383         }
1384 
1385         // One sort state for each column.  Both are ascending by default
1386         final boolean fSortAscending[] = {true, true};
1387 
1388         // Instead of dragging, it selects which one to sort by
1389         public void setDraggedColumn(final TableColumn aColumn) {
1390             if (aColumn != null) {
1391                 final int colIndex = aColumn.getModelIndex();
1392                 if (colIndex != fSortColumn) {
1393                     filechooser.firePropertyChange(AquaFileSystemModel.SORT_BY_CHANGED, fSortColumn, colIndex);
1394                     fSortColumn = colIndex;
1395                 } else {
1396                     fSortAscending[colIndex] = !fSortAscending[colIndex];
1397                     filechooser.firePropertyChange(AquaFileSystemModel.SORT_ASCENDING_CHANGED, !fSortAscending[colIndex], fSortAscending[colIndex]);
1398                 }
1399                 // Need to repaint the highlighted column.
1400                 repaint();
1401             }
1402         }
1403 
1404         // This stops mouseDrags from moving the column
1405         public TableColumn getDraggedColumn() {
1406             return null;
1407         }
1408 
1409         protected TableCellRenderer createDefaultRenderer() {
1410             final DefaultTableCellRenderer label = new AquaTableCellRenderer();
1411             label.setHorizontalAlignment(SwingConstants.LEFT);
1412             return label;
1413         }
1414 
1415         @SuppressWarnings("serial") // Superclass is not serializable across versions
1416         class AquaTableCellRenderer extends DefaultTableCellRenderer implements UIResource {
1417             public Component getTableCellRendererComponent(final JTable localTable, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
1418                 if (localTable != null) {
1419                     final JTableHeader header = localTable.getTableHeader();
1420                     if (header != null) {
1421                         setForeground(header.getForeground());
1422                         setBackground(header.getBackground());
1423                         setFont(UIManager.getFont("TableHeader.font"));
1424                     }
1425                 }
1426 
1427                 setText((value == null) ? "" : value.toString());
1428 
1429                 // Modify the table "border" to draw smaller, and with the titles in the right position
1430                 // and sort indicators, just like an NSSave/Open panel.
1431                 final AquaTableHeaderBorder cellBorder = AquaTableHeaderBorder.getListHeaderBorder();
1432                 cellBorder.setSelected(column == fSortColumn);
1433                 final int horizontalShift = (column == 0 ? 35 : 10);
1434                 cellBorder.setHorizontalShift(horizontalShift);
1435 
1436                 if (column == fSortColumn) {
1437                     cellBorder.setSortOrder(fSortAscending[column] ? AquaTableHeaderBorder.SORT_ASCENDING : AquaTableHeaderBorder.SORT_DECENDING);
1438                 } else {
1439                     cellBorder.setSortOrder(AquaTableHeaderBorder.SORT_NONE);
1440                 }
1441                 setBorder(cellBorder);
1442                 return this;
1443             }
1444         }
1445     }
1446 
1447     public void installComponents(final JFileChooser fc) {
1448         JPanel tPanel; // temp panel
1449         // set to a Y BoxLayout. The chooser will be laid out top to bottom.
1450         fc.setLayout(new BoxLayout(fc, BoxLayout.Y_AXIS));
1451         fc.add(Box.createRigidArea(vstrut10));
1452 
1453         // construct the top panel
1454 
1455         final JPanel topPanel = new JPanel();
1456         topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
1457         fc.add(topPanel);
1458         fc.add(Box.createRigidArea(vstrut10));
1459 
1460         // Add the textfield pane
1461 
1462         fTextfieldPanel = new JPanel();
1463         fTextfieldPanel.setLayout(new BorderLayout());
1464         // setBottomPanelForMode will make this visible if we need it
1465         fTextfieldPanel.setVisible(false);
1466         topPanel.add(fTextfieldPanel);
1467 
1468         tPanel = new JPanel();
1469         tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.Y_AXIS));
1470         final JPanel labelArea = new JPanel();
1471         labelArea.setLayout(new FlowLayout(FlowLayout.CENTER));
1472         fTextFieldLabel = new JLabel(fileNameLabelText);
1473         labelArea.add(fTextFieldLabel);
1474 
1475         // text field
1476         filenameTextField = new JTextField();
1477         fTextFieldLabel.setLabelFor(filenameTextField);
1478         filenameTextField.addActionListener(getAction(kOpen));
1479         filenameTextField.addFocusListener(new SaveTextFocusListener());
1480         final Dimension minSize = filenameTextField.getMinimumSize();
1481         Dimension d = new Dimension(250, (int)minSize.getHeight());
1482         filenameTextField.setPreferredSize(d);
1483         filenameTextField.setMaximumSize(d);
1484         labelArea.add(filenameTextField);
1485         final File f = fc.getSelectedFile();
1486         if (f != null) {
1487             setFileName(fc.getName(f));
1488         } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
1489             setFileName(newFileDefaultName);
1490         }
1491 
1492         tPanel.add(labelArea);
1493         // separator line
1494         @SuppressWarnings("serial") // anonymous class
1495         final JSeparator sep = new JSeparator(){
1496             public Dimension getPreferredSize() {
1497                 return new Dimension(((JComponent)getParent()).getWidth(), 3);
1498             }
1499         };
1500         tPanel.add(Box.createRigidArea(new Dimension(1, 8)));
1501         tPanel.add(sep);
1502         tPanel.add(Box.createRigidArea(new Dimension(1, 7)));
1503         fTextfieldPanel.add(tPanel, BorderLayout.CENTER);
1504 
1505         // DirectoryComboBox, left-justified, 200x20 not including drop shadow
1506         directoryComboBox = new JComboBox();
1507         directoryComboBox.putClientProperty("JComboBox.lightweightKeyboardNavigation", "Lightweight");
1508         fDirectoryComboBoxModel = createDirectoryComboBoxModel(fc);
1509         directoryComboBox.setModel(fDirectoryComboBoxModel);
1510         directoryComboBox.addActionListener(directoryComboBoxAction);
1511         directoryComboBox.setRenderer(createDirectoryComboBoxRenderer(fc));
1512         directoryComboBox.setToolTipText(directoryComboBoxToolTipText);
1513         d = new Dimension(250, (int)directoryComboBox.getMinimumSize().getHeight());
1514         directoryComboBox.setPreferredSize(d);
1515         directoryComboBox.setMaximumSize(d);
1516         topPanel.add(directoryComboBox);
1517 
1518         // ************************************** //
1519         // ** Add the directory/Accessory pane ** //
1520         // ************************************** //
1521         final JPanel centerPanel = new JPanel(new BorderLayout());
1522         fc.add(centerPanel);
1523 
1524         // Accessory pane (equiv to Preview pane in NavServices)
1525         final JComponent accessory = fc.getAccessory();
1526         if (accessory != null) {
1527             getAccessoryPanel().add(accessory);
1528         }
1529         centerPanel.add(getAccessoryPanel(), BorderLayout.LINE_START);
1530 
1531         // Directory list(table), right-justified, resizable
1532         final JPanel p = createList(fc);
1533         p.setMinimumSize(LIST_MIN_SIZE);
1534         centerPanel.add(p, BorderLayout.CENTER);
1535 
1536         // ********************************** //
1537         // **** Construct the bottom panel ** //
1538         // ********************************** //
1539         fBottomPanel = new JPanel();
1540         fBottomPanel.setLayout(new BoxLayout(fBottomPanel, BoxLayout.Y_AXIS));
1541         fc.add(fBottomPanel);
1542 
1543         // Filter label and combobox.
1544         // I know it's unMaclike, but the filter goes on Directory_only too.
1545         tPanel = new JPanel();
1546         tPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
1547         tPanel.setBorder(AquaGroupBorder.getTitlelessBorder());
1548         final JLabel formatLabel = new JLabel(filesOfTypeLabelText);
1549         tPanel.add(formatLabel);
1550 
1551         // Combobox
1552         filterComboBoxModel = createFilterComboBoxModel();
1553         fc.addPropertyChangeListener(filterComboBoxModel);
1554         filterComboBox = new JComboBox(filterComboBoxModel);
1555         formatLabel.setLabelFor(filterComboBox);
1556         filterComboBox.setRenderer(createFilterComboBoxRenderer());
1557         d = new Dimension(220, (int)filterComboBox.getMinimumSize().getHeight());
1558         filterComboBox.setPreferredSize(d);
1559         filterComboBox.setMaximumSize(d);
1560         filterComboBox.addActionListener(filterComboBoxAction);
1561         filterComboBox.setOpaque(false);
1562         tPanel.add(filterComboBox);
1563 
1564         fBottomPanel.add(tPanel);
1565 
1566         // fDirectoryPanel: New, Open, Cancel, Approve buttons, right-justified, 82x22
1567         // (sometimes the NewFolder and OpenFolder buttons are invisible)
1568         fDirectoryPanel = new JPanel();
1569         fDirectoryPanel.setLayout(new BoxLayout(fDirectoryPanel, BoxLayout.PAGE_AXIS));
1570         JPanel directoryPanel = new JPanel(new BorderLayout());
1571         JPanel newFolderButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
1572         newFolderButtonPanel.add(Box.createHorizontalStrut(20));
1573         fNewFolderButton = createNewFolderButton(); // Because we hide it depending on style
1574         newFolderButtonPanel.add(fNewFolderButton);
1575         directoryPanel.add(newFolderButtonPanel, BorderLayout.LINE_START);
1576         JPanel approveCancelButtonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 0, 0));
1577         fOpenButton = createButton(kOpenDirectory, openButtonText);
1578         approveCancelButtonPanel.add(fOpenButton);
1579         approveCancelButtonPanel.add(Box.createHorizontalStrut(8));
1580         fCancelButton = createButton(kCancel, null);
1581         approveCancelButtonPanel.add(fCancelButton);
1582         approveCancelButtonPanel.add(Box.createHorizontalStrut(8));
1583         // The ApproveSelection button
1584         fApproveButton = new JButton();
1585         fApproveButton.addActionListener(fApproveSelectionAction);
1586         approveCancelButtonPanel.add(fApproveButton);
1587         approveCancelButtonPanel.add(Box.createHorizontalStrut(20));
1588         directoryPanel.add(approveCancelButtonPanel, BorderLayout.LINE_END);
1589         fDirectoryPanel.add(Box.createVerticalStrut(5));
1590         fDirectoryPanel.add(directoryPanel);
1591         fDirectoryPanel.add(Box.createVerticalStrut(12));
1592         fDirectoryPanelSpacer = Box.createRigidArea(hstrut10);
1593 
1594         if (fc.getControlButtonsAreShown()) {
1595             fBottomPanel.add(fDirectoryPanelSpacer);
1596             fBottomPanel.add(fDirectoryPanel);
1597         }
1598 
1599         setBottomPanelForMode(fc); // updates ApproveButtonText etc
1600 
1601         // don't create til after the FCSubpanel and buttons are made
1602         filenameTextField.getDocument().addDocumentListener(new SaveTextDocumentListener());
1603     }
1604 
1605     void setDefaultButtonForMode(final JFileChooser fc) {
1606         final JButton defaultButton = fSubPanel.getDefaultButton(fc);
1607         final JRootPane root = defaultButton.getRootPane();
1608         if (root != null) {
1609             root.setDefaultButton(defaultButton);
1610         }
1611     }
1612 
1613     // Macs start with their focus in text areas if they have them,
1614     // lists otherwise (the other plafs start with the focus on approveButton)
1615     void setFocusForMode(final JFileChooser fc) {
1616         final JComponent focusComponent = fSubPanel.getFocusComponent(fc);
1617         if (focusComponent != null) {
1618             focusComponent.requestFocus();
1619         }
1620     }
1621 
1622     // Enable/disable buttons as needed for the current selection/focus state
1623     void updateButtonState(final JFileChooser fc) {
1624         fSubPanel.updateButtonState(fc, getFirstSelectedItem());
1625         updateApproveButton(fc);
1626     }
1627 
1628     void updateApproveButton(final JFileChooser chooser) {
1629         fApproveButton.setText(getApproveButtonText(chooser));
1630         fApproveButton.setToolTipText(getApproveButtonToolTipText(chooser));
1631         fApproveButton.setMnemonic(getApproveButtonMnemonic(chooser));
1632         fCancelButton.setToolTipText(getCancelButtonToolTipText(chooser));
1633     }
1634 
1635     // Lazy-init the subpanels
1636     synchronized FCSubpanel getSaveFilePanel() {
1637         if (fSaveFilePanel == null) fSaveFilePanel = new SaveFilePanel();
1638         return fSaveFilePanel;
1639     }
1640 
1641     synchronized FCSubpanel getOpenFilePanel() {
1642         if (fOpenFilePanel == null) fOpenFilePanel = new OpenFilePanel();
1643         return fOpenFilePanel;
1644     }
1645 
1646     synchronized FCSubpanel getOpenDirOrAnyPanel() {
1647         if (fOpenDirOrAnyPanel == null) fOpenDirOrAnyPanel = new OpenDirOrAnyPanel();
1648         return fOpenDirOrAnyPanel;
1649     }
1650 
1651     synchronized FCSubpanel getCustomFilePanel() {
1652         if (fCustomFilePanel == null) fCustomFilePanel = new CustomFilePanel();
1653         return fCustomFilePanel;
1654     }
1655 
1656     synchronized FCSubpanel getCustomDirOrAnyPanel() {
1657         if (fCustomDirOrAnyPanel == null) fCustomDirOrAnyPanel = new CustomDirOrAnyPanel();
1658         return fCustomDirOrAnyPanel;
1659     }
1660 
1661     void setBottomPanelForMode(final JFileChooser fc) {
1662         if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) fSubPanel = getSaveFilePanel();
1663         else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
1664             if (fc.getFileSelectionMode() == JFileChooser.FILES_ONLY) fSubPanel = getOpenFilePanel();
1665             else fSubPanel = getOpenDirOrAnyPanel();
1666         } else if (fc.getDialogType() == JFileChooser.CUSTOM_DIALOG) {
1667             if (fc.getFileSelectionMode() == JFileChooser.FILES_ONLY) fSubPanel = getCustomFilePanel();
1668             else fSubPanel = getCustomDirOrAnyPanel();
1669         }
1670 
1671         fSubPanel.installPanel(fc, true);
1672         updateApproveButton(fc);
1673         updateButtonState(fc);
1674         setDefaultButtonForMode(fc);
1675         setFocusForMode(fc);
1676         fc.invalidate();
1677     }
1678 
1679     // fTextfieldPanel and fDirectoryPanel both have NewFolder buttons; only one should be visible at a time
1680     JButton createNewFolderButton() {
1681         final JButton b = new JButton(newFolderButtonText);
1682         b.setToolTipText(newFolderToolTipText);
1683         b.getAccessibleContext().setAccessibleName(newFolderAccessibleName);
1684         b.setHorizontalTextPosition(SwingConstants.LEFT);
1685         b.setAlignmentX(Component.LEFT_ALIGNMENT);
1686         b.setAlignmentY(Component.CENTER_ALIGNMENT);
1687         b.addActionListener(getAction(kNewFolder));
1688         return b;
1689     }
1690 
1691     JButton createButton(final int which, String label) {
1692         if (label == null) label = UIManager.getString(sDataPrefix + sButtonKinds[which] + sButtonData[0]);
1693         final int mnemonic = UIManager.getInt(sDataPrefix + sButtonKinds[which] + sButtonData[1]);
1694         final String tipText = UIManager.getString(sDataPrefix + sButtonKinds[which] + sButtonData[2]);
1695         final JButton b = new JButton(label);
1696         b.setMnemonic(mnemonic);
1697         b.setToolTipText(tipText);
1698         b.addActionListener(getAction(which));
1699         return b;
1700     }
1701 
1702     AbstractAction getAction(final int which) {
1703         return fButtonActions[which];
1704     }
1705 
1706     public void uninstallComponents(final JFileChooser fc) { //$ Metal (on which this is based) doesn't uninstall its components.
1707     }
1708 
1709     // Consistent with the AppKit NSSavePanel, clicks on a file (not a directory) should populate the text field
1710     // with that file's display name.
1711     protected class FileListMouseListener extends MouseAdapter {
1712         public void mouseClicked(final MouseEvent e) {
1713             final Point p = e.getPoint();
1714             final int row = fFileList.rowAtPoint(p);
1715             final int column = fFileList.columnAtPoint(p);
1716 
1717             // The autoscroller can generate drag events outside the Table's range.
1718             if ((column == -1) || (row == -1)) { return; }
1719 
1720             final File clickedFile = (File)(fFileList.getValueAt(row, 0));
1721 
1722             // rdar://problem/3734130 -- don't populate the text field if this file isn't selectable in this mode.
1723             if (isSelectableForMode(getFileChooser(), clickedFile)) {
1724                 // [3188387] Populate the file name field with the selected file name
1725                 // [3484163] It should also use the display name, not the actual name.
1726                 setFileName(fileView.getName(clickedFile));
1727             }
1728         }
1729     }
1730 
1731     protected JPanel createList(final JFileChooser fc) {
1732         // The first part is similar to MetalFileChooserUI.createList - same kind of listeners
1733         final JPanel p = new JPanel(new BorderLayout());
1734         fFileList = new JTableExtension();
1735         fFileList.setToolTipText(null); // Workaround for 2487689
1736         fFileList.addMouseListener(new FileListMouseListener());
1737         model = new AquaFileSystemModel(fc, fFileList, fColumnNames);
1738         final MacListSelectionModel listSelectionModel = new MacListSelectionModel(model);
1739 
1740         if (getFileChooser().isMultiSelectionEnabled()) {
1741             listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
1742         } else {
1743             listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1744         }
1745 
1746         fFileList.setModel(model);
1747         fFileList.setSelectionModel(listSelectionModel);
1748         fFileList.getSelectionModel().addListSelectionListener(createListSelectionListener(fc));
1749 
1750         // Now we're different, because we're a table, not a list
1751         fc.addPropertyChangeListener(model);
1752         fFileList.addFocusListener(new SaveTextFocusListener());
1753         final JTableHeader th = new JSortingTableHeader(fFileList.getColumnModel());
1754         fFileList.setTableHeader(th);
1755         fFileList.setRowMargin(0);
1756         fFileList.setIntercellSpacing(new Dimension(0, 1));
1757         fFileList.setShowVerticalLines(false);
1758         fFileList.setShowHorizontalLines(false);
1759         final Font f = fFileList.getFont(); //ThemeFont.GetThemeFont(AppearanceConstants.kThemeViewsFont);
1760         //fc.setFont(f);
1761         //fFileList.setFont(f);
1762         fFileList.setDefaultRenderer(File.class, new FileRenderer(f));
1763         fFileList.setDefaultRenderer(Date.class, new DateRenderer(f));
1764         final FontMetrics fm = fFileList.getFontMetrics(f);
1765 
1766         // Row height isn't based on the renderers.  It defaults to 16 so we have to set it
1767         fFileList.setRowHeight(Math.max(fm.getHeight(), fileIcon.getIconHeight() + 2));
1768 
1769         // Add a binding for the file list that triggers return and escape
1770         fFileList.registerKeyboardAction(new CancelSelectionAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED);
1771         // Add a binding for the file list that triggers the default button (see DefaultButtonAction)
1772         fFileList.registerKeyboardAction(new DefaultButtonAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
1773         fFileList.setDropTarget(dragAndDropTarget);
1774 
1775         final JScrollPane scrollpane = new JScrollPane(fFileList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
1776         scrollpane.setComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
1777         scrollpane.setCorner(ScrollPaneConstants.UPPER_TRAILING_CORNER, new ScrollPaneCornerPanel());
1778         p.add(scrollpane, BorderLayout.CENTER);
1779         return p;
1780     }
1781 
1782     @SuppressWarnings("serial") // Superclass is not serializable across versions
1783     protected class ScrollPaneCornerPanel extends JPanel {
1784         final Border border = UIManager.getBorder("TableHeader.cellBorder");
1785 
1786         protected void paintComponent(final Graphics g) {
1787             border.paintBorder(this, g, 0, 0, getWidth() + 1, getHeight());
1788         }
1789     }
1790 
1791     JComboBox directoryComboBox;
1792     DirectoryComboBoxModel fDirectoryComboBoxModel;
1793     private final Action directoryComboBoxAction = new DirectoryComboBoxAction();
1794 
1795     JTextField filenameTextField;
1796 
1797     JTableExtension fFileList;
1798 
1799     private FilterComboBoxModel filterComboBoxModel;
1800     JComboBox filterComboBox;
1801     private final Action filterComboBoxAction = new FilterComboBoxAction();
1802 
1803     private static final Dimension hstrut10 = new Dimension(10, 1);
1804     private static final Dimension vstrut10 = new Dimension(1, 10);
1805 
1806     private static final int PREF_WIDTH = 550;
1807     private static final int PREF_HEIGHT = 400;
1808     private static final Dimension PREF_SIZE = new Dimension(PREF_WIDTH, PREF_HEIGHT);
1809 
1810     private static final int MIN_WIDTH = 400;
1811     private static final int MIN_HEIGHT = 250;
1812     private static final Dimension MIN_SIZE = new Dimension(MIN_WIDTH, MIN_HEIGHT);
1813 
1814     private static final int LIST_MIN_WIDTH = 400;
1815     private static final int LIST_MIN_HEIGHT = 100;
1816     private static final Dimension LIST_MIN_SIZE = new Dimension(LIST_MIN_WIDTH, LIST_MIN_HEIGHT);
1817 
1818     static String fileNameLabelText = null;
1819     JLabel fTextFieldLabel = null;
1820 
1821     private static String filesOfTypeLabelText = null;
1822 
1823     private static String newFolderToolTipText = null;
1824     static String newFolderAccessibleName = null;
1825 
1826     private static final String[] fColumnNames = new String[2];
1827 
1828     JPanel fTextfieldPanel; // Filename textfield for Save or Custom
1829     private JPanel fDirectoryPanel; // NewFolder/OpenFolder/Cancel/Approve buttons
1830     private Component fDirectoryPanelSpacer;
1831     private JPanel fBottomPanel; // The panel that holds fDirectoryPanel and filterComboBox
1832 
1833     private FCSubpanel fSaveFilePanel = null;
1834     private FCSubpanel fOpenFilePanel = null;
1835     private FCSubpanel fOpenDirOrAnyPanel = null;
1836     private FCSubpanel fCustomFilePanel = null;
1837     private FCSubpanel fCustomDirOrAnyPanel = null;
1838 
1839     FCSubpanel fSubPanel = null; // Current FCSubpanel
1840 
1841     JButton fApproveButton; // mode-specific behavior is managed by FCSubpanel.approveSelection
1842     JButton fOpenButton; // for Directories
1843     JButton fNewFolderButton; // for fDirectoryPanel
1844 
1845     // ToolTip text varies with type of dialog
1846     private JButton fCancelButton;
1847 
1848     private final ApproveSelectionAction fApproveSelectionAction = new ApproveSelectionAction();
1849     protected int fSortColumn = 0;
1850     protected int fPackageIsTraversable = -1;
1851     protected int fApplicationIsTraversable = -1;
1852 
1853     protected static final int sGlobalPackageIsTraversable;
1854     protected static final int sGlobalApplicationIsTraversable;
1855 
1856     protected static final String PACKAGE_TRAVERSABLE_PROPERTY = "JFileChooser.packageIsTraversable";
1857     protected static final String APPLICATION_TRAVERSABLE_PROPERTY = "JFileChooser.appBundleIsTraversable";
1858     protected static final String[] sTraversableProperties = {"always", // Bundle is always traversable
1859             "never", // Bundle is never traversable
1860             "conditional"}; // Bundle is traversable on command click
1861     protected static final int kOpenAlways = 0, kOpenNever = 1, kOpenConditional = 2;
1862 
1863     AbstractAction[] fButtonActions = {fApproveSelectionAction, fApproveSelectionAction, new CancelSelectionAction(), new OpenSelectionAction(), null, new NewFolderAction()};
1864 
1865     static int parseTraversableProperty(final String s) {
1866         if (s == null) return -1;
1867         for (int i = 0; i < sTraversableProperties.length; i++) {
1868             if (s.equals(sTraversableProperties[i])) return i;
1869         }
1870         return -1;
1871     }
1872 
1873     static {
1874         Object o = UIManager.get(PACKAGE_TRAVERSABLE_PROPERTY);
1875         if (o != null && o instanceof String) sGlobalPackageIsTraversable = parseTraversableProperty((String)o);
1876         else sGlobalPackageIsTraversable = kOpenConditional;
1877 
1878         o = UIManager.get(APPLICATION_TRAVERSABLE_PROPERTY);
1879         if (o != null && o instanceof String) sGlobalApplicationIsTraversable = parseTraversableProperty((String)o);
1880         else sGlobalApplicationIsTraversable = kOpenConditional;
1881     }
1882     static final String sDataPrefix = "FileChooser.";
1883     static final String[] sButtonKinds = {"openButton", "saveButton", "cancelButton", "openDirectoryButton", "helpButton", "newFolderButton"};
1884     static final String[] sButtonData = {"Text", "Mnemonic", "ToolTipText"};
1885     static final int kOpen = 0, kSave = 1, kCancel = 2, kOpenDirectory = 3, kHelp = 4, kNewFolder = 5;
1886 
1887     /*-------
1888 
1889      Possible states: Save, {Open, Custom}x{Files, File and Directory, Directory}
1890      --------- */
1891 
1892     // This class returns the values for the Custom type, to avoid duplicating code in the two Custom subclasses
1893     abstract class FCSubpanel {
1894         // Install the appropriate panels for this mode
1895         abstract void installPanel(JFileChooser fc, boolean controlButtonsAreShown);
1896 
1897         abstract void updateButtonState(JFileChooser fc, File f);
1898 
1899         // Can this item be selected?
1900         // if not, it's disabled in the list
1901         boolean isSelectableInList(final JFileChooser fc, final File f) {
1902             if (f == null) return false;
1903             if (fc.getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) return fc.isTraversable(f);
1904             return fc.accept(f);
1905         }
1906 
1907         void approveSelection(final JFileChooser fc) {
1908             fc.approveSelection();
1909         }
1910 
1911         JButton getDefaultButton(final JFileChooser fc) {
1912             return fApproveButton;
1913         }
1914 
1915         // Default to the textfield, panels without one should subclass
1916         JComponent getFocusComponent(final JFileChooser fc) {
1917             return filenameTextField;
1918         }
1919 
1920         String getApproveButtonText(final JFileChooser fc) {
1921             // Fallback to "choose"
1922             return this.getApproveButtonText(fc, chooseButtonText);
1923         }
1924 
1925         // Try to get the custom text.  If none, use the fallback
1926         String getApproveButtonText(final JFileChooser fc, final String fallbackText) {
1927             final String buttonText = fc.getApproveButtonText();
1928             if (buttonText != null) {
1929                 buttonText.trim();
1930                 if (!buttonText.equals("")) return buttonText;
1931             }
1932             return fallbackText;
1933         }
1934 
1935         int getApproveButtonMnemonic(final JFileChooser fc) {
1936             // Don't use a default
1937             return fc.getApproveButtonMnemonic();
1938         }
1939 
1940         // No fallback
1941         String getApproveButtonToolTipText(final JFileChooser fc) {
1942             return getApproveButtonToolTipText(fc, null);
1943         }
1944 
1945         String getApproveButtonToolTipText(final JFileChooser fc, final String fallbackText) {
1946             final String tooltipText = fc.getApproveButtonToolTipText();
1947             if (tooltipText != null) {
1948                 tooltipText.trim();
1949                 if (!tooltipText.equals("")) return tooltipText;
1950             }
1951             return fallbackText;
1952         }
1953 
1954         String getCancelButtonToolTipText(final JFileChooser fc) {
1955             return cancelChooseButtonToolTipText;
1956         }
1957     }
1958 
1959     // Custom FILES_ONLY dialog
1960     /*
1961      NavServices Save appearance with Open behavior
1962      Approve button label = Open when list has focus and a directory is selected, Custom otherwise
1963      No OpenDirectory button - Approve button is overloaded
1964      Default button / double click = Approve
1965      Has text field
1966      List - everything is enabled
1967      */
1968     class CustomFilePanel extends FCSubpanel {
1969         void installPanel(final JFileChooser fc, final boolean controlButtonsAreShown) {
1970             fTextfieldPanel.setVisible(true); // do we really want one in multi-select?  It's confusing
1971             fOpenButton.setVisible(false);
1972             fNewFolderButton.setVisible(true);
1973         }
1974 
1975         // If the list has focus, the mode depends on the selection
1976         // - directory = open, file = approve
1977         // If something else has focus and we have text, it's approve
1978         // otherwise, it depends on selection again.
1979         boolean inOpenDirectoryMode(final JFileChooser fc, final File f) {
1980             final boolean selectionIsDirectory = (f != null && fc.isTraversable(f));
1981             if (fFileList.hasFocus()) return selectionIsDirectory;
1982             else if (textfieldIsValid()) return false;
1983             return selectionIsDirectory;
1984         }
1985 
1986         // The approve button is overloaded to mean OpenDirectory or Save
1987         void approveSelection(final JFileChooser fc) {
1988             File f = getFirstSelectedItem();
1989             if (inOpenDirectoryMode(fc, f)) {
1990                 openDirectory(f);
1991             } else {
1992                 f = makeFile(fc, getFileName());
1993                 if (f != null) {
1994                     selectionInProgress = true;
1995                     getFileChooser().setSelectedFile(f);
1996                     selectionInProgress = false;
1997                 }
1998                 getFileChooser().approveSelection();
1999             }
2000         }
2001 
2002         // The approve button should be enabled
2003         // - if something in the list can be opened
2004         // - if the textfield has something in it
2005         void updateButtonState(final JFileChooser fc, final File f) {
2006             boolean enabled = true;
2007             if (!inOpenDirectoryMode(fc, f)) {
2008                 enabled = (f != null) || textfieldIsValid();
2009             }
2010             getApproveButton(fc).setEnabled(enabled);
2011 
2012             // The OpenDirectory button should be disabled if there's no directory selected
2013             fOpenButton.setEnabled(f != null && fc.isTraversable(f));
2014 
2015             // Update the default button, since we may have disabled the current default.
2016             setDefaultButtonForMode(fc);
2017         }
2018 
2019         // everything's enabled, because we don't know what they're doing with them
2020         boolean isSelectableInList(final JFileChooser fc, final File f) {
2021             if (f == null) return false;
2022             return fc.accept(f);
2023         }
2024 
2025         String getApproveButtonToolTipText(final JFileChooser fc) {
2026             // The approve Button should have openDirectoryButtonToolTipText when the selection is a folder...
2027             if (inOpenDirectoryMode(fc, getFirstSelectedItem())) return openDirectoryButtonToolTipText;
2028             return super.getApproveButtonToolTipText(fc);
2029         }
2030     }
2031 
2032     // All Save dialogs
2033     /*
2034      NavServices Save
2035      Approve button label = Open when list has focus and a directory is selected, Save otherwise
2036      No OpenDirectory button - Approve button is overloaded
2037      Default button / double click = Approve
2038      Has text field
2039      Has NewFolder button (by text field)
2040      List - only traversables are enabled
2041      List is always SINGLE_SELECT
2042      */
2043     // Subclasses CustomFilePanel because they look alike and have some common behavior
2044     class SaveFilePanel extends CustomFilePanel {
2045         void installPanel(final JFileChooser fc, final boolean controlButtonsAreShown) {
2046             fTextfieldPanel.setVisible(true);
2047             fOpenButton.setVisible(false);
2048             fNewFolderButton.setVisible(true);
2049         }
2050 
2051         // only traversables are enabled, regardless of mode
2052         // because all you can do is select the next folder to open
2053         boolean isSelectableInList(final JFileChooser fc, final File f) {
2054             return fc.accept(f) && fc.isTraversable(f);
2055         }
2056 
2057         // The approve button means 'approve the file name in the text field.'
2058         void approveSelection(final JFileChooser fc) {
2059             final File f = makeFile(fc, getFileName());
2060             if (f != null) {
2061                 selectionInProgress = true;
2062                 getFileChooser().setSelectedFile(f);
2063                 selectionInProgress = false;
2064                 getFileChooser().approveSelection();
2065             }
2066         }
2067 
2068         // The approve button should be enabled if the textfield has something in it
2069         void updateButtonState(final JFileChooser fc, final File f) {
2070             final boolean enabled = textfieldIsValid();
2071             getApproveButton(fc).setEnabled(enabled);
2072         }
2073 
2074         String getApproveButtonText(final JFileChooser fc) {
2075             // Get the custom text, or fallback to "Save"
2076             return this.getApproveButtonText(fc, saveButtonText);
2077         }
2078 
2079         int getApproveButtonMnemonic(final JFileChooser fc) {
2080             return saveButtonMnemonic;
2081         }
2082 
2083         String getApproveButtonToolTipText(final JFileChooser fc) {
2084             // The approve Button should have openDirectoryButtonToolTipText when the selection is a folder...
2085             if (inOpenDirectoryMode(fc, getFirstSelectedItem())) return openDirectoryButtonToolTipText;
2086             return this.getApproveButtonToolTipText(fc, saveButtonToolTipText);
2087         }
2088 
2089         String getCancelButtonToolTipText(final JFileChooser fc) {
2090             return cancelSaveButtonToolTipText;
2091         }
2092     }
2093 
2094     // Open FILES_ONLY
2095     /*
2096      NSOpenPanel-style
2097      Approve button label = Open
2098      Default button / double click = Approve
2099      No text field
2100      No NewFolder button
2101      List - all items are enabled
2102      */
2103     class OpenFilePanel extends FCSubpanel {
2104         void installPanel(final JFileChooser fc, final boolean controlButtonsAreShown) {
2105             fTextfieldPanel.setVisible(false);
2106             fOpenButton.setVisible(false);
2107             fNewFolderButton.setVisible(false);
2108             setDefaultButtonForMode(fc);
2109         }
2110 
2111         boolean inOpenDirectoryMode(final JFileChooser fc, final File f) {
2112             return (f != null && fc.isTraversable(f));
2113         }
2114 
2115         // Default to the list
2116         JComponent getFocusComponent(final JFileChooser fc) {
2117             return fFileList;
2118         }
2119 
2120         void updateButtonState(final JFileChooser fc, final File f) {
2121             // Button is disabled if there's nothing selected
2122             final boolean enabled = (f != null) && !fc.isTraversable(f);
2123             getApproveButton(fc).setEnabled(enabled);
2124         }
2125 
2126         // all items are enabled
2127         boolean isSelectableInList(final JFileChooser fc, final File f) {
2128             return f != null && fc.accept(f);
2129         }
2130 
2131         String getApproveButtonText(final JFileChooser fc) {
2132             // Get the custom text, or fallback to "Open"
2133             return this.getApproveButtonText(fc, openButtonText);
2134         }
2135 
2136         int getApproveButtonMnemonic(final JFileChooser fc) {
2137             return openButtonMnemonic;
2138         }
2139 
2140         String getApproveButtonToolTipText(final JFileChooser fc) {
2141             return this.getApproveButtonToolTipText(fc, openButtonToolTipText);
2142         }
2143 
2144         String getCancelButtonToolTipText(final JFileChooser fc) {
2145             return cancelOpenButtonToolTipText;
2146         }
2147     }
2148 
2149     // used by open and custom panels for Directory only or files and directories
2150     abstract class DirOrAnyPanel extends FCSubpanel {
2151         void installPanel(final JFileChooser fc, final boolean controlButtonsAreShown) {
2152             fOpenButton.setVisible(false);
2153         }
2154 
2155         JButton getDefaultButton(final JFileChooser fc) {
2156             return getApproveButton(fc);
2157         }
2158 
2159         void updateButtonState(final JFileChooser fc, final File f) {
2160             // Button is disabled if there's nothing selected
2161             // Approve button is handled by the subclasses
2162             // getApproveButton(fc).setEnabled(f != null);
2163 
2164             // The OpenDirectory button should be disabled if there's no directory selected
2165             // - we only check the first item
2166 
2167             fOpenButton.setEnabled(false);
2168             setDefaultButtonForMode(fc);
2169         }
2170     }
2171 
2172     // Open FILES_AND_DIRECTORIES or DIRECTORIES_ONLY
2173     /*
2174      NavServices Choose
2175      Approve button label = Choose/Custom
2176      Has OpenDirectory button
2177      Default button / double click = OpenDirectory
2178      No text field
2179      List - files are disabled in DIRECTORIES_ONLY
2180      */
2181     class OpenDirOrAnyPanel extends DirOrAnyPanel {
2182         void installPanel(final JFileChooser fc, final boolean controlButtonsAreShown) {
2183             super.installPanel(fc, controlButtonsAreShown);
2184             fTextfieldPanel.setVisible(false);
2185             fNewFolderButton.setVisible(false);
2186         }
2187 
2188         // Default to the list
2189         JComponent getFocusComponent(final JFileChooser fc) {
2190             return fFileList;
2191         }
2192 
2193         int getApproveButtonMnemonic(final JFileChooser fc) {
2194             return chooseButtonMnemonic;
2195         }
2196 
2197         String getApproveButtonToolTipText(final JFileChooser fc) {
2198             String fallbackText;
2199             if (fc.getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) fallbackText = chooseFolderButtonToolTipText;
2200             else fallbackText = chooseItemButtonToolTipText;
2201             return this.getApproveButtonToolTipText(fc, fallbackText);
2202         }
2203 
2204         void updateButtonState(final JFileChooser fc, final File f) {
2205             // Button is disabled if there's nothing selected
2206             getApproveButton(fc).setEnabled(f != null);
2207             super.updateButtonState(fc, f);
2208         }
2209     }
2210 
2211     // Custom FILES_AND_DIRECTORIES or DIRECTORIES_ONLY
2212     /*
2213      No NavServices equivalent
2214      Approve button label = user defined or Choose
2215      Has OpenDirectory button
2216      Default button / double click = OpenDirectory
2217      Has text field
2218      Has NewFolder button (by text field)
2219      List - files are disabled in DIRECTORIES_ONLY
2220      */
2221     class CustomDirOrAnyPanel extends DirOrAnyPanel {
2222         void installPanel(final JFileChooser fc, final boolean controlButtonsAreShown) {
2223             super.installPanel(fc, controlButtonsAreShown);
2224             fTextfieldPanel.setVisible(true);
2225             fNewFolderButton.setVisible(true);
2226         }
2227 
2228         // If there's text, make a file and select it
2229         void approveSelection(final JFileChooser fc) {
2230             final File f = makeFile(fc, getFileName());
2231             if (f != null) {
2232                 selectionInProgress = true;
2233                 getFileChooser().setSelectedFile(f);
2234                 selectionInProgress = false;
2235             }
2236             getFileChooser().approveSelection();
2237         }
2238 
2239         void updateButtonState(final JFileChooser fc, final File f) {
2240             // Button is disabled if there's nothing selected
2241             getApproveButton(fc).setEnabled(f != null || textfieldIsValid());
2242             super.updateButtonState(fc, f);
2243         }
2244     }
2245 
2246     // See FileRenderer - documents in Save dialogs draw disabled, so they shouldn't be selected
2247     @SuppressWarnings("serial") // Superclass is not serializable across versions
2248     class MacListSelectionModel extends DefaultListSelectionModel {
2249         AquaFileSystemModel fModel;
2250 
2251         MacListSelectionModel(final AquaFileSystemModel model) {
2252             fModel = model;
2253         }
2254 
2255         // Can the file be selected in this mode?
2256         // (files are visible even if they can't be selected)
2257         boolean isSelectableInListIndex(final int index) {
2258             final File file = (File)fModel.getValueAt(index, 0);
2259             return (file != null && isSelectableInList(file));
2260         }
2261 
2262         // Make sure everything in the selection interval is valid
2263         void verifySelectionInterval(int index0, int index1, boolean isSetSelection) {
2264             if (index0 > index1) {
2265                 final int tmp = index1;
2266                 index1 = index0;
2267                 index0 = tmp;
2268             }
2269             int start = index0;
2270             int end;
2271             do {
2272                 // Find the first selectable file in the range
2273                 for (; start <= index1; start++) {
2274                     if (isSelectableInListIndex(start)) break;
2275                 }
2276                 end = -1;
2277                 // Find the last selectable file in the range
2278                 for (int i = start; i <= index1; i++) {
2279                     if (!isSelectableInListIndex(i)) {
2280                         break;
2281                     }
2282                     end = i;
2283                 }
2284                 // Select the range
2285                 if (end >= 0) {
2286                     // If setting the selection, do "set" the first time to clear the old one
2287                     // after that do "add" to extend it
2288                     if (isSetSelection) {
2289                         super.setSelectionInterval(start, end);
2290                         isSetSelection = false;
2291                     } else {
2292                         super.addSelectionInterval(start, end);
2293                     }
2294                     start = end + 1;
2295                 } else {
2296                     break;
2297                 }
2298             } while (start <= index1);
2299         }
2300 
2301         public void setAnchorSelectionIndex(final int anchorIndex) {
2302             if (isSelectableInListIndex(anchorIndex)) super.setAnchorSelectionIndex(anchorIndex);
2303         }
2304 
2305         public void setLeadSelectionIndex(final int leadIndex) {
2306             if (isSelectableInListIndex(leadIndex)) super.setLeadSelectionIndex(leadIndex);
2307         }
2308 
2309         public void setSelectionInterval(final int index0, final int index1) {
2310             if (index0 == -1 || index1 == -1) { return; }
2311 
2312             if ((getSelectionMode() == SINGLE_SELECTION) || (index0 == index1)) {
2313                 if (isSelectableInListIndex(index1)) super.setSelectionInterval(index1, index1);
2314             } else {
2315                 verifySelectionInterval(index0, index1, true);
2316             }
2317         }
2318 
2319         public void addSelectionInterval(final int index0, final int index1) {
2320             if (index0 == -1 || index1 == -1) { return; }
2321 
2322             if (index0 == index1) {
2323                 if (isSelectableInListIndex(index1)) super.addSelectionInterval(index1, index1);
2324                 return;
2325             }
2326 
2327             if (getSelectionMode() != MULTIPLE_INTERVAL_SELECTION) {
2328                 setSelectionInterval(index0, index1);
2329                 return;
2330             }
2331 
2332             verifySelectionInterval(index0, index1, false);
2333         }
2334     }
2335 
2336     // Convenience, to translate from the JList directory view to the Mac-style JTable
2337     //   & minimize diffs between this and BasicFileChooserUI
2338     @SuppressWarnings("serial") // Superclass is not serializable across versions
2339     class JTableExtension extends JTable {
2340         public void setSelectedIndex(final int index) {
2341             getSelectionModel().setSelectionInterval(index, index);
2342         }
2343 
2344         public void removeSelectedIndex(final int index) {
2345             getSelectionModel().removeSelectionInterval(index, index);
2346         }
2347 
2348         public void ensureIndexIsVisible(final int index) {
2349             final Rectangle cellBounds = getCellRect(index, 0, false);
2350             if (cellBounds != null) {
2351                 scrollRectToVisible(cellBounds);
2352             }
2353         }
2354 
2355         public int locationToIndex(final Point location) {
2356             return rowAtPoint(location);
2357         }
2358     }
2359 }