diff options
author | Damian Minkov <damencho@jitsi.org> | 2012-08-16 21:29:02 +0000 |
---|---|---|
committer | Damian Minkov <damencho@jitsi.org> | 2012-08-16 21:29:02 +0000 |
commit | 5012fc910a3cfe22d82ba13125c394e0555e9a5c (patch) | |
tree | 502235a3cb95ac22bed0ed3bb4fa9eebcc8898f3 | |
parent | 5f866b4185e9a9833c3180a70bed012ca264edf1 (diff) | |
download | jitsi-5012fc910a3cfe22d82ba13125c394e0555e9a5c.zip jitsi-5012fc910a3cfe22d82ba13125c394e0555e9a5c.tar.gz jitsi-5012fc910a3cfe22d82ba13125c394e0555e9a5c.tar.bz2 |
Adds custom actions to source contacts.
-rw-r--r-- | src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/SourceUIContact.java | 236 | ||||
-rw-r--r-- | src/net/java/sip/communicator/service/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 <tt>SourceUIContact</tt> is the implementation of the UIContact for the @@ -52,6 +55,12 @@ public class SourceUIContact private final List<String> searchStrings = new LinkedList<String>(); /** + * The list of action buttons for this source contact. + */ + private Map<ContactAction<SourceContact>, SIPCommButton> + customActionButtons; + + /** * Creates an instance of <tt>SourceUIContact</tt> by specifying the * <tt>SourceContact</tt>, on which this abstraction is based and the * parent <tt>UIGroup</tt>. @@ -310,6 +319,25 @@ public class SourceUIContact } /** + * Creates an instance of <tt>SourceContactDetail</tt> by specifying + * the underlying <tt>detail</tt> and the <tt>OperationSet</tt> 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<SIPCommButton> getContactCustomActionButtons() { - return null; + if (customActionButtons == null) + initCustomActionButtons(); + + Iterator<ContactAction<SourceContact>> customActionsIter + = customActionButtons.keySet().iterator(); + + Collection<SIPCommButton> availableCustomActionButtons + = new LinkedList<SIPCommButton>(); + + while (customActionsIter.hasNext()) + { + ContactAction<SourceContact> contactAction + = customActionsIter.next(); + + SIPCommButton actionButton = customActionButtons.get(contactAction); + + if (isContactActionVisible( contactAction, + sourceContact)) + { + availableCustomActionButtons.add(actionButton); + } + } + + return availableCustomActionButtons; + } + + /** + * Indicates if the given <tt>ContactAction</tt> should be visible for the + * given <tt>SourceContact</tt>. + * + * @param contactAction the <tt>ContactAction</tt> to verify + * if the given action should be visible + * @return <tt>true</tt> if the given <tt>ContactAction</tt> is visible for + * the given <tt>SourceContact</tt>, <tt>false</tt> - otherwise + */ + private static boolean isContactActionVisible( + ContactAction<SourceContact> 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 + <ContactAction<SourceContact>, SIPCommButton>(); + + for (CustomContactActionsService<SourceContact> ccas + : getContactActionsServices()) + { + Iterator<ContactAction<SourceContact>> actionIterator + = ccas.getCustomContactActions(); + + while (actionIterator!= null && actionIterator.hasNext()) + { + final ContactAction<SourceContact> 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<UIContactDetail> 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<CustomContactActionsService<SourceContact>> + getContactActionsServices() + { + List<CustomContactActionsService<SourceContact>> + contactActionsServices + = new ArrayList<CustomContactActionsService + <SourceContact>>(); + + 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<SourceContact>) + customActionService); + } + } + } + return contactActionsServices; + } + + /** + * An implementation of <tt>UIContactDetail</tt> for a custom action. + */ + private static class UIContactDetailCustomAction + implements UIContactDetailAction + { + /** + * The contact action. + */ + private final ContactAction<SourceContact> contactAction; + + /** + * Creates an instance of <tt>UIContactDetailCustomAction</tt>. + */ + public UIContactDetailCustomAction( + ContactAction<SourceContact> 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<T> * 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; |