View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.gui;
22  
23  import java.io.File;
24  import java.util.ResourceBundle;
25  
26  import javax.swing.filechooser.FileFilter;
27  
28  /**
29   *  Class responsible for filtering the type of files to show.
30   *
31   *@author   Rommel N. Carvalho (rommel.carvalho@gmail.com)
32   *@author 	Michael S. Onishi (michael.onishi@gmail.com)
33   *@see      FileFilter
34   */
35  public class SimpleFileFilter extends FileFilter {
36  
37      private String[] extensions;
38      private String description;
39  
40  	/** Load resource file from this package */
41    	private static ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
42    			unbbayes.gui.resources.GuiResources.class.getName());
43  
44  
45      /**
46       *  Creates a <code>FileFilter</code> with the desired file extension.
47       *
48       *@param  ext  Extension of the files to be shown.
49       *@see         FileFilter
50       */
51      public SimpleFileFilter(String ext) {
52          this(new String[]{ext}, null);
53      }
54  
55  
56      /**
57       *  Creates a <code>FileFilter</code> with the desired file extension and description.
58       *
59       *@param  exts   Extensions of the files to be shown.
60       *@param  descr  Descriptions of the files to be shown.
61       *@see	FileFilter
62       */
63      public SimpleFileFilter(String[] exts, String descr) {
64          //clona e coloca em lowercase as extens�es
65          extensions = new String[exts.length];
66          for (int i = exts.length - 1; i >= 0; i--) {
67              extensions[i] = exts[i].toLowerCase();
68          }
69          //verificar se temos uma descri��o v�lida
70          description = (descr == null ? exts[0] + resource.getString("filesText") : descr);
71      }
72  
73  
74      /**
75       *  Returns a generic description.
76       *
77       *@return    A generic description.
78       */
79      public String getDescription() {
80          return description;
81      }
82  
83  
84      /**
85       *  Verify if the file has one of the valid extensions.
86       *
87       *@param  f  File to be checked for valid extension.
88       *@return    True if it has a valid extension, false otherwise.
89       */
90      public boolean accept(File f) {
91          // We will always allow directories
92          if (f.isDirectory()) {
93              return true;
94          }
95  
96          // Check for valid extension
97          String name = f.getName().toLowerCase();
98          for (int i = extensions.length - 1; i >= 0; i--) {
99              if (name.endsWith(extensions[i])) {
100                 return true;
101             }
102         }
103         return false;
104     }
105 }
106