diff --git a/src/net/java/sip/communicator/plugin/notificationconfiguration/NotificationConfigurationPanel.java b/src/net/java/sip/communicator/plugin/notificationconfiguration/NotificationConfigurationPanel.java index 590948210..b5cc785a5 100644 --- a/src/net/java/sip/communicator/plugin/notificationconfiguration/NotificationConfigurationPanel.java +++ b/src/net/java/sip/communicator/plugin/notificationconfiguration/NotificationConfigurationPanel.java @@ -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)); } /** diff --git a/src/net/java/sip/communicator/plugin/notificationconfiguration/SoundFilter.java b/src/net/java/sip/communicator/plugin/notificationconfiguration/SoundFilter.java index cef7cf883..83c7e90f8 100644 --- a/src/net/java/sip/communicator/plugin/notificationconfiguration/SoundFilter.java +++ b/src/net/java/sip/communicator/plugin/notificationconfiguration/SoundFilter.java @@ -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; } } diff --git a/src/net/java/sip/communicator/util/SoundFileUtils.java b/src/net/java/sip/communicator/util/SoundFileUtils.java index e5adfa4b6..136085ec7 100644 --- a/src/net/java/sip/communicator/util/SoundFileUtils.java +++ b/src/net/java/sip/communicator/util/SoundFileUtils.java @@ -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 File 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 true 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), false 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 diff --git a/src/net/java/sip/communicator/util/swing/SipCommFileChooser.java b/src/net/java/sip/communicator/util/swing/SipCommFileChooser.java index e7a95dc42..4a59e2340 100644 --- a/src/net/java/sip/communicator/util/swing/SipCommFileChooser.java +++ b/src/net/java/sip/communicator/util/swing/SipCommFileChooser.java @@ -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. * diff --git a/src/net/java/sip/communicator/util/swing/SipCommFileChooserImpl.java b/src/net/java/sip/communicator/util/swing/SipCommFileChooserImpl.java index 7a2fbc5d1..dfb27b7c5 100644 --- a/src/net/java/sip/communicator/util/swing/SipCommFileChooserImpl.java +++ b/src/net/java/sip/communicator/util/swing/SipCommFileChooserImpl.java @@ -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. * diff --git a/src/net/java/sip/communicator/util/swing/SipCommFileDialogImpl.java b/src/net/java/sip/communicator/util/swing/SipCommFileDialogImpl.java index f0b39a481..8c5cc1e0e 100644 --- a/src/net/java/sip/communicator/util/swing/SipCommFileDialogImpl.java +++ b/src/net/java/sip/communicator/util/swing/SipCommFileDialogImpl.java @@ -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. *