aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/skinresourcepack/SkinResourcesPack.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/java/sip/communicator/plugin/skinresourcepack/SkinResourcesPack.java')
-rw-r--r--src/net/java/sip/communicator/plugin/skinresourcepack/SkinResourcesPack.java383
1 files changed, 383 insertions, 0 deletions
diff --git a/src/net/java/sip/communicator/plugin/skinresourcepack/SkinResourcesPack.java b/src/net/java/sip/communicator/plugin/skinresourcepack/SkinResourcesPack.java
new file mode 100644
index 0000000..bd47802
--- /dev/null
+++ b/src/net/java/sip/communicator/plugin/skinresourcepack/SkinResourcesPack.java
@@ -0,0 +1,383 @@
+/*
+ * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package net.java.sip.communicator.plugin.skinresourcepack;
+
+import java.net.*;
+import java.util.*;
+
+import org.osgi.framework.*;
+
+import net.java.sip.communicator.service.resources.*;
+
+/**
+ * The skin resource pack.
+ * @author Adam Netocny, CircleTech, s.r.o.
+ */
+public class SkinResourcesPack
+ implements BundleActivator, SkinPack
+{
+ /**
+ * The default resource path.
+ */
+ private static final String DEFAULT_RESOURCE_PATH = "info";
+
+ /**
+ * The resource path of skin images.
+ */
+ private static final String DEFAULT_IMAGE_RESOURCE_PATH = "images.images";
+
+ /**
+ * The resource path of skin colors.
+ */
+ private static final String DEFAULT_COLOR_RESOURCE_PATH = "colors.colors";
+
+ /**
+ * The resource path f skin styles.
+ */
+ private static final String DEFAULT_STYLE_RESOURCE_PATH = "styles.styles";
+
+ /**
+ * The bundle context.
+ */
+ private static BundleContext bundleContext;
+
+ /**
+ * Buffer for resource files found.
+ */
+ private static Hashtable<String, Iterator<String>> ressourcesFiles
+ = new Hashtable<String, Iterator<String>>();
+
+ /**
+ * A map of all skin image resources.
+ */
+ private Map<String, String> imageResources = null;
+
+ /**
+ * A map of all skin style resources.
+ */
+ private Map<String, String> styleResources = null;
+
+ /**
+ * A map of all skin color resources.
+ */
+ private Map<String, String> colorResources = null;
+
+ /**
+ * Starts the bundle.
+ * @param bc BundleContext
+ * @throws Exception -
+ */
+ public void start(BundleContext bc)
+ throws Exception
+ {
+ bundleContext = bc;
+
+ Hashtable<String, String> props = new Hashtable<String, String>();
+ props.put(ResourcePack.RESOURCE_NAME,
+ SkinPack.RESOURCE_NAME_DEFAULT_VALUE);
+
+ bundleContext.registerService(SkinPack.class.getName(),
+ this,
+ props);
+ }
+
+ /**
+ * Stops the bundle.
+ * @param bc BundleContext
+ * @throws Exception -
+ */
+ public void stop(BundleContext bc) throws Exception {}
+
+ /**
+ * Returns a <tt>Map</tt>, containing all [key, value] pairs for image
+ * resource pack.
+ *
+ * @return a <tt>Map</tt>, containing all [key, value] pairs for image
+ * resource pack.
+ */
+ public Map<String, String> getImageResources()
+ {
+ if(imageResources != null)
+ {
+ return imageResources;
+ }
+
+ ResourceBundle resourceBundle
+ = ResourceBundle.getBundle(DEFAULT_IMAGE_RESOURCE_PATH);
+
+ Map<String, String> resources = new TreeMap<String, String>();
+
+ this.initResources(resourceBundle, resources);
+
+ this.initImagePluginResources(resources);
+
+ imageResources = resources;
+
+ return resources;
+ }
+
+ /**
+ * Returns a <tt>Map</tt>, containing all [key, value] pairs for style
+ * resource pack.
+ *
+ * @return a <tt>Map</tt>, containing all [key, value] pairs for style
+ * resource pack.
+ */
+ public Map<String, String> getStyleResources()
+ {
+ if(styleResources != null)
+ {
+ return styleResources;
+ }
+
+ ResourceBundle resourceBundle
+ = ResourceBundle.getBundle(DEFAULT_STYLE_RESOURCE_PATH);
+
+ Map<String, String> resources = new TreeMap<String, String>();
+
+ this.initResources(resourceBundle, resources);
+
+ this.initStylePluginResources(resources);
+
+ styleResources = resources;
+
+ return resources;
+ }
+
+ /**
+ * Returns a <tt>Map</tt>, containing all [key, value] pairs for color
+ * resource pack.
+ *
+ * @return a <tt>Map</tt>, containing all [key, value] pairs for color
+ * resource pack.
+ */
+ public Map<String, String> getColorResources()
+ {
+ if(colorResources != null)
+ {
+ return colorResources;
+ }
+
+ ResourceBundle resourceBundle
+ = ResourceBundle.getBundle(DEFAULT_COLOR_RESOURCE_PATH);
+
+ Map<String, String> resources = new TreeMap<String, String>();
+
+ this.initResources(resourceBundle, resources);
+
+ this.initColorPluginResources(resources);
+
+ colorResources = resources;
+
+ return 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);
+
+ Map<String, String> resources = new TreeMap<String, String>();
+
+ this.initResources(resourceBundle, resources);
+
+ resources.putAll(getImageResources());
+
+ resources.putAll(getStyleResources());
+
+ resources.putAll(getColorResources());
+
+ return resources;
+ }
+
+ /**
+ * Returns the name of this resource pack.
+ *
+ * @return the name of this resource pack.
+ */
+ public String getName()
+ {
+ Map<String, String> resources = getResources();
+ String name = resources.get("display_name");
+ if(name != null)
+ {
+ return name + " Skin Resources";
+ }
+ else
+ {
+ return "Skin Resources";
+ }
+ }
+
+ /**
+ * Returns the description of this resource pack.
+ *
+ * @return the description of this resource pack.
+ */
+ public String getDescription()
+ {
+ Map<String, String> resources = getResources();
+ String name = resources.get("display_name");
+ if(name != null)
+ {
+ return "Provide SIP Communicator " + name + " skin resource pack.";
+ }
+ else
+ {
+ return "Provide SIP Communicator skin 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<String> colorKeys = resourceBundle.getKeys();
+
+ while (colorKeys.hasMoreElements())
+ {
+ String key = colorKeys.nextElement();
+ String value = resourceBundle.getString(key);
+
+ resources.put(key, value);
+ }
+ }
+
+ /**
+ * Finds all plugin image resources, matching the "images-*.properties"
+ * pattern and adds them to this resource pack.
+ * @param resources the map of key, value image resource pairs
+ */
+ private void initImagePluginResources(Map<String, String> resources)
+ {
+ Iterator<String> pluginProperties
+ = findResourcePaths("images", "images-*.properties");
+
+ while (pluginProperties.hasNext())
+ {
+ String resourceBundleName = pluginProperties.next();
+
+ ResourceBundle resourceBundle
+ = ResourceBundle.getBundle(
+ resourceBundleName.substring(
+ 0, resourceBundleName.indexOf(".properties")));
+
+ initResources(resourceBundle, resources);
+ }
+ }
+
+ /**
+ * Finds all plugin style resources, matching the "styles-*.properties"
+ * pattern and adds them to this resource pack.
+ * @param resources the map of key, value stype resource pairs
+ */
+ private void initStylePluginResources(Map<String, String> resources)
+ {
+ Iterator<String> pluginProperties
+ = findResourcePaths("styles", "styles-*.properties");
+
+ while (pluginProperties.hasNext())
+ {
+ String resourceBundleName = pluginProperties.next();
+
+ ResourceBundle resourceBundle
+ = ResourceBundle.getBundle(
+ resourceBundleName.substring(
+ 0, resourceBundleName.indexOf(".properties")));
+
+ initResources(resourceBundle, resources);
+ }
+ }
+
+ /**
+ * Finds all plugin color resources, matching the "colors-*.properties"
+ * pattern and adds them to this resource pack.
+ * @param resources the map of key, value color resource pairs
+ */
+ private void initColorPluginResources(Map<String, String> resources)
+ {
+ Iterator<String> pluginProperties
+ = findResourcePaths("colors", "colors-*.properties");
+
+ while (pluginProperties.hasNext())
+ {
+ String resourceBundleName = pluginProperties.next();
+
+ ResourceBundle resourceBundle
+ = ResourceBundle.getBundle(
+ resourceBundleName.substring(
+ 0, resourceBundleName.indexOf(".properties")));
+
+ initResources(resourceBundle, resources);
+ }
+ }
+
+ /**
+ * Finds all properties files for the given path in this bundle.
+ *
+ * @param path the path pointing to the properties files.
+ * @param pattern the pattern for properties files
+ * (ex. "colors-*.properties")
+ * @return an <tt>Iterator</tt> over a list of all properties files found
+ * for the given path and pattern
+ */
+ protected static Iterator<String> findResourcePaths(String path,
+ String pattern)
+ {
+ Iterator<String> bufferedResult
+ = ressourcesFiles.get(path + "/" + pattern);
+ if (bufferedResult != null) {
+ return bufferedResult;
+ }
+
+ ArrayList<String> propertiesList = new ArrayList<String>();
+
+ @SuppressWarnings ("unchecked")
+ Enumeration<URL> propertiesUrls = bundleContext.getBundle()
+ .findEntries(path,
+ pattern,
+ false);
+
+ if (propertiesUrls != null)
+ {
+ while (propertiesUrls.hasMoreElements())
+ {
+ URL propertyUrl = propertiesUrls.nextElement();
+
+ // Remove the first slash.
+ String propertyFilePath
+ = propertyUrl.getPath().substring(1);
+
+ // Replace all slashes with dots.
+ propertyFilePath = propertyFilePath.replaceAll("/", ".");
+
+ propertiesList.add(propertyFilePath);
+ }
+ }
+
+ Iterator<String> result = propertiesList.iterator();
+ ressourcesFiles.put(path + pattern, result);
+
+ return result;
+ }
+}