diff options
author | Damian Minkov <damencho@jitsi.org> | 2012-08-17 08:43:13 +0000 |
---|---|---|
committer | Damian Minkov <damencho@jitsi.org> | 2012-08-17 08:43:13 +0000 |
commit | d100493139446a9fc27e40932b1bb9dabd1e0a8f (patch) | |
tree | bd4e3daac0c8923d0622967654373f32bf7f43ff /src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource | |
parent | 5012fc910a3cfe22d82ba13125c394e0555e9a5c (diff) | |
download | jitsi-d100493139446a9fc27e40932b1bb9dabd1e0a8f.zip jitsi-d100493139446a9fc27e40932b1bb9dabd1e0a8f.tar.gz jitsi-d100493139446a9fc27e40932b1bb9dabd1e0a8f.tar.bz2 |
Adds get/set data to SourceContacts. Moves custom buttons to ExternalContactSource. Adds listener to refresh contact sources.
Diffstat (limited to 'src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource')
2 files changed, 336 insertions, 221 deletions
diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/ExternalContactSource.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/ExternalContactSource.java index afa0ee9..d60a214 100644 --- a/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/ExternalContactSource.java +++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/ExternalContactSource.java @@ -8,8 +8,18 @@ package net.java.sip.communicator.impl.gui.main.contactlist.contactsource; import javax.swing.*; +import net.java.sip.communicator.impl.gui.*; import net.java.sip.communicator.impl.gui.main.contactlist.*; 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.util.swing.*; +import org.osgi.framework.*; + +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import java.util.List; /** * The <tt>ExternalContactSource</tt> is the UI abstraction of the @@ -20,6 +30,13 @@ import net.java.sip.communicator.service.contactsource.*; public class ExternalContactSource { /** + * The data key of the SourceContactDescriptor object used to store a + * reference to this object in its corresponding Sourcecontact. + */ + public static final String UI_CONTACT_DATA_KEY + = SourceUIContact.class.getName() + ".uiContactDescriptor"; + + /** * The <tt>SourceUIGroup</tt> containing all contacts from this source. */ private final SourceUIGroup sourceUIGroup; @@ -30,6 +47,17 @@ public class ExternalContactSource private final ContactSourceService contactSource; /** + * The current custom action contact. + */ + private static SourceContact customActionContact; + + /** + * The list of action buttons for this source contact. + */ + private static Map<ContactAction<SourceContact>, SIPCommButton> + customActionButtons; + + /** * Creates an <tt>ExternalContactSource</tt> based on the given * <tt>ContactSourceService</tt>. * @@ -75,7 +103,222 @@ public class ExternalContactSource */ public UIContact createUIContact(SourceContact sourceContact) { - return new SourceUIContact(sourceContact, sourceUIGroup); + SourceUIContact descriptor + = new SourceUIContact(sourceContact, sourceUIGroup); + + sourceContact.setData(UI_CONTACT_DATA_KEY, descriptor); + + return descriptor; + } + + /** + * Removes the <tt>UIContact</tt> from the given <tt>sourceContact</tt>. + * @param sourceContact the <tt>SourceContact</tt>, which corresponding UI + * contact we would like to remove + */ + public static void removeUIContact(SourceContact sourceContact) + { + sourceContact.setData(UI_CONTACT_DATA_KEY, null); + } + + /** + * Returns the <tt>UIContact</tt> corresponding to the given + * <tt>SourceContact</tt>. + * @param sourceContact the <tt>SourceContact</tt>, which corresponding UI + * contact we're looking for + * @return the <tt>UIContact</tt> corresponding to the given + * <tt>MetaContact</tt> + */ + public static UIContact getUIContact(SourceContact sourceContact) + { + return (UIContact) sourceContact.getData(UI_CONTACT_DATA_KEY); + } + + /** + * Returns all custom action buttons for this meta contact. + * + * @return a list of all custom action buttons for this meta contact + */ + public static Collection<SIPCommButton> getContactCustomActionButtons( + final SourceContact sourceContact) + { + customActionContact = sourceContact; + + 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 static void initCustomActionButtons() + { + customActionButtons = new LinkedHashMap + <ContactAction<SourceContact>, SIPCommButton>(); + + CustomContactActionsChangeListener changeListener + = new CustomContactActionsChangeListener(); + + for (CustomContactActionsService<SourceContact> ccas + : getContactActionsServices()) + { + ccas.addCustomContactActionsListener(changeListener); + + 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 + = SourceUIContact.getContactDetails( + customActionContact); + contactDetails.add( + new SourceUIContact.SourceContactDetail( + customActionContact.getDisplayName(), + customActionContact)); + + 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; } /** @@ -209,4 +452,67 @@ public class ExternalContactSource return null; } } + + /** + * Listens for updates on actions and when received update the source contact. + */ + private static class CustomContactActionsChangeListener + implements CustomContactActionsListener + { + /** + * Update for custom action has occurred. + * @param event the event containing the source which was updated. + */ + public void updated(CustomContactActionsEvent event) + { + if(!(event.getSource() instanceof SourceContact)) + return; + + ContactNode contactNode + = getUIContact((SourceContact)event.getSource()).getContactNode(); + + if (contactNode != null) + GuiActivator.getContactList().nodeChanged(contactNode); + } + } + + /** + * 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/impl/gui/main/contactlist/contactsource/SourceUIContact.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/SourceUIContact.java index 4d9c6b1..0098beb 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,7 +7,6 @@ package net.java.sip.communicator.impl.gui.main.contactlist.contactsource; import java.awt.*; -import java.awt.event.*; import java.util.*; import java.util.List; @@ -55,12 +54,6 @@ 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>. @@ -203,6 +196,18 @@ public class SourceUIContact */ public List<UIContactDetail> getContactDetails() { + return getContactDetails(sourceContact); + } + + /** + * Returns a list of all contained <tt>UIContactDetail</tt>s. + * + * @param sourceContact the source contact we get details from. + * @return a list of all contained <tt>UIContactDetail</tt>s + */ + public static List<UIContactDetail> getContactDetails( + SourceContact sourceContact) + { List<UIContactDetail> resultList = new LinkedList<UIContactDetail>(); @@ -213,7 +218,7 @@ public class SourceUIContact { ContactDetail detail = details.next(); - resultList.add(new SourceContactDetail(detail, null)); + resultList.add(new SourceContactDetail(detail, null, sourceContact)); } return resultList; } @@ -244,7 +249,8 @@ public class SourceUIContact if ((supportedOperationSets != null) && supportedOperationSets.contains(opSetClass)) { - resultList.add(new SourceContactDetail(detail, opSetClass)); + resultList.add(new SourceContactDetail( + detail, opSetClass, sourceContact)); } } return resultList; @@ -277,13 +283,15 @@ public class SourceUIContact public void setContactNode(ContactNode contactNode) { this.contactNode = contactNode; + if (contactNode == null) + ExternalContactSource.removeUIContact(sourceContact); } /** * The implementation of the <tt>UIContactDetail</tt> interface for the * external source <tt>ContactDetail</tt>s. */ - private class SourceContactDetail extends UIContactDetail + protected static class SourceContactDetail extends UIContactDetail { /** * Creates an instance of <tt>SourceContactDetail</tt> by specifying @@ -294,7 +302,8 @@ public class SourceUIContact * preferred protocol provider */ public SourceContactDetail( ContactDetail detail, - Class<? extends OperationSet> opSetClass) + Class<? extends OperationSet> opSetClass, + SourceContact sourceContact) { super( detail.getContactAddress(), detail.getContactAddress(), @@ -324,8 +333,10 @@ public class SourceUIContact * for it. * * @param displayName the display name + * @param sourceContact the source contact */ - public SourceContactDetail(String displayName) + public SourceContactDetail(String displayName, + SourceContact sourceContact) { super( displayName, displayName, @@ -461,218 +472,16 @@ public class SourceUIContact } /** - * Returns all custom action buttons for this meta contact. + * Returns all custom action buttons for this notification contact. * - * @return a list of all custom action buttons for this meta contact + * @return a list of all custom action buttons for this notification contact */ public Collection<SIPCommButton> getContactCustomActionButtons() { - 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; - } + if (sourceContact != null) + return ExternalContactSource + .getContactCustomActionButtons(sourceContact); - /** - * 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()); - } - } + return null; } } |