aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java
diff options
context:
space:
mode:
authorLyubomir Marinov <lyubomir.marinov@jitsi.org>2009-08-03 10:38:04 +0000
committerLyubomir Marinov <lyubomir.marinov@jitsi.org>2009-08-03 10:38:04 +0000
commitc2dbb6bd0bd0e60ba1bfe224a2a84b5c297d2f13 (patch)
treec6f8f28a84d2c95c9285369cd3cad1381e5a1f67 /src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java
parent8c079eed95f907bb68ff0b294abf86d1c867c84b (diff)
downloadjitsi-c2dbb6bd0bd0e60ba1bfe224a2a84b5c297d2f13.zip
jitsi-c2dbb6bd0bd0e60ba1bfe224a2a84b5c297d2f13.tar.gz
jitsi-c2dbb6bd0bd0e60ba1bfe224a2a84b5c297d2f13.tar.bz2
Reduces the impact of ImageUtils.getScaledRoundedIcon() on the garbage collections in the contact list and chat UI. The optimization of the garbage collections and thus the execution speed come at the expense of increased memory use because of the introduced caching (but I tried to keep that to the minimum).
Diffstat (limited to 'src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java')
-rw-r--r--src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java81
1 files changed, 63 insertions, 18 deletions
diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java
index b8c32ce..1297e51 100644
--- a/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java
+++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/ContactListCellRenderer.java
@@ -23,6 +23,7 @@ import net.java.sip.communicator.util.swing.*;
* The cell border and background are repainted.
*
* @author Yana Stamcheva
+ * @author Lubomir Marinov
*/
public class ContactListCellRenderer
extends JPanel
@@ -32,6 +33,13 @@ public class ContactListCellRenderer
private static final int AVATAR_WIDTH = 30;
+ /**
+ * The key of the user data in <code>MetaContact</code> which specifies
+ * the avatar cached from previous invocations.
+ */
+ private static final String AVATAR_DATA_KEY
+ = ContactListCellRenderer.class.getName() + ".avatar";
+
private final ImageIcon openedGroupIcon =
new ImageIcon(ImageLoader.getImage(ImageLoader.DOWN_ARROW_ICON));
@@ -46,9 +54,9 @@ public class ContactListCellRenderer
protected final JLabel photoLabel = new JLabel();
- private final int rowTransparency =
- GuiActivator.getResources()
- .getSettingsInt("impl.gui.CONTACT_LIST_TRANSPARENCY");
+// private final int rowTransparency
+// = GuiActivator.getResources()
+// .getSettingsInt("impl.gui.CONTACT_LIST_TRANSPARENCY");
private final Image msgReceivedImage =
ImageLoader.getImage(ImageLoader.MESSAGE_RECEIVED_ICON);
@@ -143,17 +151,9 @@ public class ContactListCellRenderer
this.setBorder(BorderFactory.createEmptyBorder(1, 5, 1, 3));
- byte[] avatar = metaContact.getAvatar(true);
- if (avatar != null && avatar.length > 0)
- {
- ImageIcon roundedAvatar
- = ImageUtils.getScaledRoundedIcon( avatar,
- AVATAR_WIDTH,
- AVATAR_HEIGHT);
-
- if (roundedAvatar != null)
- this.photoLabel.setIcon(roundedAvatar);
- }
+ ImageIcon avatar = getAvatar(metaContact);
+ if (avatar != null)
+ this.photoLabel.setIcon(avatar);
// We should set the bounds of the cell explicitly in order to
// make getComponentAt work properly.
@@ -185,10 +185,10 @@ public class ContactListCellRenderer
// make getComponentAt work properly.
this.setBounds(0, 0, list.getWidth() - 2, 20);
- if(contactList.isGroupClosed(groupItem))
- this.nameLabel.setIcon(closedGroupIcon);
- else
- this.nameLabel.setIcon(openedGroupIcon);
+ this.nameLabel.setIcon(
+ contactList.isGroupClosed(groupItem)
+ ? closedGroupIcon
+ : openedGroupIcon);
// We have no photo icon for groups.
this.photoLabel.setIcon(null);
@@ -202,6 +202,51 @@ public class ContactListCellRenderer
}
/**
+ * Gets the avatar of a specific <code>MetaContact</code> in the form of an
+ * <code>ImageIcon</code> value.
+ *
+ * @param metaContact
+ * the <code>MetaContact</code> to retrieve the avatar of
+ * @return an <code>ImageIcon</code> which represents the avatar of the
+ * specified <code>MetaContact</code>
+ */
+ private ImageIcon getAvatar(MetaContact metaContact)
+ {
+ byte[] avatarBytes = metaContact.getAvatar(true);
+ ImageIcon avatar = null;
+
+ // Try to get the avatar from the cache.
+ Object[] avatarCache = (Object[]) metaContact.getData(AVATAR_DATA_KEY);
+ if ((avatarCache != null) && (avatarCache[0] == avatarBytes))
+ avatar = (ImageIcon) avatarCache[1];
+
+ // If the avatar isn't available or it's not up-to-date, create it.
+ if ((avatar == null)
+ && (avatarBytes != null) && (avatarBytes.length > 0))
+ avatar
+ = ImageUtils.getScaledRoundedIcon(
+ avatarBytes,
+ AVATAR_WIDTH,
+ AVATAR_HEIGHT);
+
+ // Cache the avatar in case it has changed.
+ if (avatarCache == null)
+ {
+ if (avatar != null)
+ metaContact.setData(
+ AVATAR_DATA_KEY,
+ new Object[] { avatarBytes, avatar });
+ }
+ else
+ {
+ avatarCache[0] = avatarBytes;
+ avatarCache[1] = avatar;
+ }
+
+ return avatar;
+ }
+
+ /**
* Paint a background for all groups and a round blue border and background
* when a cell is selected.
*/