From 2da8de986255edff3397e00098cd2063170875e2 Mon Sep 17 00:00:00 2001 From: Emil Ivov Date: Mon, 21 May 2007 17:49:56 +0000 Subject: When handling ServiceEvent's, make sure they are triggerred by a ProtocolProviderService before handling them. --- .../impl/systray/jdic/SystrayServiceJdicImpl.java | 177 +++++++++++---------- 1 file changed, 92 insertions(+), 85 deletions(-) (limited to 'src/net/java/sip/communicator/impl/systray/jdic') diff --git a/src/net/java/sip/communicator/impl/systray/jdic/SystrayServiceJdicImpl.java b/src/net/java/sip/communicator/impl/systray/jdic/SystrayServiceJdicImpl.java index 03a5824..d5aee8c 100644 --- a/src/net/java/sip/communicator/impl/systray/jdic/SystrayServiceJdicImpl.java +++ b/src/net/java/sip/communicator/impl/systray/jdic/SystrayServiceJdicImpl.java @@ -32,9 +32,9 @@ import org.jdesktop.jdic.tray.TrayIcon; import org.osgi.framework.*; /** - * The Systray provides a Icon and the associated TrayMenu + * The Systray provides a Icon and the associated TrayMenu * in the system tray using the Jdic library. - * + * * @author Nicolas Chamouard * @author Yana Stamcheva */ @@ -43,45 +43,45 @@ public class SystrayServiceJdicImpl ServiceListener, MessageListener, ChatFocusListener -{ +{ /** * A reference of the UIservice. */ private UIService uiService; - + /** - * The systray. + * The systray. */ private SystemTray systray; - + /** * The icon in the system tray. */ private TrayIcon trayIcon; - + /** * The menu that spring with a right click. */ private TrayMenu menu; - + /** * The list of all providers. */ private Map protocolProviderTable = new LinkedHashMap(); - + /** * The list of all added popup message listeners. */ private Vector popupMessageListeners = new Vector(); - + /** * The logger for this class. */ private static Logger logger = Logger.getLogger(SystrayServiceJdicImpl.class.getName()); - + private ImageIcon logoIcon; - + /** * Creates an instance of Systray. * @param service a reference of the current UIservice @@ -89,7 +89,7 @@ public class SystrayServiceJdicImpl public SystrayServiceJdicImpl(UIService service) { this.uiService = service; - + try { systray = SystemTray.getDefaultSystemTray(); @@ -98,25 +98,25 @@ public class SystrayServiceJdicImpl { logger.error("Failed to create a systray!", e); } - + if(systray != null) { this.initSystray(); this.initProvidersTable(); - + uiService.setExitOnMainWindowClose(false); - + SystrayActivator.bundleContext.addServiceListener(this); } } - + /** * Initializes the systray icon and related listeners. */ private void initSystray() - { + { menu = new TrayMenu(uiService,this); - + String osName = System.getProperty("os.name"); // If we're running under Windows, we use a special icon without // background. @@ -130,43 +130,43 @@ public class SystrayServiceJdicImpl logoIcon = new ImageIcon( Resources.getImage("trayIcon")); } - + trayIcon = new TrayIcon(logoIcon, "SIP Communicator", menu); trayIcon.setIconAutoSize(true); - + //Show/hide the contact list when user clicks on the systray. - trayIcon.addActionListener(new ActionListener() + trayIcon.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) + public void actionPerformed(ActionEvent e) { boolean isVisible; - + isVisible = ! uiService.isVisible(); - + uiService.setVisible(isVisible); - + ConfigurationService configService = SystrayActivator.getConfigurationService(); - + configService.setProperty( "net.java.sip.communicator.impl.systray.showApplication", new Boolean(isVisible)); } }); - + //Notify all interested listener that user has clicked on the systray //popup message. - trayIcon.addBalloonActionListener(new ActionListener() + trayIcon.addBalloonActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) + public void actionPerformed(ActionEvent e) { firePopupMessageEvent(e.getSource()); - + ExportedWindow chatWindow = uiService.getExportedWindow(ExportedWindow.CHAT_WINDOW); - + if(chatWindow != null && chatWindow.isVisible()) - { + { chatWindow.bringToFront(); } } @@ -174,7 +174,7 @@ public class SystrayServiceJdicImpl systray.addTrayIcon(trayIcon); } - + /** * We fill the protocolProviderTable with all * running protocol providers at the start of @@ -183,7 +183,7 @@ public class SystrayServiceJdicImpl private void initProvidersTable() { BundleContext bc = SystrayActivator.bundleContext; - + bc.addServiceListener(this); ServiceReference[] protocolProviderRefs = null; try @@ -202,31 +202,31 @@ public class SystrayServiceJdicImpl // in case we found any if (protocolProviderRefs != null) { - + for (int i = 0; i < protocolProviderRefs.length; i++) { ProtocolProviderService provider = (ProtocolProviderService) bc .getService(protocolProviderRefs[i]); - + this.protocolProviderTable.put( provider.getAccountID(), provider); - + handleProviderAdded(provider); } } } - + /** * Returns a set of all protocol providers. * * @return a set of all protocol providers. */ public Iterator getProtocolProviders() - { - return this.protocolProviderTable.values().iterator(); + { + return this.protocolProviderTable.values().iterator(); } - + /** * Currently unused * @param evt ignored @@ -234,7 +234,7 @@ public class SystrayServiceJdicImpl public void messageDelivered(MessageDeliveredEvent evt) { } - + /** * Currently unused * @param evt ignored @@ -242,7 +242,7 @@ public class SystrayServiceJdicImpl public void messageDeliveryFailed(MessageDeliveryFailedEvent evt) { } - + /** * Display in a balloon the newly received message * @param evt the event containing the message @@ -250,46 +250,46 @@ public class SystrayServiceJdicImpl public void messageReceived(MessageReceivedEvent evt) { Chat chat = uiService.getChat(evt.getSourceContact()); - + if(!chat.isChatFocused()) - { + { String title = Resources.getString("messageReceived") + " " + evt.getSourceContact().getDisplayName(); - + String message = evt.getSourceMessage().getContent(); - + if(message.length() > 100) message = message.substring(0, 100).concat("..."); - + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); - + // Create an image that does not support transparency BufferedImage img = gc.createCompatibleImage(logoIcon.getIconWidth(), logoIcon.getIconHeight(), Transparency.TRANSLUCENT); - + Image msgImg = new ImageIcon( Resources.getImage("messageIcon")).getImage(); - + Graphics2D g = (Graphics2D) img.getGraphics(); - + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(logoIcon.getImage(), 0, 0, null); g.drawImage(msgImg, logoIcon.getIconWidth()/2 - msgImg.getWidth(null)/2, logoIcon.getIconHeight()/2 - msgImg.getHeight(null)/2, null); - + this.trayIcon.setIcon(new ImageIcon(img)); - + this.trayIcon.displayMessage( title, message, TrayIcon.NONE_MESSAGE_TYPE); - + chat.addChatFocusListener(this); } } - + /** * When a service ist registered or unregistered, we update * the provider tables and add/remove listeners (if it supports @@ -298,24 +298,31 @@ public class SystrayServiceJdicImpl * @param event ServiceEvent */ public void serviceChanged(ServiceEvent event) - { - ProtocolProviderService provider = (ProtocolProviderService) - SystrayActivator.bundleContext.getService(event.getServiceReference()); - - if (event.getType() == ServiceEvent.REGISTERED){ + { + Object service = SystrayActivator.bundleContext + .getService( event.getServiceReference()); + + if (! (service instanceof ProtocolProviderService)) + return; + + ProtocolProviderService provider = (ProtocolProviderService)service; + + if (event.getType() == ServiceEvent.REGISTERED) + { protocolProviderTable.put(provider.getAccountID(),provider); handleProviderAdded(provider); - + } - if (event.getType() == ServiceEvent.UNREGISTERING){ + + if (event.getType() == ServiceEvent.UNREGISTERING) + { protocolProviderTable.remove(provider.getAccountID()); handleProviderRemoved(provider); } - } - + /** - * Checks if the provider has an implementation + * Checks if the provider has an implementation * of OperationSetBasicInstantMessaging and * if so add a listerner to it * @@ -327,14 +334,14 @@ public class SystrayServiceJdicImpl = (OperationSetBasicInstantMessaging) provider .getSupportedOperationSets().get( OperationSetBasicInstantMessaging.class.getName()); - + if(opSetIm != null) - opSetIm.addMessageListener(this); - + opSetIm.addMessageListener(this); + } - + /** - * Checks if the provider has an implementation + * Checks if the provider has an implementation * of OperationSetBasicInstantMessaging and * if so remove its listerner * @@ -346,10 +353,10 @@ public class SystrayServiceJdicImpl = (OperationSetBasicInstantMessaging) provider .getSupportedOperationSets().get( OperationSetBasicInstantMessaging.class.getName()); - + if(opSetIm != null) opSetIm.removeMessageListener(this); - + } /** @@ -362,7 +369,7 @@ public class SystrayServiceJdicImpl { ConfigurationService configService = SystrayActivator.getConfigurationService(); - + if(configService != null) { String prefix = "net.java.sip.communicator.impl.gui.accounts"; @@ -423,14 +430,14 @@ public class SystrayServiceJdicImpl int messageType) { int trayMsgType = TrayIcon.NONE_MESSAGE_TYPE; - + if (messageType == SystrayService.ERROR_MESSAGE_TYPE) trayMsgType = TrayIcon.ERROR_MESSAGE_TYPE; else if (messageType == SystrayService.INFORMATION_MESSAGE_TYPE) trayMsgType = TrayIcon.INFO_MESSAGE_TYPE; else if (messageType == SystrayService.WARNING_MESSAGE_TYPE) trayMsgType = TrayIcon.WARNING_MESSAGE_TYPE; - + this.trayIcon.displayMessage( title, messageContent, trayMsgType); } @@ -456,25 +463,25 @@ public class SystrayServiceJdicImpl this.popupMessageListeners.remove(listener); } } - + private void firePopupMessageEvent(Object sourceObject) { SystrayPopupMessageEvent evt = new SystrayPopupMessageEvent(sourceObject); - + logger.trace("Will dispatch the following systray msg event: " + evt); - + Iterator listeners = null; synchronized (popupMessageListeners) { listeners = new ArrayList(popupMessageListeners).iterator(); } - + while (listeners.hasNext()) { SystrayPopupMessageListener listener = (SystrayPopupMessageListener) listeners.next(); - + listener.popupMessageClicked(evt); } } @@ -482,13 +489,13 @@ public class SystrayServiceJdicImpl public void chatFocusGained(ChatFocusEvent event) { Chat chat = event.getChat(); - + chat.removeChatFocusListener(this); - + this.trayIcon.setIcon(logoIcon); } public void chatFocusLost(ChatFocusEvent event) - { + { } } -- cgit v1.1