1 package org.columba.addressbook.gui.action; 2 3 import java.net.URI ; 4 import java.net.URISyntaxException ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 8 import javax.swing.event.ListSelectionEvent ; 9 import javax.swing.event.ListSelectionListener ; 10 import javax.swing.event.TreeSelectionEvent ; 11 import javax.swing.event.TreeSelectionListener ; 12 import javax.swing.tree.TreePath ; 13 14 import org.columba.addressbook.facade.IContactItem; 15 import org.columba.addressbook.folder.IFolder; 16 import org.columba.addressbook.gui.frame.AddressbookFrameMediator; 17 import org.columba.addressbook.util.AddressbookResourceLoader; 18 import org.columba.api.gui.frame.IFrameMediator; 19 import org.columba.core.association.AssociationStore; 20 import org.columba.core.association.api.IAssociation; 21 import org.columba.core.gui.tagging.TaggingMenu; 22 import org.columba.core.tagging.TagManager; 23 import org.columba.core.tagging.api.ITag; 24 25 public class TagContactMenu extends TaggingMenu implements 26 ListSelectionListener , TreeSelectionListener { 27 28 final static private String SERVICE_ID = "tagging"; 29 30 private String folderId; 31 32 private String contactId; 33 34 37 public TagContactMenu(IFrameMediator frameMediator) { 38 super(frameMediator, AddressbookResourceLoader.getString("dialog", 39 "tagging", "contact_tag_message"), "menu_tag_message"); 40 41 ((AddressbookFrameMediator) frameMediator) 43 .addTableSelectionListener(this); 44 45 ((AddressbookFrameMediator) frameMediator) 47 .addTreeSelectionListener(this); 48 } 49 50 52 public void valueChanged(TreeSelectionEvent e) { 53 TreePath path = e.getNewLeadSelectionPath(); 54 55 if (path != null) { 57 IFolder folder = (IFolder) path.getLastPathComponent(); 58 folderId = folder.getId(); 59 } 60 61 } 62 63 66 public void valueChanged(ListSelectionEvent event) { 67 if (event.getValueIsAdjusting()) { 69 return; 70 } 71 72 Object [] uids = ((AddressbookFrameMediator) getFrameMediator()) 73 .getTable().getUids(); 74 75 if (uids.length > 0) { 76 setEnabled(true); 77 78 removeAll(); 79 createSubMenu(); 80 } else { 81 setEnabled(false); 82 } 83 } 84 85 @Override 86 protected void assignTag(String tagId) { 87 88 String [] ids = ((AddressbookFrameMediator) getFrameMediator()) 89 .getTable().getUids(); 90 for (int i = 0; i < ids.length; i++) { 91 String contactId = ids[i]; 92 AssociationStore.getInstance().addAssociation(SERVICE_ID, tagId, 93 createURI(folderId, contactId).toString()); 94 } 95 } 96 97 @Override 98 protected boolean isTagged(ITag tag) { 99 String [] ids = ((AddressbookFrameMediator) getFrameMediator()) 100 .getTable().getUids(); 101 boolean tagged = true; 102 for (int i = 0; i < ids.length; i++) { 103 String contactId = ids[i]; 104 105 tagged &= checkAssocation(folderId, contactId, tag); 106 } 107 108 return tagged; 109 } 110 111 @Override 112 protected void removeAllTags() { 113 IContactItem contactItem = ((AddressbookFrameMediator) getFrameMediator()) 114 .getTable().getSelectedItem(); 115 String contactId = contactItem.getId(); 116 117 for (Iterator <ITag> iter = TagManager.getInstance().getAllTags(); iter 118 .hasNext();) { 119 ITag tag = iter.next(); 120 121 AssociationStore.getInstance().removeAssociation(SERVICE_ID, 122 tag.getId(), createURI(folderId, contactId).toString()); 123 } 124 } 125 126 @Override 127 protected void removeTag(String tagId) { 128 129 String [] ids = ((AddressbookFrameMediator) getFrameMediator()) 130 .getTable().getUids(); 131 for (int i = 0; i < ids.length; i++) { 132 String contactId = ids[i]; 133 AssociationStore.getInstance().removeAssociation(SERVICE_ID, tagId, 134 createURI(folderId, contactId).toString()); 135 } 136 137 } 138 139 public static URI createURI(String folderId, Object contactId) { 141 URI uri = null; 142 try { 143 uri = new URI ("columba://org.columba.contact/" + folderId + "/" 144 + contactId); 145 } catch (URISyntaxException e) { 146 e.printStackTrace(); 147 } 148 return uri; 149 } 150 151 private boolean checkAssocation(String folderId, String contactId, ITag tag) { 153 URI uri = createURI(folderId, contactId); 154 Collection <IAssociation> c = AssociationStore.getInstance() 155 .getAllAssociations(uri.toString()); 156 157 for (IAssociation as : c) { 158 if (as.getServiceId().equals(SERVICE_ID) 159 && (as.getMetaDataId() != null) 160 && (as.getMetaDataId().equals(tag.getId()))) { 161 return true; 162 } 163 } 164 return false; 165 } 166 167 } 168 | Popular Tags |