Enhanced resource management.

cusax-fix
Yana Stamcheva 18 years ago
parent ebb735b2d0
commit cc7bb6ce26

@ -14,10 +14,8 @@
import javax.imageio.*; import javax.imageio.*;
import javax.swing.*; import javax.swing.*;
import net.java.sip.communicator.service.resources.*; import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*; import net.java.sip.communicator.util.*;
import org.osgi.framework.*; import org.osgi.framework.*;
/** /**
@ -33,58 +31,72 @@ public class ResourceManagementServiceImpl
private static Logger logger = private static Logger logger =
Logger.getLogger(ResourceManagementServiceImpl.class); Logger.getLogger(ResourceManagementServiceImpl.class);
private Hashtable colorPacks; private Map<String, String> colorResources;
private ResourcePack colorPack = null;
private Hashtable imagePacks; private Map<String, String> imageResources;
private ResourcePack imagePack = null;
private Hashtable languagePacks; private LanguagePack languagePack = null;
private Hashtable settingsPacks; private Map<String, String> settingsResources;
private ResourcePack settingsPack = null;
private Hashtable soundPacks; private Map<String, String> soundResources;
private ResourcePack soundPack = null;
/**
* Initializes already registered default resource packs.
*/
ResourceManagementServiceImpl() ResourceManagementServiceImpl()
{ {
ResourceManagementActivator.bundleContext.addServiceListener(this); ResourceManagementActivator.bundleContext.addServiceListener(this);
colorPacks = colorPack =
getDefaultPacks(ColorPack.class.getName(), getDefaultResourcePack(ColorPack.class.getName(),
ColorPack.RESOURCE_NAME_DEFAULT_VALUE); ColorPack.RESOURCE_NAME_DEFAULT_VALUE);
imagePacks = if (colorPack != null)
getDefaultPacks(ImagePack.class.getName(), colorResources = getResources(colorPack);
ImagePack.RESOURCE_NAME_DEFAULT_VALUE);
imagePack =
getDefaultResourcePack(ImagePack.class.getName(),
ImagePack.RESOURCE_NAME_DEFAULT_VALUE);
languagePacks = if (imagePack != null)
getDefaultPacks(LanguagePack.class.getName(), imageResources = getResources(imagePack);
LanguagePack.RESOURCE_NAME_DEFAULT_VALUE);
settingsPacks = languagePack =
getDefaultPacks(SettingsPack.class.getName(), (LanguagePack) getDefaultResourcePack(LanguagePack.class.getName(),
SettingsPack.RESOURCE_NAME_DEFAULT_VALUE); LanguagePack.RESOURCE_NAME_DEFAULT_VALUE);
soundPacks = settingsPack =
getDefaultPacks( SoundPack.class.getName(), getDefaultResourcePack(SettingsPack.class.getName(),
SoundPack.RESOURCE_NAME_DEFAULT_VALUE); SettingsPack.RESOURCE_NAME_DEFAULT_VALUE);
if (settingsPack != null)
settingsResources = getResources(settingsPack);
soundPack =
getDefaultResourcePack(SoundPack.class.getName(),
SoundPack.RESOURCE_NAME_DEFAULT_VALUE);
if (soundPack != null)
soundResources = getResources(soundPack);
} }
/** /**
* Returns a map of default <tt>ResourcePack</tt>s, corresponding to the * Searches for the <tt>ResourcePack</tt> corresponding to the given
* given className and typeName. * <tt>className</tt> and <tt></tt>.
* * @param className The name of the resource class.
* @param className The name of the class of resource packs, for example * @param typeName The name of the type we're looking for.
* ColorPacl class name. * For example: RESOURCE_NAME_DEFAULT_VALUE
* @param typeName The name of type of resource packs. * @return the <tt>ResourcePack</tt> corresponding to the given
* @return a map of default <tt>ResourcePack</tt>s, corresponding to the * <tt>className</tt> and <tt></tt>.
* given className and typeName.
*/ */
private Hashtable<ResourcePack, ResourceBundle> getDefaultPacks( private ResourcePack getDefaultResourcePack(String className,
String className, String typeName)
String typeName)
{ {
Hashtable<ResourcePack, ResourceBundle> resourcePackList
= new Hashtable<ResourcePack, ResourceBundle>();
ServiceReference[] serRefs = null; ServiceReference[] serRefs = null;
String osgiFilter = String osgiFilter =
@ -110,104 +122,30 @@ private Hashtable<ResourcePack, ResourceBundle> getDefaultPacks(
(ResourcePack) ResourceManagementActivator.bundleContext. (ResourcePack) ResourceManagementActivator.bundleContext.
getService(serRefs[i]); getService(serRefs[i]);
ResourceBundle rb return rp;
= getResourceBundle(rp);
resourcePackList.put(rp, rb);
} }
} }
return resourcePackList; return null;
}
/**
* Returns the <tt>ResourceBundle</tt> corresponding to the given
* <tt>ResourcePack</tt>.
*
* @param resourcePack the <tt>ResourcePack</tt> for which we're searching
* the bundle.
* @return the <tt>ResourceBundle</tt> corresponding to the given
* <tt>ResourcePack</tt>.
*/
private ResourceBundle getResourceBundle(ResourcePack resourcePack)
{
String baseName = resourcePack.getResourcePackBaseName();
return ResourceBundle.getBundle(
baseName,
Locale.getDefault(),
resourcePack.getClass().getClassLoader());
}
/**
* Returns a list of language <tt>ResourceBundle</tt>s obtained for the
* given locale.
*
* @param locale the <tt>Locale</tt> which we're searching for
* @return the list of language <tt>ResourceBundle</tt>s obtained for the
* given locale.
*/
private Vector<ResourceBundle> getLanguagePacksForLocale(Locale locale)
{
Vector localePacks = new Vector();
Enumeration packsEnum = languagePacks.keys();
while (packsEnum.hasMoreElements())
{
LanguagePack langPack = (LanguagePack) packsEnum.nextElement();
ResourceBundle rBundle = ResourceBundle.getBundle(
langPack.getResourcePackBaseName(),
locale,
langPack.getClass().getClassLoader());
if (rBundle != null)
localePacks.add(rBundle);
}
return localePacks;
} }
/** /**
* Returns the String corresponding to the given key. * Returns the <tt>Map</tt> of (key, value) pairs contained in the given
* resource pack.
* *
* @param key the key for the string we're searching for. * @param resourcePack The <tt>ResourcePack</tt> from which we're obtaining
* @param resourceBundles the Enumeration of <tt>ResourceBundle</tt>s, where * the resources.
* to search * @return the <tt>Map</tt> of (key, value) pairs contained in the given
* @return the String corresponding to the given key. * resource pack.
*/ */
private String findString( String key, private Map<String, String> getResources(ResourcePack resourcePack)
Enumeration<ResourceBundle> resourceBundles)
{ {
ResourceBundle resourceBundle; return resourcePack.getResources();
while (resourceBundles.hasMoreElements())
{
resourceBundle = resourceBundles.nextElement();
try
{
String value = resourceBundle.getString(key);
if (value != null)
{
return value;
}
}
catch (MissingResourceException e)
{
// If we don't find the resource in the this resource bundle
// we continue the search.
continue;
}
}
// nothing found
return null;
} }
/** /**
* Handles <tt>ServiceEvent</tt>s in order to update the list of registered * Handles all <tt>ServiceEvent</tt>s corresponding to <tt>ResourcePack</tt>
* <tt>ResourcePack</tt>s. * being registered or unregistered.
*/ */
public void serviceChanged(ServiceEvent event) public void serviceChanged(ServiceEvent event)
{ {
@ -225,94 +163,97 @@ public void serviceChanged(ServiceEvent event)
{ {
logger.info("Resource registered " + resourcePack); logger.info("Resource registered " + resourcePack);
String resourceBaseName = resourcePack.getResourcePackBaseName(); Map<String, String> resources = getResources(resourcePack);
ResourceBundle resourceBundle = ResourceBundle.getBundle( if(resourcePack instanceof ColorPack && colorPack == null)
resourceBaseName,
Locale.getDefault(),
resourcePack.getClass().getClassLoader());
if(resourcePack instanceof ColorPack)
{ {
if (colorPacks == null) colorPack = resourcePack;
colorPacks = new Hashtable(); colorResources = resources;
colorPacks.put(resourcePack, getResourceBundle(resourcePack));
} }
else if(resourcePack instanceof ImagePack) else if(resourcePack instanceof ImagePack && imagePack == null)
{ {
if (imagePacks == null) imagePack = resourcePack;
imagePacks = new Hashtable(); imageResources = resources;
imagePacks.put(resourcePack, getResourceBundle(resourcePack));
} }
else if(resourcePack instanceof LanguagePack) else if(resourcePack instanceof LanguagePack && languagePack == null)
{ {
if (languagePacks == null) languagePack = (LanguagePack) resourcePack;
languagePacks = new Hashtable();
languagePacks.put(resourcePack, getResourceBundle(resourcePack));
} }
else if(resourcePack instanceof SettingsPack) else if(resourcePack instanceof SettingsPack && settingsPack == null)
{ {
if (settingsPacks == null) settingsPack = resourcePack;
settingsPacks = new Hashtable(); settingsResources = resources;
settingsPacks.put(resourcePack, getResourceBundle(resourcePack));
} }
else if(resourcePack instanceof SoundPack) else if(resourcePack instanceof SoundPack && soundPack == null)
{ {
if (soundPacks == null) soundPack = resourcePack;
soundPacks = new Hashtable(); soundResources = resources;
soundPacks.put(resourcePack, getResourceBundle(resourcePack));
} }
} }
else if (event.getType() == ServiceEvent.UNREGISTERING) else if (event.getType() == ServiceEvent.UNREGISTERING)
{ {
if(resourcePack instanceof ColorPack) if(resourcePack instanceof ColorPack
&& colorPack.equals(resourcePack))
{ {
colorPacks = colorPack =
getDefaultPacks(ColorPack.class.getName(), getDefaultResourcePack(ColorPack.class.getName(),
ColorPack.RESOURCE_NAME_DEFAULT_VALUE); ColorPack.RESOURCE_NAME_DEFAULT_VALUE);
if (colorPack != null)
colorResources = getResources(colorPack);
} }
else if(resourcePack instanceof ImagePack) else if(resourcePack instanceof ImagePack
&& imagePack.equals(resourcePack))
{ {
imagePacks = imagePack =
getDefaultPacks(ImagePack.class.getName(), getDefaultResourcePack(ImagePack.class.getName(),
ImagePack.RESOURCE_NAME_DEFAULT_VALUE); ImagePack.RESOURCE_NAME_DEFAULT_VALUE);
if (imagePack != null)
imageResources = getResources(imagePack);
} }
else if(resourcePack instanceof LanguagePack) else if(resourcePack instanceof LanguagePack
&& languagePack.equals(resourcePack))
{ {
languagePacks = languagePack =
getDefaultPacks(LanguagePack.class.getName(), (LanguagePack) getDefaultResourcePack(
LanguagePack.RESOURCE_NAME_DEFAULT_VALUE); LanguagePack.class.getName(),
LanguagePack.RESOURCE_NAME_DEFAULT_VALUE);
} }
else if(resourcePack instanceof SettingsPack) else if(resourcePack instanceof SettingsPack
&& settingsPack.equals(resourcePack))
{ {
settingsPacks = settingsPack =
getDefaultPacks(SettingsPack.class.getName(), getDefaultResourcePack(SettingsPack.class.getName(),
SettingsPack.RESOURCE_NAME_DEFAULT_VALUE); SettingsPack.RESOURCE_NAME_DEFAULT_VALUE);
if (settingsPack != null)
settingsResources = getResources(settingsPack);
} }
else if(resourcePack instanceof SoundPack) else if(resourcePack instanceof SoundPack
&& soundPack.equals(resourcePack))
{ {
soundPacks = soundPack =
getDefaultPacks(SoundPack.class.getName(), getDefaultResourcePack(SoundPack.class.getName(),
SoundPack.RESOURCE_NAME_DEFAULT_VALUE); SoundPack.RESOURCE_NAME_DEFAULT_VALUE);
if (soundPack != null)
soundResources = getResources(soundPack);
} }
} }
} }
/** /**
* Returns the int representation of the color corresponding to the given * Returns the int representation of the color corresponding to the
* key. * given key.
* *
* @return the int representation of the color corresponding to the given * @param key The key of the color in the colors properties file.
* key. * @return the int representation of the color corresponding to the
* given key.
*/ */
public int getColor(String key) public int getColor(String key)
{ {
String res = findString(key, colorPacks.elements()); String res = colorResources.get(key);
if(res == null) if(res == null)
{ {
@ -325,15 +266,16 @@ public int getColor(String key)
} }
/** /**
* Returns the String representation of the color corresponding to the given * Returns the string representation of the color corresponding to the
* key. * given key.
* *
* @return the String representation of the color corresponding to the given * @param key The key of the color in the colors properties file.
* key. * @return the string representation of the color corresponding to the
* given key.
*/ */
public String getColorString(String key) public String getColorString(String key)
{ {
String res = findString(key, colorPacks.elements()); String res = colorResources.get(key);
if(res == null) if(res == null)
{ {
@ -346,34 +288,30 @@ public String getColorString(String key)
} }
/** /**
* Loads a stream from a given identifier. * Returns the <tt>InputStream</tt> of the image corresponding to the given
* path.
* *
* @param path The path to the image from a location that's present in the * @param path The path to the image file.
* classpath. * @return the <tt>InputStream</tt> of the image corresponding to the given
* @return The stream for the given identifier. * path.
*/ */
public InputStream getImageInputStreamForPath(String path) public InputStream getImageInputStreamForPath(String path)
{ {
// Get the first image pack class loader and try to obtain the image return imagePack.getClass().getClassLoader().getResourceAsStream(path);
// input stream from the given path.
if (imagePacks.keys().hasMoreElements())
{
return imagePacks.keys().nextElement().getClass()
.getClassLoader().getResourceAsStream(path);
}
return null;
} }
/** /**
* Loads a stream from a given identifier. * Returns the <tt>InputStream</tt> of the image corresponding to the given
* key.
* *
* @param streamKey The identifier of the stream. * @param streamKey The identifier of the image in the resource properties
* @return The stream for the given identifier. * file.
* @return the <tt>InputStream</tt> of the image corresponding to the given
* key.
*/ */
public InputStream getImageInputStream(String streamKey) public InputStream getImageInputStream(String streamKey)
{ {
String path = findString(streamKey, imagePacks.elements()); String path = imageResources.get(streamKey);
if (path == null || path.length() == 0) if (path == null || path.length() == 0)
{ {
@ -383,16 +321,16 @@ public InputStream getImageInputStream(String streamKey)
return getImageInputStreamForPath(path); return getImageInputStreamForPath(path);
} }
/** /**
* Loads an url from a given identifier. * Returns the <tt>URL</tt> of the image corresponding to the given key.
* *
* @param urlKey The identifier of the url. * @param urlKey The identifier of the image in the resource properties file.
* @return The url for the given identifier. * @return the <tt>URL</tt> of the image corresponding to the given key
*/ */
public URL getImageURL(String urlKey) public URL getImageURL(String urlKey)
{ {
String path = findString(urlKey, imagePacks.elements()); String path = imageResources.get(urlKey);
if (path == null || path.length() == 0) if (path == null || path.length() == 0)
{ {
@ -403,37 +341,32 @@ public URL getImageURL(String urlKey)
} }
/** /**
* Returns the path corresponding to the given key. * Returns the image path corresponding to the given key.
* *
* @return the path corresponding to the given key. * @param key The identifier of the image in the resource properties file.
* @return the image path corresponding to the given key.
*/ */
public String getImagePath(String key) public String getImagePath(String key)
{ {
return findString(key, imagePacks.elements()); return imageResources.get(key);
} }
/** /**
* Returns the URL corresponding to the given path. * Returns the <tt>URL</tt> of the image corresponding to the given path.
* @return the URL corresponding to the given path. *
* @param path The path to the given image file.
* @return the <tt>URL</tt> of the image corresponding to the given path.
*/ */
public URL getImageURLForPath(String path) public URL getImageURLForPath(String path)
{ {
// Get the first image pack class loader and try to obtain the image return imagePack.getClass().getClassLoader().getResource(path);
// input stream from the given path.
if (imagePacks.keys().hasMoreElements())
{
return imagePacks.keys().nextElement().getClass()
.getClassLoader().getResource(path);
}
return null;
} }
// Language pack methods // Language pack methods
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public String getI18NString(String key) public String getI18NString(String key)
@ -444,7 +377,7 @@ public String getI18NString(String key)
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @param locale The locale. * @param locale The locale.
* @return An internationalized string corresponding to the given key and * @return An internationalized string corresponding to the given key and
* given locale. * given locale.
@ -457,7 +390,7 @@ public String getI18NString(String key, Locale locale)
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public String getI18NString(String key, String[] params) public String getI18NString(String key, String[] params)
@ -468,16 +401,17 @@ public String getI18NString(String key, String[] params)
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties
* file.
* @param locale The locale. * @param locale The locale.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public String getI18NString(String key, String[] params, Locale locale) public String getI18NString(String key, String[] params, Locale locale)
{ {
Enumeration resourceBundles Map<String, String> stringResources
= getLanguagePacksForLocale(locale).elements(); = languagePack.getResources(locale);
String resourceString = findString(key, resourceBundles); String resourceString = stringResources.get(key);
if (resourceString == null) if (resourceString == null)
{ {
@ -505,7 +439,7 @@ public String getI18NString(String key, String[] params, Locale locale)
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public char getI18nMnemonic(String key) public char getI18nMnemonic(String key)
@ -516,16 +450,16 @@ public char getI18nMnemonic(String key)
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @param locale The locale that we'd like to receive the result in. * @param locale The locale that we'd like to receive the result in.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public char getI18nMnemonic(String key, Locale locale) public char getI18nMnemonic(String key, Locale locale)
{ {
Enumeration resourceBundles Map<String,String> stringResources
= getLanguagePacksForLocale(locale).elements(); = languagePack.getResources(locale);
String resourceString = findString(key, resourceBundles); String resourceString = stringResources.get(key);
if (resourceString == null) if (resourceString == null)
{ {
@ -542,27 +476,27 @@ public char getI18nMnemonic(String key, Locale locale)
return 0; return 0;
} }
/** /**
* Returns the configuration String corresponding to the given * Returns the int value of the corresponding configuration key.
* key.
* *
* @return the configuration String corresponding to the given * @param key The identifier of the string in the resources properties file.
* key. * @return the int value of the corresponding configuration key.
*/ */
public String getSettingsString(String key) public String getSettingsString(String key)
{ {
return findString(key, settingsPacks.elements()); return settingsResources.get(key);
} }
/** /**
* Returns the int value corresponding to the given key. * Returns the int value of the corresponding configuration key.
* *
* @return the int value corresponding to the given key. * @param key The identifier of the string in the resources properties file.
* @return the int value of the corresponding configuration key.
*/ */
public int getSettingsInt(String key) public int getSettingsInt(String key)
{ {
String resourceString = findString(key, settingsPacks.elements()); String resourceString = settingsResources.get(key);
if (resourceString == null) if (resourceString == null)
{ {
@ -572,52 +506,34 @@ public int getSettingsInt(String key)
return Integer.parseInt(resourceString); return Integer.parseInt(resourceString);
} }
/** /**
* Loads an url from a given identifier. * Returns an <tt>URL</tt> from a given identifier.
* *
* @param urlKey The identifier of the url. * @param urlKey The identifier of the url.
* @return The url for the given identifier. * @return The url for the given identifier.
*/ */
public URL getSettingsURL(String urlKey) public URL getSettingsURL(String urlKey)
{ {
String path = findString(urlKey, settingsPacks.elements()); String path = settingsResources.get(urlKey);
if (path == null || path.length() == 0) if (path == null || path.length() == 0)
{ {
logger.warn("Missing resource for key: " + urlKey); logger.warn("Missing resource for key: " + urlKey);
return null; return null;
} }
return settingsPack.getClass().getClassLoader().getResource(path);
return getSettingsURLForPath(path);
}
/**
* Returns the URL corresponding to the given path.
* @return the URL corresponding to the given path.
*/
private URL getSettingsURLForPath(String path)
{
// Get the first settings pack class loader and try to obtain the image
// input stream from the given path.
if (settingsPacks.keys().hasMoreElements())
{
return settingsPacks.keys().nextElement().getClass()
.getClassLoader().getResource(path);
}
return null;
} }
/** /**
* Loads a stream from a given identifier. * Returns a stream from a given identifier.
* *
* @param streamKey The identifier of the stream. * @param streamKey The identifier of the stream.
* @return The stream for the given identifier. * @return The stream for the given identifier.
*/ */
public InputStream getSettingsInputStream(String streamKey) public InputStream getSettingsInputStream(String streamKey)
{ {
String path = findString(streamKey, settingsPacks.elements()); String path = settingsResources.get(streamKey);
if (path == null || path.length() == 0) if (path == null || path.length() == 0)
{ {
@ -625,35 +541,20 @@ public InputStream getSettingsInputStream(String streamKey)
return null; return null;
} }
return getSettingsInputStreamForPath(path); return settingsPack.getClass()
} .getClassLoader().getResourceAsStream(path);
/**
* Returns the InputStream corresponding to the given path.
*
* @return the InputStream corresponding to the given path.
*/
private InputStream getSettingsInputStreamForPath(String path)
{
// Get the first settings pack class loader and try to obtain the image
// input stream from the given path.
if (settingsPacks.keys().hasMoreElements())
{
return settingsPacks.keys().nextElement().getClass()
.getClassLoader().getResourceAsStream(path);
}
return null;
} }
/** /**
* Return the URL corresponding to the given key. * Returns the <tt>URL</tt> of the sound corresponding to the given
* property key.
* *
* @return the URL corresponding to the given key. * @return the <tt>URL</tt> of the sound corresponding to the given
* property key.
*/ */
public URL getSoundURL(String urlKey) public URL getSoundURL(String urlKey)
{ {
String path = findString(urlKey, soundPacks.elements()); String path = settingsResources.get(urlKey);
if (path == null || path.length() == 0) if (path == null || path.length() == 0)
{ {
@ -663,108 +564,16 @@ public URL getSoundURL(String urlKey)
return getSoundURLForPath(path); return getSoundURLForPath(path);
} }
/**
* Returns the <tt>URL</tt> of the sound corresponding to the given path.
*
* @return the <tt>URL</tt> of the sound corresponding to the given path.
*/
public URL getSoundURLForPath(String path) public URL getSoundURLForPath(String path)
{ {
// Get the first settings pack class loader and try to obtain the image return soundPack.getClass().getClassLoader().getResource(path);
// input stream from the given path.
if (soundPacks.keys().hasMoreElements())
{
return soundPacks.keys().nextElement().getClass()
.getClassLoader().getResource(path);
}
return null;
}
public Iterator getCurrentColors()
{
ResourceBundle colorResourceBundle
= (ResourceBundle) colorPacks.elements().nextElement();
Enumeration colorKeys = colorResourceBundle.getKeys();
List colorList = new ArrayList();
while (colorKeys.hasMoreElements())
{
colorList.add(colorKeys.nextElement());
}
return colorList.iterator();
}
public Iterator getCurrentImages()
{
ResourceBundle imageResourceBundle
= (ResourceBundle) imagePacks.elements().nextElement();
Enumeration imageKeys = imageResourceBundle.getKeys();
List imageList = new ArrayList();
while (imageKeys.hasMoreElements())
{
imageList.add(imageKeys.nextElement());
}
return imageList.iterator();
}
public Iterator getCurrentSettings()
{
ResourceBundle settingsResourceBundle
= (ResourceBundle) settingsPacks.elements().nextElement();
Enumeration settingKeys = settingsResourceBundle.getKeys();
List settingList = new ArrayList();
while (settingKeys.hasMoreElements())
{
settingList.add(settingKeys.nextElement());
}
return settingList.iterator();
}
public Iterator getCurrentSounds()
{
ResourceBundle soundResourceBundle
= (ResourceBundle) soundPacks.elements().nextElement();
Enumeration soundKeys = soundResourceBundle.getKeys();
List soundList = new ArrayList();
while (soundKeys.hasMoreElements())
{
soundList.add(soundKeys.nextElement());
}
return soundList.iterator();
} }
public Iterator getAvailableLocales()
{
LanguagePack languagePack
= (LanguagePack) languagePacks.keys().nextElement();
return languagePack.getAvailableLocales();
}
public Iterator getI18nStringsByLocale(Locale l)
{
ResourceBundle langResourceBundle
= (ResourceBundle) languagePacks.elements().nextElement();
Enumeration languageKeys = langResourceBundle.getKeys();
List languageList = new ArrayList();
while (languageKeys.hasMoreElements())
{
languageList.add(languageKeys.nextElement());
}
return languageList.iterator();
}
/** /**
* Loads an image from a given image identifier. * Loads an image from a given image identifier.
* *

@ -6,28 +6,93 @@
*/ */
package net.java.sip.communicator.plugin.defaultresourcepack; package net.java.sip.communicator.plugin.defaultresourcepack;
import java.util.*;
import net.java.sip.communicator.service.resources.*; import net.java.sip.communicator.service.resources.*;
/** /**
* * The default color resource pack.
*
* @author Damian Minkov * @author Damian Minkov
* @author Yana Stamcheva
*/ */
public class DefaultColorPackImpl public class DefaultColorPackImpl
implements ColorPack implements ColorPack
{ {
private static final String DEFAULT_RESOURCE_PATH
= "resources.colors.colors";
public String getResourcePackBaseName() /**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*
* @return a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*/
public Map<String, String> getResources()
{ {
return "resources.colors.colorResources"; ResourceBundle resourceBundle
= ResourceBundle.getBundle(DEFAULT_RESOURCE_PATH);
Map<String, String> resources = new Hashtable<String, String>();
this.initResources(resourceBundle, resources);
return resources;
} }
/**
* Returns the name of this resource pack.
*
* @return the name of this resource pack.
*/
public String getName() public String getName()
{ {
return "Default Color Resources"; return "Default Color Resources";
} }
/**
* Returns the description of this resource pack.
*
* @return the description of this resource pack.
*/
public String getDescription() public String getDescription()
{ {
return "Provide SIP Communicator default Color resource pack."; return "Provide SIP Communicator default color resource pack.";
}
/**
* Fills the given resource map with all (key,value) pairs obtained from the
* given <tt>ResourceBundle</tt>. This method will look in the properties
* files for references to other properties files and will include in the
* final map data from all referenced files.
*
* @param resourceBundle The initial <tt>ResourceBundle</tt>, corresponding
* to the "main" properties file.
* @param resources A <tt>Map</tt> that would store the data.
*/
private void initResources( ResourceBundle resourceBundle,
Map<String, String> resources)
{
Enumeration colorKeys = resourceBundle.getKeys();
while (colorKeys.hasMoreElements())
{
String key = (String) colorKeys.nextElement();
String value = resourceBundle.getString(key);
if (key.startsWith("$reference"))
{
ResourceBundle referenceBundle
= ResourceBundle.getBundle(value);
initResources( referenceBundle,
resources);
}
else
{
resources.put(key, value);
}
}
} }
} }

@ -6,28 +6,92 @@
*/ */
package net.java.sip.communicator.plugin.defaultresourcepack; package net.java.sip.communicator.plugin.defaultresourcepack;
import java.util.*;
import net.java.sip.communicator.service.resources.*; import net.java.sip.communicator.service.resources.*;
/** /**
* * The default image resource pack.
*
* @author Damian Minkov * @author Damian Minkov
* @author Yana Stamcheva
*/ */
public class DefaultImagePackImpl public class DefaultImagePackImpl
implements ImagePack implements ImagePack
{ {
private static final String DEFAULT_RESOURCE_PATH
= "resources.images.images";
public String getResourcePackBaseName() /**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*
* @return a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*/
public Map<String, String> getResources()
{ {
return "resources.images.images"; ResourceBundle resourceBundle
= ResourceBundle.getBundle(DEFAULT_RESOURCE_PATH);
Map<String, String> resources = new Hashtable<String, String>();
this.initResources(resourceBundle, resources);
return resources;
} }
/**
* Returns the name of this resource pack.
*
* @return the name of this resource pack.
*/
public String getName() public String getName()
{ {
return "Default Image Resources"; return "Default Image Resources";
} }
/**
* Returns the description of this resource pack.
*
* @return the description of this resource pack.
*/
public String getDescription() public String getDescription()
{ {
return "Provide SIP Communicator default Image resource pack."; return "Provide SIP Communicator default Image resource pack.";
} }
/**
* Fills the given resource map with all (key,value) pairs obtained from the
* given <tt>ResourceBundle</tt>. This method will look in the properties
* files for references to other properties files and will include in the
* final map data from all referenced files.
*
* @param resourceBundle The initial <tt>ResourceBundle</tt>, corresponding
* to the "main" properties file.
* @param resources A <tt>Map</tt> that would store the data.
*/
private void initResources( ResourceBundle resourceBundle,
Map<String, String> resources)
{
Enumeration imageKeys = resourceBundle.getKeys();
while (imageKeys.hasMoreElements())
{
String key = (String) imageKeys.nextElement();
String value = resourceBundle.getString(key);
if (key.startsWith("$reference"))
{
ResourceBundle referenceBundle
= ResourceBundle.getBundle(value);
initResources(referenceBundle, resources);
}
else
{
resources.put(key, value);
}
}
}
} }

@ -20,81 +20,102 @@
public class DefaultLanguagePackImpl public class DefaultLanguagePackImpl
implements LanguagePack implements LanguagePack
{ {
private Logger logger = Logger.getLogger(DefaultLanguagePackImpl.class); private static final String DEFAULT_RESOURCE_PATH
= "resources.languages.resources";
/**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*
* @return a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*/
public Map<String, String> getResources()
{
ResourceBundle resourceBundle
= ResourceBundle.getBundle(DEFAULT_RESOURCE_PATH);
private ArrayList localeList = new ArrayList(); Map<String, String> resources = new Hashtable<String, String>();
public DefaultLanguagePackImpl() this.initResources(resourceBundle, Locale.getDefault(), resources);
{
try
{
JarFile jf = new JarFile(getJarfileName());
Enumeration resources = jf.entries(); return resources;
while (resources.hasMoreElements())
{
JarEntry je = (JarEntry) resources.nextElement();
Locale locale;
String entryName = je.getName();
if (entryName.matches("resources/languages/.*\\.properties"))
{
int localeIndex = entryName.indexOf('_');
if (localeIndex == -1)
locale = new Locale("EN");
else
{
String localeName =
entryName.substring(localeIndex + 1,
entryName.indexOf('.'));
locale = new Locale(localeName);
}
localeList.add(locale);
}
}
}
catch (java.io.IOException e)
{
logger.error("Cannot load locales.", e);
}
} }
public String getResourcePackBaseName() /**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for the given
* locale.
*
* @param locale The <tt>Locale</tt> we're looking for.
* @return a <tt>Map</tt>, containing all [key, value] pairs for the given
* locale.
*/
public Map<String, String> getResources(Locale locale)
{ {
return "resources.languages.resources"; ResourceBundle resourceBundle
} = ResourceBundle.getBundle(DEFAULT_RESOURCE_PATH);
public Iterator getAvailableLocales() Map<String, String> resources = new Hashtable<String, String>();
{
return localeList.iterator(); this.initResources(resourceBundle, locale, resources);
return resources;
} }
/**
* Returns the name of this resource pack.
*
* @return the name of this resource pack.
*/
public String getName() public String getName()
{ {
return "Default Language Resources"; return "Default Language Resources";
} }
/**
* Returns the description of this resource pack.
*
* @return the description of this resource pack.
*/
public String getDescription() public String getDescription()
{ {
return "Provide SIP Communicator default Language resource pack."; return "Provide SIP Communicator default Language resource pack.";
} }
private String getJarfileName() /**
* Fills the given resource map with all (key,value) pairs obtained from the
* given <tt>ResourceBundle</tt>. This method will look in the properties
* files for references to other properties files and will include in the
* final map data from all referenced files.
*
* @param resourceBundle The initial <tt>ResourceBundle</tt>, corresponding
* to the "main" properties file.
* @param locale The locale we're looking for.
* @param resources A <tt>Map</tt> that would store the data.
*/
private void initResources( ResourceBundle resourceBundle,
Locale locale,
Map<String, String> resources)
{ {
// Get the location of the jar file and the jar file name Enumeration colorKeys = resourceBundle.getKeys();
java.net.URL outputURL =
DefaultLanguagePackImpl.class.getProtectionDomain().getCodeSource()
.getLocation();
String outputString = outputURL.toString(); while (colorKeys.hasMoreElements())
{
String key = (String) colorKeys.nextElement();
String value = resourceBundle.getString(key);
String[] parseString; if (key.startsWith("$reference"))
parseString = outputString.split("file:"); {
ResourceBundle referenceBundle
= ResourceBundle.getBundle( value,
locale);
String jarFilename = parseString[1]; initResources(referenceBundle, locale, resources);
return jarFilename; }
else
{
resources.put(key, value);
}
}
} }
} }

@ -6,28 +6,92 @@
*/ */
package net.java.sip.communicator.plugin.defaultresourcepack; package net.java.sip.communicator.plugin.defaultresourcepack;
import java.util.*;
import net.java.sip.communicator.service.resources.*; import net.java.sip.communicator.service.resources.*;
/** /**
* * The default settings resource pack.
*
* @author Damian Minkov * @author Damian Minkov
* @author Yana Stamcheva
*/ */
public class DefaultSettingsPackImpl public class DefaultSettingsPackImpl
implements SettingsPack implements SettingsPack
{ {
private static final String DEFAULT_RESOURCE_PATH
= "resources.config.defaults";
public String getResourcePackBaseName() /**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*
* @return a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*/
public Map<String, String> getResources()
{ {
return "resources.config.defaults"; ResourceBundle resourceBundle
= ResourceBundle.getBundle(DEFAULT_RESOURCE_PATH);
Map<String, String> resources = new Hashtable<String, String>();
this.initResources(resourceBundle, resources);
return resources;
} }
/**
* Returns the name of this resource pack.
*
* @return the name of this resource pack.
*/
public String getName() public String getName()
{ {
return "Default Settings Resources"; return "Default Settings Resources";
} }
/**
* Returns the description of this resource pack.
*
* @return the description of this resource pack.
*/
public String getDescription() public String getDescription()
{ {
return "Provide SIP Communicator default Settings resource pack."; return "Provide SIP Communicator default settings resource pack.";
}
/**
* Fills the given resource map with all (key,value) pairs obtained from the
* given <tt>ResourceBundle</tt>. This method will look in the properties
* files for references to other properties files and will include in the
* final map data from all referenced files.
*
* @param resourceBundle The initial <tt>ResourceBundle</tt>, corresponding
* to the "main" properties file.
* @param resources A <tt>Map</tt> that would store the data.
*/
private void initResources( ResourceBundle resourceBundle,
Map<String, String> resources)
{
Enumeration settingKeys = resourceBundle.getKeys();
while (settingKeys.hasMoreElements())
{
String key = (String) settingKeys.nextElement();
String value = resourceBundle.getString(key);
if (key.startsWith("$reference"))
{
ResourceBundle referenceBundle
= ResourceBundle.getBundle(value);
initResources(referenceBundle, resources);
}
else
{
resources.put(key, value);
}
}
} }
} }

@ -6,28 +6,93 @@
*/ */
package net.java.sip.communicator.plugin.defaultresourcepack; package net.java.sip.communicator.plugin.defaultresourcepack;
import java.util.*;
import net.java.sip.communicator.service.resources.*; import net.java.sip.communicator.service.resources.*;
/** /**
* * The default sound resource pack.
*
* @author Damian Minkov * @author Damian Minkov
* @author Yana Stamcheva
*/ */
public class DefaultSoundPackImpl public class DefaultSoundPackImpl
implements SoundPack implements SoundPack
{ {
public String getResourcePackBaseName() private static final String DEFAULT_RESOURCE_PATH
= "resources.sounds.sounds";
/**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*
* @return a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*/
public Map<String, String> getResources()
{ {
return "resources.sounds.sounds"; ResourceBundle resourceBundle
= ResourceBundle.getBundle(DEFAULT_RESOURCE_PATH);
Map<String, String> resources = new Hashtable<String, String>();
this.initResources(resourceBundle, resources);
return resources;
} }
/**
* Returns the name of this resource pack.
*
* @return the name of this resource pack.
*/
public String getName() public String getName()
{ {
return "Default Sounds Resources"; return "Default Sounds Resources";
} }
/**
* Returns the description of this resource pack.
*
* @return the description of this resource pack.
*/
public String getDescription() public String getDescription()
{ {
return "Provide SIP Communicator default Sounds resource pack."; return "Provide SIP Communicator default sounds resource pack.";
}
/**
* Fills the given resource map with all (key,value) pairs obtained from the
* given <tt>ResourceBundle</tt>. This method will look in the properties
* files for references to other properties files and will include in the
* final map data from all referenced files.
*
* @param resourceBundle The initial <tt>ResourceBundle</tt>, corresponding
* to the "main" properties file.
* @param resources A <tt>Map</tt> that would store the data.
*/
private void initResources( ResourceBundle resourceBundle,
Map<String, String> resources)
{
Enumeration soundKeys = resourceBundle.getKeys();
while (soundKeys.hasMoreElements())
{
String key = (String) soundKeys.nextElement();
String value = resourceBundle.getString(key);
if (key.startsWith("$reference"))
{
ResourceBundle referenceBundle
= ResourceBundle.getBundle(value);
initResources(referenceBundle, resources);
}
else
{
resources.put(key, value);
}
}
} }
} }

@ -16,6 +16,15 @@ public interface LanguagePack
extends ResourcePack extends ResourcePack
{ {
public String RESOURCE_NAME_DEFAULT_VALUE = "DefaultLanguagePack"; public String RESOURCE_NAME_DEFAULT_VALUE = "DefaultLanguagePack";
public Iterator getAvailableLocales(); /**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for the given
* locale.
*
* @param locale The <tt>Locale</tt> we're looking for.
* @return a <tt>Map</tt>, containing all [key, value] pairs for the given
* locale.
*/
public Map<String, String> getResources(Locale locale);
} }

@ -22,58 +22,67 @@ public interface ResourceManagementService
{ {
// Color pack methods // Color pack methods
/** /**
* Loads an Color value for the given key * Returns the int representation of the color corresponding to the
* given key.
* *
* @param key The identifier of the color. * @param key The key of the color in the colors properties file.
* @return The color value as int. * @return the int representation of the color corresponding to the
* given key.
*/ */
public int getColor(String key); public int getColor(String key);
/** /**
* Loads an Color value for the given key * Returns the string representation of the color corresponding to the
* given key.
* *
* @param key The identifier of the color. * @param key The key of the color in the colors properties file.
* @return The color value as String. * @return the string representation of the color corresponding to the
* given key.
*/ */
public String getColorString(String key); public String getColorString(String key);
/** /**
* Loads a stream from a given path. * Returns the <tt>InputStream</tt> of the image corresponding to the given
* path.
* *
* @param path The path of the stream. * @param path The path to the image file.
* @return The stream for the given identifier. * @return the <tt>InputStream</tt> of the image corresponding to the given
* path.
*/ */
public InputStream getImageInputStreamForPath(String path); public InputStream getImageInputStreamForPath(String path);
/** /**
* Loads a stream from a given identifier. * Returns the <tt>InputStream</tt> of the image corresponding to the given
* key.
* *
* @param streamKey The identifier of the stream. * @param streamKey The identifier of the image in the resource properties
* @return The stream for the given identifier. * file.
* @return the <tt>InputStream</tt> of the image corresponding to the given
* key.
*/ */
public InputStream getImageInputStream(String streamKey); public InputStream getImageInputStream(String streamKey);
/** /**
* Loads an url from a given identifier. * Returns the <tt>URL</tt> of the image corresponding to the given key.
* *
* @param urlKey The identifier of the url. * @param urlKey The identifier of the image in the resource properties file.
* @return The url for the given identifier. * @return the <tt>URL</tt> of the image corresponding to the given key
*/ */
public URL getImageURL(String urlKey); public URL getImageURL(String urlKey);
/** /**
* Loads an url from a given path. * Returns the <tt>URL</tt> of the image corresponding to the given path.
* *
* @param path The path for the url. * @param path The path to the given image file.
* @return The url for the given identifier. * @return the <tt>URL</tt> of the image corresponding to the given path.
*/ */
public URL getImageURLForPath(String path); public URL getImageURLForPath(String path);
/** /**
* Returns the value of the given key for image resources. * Returns the image path corresponding to the given key.
* *
* @param key The key. * @param key The identifier of the image in the resource properties file.
* @return String value for the given image key. * @return the image path corresponding to the given key.
*/ */
public String getImagePath(String key); public String getImagePath(String key);
@ -81,7 +90,7 @@ public interface ResourceManagementService
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public String getI18NString(String key); public String getI18NString(String key);
@ -89,36 +98,39 @@ public interface ResourceManagementService
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @param l The locale. * @param locale The locale.
* @return An internationalized string corresponding to the given key and * @return An internationalized string corresponding to the given key and
* given locale. * given locale.
*/ */
public String getI18NString(String key, Locale l); public String getI18NString(String key, Locale locale);
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @param params params to be replaced in the returned string * @param locale The locale.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key and
* given locale.
*/ */
public String getI18NString(String key, String[] params); public String getI18NString(String key, String[] params);
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string * @param key The identifier of the string in the resources properties file.
* @param params params to be replaced in the returned string. * @param params An array of parameters to be replaced in the returned
* @param l The locale. * string.
* @param locale The locale.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public String getI18NString(String key, String[] params, Locale l); public String getI18NString(String key, String[] params, Locale locale);
/** /**
* Returns an internationalized string corresponding to the given key. * Returns an internationalized string corresponding to the given key.
* *
* @param key The key of the string. * @param key The identifier of the string in the resources properties file.
* @param locale The locale that we'd like to receive the result in.
* @return An internationalized string corresponding to the given key. * @return An internationalized string corresponding to the given key.
*/ */
public char getI18nMnemonic(String key); public char getI18nMnemonic(String key);
@ -151,18 +163,18 @@ public interface ResourceManagementService
public InputStream getSettingsInputStream(String streamKey); public InputStream getSettingsInputStream(String streamKey);
/** /**
* Returns a String for the setting corresponding to the given key. * Returns the int value of the corresponding configuration key.
* *
* @param key The key of the setting. * @param key The identifier of the string in the resources properties file.
* @return String to the corresponding resource. * @return the int value of the corresponding configuration key.
*/ */
public String getSettingsString(String key); public String getSettingsString(String key);
/** /**
* Returns an int for the setting corresponding to the given key. * Returns the int value of the corresponding configuration key.
* *
* @param key The key of the setting. * @param key The identifier of the string in the resources properties file.
* @return int to the corresponding resource. * @return the int value of the corresponding configuration key.
*/ */
public int getSettingsInt(String key); public int getSettingsInt(String key);
@ -182,50 +194,7 @@ public interface ResourceManagementService
* @return Url to the corresponding resource. * @return Url to the corresponding resource.
*/ */
public URL getSoundURLForPath(String path); public URL getSoundURLForPath(String path);
/**
* Returns all color keys that can be obtained from this service.
*
* @return Iterator to all color keys.
*/
public Iterator getCurrentColors();
/**
* Returns all image keys that can be obtained from this service.
*
* @return Iterator to all image keys.
*/
public Iterator getCurrentImages();
/**
* Returns all color settings that can be obtained from this service.
*
* @return Iterator to all settings keys.
*/
public Iterator getCurrentSettings();
/**
* Returns all color sounds that can be obtained from this service.
*
* @return Iterator to all sounds keys.
*/
public Iterator getCurrentSounds();
/**
* Returns all available locales for the translated texts.
*
* @return Iterator to all locales.
*/
public Iterator getAvailableLocales();
/**
* Returns all string keys that can be obtained from
* this service for the given locale.
*
* @return Iterator to all string keys.
*/
public Iterator getI18nStringsByLocale(Locale l);
/** /**
* Constructs an <tt>ImageIcon</tt> from the specified image ID and returns * Constructs an <tt>ImageIcon</tt> from the specified image ID and returns
* it. * it.
@ -235,7 +204,7 @@ public interface ResourceManagementService
* identifier. * identifier.
*/ */
public ImageIcon getImage(String imageID); public ImageIcon getImage(String imageID);
/** /**
* Loads the image with the specified ID and returns a byte array * Loads the image with the specified ID and returns a byte array
* containing it. * containing it.

@ -6,15 +6,37 @@
*/ */
package net.java.sip.communicator.service.resources; package net.java.sip.communicator.service.resources;
import java.util.*;
/** /**
* * The <tt>ResourcePack</tt> service.
*
* @author Damian Minkov * @author Damian Minkov
*/ */
public interface ResourcePack public interface ResourcePack
{ {
public String RESOURCE_NAME = "ResourceName"; public String RESOURCE_NAME = "ResourceName";
public String getResourcePackBaseName(); /**
* Returns a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*
* @return a <tt>Map</tt>, containing all [key, value] pairs for this
* resource pack.
*/
public Map<String, String> getResources();
/**
* Returns the name of this resource pack.
*
* @return the name of this resource pack.
*/
public String getName(); public String getName();
/**
* Returns the description of this resource pack.
*
* @return the description of this resource pack.
*/
public String getDescription(); public String getDescription();
} }

Loading…
Cancel
Save