001    package net.sourceforge.mp3dings;
002    
003    /* Mp3dings - manage mp3 meta-information
004    * Copyright (C) 2003 Moritz Ringler
005    * $Id: Mp3FileChooser.java 43 2007-10-20 17:39:59Z ringler $
006    *
007    * This program is free software; you can redistribute it and/or
008    * modify it under the terms of the GNU General Public License
009    * as published by the Free Software Foundation; either version 2
010    * of the License, or (at your option) any later version.
011    *
012    * This program is distributed in the hope that it will be useful,
013    * but WITHOUT ANY WARRANTY; without even the implied warranty of
014    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015    * GNU General Public License for more details.
016    *
017    * You should have received a copy of the GNU General Public License
018    * along with this program; if not, write to the Free Software
019    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020    */
021    
022    import java.awt.event.ActionEvent;
023    import java.awt.event.ActionListener;
024    import java.io.File;
025    import java.util.prefs.Preferences;
026    import javax.swing.JFileChooser;
027    import javax.swing.filechooser.FileFilter;
028    
029    /**
030    * A FileChooser for opening mp3 audio files.
031    * @author Moritz Ringler
032    * @version $Revision: 43 $ ($Date: 2007-10-20 19:39:59 +0200 (Sa, 20 Okt 2007) $)
033    */
034    
035    public final class Mp3FileChooser extends JFileChooser{
036        private Mp3Filter mp3Filter=new Mp3Filter();
037        static Preferences pref;
038    
039        public Mp3FileChooser(){
040            setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
041            setMultiSelectionEnabled(true);
042            setAcceptAllFileFilterUsed(false);
043            setFileFilter((FileFilter) mp3Filter);
044            setDialogTitle(Globals.getString("DATA_ADD_FILES_TEXT"));
045            pref = Globals.getPrefNode();
046            String cwd = pref.get("CurrentDirectory","_notFound_");
047            File cd = (cwd != "_notFound_")?new File(cwd):null;
048            setCurrentDirectory(cd);
049            this.addActionListener(new ActionListener(){
050                public void actionPerformed(ActionEvent e){
051                    if (e.getActionCommand()==APPROVE_SELECTION){
052                        pref.put("CurrentDirectory",getCurrentDirectory().
053                        getAbsolutePath());
054                    }
055                }
056            });
057        }
058    
059        /** A file filter that accepts mp3 files and directories. */
060        public static class Mp3Filter extends FileFilter implements java.io.FileFilter{
061            //Accept all directories and all mp3 files.
062    
063            private static String getExtension(File f) {
064                String ext = null;
065                String s = f.getName();
066                int i = s.lastIndexOf('.');
067                if (i > 0 && i < s.length() - 1) {
068                    ext = s.substring(i+1).toLowerCase();
069                }
070                return ext;
071            }
072    
073            public boolean accept(File f) {
074                if (f.isDirectory()) {
075                    return true;
076                }
077                String extension = getExtension(f);
078                if (extension != null) {
079                    if (extension.equals("mp3")){
080                        return true;
081                    }
082                } else {
083                    return false;
084                }
085                return false;
086            }
087    
088            //The description of this filter
089            public String getDescription() {
090                return "MPEG 1 Layer III";
091            }
092        }
093    }