Customizing Icons

The DevOps Modeling Platform uses icons as visual cue to help users recognize model elements in multiple user interface components. The Project Explorer, the Inheritance Explorer, the Select Element Dialog, the Properties View, Popup Bars, Context Menus, the Palette and diagram shapes are examples of such components.

There is no uniform way of providing icons for all of these user interface components.

User Interface Component Icon Contribution
Context Menus icon attribute of the org.eclipse.ui.popupMenus extension-point
Diagram Shapes
  • General Shapes - Implementation referenced by the class attribute of the org.eclipse.gmf.runtime.diagram.core.viewProviders extension-point.
  • Compartment stereotype icons - Icon associated with the stereotype using the profile editor.
  • Decorators - Implementation referenced by the class attribute of the org.eclipse.gmf.runtime.diagram.ui.decoratorProviders extension-point.
Inheritance Explorer Delegates to the IconService.
Menu bar icon attribute of the org.eclipse.ui.actionSet extension-point
Palette small_icon and large_icon attributes of the org.eclipse.ui.popupMenus extension-point
Popup Bars Delegates to the IconService.
Project Explorer Delegates to the IconService.
Properties View Delegates to the IconService.
Select Element Dialog Implementation referenced by the class attribute of the org.eclipse.gmf.runtime.common.ui.services.elementSelectionProviders extension-point. Note that the provider for UML elements delegates this responsibility to the IconService.

The following sub-sections present how to contribute icons using the Icon Service and the Profile editor.

Icon Service

The IconService provides a centralized means to obtain an icon for a specific element by consulting all icon providers registered with the service. It is used in label providers, wrap label figures, and other areas where an icon is to be retrieved for a given object.

To register an icon provider extend the org.eclipse.gmf.runtime.common.ui.services.iconProviders extension-point.

   <extension
         point="org.eclipse.gmf.runtime.common.ui.services.iconProviders">
      <IconProvider class="com.ibm.examples.providers.MyIconProvider">
         <Priority name="Medium"/>
         <object
               class="org.eclipse.uml2.uml.Class(org.eclipse.uml2.uml)"
               id="MyElement">
         </object>
         <context elements="MyElement"/>
      </IconProvider>
   </extension>

The MyIconProvider class should provide for the same, or stricter, criteria which was used in the xml extension.

public class MyIconProvider extends AbstractProvider implements IIconProvider {

    private static Image myImage = ImageDescriptor.createFromURL(
        "file://myIcon.gif").createImage();
    
    public Image getIcon(IAdaptable hint, int flags) {
        return myIcon;
    }

    public boolean provides(IOperation operation) {
        if (operation instanceof IconOperation) {
            IconOperation iconOperation = (IconOperation)operation;
            
            IAdaptable adapter = iconOperation.getHint();
            
            if (adapter == null){
                return false;
            }
            
            Class myElement = (Class)adapter.getAdapter(Class.class);
            if (myElement != null) {
                return true;
            }
        }
        return false;
    }
}

To query the IconService to retrieve an icon, create an IAdaptable for the element you wish to get an icon for, and pass it to the getIcon(IAdaptable hint) convenience method.

    final Class myElement = UMLFactory.eINSTANCE.createClass();
    IAdaptable adaptable = new IAdaptable() {
    
        public Object getAdapter(Class adapter) {
            if (adapter.isInstance(myElement)) {
                return myElement;
            }
            return null;
        }
    };
    Image myImage = IconService.getInstance().getIcon(adaptable);

If the element is an instance of EObject (in this example Class is an instance of EObject), then use GMF's EObjectAdapter class to create the adaptable.

    final Class myElement = UMLFactory.eINSTANCE.createClass();
    EObjectAdapter adaptable = new EObjectAdapter(myElement);
    Image myImage = IconService.getInstance().getIcon(adaptable);

The getIcon(IAdaptable hint) method is a convenience method which delegates to getIcon(IAdaptable hint, int flags) with no flags set.

The getIcon(IAdaptable hint, int flags) method allows for specifying flag values. The flags can be any arbitrary int value understood by your icon provider. org.eclipse.gmf.runtime.common.ui.services.icon.IconOptions provides 4 pre-defined flags:

Customizing Icons for Stereotypes

Providing icons for stereotypes is accomplished through the Profile editor. Select the Stereotype for which you want to change the icon for and navigate to the properties view. In the general tab, browse for a new icon. If no icon has yet been assigned, "Not defined" will appear in the text box to the right of the "Icon:" label. Once an icon has been assigned, the text box will display "Defined".

Once the Stereotype is applied to the UML element, the icon will change to that which was specified in the profile.


Legal notices