View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.customizer;
21  
22  import java.io.File;
23  
24  import javax.swing.JFileChooser;
25  
26  /**
27   * <code>ImageFilter</code> is a file filter for common image files.
28   * This filter can be used by a {@link JFileChooser} and will filter out
29   * all but <i>png</i>, <i>jpg</i> and <i>jpeg</i> files.
30   *
31   */
32  public abstract class ImageFilter extends javax.swing.filechooser.FileFilter
33  {
34    /**
35      This is the one of the methods that is declared in
36      the abstract class
37     */
38    public boolean accept(File f)
39    {
40      //if it is a directory -- we want to show it so return true.
41      if (f.isDirectory())
42        return true;
43    
44      //get the extension of the file
45  
46      String extension = getExtension(f);
47      if ((extension.equals("png")) || (extension.equals("jpeg"))||
48      		(extension.equals("jpg")))
49         return true;
50  
51      //default -- fall through. False is return on all
52      //occasions except:
53      //a) the file is a directory
54      //b) the file's extension is what we are looking for.
55      return false;
56    }
57      
58    /**
59      Again, this is declared in the abstract class
60  
61      The description of this filter
62     */
63    public String getDescription()
64    {
65        return "Picture files";
66    }
67  
68    /**
69      Method to get the extension of the file, in lowercase
70     */
71    protected String getExtension(File f)
72    {
73      String s = f.getName();
74      int i = s.lastIndexOf('.');
75      if (i > 0 &&  i < s.length() - 1)
76        return s.substring(i+1).toLowerCase();
77      return "";
78    }
79  }