From 5012fc910a3cfe22d82ba13125c394e0555e9a5c Mon Sep 17 00:00:00 2001 From: Damian Minkov Date: Thu, 16 Aug 2012 21:29:02 +0000 Subject: Adds custom actions to source contacts. --- .../contactlist/contactsource/SourceUIContact.java | 236 ++++++++++++++++++++- .../customcontactactions/ContactAction.java | 4 +- 2 files changed, 237 insertions(+), 3 deletions(-) diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/SourceUIContact.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/SourceUIContact.java index 957b8b1..4d9c6b1 100644 --- a/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/SourceUIContact.java +++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/SourceUIContact.java @@ -7,6 +7,7 @@ package net.java.sip.communicator.impl.gui.main.contactlist.contactsource; import java.awt.*; +import java.awt.event.*; import java.util.*; import java.util.List; @@ -16,10 +17,12 @@ import net.java.sip.communicator.impl.gui.*; import net.java.sip.communicator.impl.gui.main.contactlist.*; import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.service.contactsource.*; +import net.java.sip.communicator.service.customcontactactions.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.service.protocol.globalstatus.*; import net.java.sip.communicator.util.*; import net.java.sip.communicator.util.swing.*; +import org.osgi.framework.*; /** * The SourceUIContact is the implementation of the UIContact for the @@ -52,6 +55,12 @@ public class SourceUIContact private final List searchStrings = new LinkedList(); /** + * The list of action buttons for this source contact. + */ + private Map, SIPCommButton> + customActionButtons; + + /** * Creates an instance of SourceUIContact by specifying the * SourceContact, on which this abstraction is based and the * parent UIGroup. @@ -310,6 +319,25 @@ public class SourceUIContact } /** + * Creates an instance of SourceContactDetail by specifying + * the underlying detail and the OperationSet class + * for it. + * + * @param displayName the display name + */ + public SourceContactDetail(String displayName) + { + super( displayName, + displayName, + null, + null, + null, + null, + null, + sourceContact); + } + + /** * Returns null to indicate that this detail doesn't support presence. * @return null */ @@ -439,6 +467,212 @@ public class SourceUIContact */ public Collection getContactCustomActionButtons() { - return null; + if (customActionButtons == null) + initCustomActionButtons(); + + Iterator> customActionsIter + = customActionButtons.keySet().iterator(); + + Collection availableCustomActionButtons + = new LinkedList(); + + while (customActionsIter.hasNext()) + { + ContactAction contactAction + = customActionsIter.next(); + + SIPCommButton actionButton = customActionButtons.get(contactAction); + + if (isContactActionVisible( contactAction, + sourceContact)) + { + availableCustomActionButtons.add(actionButton); + } + } + + return availableCustomActionButtons; + } + + /** + * Indicates if the given ContactAction should be visible for the + * given SourceContact. + * + * @param contactAction the ContactAction to verify + * if the given action should be visible + * @return true if the given ContactAction is visible for + * the given SourceContact, false - otherwise + */ + private static boolean isContactActionVisible( + ContactAction contactAction, + SourceContact contact) + { + if (contactAction.isVisible(contact)) + return true; + + return false; + } + + /** + * Initializes custom action buttons for this contact source. + */ + private void initCustomActionButtons() + { + customActionButtons = new LinkedHashMap + , SIPCommButton>(); + + for (CustomContactActionsService ccas + : getContactActionsServices()) + { + Iterator> actionIterator + = ccas.getCustomContactActions(); + + while (actionIterator!= null && actionIterator.hasNext()) + { + final ContactAction ca = actionIterator.next(); + + SIPCommButton actionButton = customActionButtons.get(ca); + + if (actionButton == null) + { + actionButton = new SIPCommButton( + new ImageIcon(ca.getIcon()).getImage(), + new ImageIcon(ca.getPressedIcon()).getImage(), + null); + + actionButton.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + List contactDetails + = getContactDetails(); + contactDetails.add(new SourceContactDetail( + sourceContact.getDisplayName())); + + UIContactDetailCustomAction contactAction + = new UIContactDetailCustomAction(ca); + + if (contactDetails.size() > 1) + { + ChooseUIContactDetailPopupMenu + detailsPopupMenu + = new ChooseUIContactDetailPopupMenu( + (JButton) e.getSource(), + contactDetails, + contactAction); + + detailsPopupMenu.showPopupMenu(); + } + else if (contactDetails.size() == 1) + { + JButton button = (JButton) e.getSource(); + Point location = new Point(button.getX(), + button.getY() + button.getHeight()); + + SwingUtilities.convertPointToScreen( + location, GuiActivator.getContactList()); + + location.y = location.y + + GuiActivator.getContactList() + .getPathBounds( + GuiActivator.getContactList() + .getSelectionPath()).y; + + contactAction.actionPerformed( + contactDetails.get(0), + location.x, + location.y); + } + } + }); + + customActionButtons.put(ca, actionButton); + } + } + } + } + + /** + * Returns a list of all custom contact action services. + * + * @return a list of all custom contact action services. + */ + @SuppressWarnings ("unchecked") + private static List> + getContactActionsServices() + { + List> + contactActionsServices + = new ArrayList>(); + + ServiceReference[] serRefs = null; + try + { + // get all registered provider factories + serRefs + = GuiActivator.bundleContext.getServiceReferences( + CustomContactActionsService.class.getName(), null); + } + catch (InvalidSyntaxException e) + {} + + if (serRefs != null) + { + for (ServiceReference serRef : serRefs) + { + CustomContactActionsService customActionService + = (CustomContactActionsService) + GuiActivator.bundleContext.getService(serRef); + + if (customActionService.getContactSourceClass() + .equals(SourceContact.class)) + { + contactActionsServices.add( + (CustomContactActionsService) + customActionService); + } + } + } + return contactActionsServices; + } + + /** + * An implementation of UIContactDetail for a custom action. + */ + private static class UIContactDetailCustomAction + implements UIContactDetailAction + { + /** + * The contact action. + */ + private final ContactAction contactAction; + + /** + * Creates an instance of UIContactDetailCustomAction. + */ + public UIContactDetailCustomAction( + ContactAction contactAction) + { + this.contactAction = contactAction; + } + + /** + * Performs the action on button click. + */ + public void actionPerformed(UIContactDetail contactDetail, int x, int y) + { + try + { + contactAction.actionPerformed( + (SourceContact) contactDetail.getDescriptor(), x, y); + } + catch (OperationFailedException e) + { + new ErrorDialog(null, + GuiActivator.getResources() + .getI18NString("service.gui.ERROR"), + e.getMessage()); + } + } } } diff --git a/src/net/java/sip/communicator/service/customcontactactions/ContactAction.java b/src/net/java/sip/communicator/service/customcontactactions/ContactAction.java index 981c9a3..d6afda4 100644 --- a/src/net/java/sip/communicator/service/customcontactactions/ContactAction.java +++ b/src/net/java/sip/communicator/service/customcontactactions/ContactAction.java @@ -21,8 +21,8 @@ public interface ContactAction * Invoked when an action occurs. * * @param actionSource the source of the action - * @param int x the x coordinate of the action - * @param int y the y coordinate of the action + * @param x the x coordinate of the action + * @param y the y coordinate of the action */ public void actionPerformed(T actionSource, int x, int y) throws OperationFailedException; -- cgit v1.1