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
- Between the class declaration and the
decorate
method, add two staticImageDescriptor
variables that contain theImageDescriptor
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 theicons
folder. - 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 theMemberInfoMap
has not been set. The following pseudocode demonstrates this:
Use the following example sample code to implement this functionality.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 } } }
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)"); } } }
- 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;
- 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, "Addcom.ibm.carma.model
to list of imported packages." - Save the source and debug any errors.