View Javadoc
1   package org.argeo.connect.e4.resources.parts;
2   
3   import static org.argeo.connect.resources.ResourcesNames.RESOURCES_TAGGABLE_PROP_NAME;
4   
5   import java.util.List;
6   
7   import javax.jcr.Node;
8   import javax.jcr.Session;
9   
10  import org.argeo.connect.resources.ResourcesNames;
11  import org.argeo.connect.resources.ResourcesService;
12  import org.argeo.connect.resources.core.TagUtils;
13  import org.argeo.connect.ui.AppWorkbenchService;
14  import org.argeo.connect.ui.ConnectWorkbenchUtils;
15  import org.argeo.connect.util.ConnectJcrUtils;
16  import org.argeo.eclipse.ui.EclipseUiUtils;
17  import org.eclipse.jface.viewers.ColumnLabelProvider;
18  
19  /**
20   * A label provider to display clickable tag-like resources like in
21   * markup-enabled viewers
22   */
23  public class OtherTagsLabelProvider extends ColumnLabelProvider {
24  	private static final long serialVersionUID = -3486673830618518227L;
25  
26  	// private final String tagTypeId;
27  	// Context
28  	private final Session session;
29  	private final ResourcesService resourcesService;
30  	private final AppWorkbenchService appWorkbenchService;
31  	private String tagId;
32  
33  	private final String taggablePropName;
34  	private final String currentTag;
35  	private Node tagLikeResPar;
36  	private String selectorName;
37  
38  	// enhance labels
39  	private String labelPrefix = "#";
40  
41  	public OtherTagsLabelProvider(ResourcesService resourcesService, AppWorkbenchService appWorkbenchService,
42  			Session session, String tagId, String currentTag) {
43  		this.resourcesService = resourcesService;
44  		this.appWorkbenchService = appWorkbenchService;
45  		this.session = session;
46  		this.tagId = tagId;
47  		this.currentTag = currentTag;
48  		tagLikeResPar = resourcesService.getTagLikeResourceParent(session, tagId);
49  		if (tagLikeResPar != null) {// workaround
50  			taggablePropName = ConnectJcrUtils.getMultiAsString(tagLikeResPar, RESOURCES_TAGGABLE_PROP_NAME, ",");
51  		} else {
52  			taggablePropName = null;
53  		}
54  	}
55  
56  	public OtherTagsLabelProvider(ResourcesService resourcesService, AppWorkbenchService appWorkbenchService,
57  			Node tagInstance, String selectorName) {
58  		this.session = ConnectJcrUtils.getSession(tagInstance);
59  		this.resourcesService = resourcesService;
60  		this.appWorkbenchService = appWorkbenchService;
61  		this.selectorName = selectorName;
62  		tagLikeResPar = TagUtils.retrieveTagParentFromTag(tagInstance);
63  		tagId = ConnectJcrUtils.get(tagLikeResPar, ResourcesNames.RESOURCES_TAG_ID);
64  		taggablePropName = ConnectJcrUtils.getMultiAsString(tagLikeResPar, RESOURCES_TAGGABLE_PROP_NAME, ",");
65  		currentTag = TagUtils.retrieveTagId(tagInstance);
66  	}
67  
68  	public void setLabelPrefix(String labelPrefix) {
69  		this.labelPrefix = labelPrefix;
70  	}
71  
72  	@Override
73  	public String getText(Object element) {
74  		// if(true)
75  		// return "";
76  		// try {
77  		Node currNode = ConnectJcrUtils.getNodeFromElement(element, selectorName);
78  		// TODO also handle encoded tag case.
79  		StringBuilder builder = new StringBuilder();
80  		List<String> currTags = ConnectJcrUtils.getMultiAsList(currNode, taggablePropName);
81  		loop: for (String tag : currTags) {
82  			if (EclipseUiUtils.notEmpty(currentTag) && tag.equals(currentTag))
83  				continue loop;
84  			// TODO rather display links to open corresponding tag editor
85  			// String value = ConnectWorkbenchUtils.getTagLink(session, resourcesService,
86  			// appWorkbenchService, tagId, tag);
87  			builder.append(labelPrefix).append(tag).append(" ");
88  		}
89  
90  		if (builder.length() > 2)
91  			return builder.toString();
92  		else
93  			return "";
94  		// } catch (RepositoryException re) {
95  		// throw new ActivitiesException("Unable to get date from node " +
96  		// element, re);
97  		// }
98  	}
99  }