To add a context palette to a node on a scene add the DefaultContextPaletteModel to the nodes lookup. The DefaultContextPaletteModel class has an initialize method that uses the XML layer filesystem to initialize the content of the context palette. The initialize method has one parameter that is used to specify the path used to populate the palette. The following example is from the UMLClassWidget class and is used to populate the context palette for the widget. The ContextPaletteModel is then added to the nodes lookup.

private DefaultContextPaletteModel initializeContextPalette()
{
    DefaultContextPaletteModel paletteModel = new DefaultContextPaletteModel(this);
    paletteModel.initialize("UML/context-palette/Class");
    return paletteModel;
}

The layer file is used to define the items on the context palette. The contents of the folder specified in the above code snippet are files of type context_palette_item objects. The following is an example of the generalization context palette item.

<file name="Generalization.context_palette_item" url="resources/context/Generalization.context_palette_item">
   <attr name="factory" newvalue="org.netbeans.modules.uml.diagrams.edges.factories.GeneralizationFactory"/>
   <attr name="element_type" stringvalue="Generalization" />
   <attr name="default-node" stringvalue="Class" />
</file>

An interface node should not be able to generalize from a class. Instead interface must extend an interface. So, having the generalization palette item create a class when releasing the mouse over the diagram is not correct. Instead an interface must be created. To create an interface model element instead, simply change the 'default-node' property.

<file name="Generalization.context_palette_item" url="resources/context/Generalization.context_palette_item" >
   <attr name="factory" newvalue="org.netbeans.modules.uml.diagrams.edges.factories.GeneralizationFactory"/>
   <attr name="element_type" stringvalue="Generalization" />
   <attr name="default-node" stringvalue="Interface" />
</file>

The factory attribute is used to specify the RelationshipFactory that is used to create the relationship between the two nodes selected by the palette button action. The element_type specifies the model element type to create. The 'default_node' specifies the model element type that will be created when the user releases the mouse over the white space in the diagram.

The context_palette_item file is used to specify the properties of the node on the context palette. The palette item file can specify the icon, name and tooltip to use in the palette.

<?xml version="1.0" encoding="UTF-8"?>

<palette_item version="1.0">

  <icon16 urlvalue="nbres:/org/netbeans/modules/uml/resources/images/general-link.png" />
  <icon32 urlvalue="nbres:/org/netbeans/modules/uml/resources/images/general-link-32.png" />

  <description localizing-bundle="org.netbeans.modules.uml.diagrams.resources.context.Bundle"
               display-name-key="NAME_Generalization" 
               tooltip-key="HINT_Generalization" />
</palette_item>

The value of factory attribute must implement the RelationshipFactory interface. An example of a RelationshipFactory is the GeneralizationFactory.

public class GeneralizationFactory implements RelationshipFactory
{

    public GeneralizationFactory()
    {
    }

    public IRelationship create(IElement source, IElement target)
    {
        IRelationFactory factory = new org.netbeans.modules.uml.core.metamodel.infrastructure.RelationFactory();
        
        IRelationship retVal = null;
        
        if((source instanceof IClassifier) && (target instanceof IClassifier))
        {
            retVal = factory.createGeneralization((IClassifier)source, 
                                                  (IClassifier)target);
        }
        
        return retVal;
    }
}

A folder is used to group palette items together. The palettes items will be create a pop-out control in the context palette. An example of a group are associations.

<folder name="Associations" >
   <file name="Association.context_palette_item" url="resources/context/Association.context_palette_item" />
   <file name="Aggregation.context_palette_item" url="resources/context/Aggregation.context_palette_item" />
   <file name="Composition.context_palette_item" url="resources/context/Composition.context_palette_item" />
   <file name="NavigableAssociation.context_palette_item" url="resources/context/NavigableAssociation.context_palette_item" />
   <file name="NavigableAggregation.context_palette_item" url="resources/context/NavigableAggregation.context_palette_item" />
   <file name="NavigableComposition.context_palette_item" url="resources/context/NavigableComposition.context_palette_item" />
   <file name="ClassAssociation.context_palette_item" url="resources/context/ClassAssociation.context_palette_item" />
</folder>