Creates image loader service to be used in notification wiring plugin.

cusax-fix
Damian Minkov 13 years ago
parent fd920ade84
commit 8385a03fdf

@ -11,6 +11,7 @@
import net.java.sip.communicator.impl.gui.main.account.*;
import net.java.sip.communicator.impl.gui.main.contactlist.*;
import net.java.sip.communicator.impl.gui.main.presence.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.browserlauncher.*;
import net.java.sip.communicator.service.callhistory.*;
import net.java.sip.communicator.service.contactlist.*;
@ -148,6 +149,11 @@ public void start(BundleContext bContext)
alertUIService,
null);
// Registers an implementation of the ImageLoaderService.
bundleContext.registerService( ImageLoaderService.class.getName(),
new ImageLoaderServiceImpl(),
null);
// Create the ui service
uiService = new UIServiceImpl();

@ -10,13 +10,12 @@
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;
@ -37,10 +36,9 @@ public class ImageLoader
private static final Logger logger = Logger.getLogger(ImageLoader.class);
/**
* Stores all already loaded images.
* The image loader service implementation.
*/
private static final Map<ImageID, BufferedImage> loadedImages =
new Hashtable<ImageID, BufferedImage>();
private static ImageLoaderServiceImpl imageLoaderService = null;
/**
* The SIP Communicator logo 16x16 icon.
@ -1353,6 +1351,23 @@ public class ImageLoader
public static final ImageID AUTO_ANSWER_CHECK
= new ImageID("service.gui.icons.AUTO_ANSWER_CHECK");
/**
* Returns the imageLoaderService instance, if missing query osgi for it.
* @return the imageLoaderService.
*/
private static ImageLoaderServiceImpl getImageLoaderService()
{
if(imageLoaderService == null)
{
imageLoaderService = (ImageLoaderServiceImpl)
ServiceUtils.getService(
GuiActivator.bundleContext,
ImageLoaderService.class);
}
return imageLoaderService;
}
/**
* Loads an image from a given image identifier.
*
@ -1361,32 +1376,7 @@ public class ImageLoader
*/
public static BufferedImage getImage(ImageID imageID)
{
BufferedImage image = null;
if (loadedImages.containsKey(imageID))
{
image = loadedImages.get(imageID);
}
else
{
URL path = GuiActivator.getResources().getImageURL(imageID.getId());
if (path != null)
{
try
{
image = ImageIO.read(path);
loadedImages.put(imageID, image);
}
catch (Exception ex)
{
logger.error("Failed to load image: " + path, ex);
}
}
}
return image;
return getImageLoaderService().getImage(imageID);
}
/**
@ -1623,7 +1613,7 @@ public static ImageIcon getImageForPath(String imagePath)
*/
public static void clearCache()
{
loadedImages.clear();
getImageLoaderService().clearCache();
}
/**

@ -0,0 +1,101 @@
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.gui.utils;
import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;
import javax.imageio.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
/**
* Service responsible for loading images and possibly cache them.
*
* @author Damian Minkov
*/
public class ImageLoaderServiceImpl
implements ImageLoaderService<BufferedImage>
{
/**
* The <tt>Logger</tt> used by the <tt>ImageLoaderServiceImpl</tt> class
* and its instances for logging output.
*/
private static final Logger logger =
Logger.getLogger(ImageLoaderServiceImpl.class);
/**
* Stores all already loaded images.
*/
private static final Map<ImageID, BufferedImage> loadedImages =
new Hashtable<ImageID, BufferedImage>();
/**
* Loads an image from a given image identifier.
*
* @param imageID The identifier of the image.
* @return The image for the given identifier.
*/
public BufferedImage getImage(ImageID imageID)
{
BufferedImage image = null;
if (loadedImages.containsKey(imageID))
{
image = loadedImages.get(imageID);
}
else
{
URL path = GuiActivator.getResources().getImageURL(imageID.getId());
if (path != null)
{
try
{
image = ImageIO.read(path);
loadedImages.put(imageID, image);
}
catch (Exception ex)
{
logger.error("Failed to load image: " + path, ex);
}
}
}
return image;
}
/**
* Loads an image from a given image identifier and return
* bytes of the image.
*
* @param imageID The identifier of the image.
* @return The image bytes for the given identifier.
*/
public byte[] getImageBytes(ImageID imageID)
{
BufferedImage image = getImage(imageID);
if(image != null)
return ImageUtils.toByteArray(image);
else
return null;
}
/**
* Clears the images cache.
*/
public void clearCache()
{
loadedImages.clear();
}
}

@ -6,15 +6,10 @@
*/
package net.java.sip.communicator.plugin.notificationwiring;
import java.awt.image.*;
import java.lang.ref.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import javax.imageio.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.notification.*;
@ -87,14 +82,6 @@ public class NotificationManager
*/
public static final String HANG_UP = "HangUp";
/**
* The cache of <tt>BufferedImage</tt> instances which we have already
* loaded by <tt>ImageID</tt> and which we store so that we do not have to
* load them again.
*/
private static final Map<ImageID, BufferedImage> images
= new Hashtable<ImageID, BufferedImage>();
/**
* Default event type for receiving calls (incoming calls).
*/
@ -171,7 +158,8 @@ public static void fireChatNotification(Object chatContact,
if(contactIcon == null)
{
contactIcon =
ImageUtils.toByteArray(getImage(DEFAULT_USER_PHOTO));
NotificationWiringActivator.getImageLoaderService()
.getImageBytes(DEFAULT_USER_PHOTO);
}
}
else if (chatContact instanceof ChatRoom)
@ -346,43 +334,6 @@ private static NotificationData fireNotification(
}
}
/**
* Loads an image from a given image identifier.
*
* @param imageID The identifier of the image.
* @return The image for the given identifier.
*/
public static BufferedImage getImage(ImageID imageID)
{
/*
* If we were mapping ImageID to null, we would be using the method
* Map.containsKey. However, that does not seem to be the case.
*/
BufferedImage image = images.get(imageID);
if (image == null)
{
URL path
= NotificationWiringActivator.getResources().getImageURL(
imageID.getId());
if (path != null)
{
try
{
image = ImageIO.read(path);
images.put(imageID, image);
}
catch (Exception ex)
{
logger.error("Failed to load image: " + path, ex);
}
}
}
return image;
}
/**
* Returns all <tt>ProtocolProviderFactory</tt>s obtained from the bundle
* context.

@ -33,6 +33,11 @@ public class NotificationWiringActivator
private static UIService uiService = null;
private static MediaService mediaService;
/**
* The image loader service.
*/
private static ImageLoaderService imageLoaderService;
public void start(BundleContext bc) throws Exception
{
bundleContext = bc;
@ -122,4 +127,21 @@ public static MediaService getMediaService()
}
return mediaService;
}
/**
* Returns an instance of the <tt>ImageLoaderService</tt> obtained from the
* bundle context.
* @return an instance of the <tt>ImageLoaderService</tt> obtained from the
* bundle context
*/
public static ImageLoaderService getImageLoaderService()
{
if (imageLoaderService == null)
{
imageLoaderService
= ServiceUtils.getService(
bundleContext, ImageLoaderService.class);
}
return imageLoaderService;
}
}

@ -0,0 +1,43 @@
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.service.gui;
import net.java.sip.communicator.service.resources.*;
/**
* Service responsible for loading images and possibly cache them.
*
* @param <T> the type of the image used in the implementers.
* In desktop/swing implementation BufferedImage(java.awt.Image)
* is used.
*
* @author Damian Minkov
*/
public interface ImageLoaderService<T>
{
/**
* Loads an image from a given image identifier.
*
* @param imageID The identifier of the image.
* @return The image for the given identifier.
*/
public T getImage(ImageID imageID);
/**
* Loads an image from a given image identifier and return
* bytes of the image.
*
* @param imageID The identifier of the image.
* @return The image bytes for the given identifier.
*/
public byte[] getImageBytes(ImageID imageID);
/**
* Clears the images cache.
*/
public void clearCache();
}
Loading…
Cancel
Save