001: package org.columba.addressbook.gui.action;
002:
003: import java.net.URI;
004: import java.net.URISyntaxException;
005: import java.util.Collection;
006: import java.util.Iterator;
007:
008: import javax.swing.event.ListSelectionEvent;
009: import javax.swing.event.ListSelectionListener;
010: import javax.swing.event.TreeSelectionEvent;
011: import javax.swing.event.TreeSelectionListener;
012: import javax.swing.tree.TreePath;
013:
014: import org.columba.addressbook.facade.IContactItem;
015: import org.columba.addressbook.folder.IFolder;
016: import org.columba.addressbook.gui.frame.AddressbookFrameMediator;
017: import org.columba.addressbook.util.AddressbookResourceLoader;
018: import org.columba.api.gui.frame.IFrameMediator;
019: import org.columba.core.association.AssociationStore;
020: import org.columba.core.association.api.IAssociation;
021: import org.columba.core.gui.tagging.TaggingMenu;
022: import org.columba.core.tagging.TagManager;
023: import org.columba.core.tagging.api.ITag;
024:
025: public class TagContactMenu extends TaggingMenu implements
026: ListSelectionListener, TreeSelectionListener {
027:
028: final static private String SERVICE_ID = "tagging";
029:
030: private String folderId;
031:
032: private String contactId;
033:
034: /**
035: * @param frameMediator
036: */
037: public TagContactMenu(IFrameMediator frameMediator) {
038: super (frameMediator, AddressbookResourceLoader.getString(
039: "dialog", "tagging", "contact_tag_message"),
040: "menu_tag_message");
041:
042: // register interest on contact selection changes
043: ((AddressbookFrameMediator) frameMediator)
044: .addTableSelectionListener(this );
045:
046: // register interest on tree selection changes
047: ((AddressbookFrameMediator) frameMediator)
048: .addTreeSelectionListener(this );
049: }
050:
051: /**
052: */
053: public void valueChanged(TreeSelectionEvent e) {
054: TreePath path = e.getNewLeadSelectionPath();
055:
056: // remember last selected folder treenode
057: if (path != null) {
058: IFolder folder = (IFolder) path.getLastPathComponent();
059: folderId = folder.getId();
060: }
061:
062: }
063:
064: /**
065: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
066: */
067: public void valueChanged(ListSelectionEvent event) {
068: // return if selection change is in flux
069: if (event.getValueIsAdjusting()) {
070: return;
071: }
072:
073: Object[] uids = ((AddressbookFrameMediator) getFrameMediator())
074: .getTable().getUids();
075:
076: if (uids.length > 0) {
077: setEnabled(true);
078:
079: removeAll();
080: createSubMenu();
081: } else {
082: setEnabled(false);
083: }
084: }
085:
086: @Override
087: protected void assignTag(String tagId) {
088:
089: String[] ids = ((AddressbookFrameMediator) getFrameMediator())
090: .getTable().getUids();
091: for (int i = 0; i < ids.length; i++) {
092: String contactId = ids[i];
093: AssociationStore.getInstance().addAssociation(SERVICE_ID,
094: tagId, createURI(folderId, contactId).toString());
095: }
096: }
097:
098: @Override
099: protected boolean isTagged(ITag tag) {
100: String[] ids = ((AddressbookFrameMediator) getFrameMediator())
101: .getTable().getUids();
102: boolean tagged = true;
103: for (int i = 0; i < ids.length; i++) {
104: String contactId = ids[i];
105:
106: tagged &= checkAssocation(folderId, contactId, tag);
107: }
108:
109: return tagged;
110: }
111:
112: @Override
113: protected void removeAllTags() {
114: IContactItem contactItem = ((AddressbookFrameMediator) getFrameMediator())
115: .getTable().getSelectedItem();
116: String contactId = contactItem.getId();
117:
118: for (Iterator<ITag> iter = TagManager.getInstance()
119: .getAllTags(); iter.hasNext();) {
120: ITag tag = iter.next();
121:
122: AssociationStore.getInstance().removeAssociation(
123: SERVICE_ID, tag.getId(),
124: createURI(folderId, contactId).toString());
125: }
126: }
127:
128: @Override
129: protected void removeTag(String tagId) {
130:
131: String[] ids = ((AddressbookFrameMediator) getFrameMediator())
132: .getTable().getUids();
133: for (int i = 0; i < ids.length; i++) {
134: String contactId = ids[i];
135: AssociationStore.getInstance().removeAssociation(
136: SERVICE_ID, tagId,
137: createURI(folderId, contactId).toString());
138: }
139:
140: }
141:
142: // create URI representing the contact
143: public static URI createURI(String folderId, Object contactId) {
144: URI uri = null;
145: try {
146: uri = new URI("columba://org.columba.contact/" + folderId
147: + "/" + contactId);
148: } catch (URISyntaxException e) {
149: e.printStackTrace();
150: }
151: return uri;
152: }
153:
154: // check if contact is tagged
155: private boolean checkAssocation(String folderId, String contactId,
156: ITag tag) {
157: URI uri = createURI(folderId, contactId);
158: Collection<IAssociation> c = AssociationStore.getInstance()
159: .getAllAssociations(uri.toString());
160:
161: for (IAssociation as : c) {
162: if (as.getServiceId().equals(SERVICE_ID)
163: && (as.getMetaDataId() != null)
164: && (as.getMetaDataId().equals(tag.getId()))) {
165: return true;
166: }
167: }
168: return false;
169: }
170:
171: }
|