AgileJ StructureViews User Guide

Containment Filters Example

Below is an example implementation which uses Java annotations to denote cardinality in the source code.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
package myjavacontainertypes;

import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;

import com.agilej.model.api.custom.ICustomModel;

public class CustomModel1 implements ICustomModel {

	@Override
	public String getContainedTypeSignatureOf(IField field) throws JavaModelException {

		// AgileJ calls this method and presents a field wanting to know
		// the contained type (if any) so that it can recognize relations
		// between classes
		// and apply filters. Supposing, for example, we have a convention that
		// the contained
		// type is denoted by an annotation of the form:

		// We can write code to recognize the intended contained type.

		// @Relation (class = "SomeContainedClass", cardinality = "Cardinality.zero_one")

		// Iterate through the annotations of this field
		for (IAnnotation annotation : field.getAnnotations()) {
			// Give up if the annotation is not named 'Relation'
			if (!annotation.getElementName().equals("Relation"))
				continue;

			// We have found an annotation called 'Relation'.
			// Iterate through its values
			for (IMemberValuePair memberValuePair : annotation.getMemberValuePairs()) {
				// Look for a String value called 'clazz' which is the
				// name of the contained class
				if ((memberValuePair.getMemberName().equals("clazz")) && (memberValuePair.getValue() instanceof String)) {
					String containedTypeName = (String) memberValuePair.getValue();

					// Ask Eclipse to resolve the contained type
					IType foundType = field.getJavaProject().findType(containedTypeName);

					// Give up if it cannot be resolved
					if (foundType == null)
						return null;

					return Signature.createTypeSignature(foundType.getFullyQualifiedName(), true);
				}
			}
		}
		// We didn't find anything for this field -
		// so return null to indicate that other plugins can try to resolve it
		return null;
	}

	@Override
	public String getTooltipText(IField field) throws JavaModelException {
		final String typeSignature = getContainedTypeSignatureOf(field);

		// if there is no recognized contained type, give up
		if (typeSignature == null)
			return null;

		// build a string which displays a meaningful tooltip
		String cardinality = getCardinality(field);
		if (cardinality == null)
			return null;

		return field.getElementName() + " references " + cardinality + " " + Signature.toString(typeSignature);
	}

	@Override
	public Boolean isOneToMany(IField field) throws JavaModelException {
		final String cardinality = getCardinality(field);
		if (cardinality == null)
			return null;
		if ("Cardinality.one".equals(cardinality))
			return Boolean.FALSE;
		if ("Cardinality.zero_one".equals(cardinality))
			return Boolean.FALSE;
		if ("Cardinality.zero_one".equals(cardinality))
			return Boolean.FALSE;
		return null;
	}

	private String getCardinality(IField field) throws JavaModelException {
		for (IAnnotation annotation : field.getAnnotations()) {
			if (!annotation.getElementName().equals("Relation"))
				continue;
			for (IMemberValuePair memberValuePair : annotation.getMemberValuePairs()) {
				final String memberName = memberValuePair.getMemberName();
				if (memberName.equals("cardinality"))
					if (memberValuePair.getValue() != null)
						return memberValuePair.getValue().toString();
			}
		}
		return null;
	}
}

Copyright © AgileJ Ltd. All rights reserved.