001    package org.shiftone.jrat.util.io;
002    
003    
004    import org.shiftone.jrat.util.log.Logger;
005    import org.shiftone.jrat.util.regex.GlobMatcher;
006    
007    import java.io.File;
008    
009    
010    /**
011     * Class GlobFileFilter
012     *
013     * @author jeff@shiftone.org (Jeff Drost)
014     */
015    public class GlobFileFilter extends javax.swing.filechooser.FileFilter implements java.io.FileFilter {
016    
017        private static final Logger LOG = Logger.getLogger(GlobFileFilter.class);
018        private GlobMatcher[] globMatchers = null;
019        private String description = null;
020    
021        public GlobFileFilter(String[] globPatterns, String description) {
022    
023            StringBuffer sb = new StringBuffer(" (");
024    
025            this.globMatchers = new GlobMatcher[globPatterns.length];
026    
027            for (int i = 0; i < globPatterns.length; i++) {
028                globMatchers[i] = new GlobMatcher(globPatterns[i]);
029    
030                if (i != 0) {
031                    sb.append(", ");
032                }
033    
034                sb.append(globPatterns[i]);
035            }
036    
037            sb.append(")");
038    
039            this.description = description + sb.toString();
040        }
041    
042    
043        /**
044         * Method accept
045         */
046        public boolean accept(File f) {
047    
048            if (f.isDirectory()) {
049                return true;
050            }
051    
052            for (int i = 0; i < globMatchers.length; i++) {
053                if (globMatchers[i].isMatch(f.getName())) {
054                    return true;
055                }
056            }
057    
058            return false;
059        }
060    
061    
062        /**
063         * Method getDescription
064         */
065        public String getDescription() {
066            return description;
067        }
068    }