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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
/*
* NAT - An universal Translator
* Copyright (C) 2005 Bruno Mascret
* Contact: bmascret@free.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ui;
import java.io.File;
import javax.swing.filechooser.FileFilter;
/**
* Classe de gestion des filtres pour les fichiers utilisés dans nat en fonction de leur extension
* d'après http://brassens.upmf-grenoble.fr/IMSS/dciss/Enseignements/PSR/Prog/Java/dialogueFichier.htm
* @author bruno
*
*/
public class FiltreFichier extends FileFilter
{
/** Tableau des suffixes (extensions) des fichiers */
private String []lesSuffixes;
/** Descriptions des suffixes de {@link #lesSuffixes}*/
private String laDescription;
/**
* Constructeur
* <p>Crée un filtre avec la description <code>laDescription</code> pour les suffixes
* <code>lesSuffixes</code></p>
* @param lesSuff liste des suffixes
* @param laDesc description pour les suffixes
*/
public FiltreFichier(String []lesSuff, String laDesc)
{
lesSuffixes = lesSuff;
laDescription = laDesc;
}
/**
* Implémentation de javax.swing.filechooser.FileFilter#getDescription()
* <p>Méthode d'accès renvoyant la description du filtre</p>
* @see javax.swing.filechooser.FileFilter#getDescription()
*/
@Override
public String getDescription(){return laDescription;}
/**
* Renvoie vrai si le suffixe <code>suffixe</code> fait partie du tableau {@link #lesSuffixes}
* @param suffixe le suffixe (extension) à rechercher
* @return true si le suffixe est dans {@link #lesSuffixes}, false sinon
*/
boolean appartient(String suffixe )
{
boolean retour = false;
for( int i = 0; i<lesSuffixes.length; ++i)
{
if(suffixe.equals(lesSuffixes[i])){retour = true;}
}
return retour;
}
/**
* Implémentation de javax.swing.filechooser.FileFilter#accept(java.io.File)
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
* @return true si le fichier <code>f</code> correspond au filtre de l'instance
*/
@Override
public boolean accept(File f)
{
boolean retour = false;
if (f.isDirectory()){retour = true;}
else
{
String suffixe = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1){suffixe = s.substring(i+1).toLowerCase();}
retour = suffixe != null && appartient(suffixe);
}
return retour;
}
}
|