Lesson 4: Develop the code for the Java™ Decorator class

In this lesson, you develop code for the Decorator class that handles the decorating of the CARMA resources in the CARMA Repositories view.

About this task

To add this functionality to theDecorator class:

Procedure

  1. Between the class declaration and the decorate method, add two static ImageDescriptor variables that contain the ImageDescriptor path names for the lock decorator and the question mark decorator. For example:
    private static ImageDescriptor lock; 	 	
    private static ImageDescriptor question; 	 	
    static 
    { 		
       lock = Activator.getImageDescriptor("icons/lock.jpg"); 		
       question = Activator.getImageDescriptor("icons/question_mark.jpg");
    }
    Tip: The file names you provide should correspond with the names of the icons you imported into the icons folder.
  2. Add the code to the body of the decorate() method that adds a locked suffix to the CARMA members and containers when they are locked, or decorate them with question marks if the MemberInfoMap has not been set. The following pseudocode demonstrates this:
    if( resource is CARMA Container or CARMA Member){
      if( Member Info Map Set){ 		
        if( Member Info Map Set contains the Key “locked”){ 
    		  if( value for the key “locked” is not empty string){ 				
            decorate CARMA Member/Container with lock decorator
            Add “locked” suffix to CARMA Member/Container  } 
          } else { 	
            decorate CARMA Member/Container with question decorator
        }
     	}
    }
    Use the following example sample code to implement this functionality.
    public void decorate(Object resource, IDecoration decoration) 
    {
       if(resource instanceof CARMAContainer || resource instanceof CARMAMember)
       {
          CARMAResource myResource = (CARMAResource) resource;
          if(myResource.isSetMemberInfoMap())
          {
             try
             {
                EMap myMap = myResource.getMemberInfoMap();
                if(myMap.containsKey("locked"))
                {
                   String value = myMap.get("locked").toString();
                   if( !value.equals(""))
                   {
                      decoration.addOverlay(lock);
                      if(myResource instanceof CARMAMember)
                         decoration.addSuffix(" - (Member Locked)");
                      else
                         decoration.addSuffix(" - (Container Locked)");
                   }
                }  	
             }
             catch(NotSynchronizedException e)
             {
                //TODO handle exception
             }
          } 
          else 
          {
             decoration.addOverlay(question);
             decoration.addSuffix(" - (Not Syncronized)");
          }
       }
    }
    
  3. Automatically import the classes and types.
    Ensure that the following imports are included:
    import com.ibm.carma.plugin.decorators.Activator;
    import org.eclipse.jface.resource.ImageDescriptor;
    import org.eclipse.jface.viewers.IDecoration;
    import org.eclipse.jface.viewers.ILightweightLabelDecorator;
    import org.eclipse.jface.viewers.LabelProvider;
    import com.ibm.carma.model.CARMAMember;
    import com.ibm.carma.model.CARMAContainer;
    import com.ibm.carma.model.CARMAResource;
    import com.ibm.carma.transport.NotSynchronizedException;
    import org.eclipse.emf.common.util.EMap;
    
  4. If the com.ibm.carma.model part of your import packages statement is still underlined in red, then right-click on it and select the quick fix, "Add com.ibm.carma.model to list of imported packages."
  5. Save the source and debug any errors.