KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > io > JFilenameFilter


1 /*
2     =========================================================================
3     Package io - Implements the IO and Streams methods.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JFilenameFilter.java,v 1.8 2007/01/28 17:39:20 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.8 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.io;
40
41 import java.util.*;
42
43
44 public class JFilenameFilter implements java.io.FilenameFilter JavaDoc {
45   
46   ArrayList<String JavaDoc> filter = null;
47   
48   
49   /**
50    * Constructor. Creates and initializes
51    * all class data.
52    */

53   public JFilenameFilter() {
54   
55     filter = new ArrayList<String JavaDoc>();
56   }
57  
58    /**
59     * Add a filter extension. Be carefull,
60     * this class only accept the filter (not wildcards
61     * like *.jar.
62     * @param strFilter The new filter to
63     * add.
64     */

65   public boolean add( String JavaDoc strFilter ) {
66      
67     int nSize = filter.size();
68     
69     
70     // Check for the string entry in the list
71
for( int nCount = 0; nCount < nSize; nCount++ ) {
72       if( ( ( java.lang.String JavaDoc ) filter.get( nCount ) ).compareTo( strFilter ) == 0 )
73         return true;
74     }
75     
76     return filter.add( strFilter );
77   }
78
79   /**
80    * Remove a extension previously added to this
81    * filter.
82    * @param strFilter The new filter to remove.
83    */

84   public boolean remove( String JavaDoc strFilter ) {
85     
86     int nSize = filter.size();
87     
88     
89     // Check for the string entry in the list
90
for( int nCount = 0; nCount < nSize; nCount++ ) {
91       if( ( ( java.lang.String JavaDoc ) filter.get( nCount ) ).compareTo( strFilter ) == 0 ) {
92         filter.remove( nCount );
93         return true;
94       }
95     }
96     
97     return false;
98   }
99
100    /**
101    * Implements the accept interface
102    * method of superclass java.io.FilenameFilter.
103    * @param file The directory wich the file was found;
104    * @param strFileName The name of the file;
105    */

106   public boolean accept( java.io.File JavaDoc file, java.lang.String JavaDoc strFileName ) {
107     
108     int nSize = filter.size();
109     int nDot = strFileName.indexOf( '.' );
110     String JavaDoc strFilter;
111     
112     
113     if( nDot >= 0 )
114       strFilter = strFileName.substring( nDot + 1 );
115     else
116       return false;
117     
118     // Check for the string entry in the list
119
for( int nCount = 0; nCount < nSize; nCount++ ) {
120       if( ( ( java.lang.String JavaDoc ) filter.get( nCount ) ).compareTo( strFilter ) == 0 )
121         return true;
122     }
123  
124     return false;
125   }
126 }
127
128 // JFilenameFilter class
Popular Tags