Adds a filter to event sound chooser dialog, in order to select only WAV files.

cusax-fix
Vincent Lucas 14 years ago
parent fc72e5e429
commit 571240d77f

@ -143,7 +143,8 @@ public NotificationConfigurationPanel()
GenericFileDialog.create(null,
Resources.getString("plugin.notificationconfig.BROWSE_PROGRAM"),
SipCommFileChooser.LOAD_FILE_OPERATION);
fileChooserSound.addFilter(new SoundFilter());
String[] soundFormats = {SoundFileUtils.wav};
fileChooserSound.setFileFilter(new SoundFilter(soundFormats));
}
/**

@ -13,42 +13,100 @@
import net.java.sip.communicator.util.swing.*;
/**
* Filter to display only the sound files in the filechooser
* Filter to display only the sound files in the filechooser.
*
* @author Alexandre Maillard
* @author Vincent Lucas
*/
public class SoundFilter
extends SipCommFileFilter
{
/**
* Method which describes differents permits extensions and defines which file or
* directory will be displayed in the filechoser.
* All acceptable sound formats. If null, then this sound filter will accept
* all sound formats available in SoundFileUtils.
*/
private String[] soundFormats = null;
/**
* Creates a new sound filter which accepts all sound format available in
* SoundFileUtils.
*/
public SoundFilter()
{
super();
}
/**
* Creates a new sound filter which accepts only sound format corresponding
* to the list given in parameter.
*
* @param soundFormats The list of sound format to accept.
*/
public SoundFilter(String[] soundFormats)
{
super();
if(soundFormats != null)
{
this.soundFormats = new String[soundFormats.length];
System.arraycopy(
soundFormats,
0,
this.soundFormats,
0,
soundFormats.length);
}
}
/**
* Method which describes differents permits extensions and defines which
* file or directory will be displayed in the filechoser.
*
* @param f file for the test
*
* @return boolean true if the File is a Directory or a sound file. And
* return false in the other cases.
*/
public boolean accept(File f)
{
/*
* Test if the file passed in argument is a directory.
*/
// Tests if the file passed in argument is a directory.
if (f.isDirectory())
{
return true;
}
/*
* Else, it tests if the exension is correct
*/
return SoundFileUtils.isSoundFile(f);
// Else, tests if the exension is correct.
else
{
return SoundFileUtils.isSoundFile(f, this.soundFormats);
}
}
/**
* Method which describes, in the file chooser, the text representing the permit extension
* files.
* Method which describes, in the file chooser, the text representing the
* permit extension files.
*
* @return String which is displayed in the sound file chooser.
*/
public String getDescription()
{
return "Sound File (*.au, *.mid, *.mod, *.mp2, *.mp3, *.ogg, *.ram," +
"*.wav, *.wma)";
String desc = "Sound File (";
if(this.soundFormats != null)
{
for(int i = 0; i < this.soundFormats.length; ++i)
{
if(i != 0)
{
desc += ", ";
}
desc += "*." + this.soundFormats[i];
}
}
else
{
desc += "*.au, *.mid, *.mod, *.mp2, *.mp3, *.ogg, *.ram, *.wav, "
+ "*.wma";
}
desc += ")";
return desc;
}
}

@ -7,12 +7,14 @@
package net.java.sip.communicator.util;
import java.io.*;
import java.util.*;
/**
* Defines the different permit extension file.
*
* @author Alexandre Maillard
* @author Dmitri Melnikov
* @author Vincent Lucas
*/
public class SoundFileUtils
{
@ -64,6 +66,44 @@ public static boolean isSoundFile(File f)
return false;
}
/**
* Checks whether this file is a sound file.
*
* @param f <tt>File</tt> to check
* @param soundFormats The sound formats to restrict the file name
* extension. If soundFormats is null, then every sound format defined by
* SoundFileUtils is correct.
*
* @return <tt>true</tt> if it's a sound file conforming to the format given
* in parameters (if soundFormats is null, then every sound format defined
* by SoundFileUtils is correct), <tt>false</tt> otherwise.
*/
public static boolean isSoundFile(File f, String[] soundFormats)
{
// If there is no specific filters, then compare the file to all sound
// extension available.
if(soundFormats == null)
{
return SoundFileUtils.isSoundFile(f);
}
// Compare the file extension to the sound formats provided in
// parameter.
else
{
String ext = getExtension(f);
// If the file has an extension
if (ext != null)
{
return (Arrays.binarySearch(
soundFormats,
ext,
String.CASE_INSENSITIVE_ORDER) != -1);
}
}
return false;
}
/**
* Gets the file extension.
* TODO: There are at least 2 other methods like this scattered around

@ -83,6 +83,13 @@ public interface SipCommFileChooser
*/
public void addFilter(SipCommFileFilter filter);
/**
* Sets a file filter to this dialog.
*
* @param filter the filter to add
*/
public void setFileFilter(SipCommFileFilter filter);
/**
* Returns the filter the user has chosen for saving a file.
*

@ -128,6 +128,16 @@ public void addFilter(SipCommFileFilter filter)
this.addChoosableFileFilter(filter);
}
/**
* Sets a file filter to this dialog.
*
* @param filter the filter to add
*/
public void setFileFilter(SipCommFileFilter filter)
{
super.setFileFilter(filter);
}
/**
* Returns the filter the user has chosen for saving a file.
*

@ -109,6 +109,16 @@ public void addFilter(SipCommFileFilter filter)
this.setFilenameFilter(filter);
}
/**
* Sets a file filter to this dialog.
*
* @param filter the filter to add
*/
public void setFileFilter(SipCommFileFilter filter)
{
this.setFilenameFilter(filter);
}
/**
* Returns the filter the user has chosen for saving a file.
*

Loading…
Cancel
Save