< prev index next >

src/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java

Print this page
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>


 742             return ((icon != null) ? icon.getIconWidth() : 0) + depth*space;
 743         }
 744 
 745         public int getIconHeight() {
 746             return (icon != null) ? icon.getIconHeight() : 0;
 747         }
 748 
 749     }
 750 
 751     //
 752     // DataModel for DirectoryComboxbox
 753     //
 754     protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
 755         return new DirectoryComboBoxModel();
 756     }
 757 
 758     /**
 759      * Data model for a type-face selection combo-box.
 760      */
 761     protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
 762         Vector directories = new Vector();
 763         int[] depths = null;
 764         File selectedDirectory = null;
 765         JFileChooser chooser = getFileChooser();
 766         FileSystemView fsv = chooser.getFileSystemView();
 767 
 768         public DirectoryComboBoxModel() {
 769             // Add the current directory to the model, and make it the
 770             // selectedDirectory
 771             File dir = getFileChooser().getCurrentDirectory();
 772             if (dir != null) {
 773                 addItem(dir);
 774             }
 775         }
 776 
 777         /**
 778          * Adds the directory to the model and sets it to be selected,
 779          * additionally clears out the previous selected directory and
 780          * the paths leading up to it, if any.
 781          */
 782         public void addItem(File directory) {
 783 
 784             if (directory == null) {
 785                 return;
 786             }
 787 
 788             int oldSize = directories.size();
 789             directories.clear();
 790             if (oldSize > 0) {
 791                 fireIntervalRemoved(this, 0, oldSize);
 792             }
 793 
 794             File[] baseFolders = (useShellFolder)
 795                     ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
 796                     : fsv.getRoots();
 797             directories.addAll(Arrays.asList(baseFolders));
 798 
 799             // Get the canonical (full) path. This has the side
 800             // benefit of removing extraneous chars from the path,
 801             // for example /foo/bar/ becomes /foo/bar
 802             File canonical = null;
 803             try {
 804                 canonical = directory.getCanonicalFile();
 805             } catch (IOException e) {
 806                 // Maybe drive is not ready. Can't abort here.
 807                 canonical = directory;
 808             }
 809 
 810             // create File instances of each directory leading up to the top
 811             try {
 812                 File sf = useShellFolder ? ShellFolder.getShellFolder(canonical)
 813                                          : canonical;
 814                 File f = sf;
 815                 Vector path = new Vector(10);
 816                 do {
 817                     path.addElement(f);
 818                 } while ((f = f.getParentFile()) != null);
 819 
 820                 int pathCount = path.size();
 821                 // Insert chain at appropriate place in vector
 822                 for (int i = 0; i < pathCount; i++) {
 823                     f = (File)path.get(i);
 824                     if (directories.contains(f)) {
 825                         int topIndex = directories.indexOf(f);
 826                         for (int j = i-1; j >= 0; j--) {
 827                             directories.insertElementAt(path.get(j), topIndex+i-j);
 828                         }
 829                         break;
 830                     }
 831                 }
 832                 calculateDepths();
 833                 setSelectedItem(sf);
 834             } catch (FileNotFoundException ex) {
 835                 calculateDepths();
 836             }
 837         }
 838 
 839         private void calculateDepths() {
 840             depths = new int[directories.size()];
 841             for (int i = 0; i < depths.length; i++) {
 842                 File dir = (File)directories.get(i);
 843                 File parent = dir.getParentFile();
 844                 depths[i] = 0;
 845                 if (parent != null) {
 846                     for (int j = i-1; j >= 0; j--) {
 847                         if (parent.equals((File)directories.get(j))) {
 848                             depths[i] = depths[j] + 1;
 849                             break;
 850                         }
 851                     }
 852                 }
 853             }
 854         }
 855 
 856         public int getDepth(int i) {
 857             return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0;
 858         }
 859 
 860         public void setSelectedItem(Object selectedDirectory) {
 861             this.selectedDirectory = (File)selectedDirectory;
 862             fireContentsChanged(this, -1, -1);
 863         }
 864 
 865         public Object getSelectedItem() {
 866             return selectedDirectory;
 867         }


 953                 fireContentsChanged(this, -1, -1);
 954             }
 955         }
 956 
 957         public void setSelectedItem(Object filter) {
 958             if(filter != null) {
 959                 getFileChooser().setFileFilter((FileFilter) filter);
 960                 fireContentsChanged(this, -1, -1);
 961             }
 962         }
 963 
 964         public Object getSelectedItem() {
 965             // Ensure that the current filter is in the list.
 966             // NOTE: we shouldnt' have to do this, since JFileChooser adds
 967             // the filter to the choosable filters list when the filter
 968             // is set. Lets be paranoid just in case someone overrides
 969             // setFileFilter in JFileChooser.
 970             FileFilter currentFilter = getFileChooser().getFileFilter();
 971             boolean found = false;
 972             if(currentFilter != null) {
 973                 for(int i=0; i < filters.length; i++) {
 974                     if(filters[i] == currentFilter) {
 975                         found = true;
 976                     }
 977                 }
 978                 if(found == false) {
 979                     getFileChooser().addChoosableFileFilter(currentFilter);
 980                 }
 981             }
 982             return getFileChooser().getFileFilter();
 983         }
 984 
 985         public int getSize() {
 986             if(filters != null) {
 987                 return filters.length;
 988             } else {
 989                 return 0;
 990             }
 991         }
 992 
 993         public Object getElementAt(int index) {
 994             if(index > getSize() - 1) {




 742             return ((icon != null) ? icon.getIconWidth() : 0) + depth*space;
 743         }
 744 
 745         public int getIconHeight() {
 746             return (icon != null) ? icon.getIconHeight() : 0;
 747         }
 748 
 749     }
 750 
 751     //
 752     // DataModel for DirectoryComboxbox
 753     //
 754     protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
 755         return new DirectoryComboBoxModel();
 756     }
 757 
 758     /**
 759      * Data model for a type-face selection combo-box.
 760      */
 761     protected class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
 762         Vector<File> directories = new Vector<File>();
 763         int[] depths = null;
 764         File selectedDirectory = null;
 765         JFileChooser chooser = getFileChooser();
 766         FileSystemView fsv = chooser.getFileSystemView();
 767 
 768         public DirectoryComboBoxModel() {
 769             // Add the current directory to the model, and make it the
 770             // selectedDirectory
 771             File dir = getFileChooser().getCurrentDirectory();
 772             if (dir != null) {
 773                 addItem(dir);
 774             }
 775         }
 776 
 777         /**
 778          * Adds the directory to the model and sets it to be selected,
 779          * additionally clears out the previous selected directory and
 780          * the paths leading up to it, if any.
 781          */
 782         public void addItem(File directory) {
 783 
 784             if (directory == null) {
 785                 return;
 786             }
 787 
 788             int oldSize = directories.size();
 789             directories.clear();
 790             if (oldSize > 0) {
 791                 fireIntervalRemoved(this, 0, oldSize);
 792             }
 793 
 794             File[] baseFolders = (useShellFolder)
 795                     ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
 796                     : fsv.getRoots();
 797             directories.addAll(Arrays.asList(baseFolders));
 798 
 799             // Get the canonical (full) path. This has the side
 800             // benefit of removing extraneous chars from the path,
 801             // for example /foo/bar/ becomes /foo/bar
 802             File canonical;
 803             try {
 804                 canonical = directory.getCanonicalFile();
 805             } catch (IOException e) {
 806                 // Maybe drive is not ready. Can't abort here.
 807                 canonical = directory;
 808             }
 809 
 810             // create File instances of each directory leading up to the top
 811             try {
 812                 File sf = useShellFolder ? ShellFolder.getShellFolder(canonical)
 813                                          : canonical;
 814                 File f = sf;
 815                 Vector<File> path = new Vector<File>(10);
 816                 do {
 817                     path.addElement(f);
 818                 } while ((f = f.getParentFile()) != null);
 819 
 820                 int pathCount = path.size();
 821                 // Insert chain at appropriate place in vector
 822                 for (int i = 0; i < pathCount; i++) {
 823                     f = path.get(i);
 824                     if (directories.contains(f)) {
 825                         int topIndex = directories.indexOf(f);
 826                         for (int j = i-1; j >= 0; j--) {
 827                             directories.insertElementAt(path.get(j), topIndex+i-j);
 828                         }
 829                         break;
 830                     }
 831                 }
 832                 calculateDepths();
 833                 setSelectedItem(sf);
 834             } catch (FileNotFoundException ex) {
 835                 calculateDepths();
 836             }
 837         }
 838 
 839         private void calculateDepths() {
 840             depths = new int[directories.size()];
 841             for (int i = 0; i < depths.length; i++) {
 842                 File dir = directories.get(i);
 843                 File parent = dir.getParentFile();
 844                 depths[i] = 0;
 845                 if (parent != null) {
 846                     for (int j = i-1; j >= 0; j--) {
 847                         if (parent.equals(directories.get(j))) {
 848                             depths[i] = depths[j] + 1;
 849                             break;
 850                         }
 851                     }
 852                 }
 853             }
 854         }
 855 
 856         public int getDepth(int i) {
 857             return (depths != null && i >= 0 && i < depths.length) ? depths[i] : 0;
 858         }
 859 
 860         public void setSelectedItem(Object selectedDirectory) {
 861             this.selectedDirectory = (File)selectedDirectory;
 862             fireContentsChanged(this, -1, -1);
 863         }
 864 
 865         public Object getSelectedItem() {
 866             return selectedDirectory;
 867         }


 953                 fireContentsChanged(this, -1, -1);
 954             }
 955         }
 956 
 957         public void setSelectedItem(Object filter) {
 958             if(filter != null) {
 959                 getFileChooser().setFileFilter((FileFilter) filter);
 960                 fireContentsChanged(this, -1, -1);
 961             }
 962         }
 963 
 964         public Object getSelectedItem() {
 965             // Ensure that the current filter is in the list.
 966             // NOTE: we shouldnt' have to do this, since JFileChooser adds
 967             // the filter to the choosable filters list when the filter
 968             // is set. Lets be paranoid just in case someone overrides
 969             // setFileFilter in JFileChooser.
 970             FileFilter currentFilter = getFileChooser().getFileFilter();
 971             boolean found = false;
 972             if(currentFilter != null) {
 973                 for (FileFilter filter : filters) {
 974                     if (filter == currentFilter) {
 975                         found = true;
 976                     }
 977                 }
 978                 if(found == false) {
 979                     getFileChooser().addChoosableFileFilter(currentFilter);
 980                 }
 981             }
 982             return getFileChooser().getFileFilter();
 983         }
 984 
 985         public int getSize() {
 986             if(filters != null) {
 987                 return filters.length;
 988             } else {
 989                 return 0;
 990             }
 991         }
 992 
 993         public Object getElementAt(int index) {
 994             if(index > getSize() - 1) {


< prev index next >