Lesson 1: Create the CustomContextProvider class

This lesson builds on top of the plug-in project you created in Exercise 5, and modified in Exercise 6. In this lesson, you create the CustomContextProvider class, which can be customized to fit any viewer you want. For example, if you want the field viewer, the context provider can extend the CarmaFieldsContentProvider.

About this task

To create the CARMAContextProvider class:

Procedure

  1. Make sure that you are in the Plug-in Development perspective. In the Package Explorer view, expand the com.ibm.carma.plugin.view plug-in project that you used for Exercises 5 and 6.
  2. Right click the view package containing the CARMADeveloperView and CustomLabelProvider classes, and select New > Class.
  3. In the New Java Class dialog box that opens, enter CustomContextProvider in the Name text field.
  4. Click the Browse button to the right of the Superclass text field. In the Superclass Selection dialog box that opens, enter the filter text, CARMATreeContentProvider. Select the matching class, and click OK.
  5. Mark the checkbox, Constructors from superclass, and click Finish. The New Java Class dialog closes and the CustomContentProvider class is created.
  6. Start by ensuring the following imports are included in the Java™ class. Add any that are missing.
    import java.util.Vector;
    
    import com.ibm.carma.model.RepositoryInstance;
    import com.ibm.carma.ui.view.CarmaTreeContentProvider;
  7. You want to modify the getChildren method to change the content that is provided to the viewer. This method is where the provider can control which items are sent to the viewer when expanding the RAM. For this tutorial, you implement the getChildren method to return only repository instances that have a CARMA token in the name and are not a listing, object, or load repository instance.

    The following pseudocode demonstrates what the getChildren method should do:

    get the children of the object that would normally be returned;
    for each child
    {
       if(the child is a repository instance)
       {
          if(the repository instance has a CARMA token and is not a listing, object, or load repository instance)
              add the child to the list of displayable children;
       }
    }
    Use the following is sample code for the getChildren method:
    public Object[] getChildren(Object parent)
    {
       Object[] children = super.getChildren(parent);
    
       //Do not parse non-existant children
       if(children == null)
       {
          return children;
       }
    
       Vector<Object> displayChildren = new Vector<Object>();
       for(int i = 0; i < children.length; i++)
       {
          if(children[i] instanceof RepositoryInstance)
          {
             RepositoryInstance myContainer = (RepositoryInstance) children[i];
             if (myContainer.getName().contains("CARMA"))
             {
                displayChildren.add(children[i]);
             }
          }
          else
          {
             displayChildren.add(children[i]);
          }
       }
       return displayChildren.toArray();
    }
  8. Save the source and debug any errors.