src/share/classes/javax/swing/plaf/basic/BasicFileChooserUI.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2013, 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


 735     public Action getChangeToParentDirectoryAction() {
 736         return changeToParentDirectoryAction;
 737     }
 738 
 739     public Action getApproveSelectionAction() {
 740         return approveSelectionAction;
 741     }
 742 
 743     public Action getCancelSelectionAction() {
 744         return cancelSelectionAction;
 745     }
 746 
 747     public Action getUpdateAction() {
 748         return updateAction;
 749     }
 750 
 751 
 752     /**
 753      * Creates a new folder.
 754      */

 755     protected class NewFolderAction extends AbstractAction {
 756         protected NewFolderAction() {
 757             super(FilePane.ACTION_NEW_FOLDER);
 758         }
 759         public void actionPerformed(ActionEvent e) {
 760             if (readOnly) {
 761                 return;
 762             }
 763             JFileChooser fc = getFileChooser();
 764             File currentDirectory = fc.getCurrentDirectory();
 765 
 766             if (!currentDirectory.exists()) {
 767                 JOptionPane.showMessageDialog(
 768                     fc,
 769                     newFolderParentDoesntExistText,
 770                     newFolderParentDoesntExistTitleText, JOptionPane.WARNING_MESSAGE);
 771                 return;
 772             }
 773 
 774             File newFolder;


 777                 if (fc.isMultiSelectionEnabled()) {
 778                     fc.setSelectedFiles(new File[] { newFolder });
 779                 } else {
 780                     fc.setSelectedFile(newFolder);
 781                 }
 782             } catch (IOException exc) {
 783                 JOptionPane.showMessageDialog(
 784                     fc,
 785                     newFolderErrorText + newFolderErrorSeparator + exc,
 786                     newFolderErrorText, JOptionPane.ERROR_MESSAGE);
 787                 return;
 788             }
 789 
 790             fc.rescanCurrentDirectory();
 791         }
 792     }
 793 
 794     /**
 795      * Acts on the "home" key event or equivalent event.
 796      */

 797     protected class GoHomeAction extends AbstractAction {
 798         protected GoHomeAction() {
 799             super("Go Home");
 800         }
 801         public void actionPerformed(ActionEvent e) {
 802             JFileChooser fc = getFileChooser();
 803             changeDirectory(fc.getFileSystemView().getHomeDirectory());
 804         }
 805     }
 806 

 807     protected class ChangeToParentDirectoryAction extends AbstractAction {
 808         protected ChangeToParentDirectoryAction() {
 809             super("Go Up");
 810             putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY);
 811         }
 812         public void actionPerformed(ActionEvent e) {
 813             getFileChooser().changeToParentDirectory();
 814         }
 815     }
 816 
 817     /**
 818      * Responds to an Open or Save request
 819      */

 820     protected class ApproveSelectionAction extends AbstractAction {
 821         protected ApproveSelectionAction() {
 822             super(FilePane.ACTION_APPROVE_SELECTION);
 823         }
 824         public void actionPerformed(ActionEvent e) {
 825             if (isDirectorySelected()) {
 826                 File dir = getDirectory();
 827                 if (dir != null) {
 828                     try {
 829                         // Strip trailing ".."
 830                         dir = ShellFolder.getNormalizedFile(dir);
 831                     } catch (IOException ex) {
 832                         // Ok, use f as is
 833                     }
 834                     changeDirectory(dir);
 835                     return;
 836                 }
 837             }
 838 
 839             JFileChooser chooser = getFileChooser();


1108         }
1109 
1110         public boolean accept(File f) {
1111             if (f == null) {
1112                 return false;
1113             }
1114             if (f.isDirectory()) {
1115                 return true;
1116             }
1117             return pattern.matcher(f.getName()).matches();
1118         }
1119 
1120         public String getDescription() {
1121             return globPattern;
1122         }
1123     }
1124 
1125     /**
1126      * Responds to a cancel request.
1127      */

1128     protected class CancelSelectionAction extends AbstractAction {
1129         public void actionPerformed(ActionEvent e) {
1130             getFileChooser().cancelSelection();
1131         }
1132     }
1133 
1134     /**
1135      * Rescans the files in the current directory
1136      */

1137     protected class UpdateAction extends AbstractAction {
1138         public void actionPerformed(ActionEvent e) {
1139             JFileChooser fc = getFileChooser();
1140             fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
1141             fc.rescanCurrentDirectory();
1142         }
1143     }
1144 
1145 
1146     private void changeDirectory(File dir) {
1147         JFileChooser fc = getFileChooser();
1148         // Traverse shortcuts on Windows
1149         if (dir != null && FilePane.usesShellFolder(fc)) {
1150             try {
1151                 ShellFolder shellFolder = ShellFolder.getShellFolder(dir);
1152 
1153                 if (shellFolder.isLink()) {
1154                     File linkedTo = shellFolder.getLinkLocation();
1155 
1156                     // If linkedTo is null we try to use dir


1269             cacheIcon(f, icon);
1270             return icon;
1271         }
1272 
1273         public Boolean isHidden(File f) {
1274             String name = f.getName();
1275             if(name != null && name.charAt(0) == '.') {
1276                 return Boolean.TRUE;
1277             } else {
1278                 return Boolean.FALSE;
1279             }
1280         }
1281     }
1282 
1283     private static final TransferHandler defaultTransferHandler = new FileTransferHandler();
1284 
1285     /**
1286      * Data transfer support for the file chooser.  Since files are currently presented
1287      * as a list, the list support is reused with the added flavor of DataFlavor.javaFileListFlavor
1288      */

1289     static class FileTransferHandler extends TransferHandler implements UIResource {
1290 
1291         /**
1292          * Create a Transferable to use as the source for a data transfer.
1293          *
1294          * @param c  The component holding the data to be transfered.  This
1295          *  argument is provided to enable sharing of TransferHandlers by
1296          *  multiple components.
1297          * @return  The representation of the data to be transfered.
1298          *
1299          */
1300         protected Transferable createTransferable(JComponent c) {
1301             Object[] values = null;
1302             if (c instanceof JList) {
1303                 values = ((JList)c).getSelectedValues();
1304             } else if (c instanceof JTable) {
1305                 JTable table = (JTable)c;
1306                 int[] rows = table.getSelectedRows();
1307                 if (rows != null) {
1308                     values = new Object[rows.length];


   1 /*
   2  * Copyright (c) 1998, 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


 735     public Action getChangeToParentDirectoryAction() {
 736         return changeToParentDirectoryAction;
 737     }
 738 
 739     public Action getApproveSelectionAction() {
 740         return approveSelectionAction;
 741     }
 742 
 743     public Action getCancelSelectionAction() {
 744         return cancelSelectionAction;
 745     }
 746 
 747     public Action getUpdateAction() {
 748         return updateAction;
 749     }
 750 
 751 
 752     /**
 753      * Creates a new folder.
 754      */
 755     @SuppressWarnings("serial") // Superclass is not serializable across versions
 756     protected class NewFolderAction extends AbstractAction {
 757         protected NewFolderAction() {
 758             super(FilePane.ACTION_NEW_FOLDER);
 759         }
 760         public void actionPerformed(ActionEvent e) {
 761             if (readOnly) {
 762                 return;
 763             }
 764             JFileChooser fc = getFileChooser();
 765             File currentDirectory = fc.getCurrentDirectory();
 766 
 767             if (!currentDirectory.exists()) {
 768                 JOptionPane.showMessageDialog(
 769                     fc,
 770                     newFolderParentDoesntExistText,
 771                     newFolderParentDoesntExistTitleText, JOptionPane.WARNING_MESSAGE);
 772                 return;
 773             }
 774 
 775             File newFolder;


 778                 if (fc.isMultiSelectionEnabled()) {
 779                     fc.setSelectedFiles(new File[] { newFolder });
 780                 } else {
 781                     fc.setSelectedFile(newFolder);
 782                 }
 783             } catch (IOException exc) {
 784                 JOptionPane.showMessageDialog(
 785                     fc,
 786                     newFolderErrorText + newFolderErrorSeparator + exc,
 787                     newFolderErrorText, JOptionPane.ERROR_MESSAGE);
 788                 return;
 789             }
 790 
 791             fc.rescanCurrentDirectory();
 792         }
 793     }
 794 
 795     /**
 796      * Acts on the "home" key event or equivalent event.
 797      */
 798     @SuppressWarnings("serial") // Superclass is not serializable across versions
 799     protected class GoHomeAction extends AbstractAction {
 800         protected GoHomeAction() {
 801             super("Go Home");
 802         }
 803         public void actionPerformed(ActionEvent e) {
 804             JFileChooser fc = getFileChooser();
 805             changeDirectory(fc.getFileSystemView().getHomeDirectory());
 806         }
 807     }
 808 
 809     @SuppressWarnings("serial") // Superclass is not serializable across versions
 810     protected class ChangeToParentDirectoryAction extends AbstractAction {
 811         protected ChangeToParentDirectoryAction() {
 812             super("Go Up");
 813             putValue(Action.ACTION_COMMAND_KEY, FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY);
 814         }
 815         public void actionPerformed(ActionEvent e) {
 816             getFileChooser().changeToParentDirectory();
 817         }
 818     }
 819 
 820     /**
 821      * Responds to an Open or Save request
 822      */
 823     @SuppressWarnings("serial") // Superclass is not serializable across versions
 824     protected class ApproveSelectionAction extends AbstractAction {
 825         protected ApproveSelectionAction() {
 826             super(FilePane.ACTION_APPROVE_SELECTION);
 827         }
 828         public void actionPerformed(ActionEvent e) {
 829             if (isDirectorySelected()) {
 830                 File dir = getDirectory();
 831                 if (dir != null) {
 832                     try {
 833                         // Strip trailing ".."
 834                         dir = ShellFolder.getNormalizedFile(dir);
 835                     } catch (IOException ex) {
 836                         // Ok, use f as is
 837                     }
 838                     changeDirectory(dir);
 839                     return;
 840                 }
 841             }
 842 
 843             JFileChooser chooser = getFileChooser();


1112         }
1113 
1114         public boolean accept(File f) {
1115             if (f == null) {
1116                 return false;
1117             }
1118             if (f.isDirectory()) {
1119                 return true;
1120             }
1121             return pattern.matcher(f.getName()).matches();
1122         }
1123 
1124         public String getDescription() {
1125             return globPattern;
1126         }
1127     }
1128 
1129     /**
1130      * Responds to a cancel request.
1131      */
1132     @SuppressWarnings("serial") // Superclass is not serializable across versions
1133     protected class CancelSelectionAction extends AbstractAction {
1134         public void actionPerformed(ActionEvent e) {
1135             getFileChooser().cancelSelection();
1136         }
1137     }
1138 
1139     /**
1140      * Rescans the files in the current directory
1141      */
1142     @SuppressWarnings("serial") // Superclass is not serializable across versions
1143     protected class UpdateAction extends AbstractAction {
1144         public void actionPerformed(ActionEvent e) {
1145             JFileChooser fc = getFileChooser();
1146             fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
1147             fc.rescanCurrentDirectory();
1148         }
1149     }
1150 
1151 
1152     private void changeDirectory(File dir) {
1153         JFileChooser fc = getFileChooser();
1154         // Traverse shortcuts on Windows
1155         if (dir != null && FilePane.usesShellFolder(fc)) {
1156             try {
1157                 ShellFolder shellFolder = ShellFolder.getShellFolder(dir);
1158 
1159                 if (shellFolder.isLink()) {
1160                     File linkedTo = shellFolder.getLinkLocation();
1161 
1162                     // If linkedTo is null we try to use dir


1275             cacheIcon(f, icon);
1276             return icon;
1277         }
1278 
1279         public Boolean isHidden(File f) {
1280             String name = f.getName();
1281             if(name != null && name.charAt(0) == '.') {
1282                 return Boolean.TRUE;
1283             } else {
1284                 return Boolean.FALSE;
1285             }
1286         }
1287     }
1288 
1289     private static final TransferHandler defaultTransferHandler = new FileTransferHandler();
1290 
1291     /**
1292      * Data transfer support for the file chooser.  Since files are currently presented
1293      * as a list, the list support is reused with the added flavor of DataFlavor.javaFileListFlavor
1294      */
1295     @SuppressWarnings("serial") // JDK-implementation class
1296     static class FileTransferHandler extends TransferHandler implements UIResource {
1297 
1298         /**
1299          * Create a Transferable to use as the source for a data transfer.
1300          *
1301          * @param c  The component holding the data to be transfered.  This
1302          *  argument is provided to enable sharing of TransferHandlers by
1303          *  multiple components.
1304          * @return  The representation of the data to be transfered.
1305          *
1306          */
1307         protected Transferable createTransferable(JComponent c) {
1308             Object[] values = null;
1309             if (c instanceof JList) {
1310                 values = ((JList)c).getSelectedValues();
1311             } else if (c instanceof JTable) {
1312                 JTable table = (JTable)c;
1313                 int[] rows = table.getSelectedRows();
1314                 if (rows != null) {
1315                     values = new Object[rows.length];