AgileJ StructureViews User Guide

Containment Filters Example

Below is an example implementation which uses Java annotations to denote cardinality in the source code.
package myjavafilters;

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

import com.agilej.model.custom.ICustomModel;

public class CustomModel3 implements ICustomModel {

	@Override
	public String getTypeOf(IField field) throws JavaModelException
	{
		for (IAnnotation annotation : field.getAnnotations())
		{
			if (!annotation.getElementName().equals("Relation"))
				continue;
			for (IMemberValuePair memberValuePair : annotation.getMemberValuePairs())
				if (memberValuePair.getMemberName().equals("clazz"))
					if (memberValuePair.getValue() instanceof String)
						return (String) memberValuePair.getValue();
		}
		return null;
	}

	@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;
		// assume all other cases are shown as one to many
		return Boolean.TRUE;
	}

	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("fromCardinality"))
					if (memberValuePair.getValue() != null)
						return memberValuePair.getValue().toString();
			}
		}
		return null;
	}

	@Override
	public String getTooltipText(IField field) throws JavaModelException
	{
		final String type = getTypeOf(field);
		if (type == null)
			return null;
		String cardinality = getCardinality(field);
		if (cardinality == null)
			cardinality = "one or more ";
		return field.getElementName() + " references " + cardinality + " " + type;
	}
}

Copyright © AgileJ Ltd. All rights reserved.