< prev index next >

src/share/classes/com/sun/java/swing/plaf/windows/WindowsFileChooserUI.java

Print this page
rev 1495 : 7024118: possible hardcoded mnemonic for JFileChooser metal and motif l&f
Reviewed-by: rupashka
rev 1580 : 6727661: Code improvement and warnings removing from the swing/plaf packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: alexp
Contributed-by: Florian Brunner <fbrunnerlist@gmx.ch>


 665             bottomPanel = new JPanel();
 666         }
 667         return bottomPanel;
 668     }
 669 
 670     protected void installIcons(JFileChooser fc) {
 671         super.installIcons(fc);
 672         viewMenuIcon = UIManager.getIcon("FileChooser.viewMenuIcon");
 673     }
 674 
 675     protected void uninstallIcons(JFileChooser fc) {
 676         super.uninstallIcons(fc);
 677         viewMenuIcon = null;
 678     }
 679 
 680     protected void installStrings(JFileChooser fc) {
 681         super.installStrings(fc);
 682 
 683         Locale l = fc.getLocale();
 684 
 685         lookInLabelMnemonic = UIManager.getInt("FileChooser.lookInLabelMnemonic");
 686         lookInLabelText = UIManager.getString("FileChooser.lookInLabelText",l);
 687         saveInLabelText = UIManager.getString("FileChooser.saveInLabelText",l);
 688 
 689         fileNameLabelMnemonic = UIManager.getInt("FileChooser.fileNameLabelMnemonic");
 690         fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText",l);
 691         folderNameLabelMnemonic = UIManager.getInt("FileChooser.folderNameLabelMnemonic");
 692         folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText",l);
 693 
 694         filesOfTypeLabelMnemonic = UIManager.getInt("FileChooser.filesOfTypeLabelMnemonic");
 695         filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText",l);
 696 
 697         upFolderToolTipText =  UIManager.getString("FileChooser.upFolderToolTipText",l);
 698         upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName",l);
 699 
 700         homeFolderToolTipText =  UIManager.getString("FileChooser.homeFolderToolTipText",l);
 701         homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName",l);
 702 
 703         newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText",l);
 704         newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName",l);
 705 
 706         listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText",l);
 707         listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName",l);
 708 
 709         detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText",l);
 710         detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName",l);
 711 
 712         viewMenuButtonToolTipText = UIManager.getString("FileChooser.viewMenuButtonToolTipText",l);
 713         viewMenuButtonAccessibleName = UIManager.getString("FileChooser.viewMenuButtonAccessibleName",l);
 714     }
 715 




 716     protected void installListeners(JFileChooser fc) {
 717         super.installListeners(fc);
 718         ActionMap actionMap = getActionMap();
 719         SwingUtilities.replaceUIActionMap(fc, actionMap);
 720     }
 721 
 722     protected ActionMap getActionMap() {
 723         return createActionMap();
 724     }
 725 
 726     protected ActionMap createActionMap() {
 727         ActionMap map = new ActionMapUIResource();
 728         FilePane.addActionsToMap(map, filePane.getActions());
 729         return map;
 730     }
 731 
 732     protected JPanel createList(JFileChooser fc) {
 733         return filePane.createList();
 734     }
 735 


 980                     doFilterChanged(e);
 981                 } else if(s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
 982                     doFileSelectionModeChanged(e);
 983                 } else if(s.equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY)) {
 984                     doAccessoryChanged(e);
 985                 } else if (s.equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY) ||
 986                            s.equals(JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY)) {
 987                     doApproveButtonTextChanged(e);
 988                 } else if(s.equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)) {
 989                     doDialogTypeChanged(e);
 990                 } else if(s.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY)) {
 991                     doApproveButtonMnemonicChanged(e);
 992                 } else if(s.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) {
 993                     doControlButtonsChanged(e);
 994                 } else if (s == "FileChooser.useShellFolder") {
 995                     updateUseShellFolder();
 996                     doDirectoryChanged(e);
 997                 } else if (s.equals("componentOrientation")) {
 998                     ComponentOrientation o = (ComponentOrientation)e.getNewValue();
 999                     JFileChooser cc = (JFileChooser)e.getSource();
1000                     if (o != (ComponentOrientation)e.getOldValue()) {
1001                         cc.applyComponentOrientation(o);
1002                     }
1003                 } else if (s.equals("ancestor")) {
1004                     if (e.getOldValue() == null && e.getNewValue() != null) {
1005                         // Ancestor was added, set initial focus
1006                         filenameTextField.selectAll();
1007                         filenameTextField.requestFocus();
1008                     }
1009                 }
1010             }
1011         };
1012     }
1013 
1014 
1015     protected void removeControlButtons() {
1016         getBottomPanel().remove(getButtonPanel());
1017     }
1018 
1019     protected void addControlButtons() {
1020         getBottomPanel().add(getButtonPanel());


1120             return icon.getIconWidth() + depth*space;
1121         }
1122 
1123         public int getIconHeight() {
1124             return icon.getIconHeight();
1125         }
1126 
1127     }
1128 
1129     //
1130     // DataModel for DirectoryComboxbox
1131     //
1132     protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
1133         return new DirectoryComboBoxModel();
1134     }
1135 
1136     /**
1137      * Data model for a type-face selection combo-box.
1138      */
1139     protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
1140         Vector directories = new Vector();
1141         int[] depths = null;
1142         File selectedDirectory = null;
1143         JFileChooser chooser = getFileChooser();
1144         FileSystemView fsv = chooser.getFileSystemView();
1145 
1146         public DirectoryComboBoxModel() {
1147             // Add the current directory to the model, and make it the
1148             // selectedDirectory
1149             File dir = getFileChooser().getCurrentDirectory();
1150             if(dir != null) {
1151                 addItem(dir);
1152             }
1153         }
1154 
1155         /**
1156          * Adds the directory to the model and sets it to be selected,
1157          * additionally clears out the previous selected directory and
1158          * the paths leading up to it, if any.
1159          */
1160         private void addItem(File directory) {
1161 
1162             if(directory == null) {
1163                 return;
1164             }
1165 
1166             directories.clear();
1167 
1168             File[] baseFolders = (useShellFolder)
1169                     ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
1170                     : fsv.getRoots();
1171             directories.addAll(Arrays.asList(baseFolders));
1172 
1173             // Get the canonical (full) path. This has the side
1174             // benefit of removing extraneous chars from the path,
1175             // for example /foo/bar/ becomes /foo/bar
1176             File canonical = null;
1177             try {
1178                 canonical = directory.getCanonicalFile();
1179             } catch (IOException e) {
1180                 // Maybe drive is not ready. Can't abort here.
1181                 canonical = directory;
1182             }
1183 
1184             // create File instances of each directory leading up to the top
1185             try {
1186                 File sf = useShellFolder ? ShellFolder.getShellFolder(canonical)
1187                                          : canonical;
1188                 File f = sf;
1189                 Vector path = new Vector(10);
1190                 do {
1191                     path.addElement(f);
1192                 } while ((f = f.getParentFile()) != null);
1193 
1194                 int pathCount = path.size();
1195                 // Insert chain at appropriate place in vector
1196                 for (int i = 0; i < pathCount; i++) {
1197                     f = (File)path.get(i);
1198                     if (directories.contains(f)) {
1199                         int topIndex = directories.indexOf(f);
1200                         for (int j = i-1; j >= 0; j--) {
1201                             directories.insertElementAt(path.get(j), topIndex+i-j);
1202                         }
1203                         break;
1204                     }
1205                 }
1206                 calculateDepths();
1207                 setSelectedItem(sf);
1208             } catch (FileNotFoundException ex) {
1209                 calculateDepths();
1210             }
1211         }
1212 
1213         private void calculateDepths() {
1214             depths = new int[directories.size()];
1215             for (int i = 0; i < depths.length; i++) {
1216                 File dir = (File)directories.get(i);
1217                 File parent = dir.getParentFile();
1218                 depths[i] = 0;
1219                 if (parent != null) {
1220                     for (int j = i-1; j >= 0; j--) {
1221                         if (parent.equals((File)directories.get(j))) {
1222                             depths[i] = depths[j] + 1;
1223                             break;
1224                         }
1225                     }
1226                 }
1227             }
1228         }
1229 
1230         public int getDepth(int i) {
1231             return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0;
1232         }
1233 
1234         public void setSelectedItem(Object selectedDirectory) {
1235             this.selectedDirectory = (File)selectedDirectory;
1236             fireContentsChanged(this, -1, -1);
1237         }
1238 
1239         public Object getSelectedItem() {
1240             return selectedDirectory;
1241         }


1300                 fireContentsChanged(this, -1, -1);
1301             }
1302         }
1303 
1304         public void setSelectedItem(Object filter) {
1305             if(filter != null) {
1306                 getFileChooser().setFileFilter((FileFilter) filter);
1307                 fireContentsChanged(this, -1, -1);
1308             }
1309         }
1310 
1311         public Object getSelectedItem() {
1312             // Ensure that the current filter is in the list.
1313             // NOTE: we shouldnt' have to do this, since JFileChooser adds
1314             // the filter to the choosable filters list when the filter
1315             // is set. Lets be paranoid just in case someone overrides
1316             // setFileFilter in JFileChooser.
1317             FileFilter currentFilter = getFileChooser().getFileFilter();
1318             boolean found = false;
1319             if(currentFilter != null) {
1320                 for(int i=0; i < filters.length; i++) {
1321                     if(filters[i] == currentFilter) {
1322                         found = true;
1323                     }
1324                 }
1325                 if(found == false) {
1326                     getFileChooser().addChoosableFileFilter(currentFilter);
1327                 }
1328             }
1329             return getFileChooser().getFileFilter();
1330         }
1331 
1332         public int getSize() {
1333             if(filters != null) {
1334                 return filters.length;
1335             } else {
1336                 return 0;
1337             }
1338         }
1339 
1340         public Object getElementAt(int index) {
1341             if(index > getSize() - 1) {




 665             bottomPanel = new JPanel();
 666         }
 667         return bottomPanel;
 668     }
 669 
 670     protected void installIcons(JFileChooser fc) {
 671         super.installIcons(fc);
 672         viewMenuIcon = UIManager.getIcon("FileChooser.viewMenuIcon");
 673     }
 674 
 675     protected void uninstallIcons(JFileChooser fc) {
 676         super.uninstallIcons(fc);
 677         viewMenuIcon = null;
 678     }
 679 
 680     protected void installStrings(JFileChooser fc) {
 681         super.installStrings(fc);
 682 
 683         Locale l = fc.getLocale();
 684 
 685         lookInLabelMnemonic = getMnemonic("FileChooser.lookInLabelMnemonic", l);
 686         lookInLabelText = UIManager.getString("FileChooser.lookInLabelText",l);
 687         saveInLabelText = UIManager.getString("FileChooser.saveInLabelText",l);
 688 
 689         fileNameLabelMnemonic = getMnemonic("FileChooser.fileNameLabelMnemonic", l);
 690         fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText",l);
 691         folderNameLabelMnemonic = getMnemonic("FileChooser.folderNameLabelMnemonic", l);
 692         folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText",l);
 693 
 694         filesOfTypeLabelMnemonic = getMnemonic("FileChooser.filesOfTypeLabelMnemonic", l);
 695         filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText",l);
 696 
 697         upFolderToolTipText =  UIManager.getString("FileChooser.upFolderToolTipText",l);
 698         upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName",l);
 699 
 700         homeFolderToolTipText =  UIManager.getString("FileChooser.homeFolderToolTipText",l);
 701         homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName",l);
 702 
 703         newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText",l);
 704         newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName",l);
 705 
 706         listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText",l);
 707         listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName",l);
 708 
 709         detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText",l);
 710         detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName",l);
 711 
 712         viewMenuButtonToolTipText = UIManager.getString("FileChooser.viewMenuButtonToolTipText",l);
 713         viewMenuButtonAccessibleName = UIManager.getString("FileChooser.viewMenuButtonAccessibleName",l);
 714     }
 715 
 716     private Integer getMnemonic(String key, Locale l) {
 717         return SwingUtilities2.getUIDefaultsInt(key, l);
 718     }
 719 
 720     protected void installListeners(JFileChooser fc) {
 721         super.installListeners(fc);
 722         ActionMap actionMap = getActionMap();
 723         SwingUtilities.replaceUIActionMap(fc, actionMap);
 724     }
 725 
 726     protected ActionMap getActionMap() {
 727         return createActionMap();
 728     }
 729 
 730     protected ActionMap createActionMap() {
 731         ActionMap map = new ActionMapUIResource();
 732         FilePane.addActionsToMap(map, filePane.getActions());
 733         return map;
 734     }
 735 
 736     protected JPanel createList(JFileChooser fc) {
 737         return filePane.createList();
 738     }
 739 


 984                     doFilterChanged(e);
 985                 } else if(s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
 986                     doFileSelectionModeChanged(e);
 987                 } else if(s.equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY)) {
 988                     doAccessoryChanged(e);
 989                 } else if (s.equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY) ||
 990                            s.equals(JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY)) {
 991                     doApproveButtonTextChanged(e);
 992                 } else if(s.equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY)) {
 993                     doDialogTypeChanged(e);
 994                 } else if(s.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY)) {
 995                     doApproveButtonMnemonicChanged(e);
 996                 } else if(s.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) {
 997                     doControlButtonsChanged(e);
 998                 } else if (s == "FileChooser.useShellFolder") {
 999                     updateUseShellFolder();
1000                     doDirectoryChanged(e);
1001                 } else if (s.equals("componentOrientation")) {
1002                     ComponentOrientation o = (ComponentOrientation)e.getNewValue();
1003                     JFileChooser cc = (JFileChooser)e.getSource();
1004                     if (o != e.getOldValue()) {
1005                         cc.applyComponentOrientation(o);
1006                     }
1007                 } else if (s.equals("ancestor")) {
1008                     if (e.getOldValue() == null && e.getNewValue() != null) {
1009                         // Ancestor was added, set initial focus
1010                         filenameTextField.selectAll();
1011                         filenameTextField.requestFocus();
1012                     }
1013                 }
1014             }
1015         };
1016     }
1017 
1018 
1019     protected void removeControlButtons() {
1020         getBottomPanel().remove(getButtonPanel());
1021     }
1022 
1023     protected void addControlButtons() {
1024         getBottomPanel().add(getButtonPanel());


1124             return icon.getIconWidth() + depth*space;
1125         }
1126 
1127         public int getIconHeight() {
1128             return icon.getIconHeight();
1129         }
1130 
1131     }
1132 
1133     //
1134     // DataModel for DirectoryComboxbox
1135     //
1136     protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
1137         return new DirectoryComboBoxModel();
1138     }
1139 
1140     /**
1141      * Data model for a type-face selection combo-box.
1142      */
1143     protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
1144         Vector<File> directories = new Vector<File>();
1145         int[] depths = null;
1146         File selectedDirectory = null;
1147         JFileChooser chooser = getFileChooser();
1148         FileSystemView fsv = chooser.getFileSystemView();
1149 
1150         public DirectoryComboBoxModel() {
1151             // Add the current directory to the model, and make it the
1152             // selectedDirectory
1153             File dir = getFileChooser().getCurrentDirectory();
1154             if(dir != null) {
1155                 addItem(dir);
1156             }
1157         }
1158 
1159         /**
1160          * Adds the directory to the model and sets it to be selected,
1161          * additionally clears out the previous selected directory and
1162          * the paths leading up to it, if any.
1163          */
1164         private void addItem(File directory) {
1165 
1166             if(directory == null) {
1167                 return;
1168             }
1169 
1170             directories.clear();
1171 
1172             File[] baseFolders = (useShellFolder)
1173                     ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
1174                     : fsv.getRoots();
1175             directories.addAll(Arrays.asList(baseFolders));
1176 
1177             // Get the canonical (full) path. This has the side
1178             // benefit of removing extraneous chars from the path,
1179             // for example /foo/bar/ becomes /foo/bar
1180             File canonical;
1181             try {
1182                 canonical = directory.getCanonicalFile();
1183             } catch (IOException e) {
1184                 // Maybe drive is not ready. Can't abort here.
1185                 canonical = directory;
1186             }
1187 
1188             // create File instances of each directory leading up to the top
1189             try {
1190                 File sf = useShellFolder ? ShellFolder.getShellFolder(canonical)
1191                                          : canonical;
1192                 File f = sf;
1193                 Vector<File> path = new Vector<File>(10);
1194                 do {
1195                     path.addElement(f);
1196                 } while ((f = f.getParentFile()) != null);
1197 
1198                 int pathCount = path.size();
1199                 // Insert chain at appropriate place in vector
1200                 for (int i = 0; i < pathCount; i++) {
1201                     f = path.get(i);
1202                     if (directories.contains(f)) {
1203                         int topIndex = directories.indexOf(f);
1204                         for (int j = i-1; j >= 0; j--) {
1205                             directories.insertElementAt(path.get(j), topIndex+i-j);
1206                         }
1207                         break;
1208                     }
1209                 }
1210                 calculateDepths();
1211                 setSelectedItem(sf);
1212             } catch (FileNotFoundException ex) {
1213                 calculateDepths();
1214             }
1215         }
1216 
1217         private void calculateDepths() {
1218             depths = new int[directories.size()];
1219             for (int i = 0; i < depths.length; i++) {
1220                 File dir = directories.get(i);
1221                 File parent = dir.getParentFile();
1222                 depths[i] = 0;
1223                 if (parent != null) {
1224                     for (int j = i-1; j >= 0; j--) {
1225                         if (parent.equals(directories.get(j))) {
1226                             depths[i] = depths[j] + 1;
1227                             break;
1228                         }
1229                     }
1230                 }
1231             }
1232         }
1233 
1234         public int getDepth(int i) {
1235             return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0;
1236         }
1237 
1238         public void setSelectedItem(Object selectedDirectory) {
1239             this.selectedDirectory = (File)selectedDirectory;
1240             fireContentsChanged(this, -1, -1);
1241         }
1242 
1243         public Object getSelectedItem() {
1244             return selectedDirectory;
1245         }


1304                 fireContentsChanged(this, -1, -1);
1305             }
1306         }
1307 
1308         public void setSelectedItem(Object filter) {
1309             if(filter != null) {
1310                 getFileChooser().setFileFilter((FileFilter) filter);
1311                 fireContentsChanged(this, -1, -1);
1312             }
1313         }
1314 
1315         public Object getSelectedItem() {
1316             // Ensure that the current filter is in the list.
1317             // NOTE: we shouldnt' have to do this, since JFileChooser adds
1318             // the filter to the choosable filters list when the filter
1319             // is set. Lets be paranoid just in case someone overrides
1320             // setFileFilter in JFileChooser.
1321             FileFilter currentFilter = getFileChooser().getFileFilter();
1322             boolean found = false;
1323             if(currentFilter != null) {
1324                 for (FileFilter filter : filters) {
1325                     if (filter == currentFilter) {
1326                         found = true;
1327                     }
1328                 }
1329                 if(found == false) {
1330                     getFileChooser().addChoosableFileFilter(currentFilter);
1331                 }
1332             }
1333             return getFileChooser().getFileFilter();
1334         }
1335 
1336         public int getSize() {
1337             if(filters != null) {
1338                 return filters.length;
1339             } else {
1340                 return 0;
1341             }
1342         }
1343 
1344         public Object getElementAt(int index) {
1345             if(index > getSize() - 1) {


< prev index next >