田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * <p>Title:</p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (C) 2011 </p>
 * <p>Company: VCI </p>
 * @author Bear
 * @time 2011-8-1
 * @version 1.0
 */
package com.vci.client.ui.swing.filechooser;
 
 
import java.io.File;
 
import javax.swing.filechooser.FileFilter;
 
public abstract class AbstractFileFilter extends FileFilter {
    protected String[] suffixs = {};//{ ".jgp", ".jpeg", ".gif", ".png" };
    protected String descroption = "";//"Image Files";
    public AbstractFileFilter(String[] suffixs, String descroption){
        this.suffixs = suffixs;
        this.descroption = descroption;
    }
    
    protected String getStuffixString(){
        StringBuffer sb = new StringBuffer();
        if(suffixs.length != 0){
            for(String str : suffixs){
                sb.append("*").append(str).append("|");
            }
            sb.replace(sb.length() - 1, sb.length(), "");
        }
        return this.descroption + "(" + sb.toString() + ")";
    }
 
    protected boolean isMatched(String file) {
        boolean res = false;
        for (String suffix : suffixs) {
            res = res || file.toLowerCase().endsWith(suffix);
        }
        return res;
    }
    
    public String[] getSuffixs() {
        return suffixs;
    }
 
    public void setSuffixs(String[] suffixs) {
        this.suffixs = suffixs;
    }
 
    public void setDescroption(String descroption) {
        this.descroption = descroption;
    }
 
    @Override
    public boolean accept(File f) {
        if (f.isDirectory())
            return true;
        return isMatched(f.getName());
    }
 
    @Override
    public String getDescription() {
        return getStuffixString();
    }
}