Task 1: Write your own Viewer Filters

 

Solution

To write your own filter for a Model Viewer, create a new class that either directly inherits from one of the standard filter classes YesManFilter or NoManFilter, or directly implements the IFilter interface.

When you derive from a standard filter class, you only need to overwrite the relevant availability method(s). However, if you directly implement IFilter, you must implement all the interface methods instead.

 

Example 1

This example implements a filter that you use to display solely files in the Tree Viewer.

The filter class inherits from the standard filter YesManFilter and overwrites only the availability method isAvailable() for the PropertyMap.

Within this method a general check takes place as to whether an object is at all a Selena element. Only those elements whose dataSource has the type IFile can pass through the filter without restriction. In this case, isAvailable() returns the value true.

 

 

public class FileOnlyFilter extends YesManFilter {

 

public boolean isAvailable(PropertyMap propertyMap) {

  boolean ret = false;

  if (propertyMap instanceof Element) {

     Element element = (Element) propertyMap;

     Object dataSource=element.getObjectPropertyValue(

                         FileAndFolderManagement.OBJECT_PROPERTY_NAME__DATA_SOURCE);

          if (dataSource instanceof IFile) {

          ret = true;

          }

  }

  return ret;

}

 

 

 

 

Example 2

The code lines below show a genuine filter example: a filter that displays solely EJB elements from a project. Here, too, the filter class inherits from the standard filter YesManFilter and merely overwrites the availability method isAvailable() for the PropertyMap. Afterwards, a general check takes place as to whether an object is at all a Selena Element  and whether it belongs as a part of an EJB project. Apart from pure references, only those elements that are under a reference Collection node and, additionally, are listed in the Bean Registry can pass the filter without restriction. The following If queries provide a further check as to whether the elements are logical EJB objects, valid EJB methods, or EJB persistence fields.

 

 

 

public class EjbFilter extends YesManFilter {

  private static final UniqueName BEAN_REGISTRY =

       ModelAccess.createUniqueName("Bean Registry", null, "Bean Registry");

 

  public boolean isAvailable(PropertyMap propertyMap) {

    boolean ret = false;

 

    if (propertyMap instanceof Element &&   

              J2eeElementManager.isEjbProject(((Element) propertyMap).getModel())) {

      if ((propertyMap instanceof ReferenceCollection && ((Element)                                  propertyMap).getUniqueName().equals(BEAN_REGISTRY))

                                 || propertyMap instanceof Reference) {

         ret = true;

      }

      if (EjbModelMetaInfo.isEjb(((Element) propertyMap).getUniqueName())) {

        ret = true;

      }

      if (EjbModelMetaInfo.isEjbMethod(((Element) propertyMap).getUniqueName())){

        ret = true;

      }

      if(EjbModelMetaInfo.isEjbField(((Element)propertyMap).getUniqueName())) {

        ret = true;

      }

    }

      return ret;

  }

}