The palette of a graphical editor or viewer provides tools to create connections and objects in the diagram.
There are different types of palette entries available in the palette:
This is a container which allows structuring the tools into different compartments, which can be collapsed and expanded. The tools (see below) can be added to the container by calling addToolEntry.
Figure: Compartments containing several creation tools
This tool lets the user create a connection between two objects by starting the connection on a source object dropping it on a target object. It is also possible (if enabled) to drop the connection on the canvas, in which case a new target object will be created.
You can add an arbitrary number of create connection features to this tool. If you add exactly one create connection feature, then this feature will be executed directly on drop. If you provide more than one create connection feature, a popup menu containing all features appears on drop and the user can select the one to execute.
This tool lets the user create objects in the diagram using one create feature.
A stack can contain an arbitrary number of creation tools (not directly create features) which belong semantically together. The user can choose the creation tool which is active on this stack in a drop down list.
Figure: Drop down list (on the left expanded) of the stack tool
The palette is defined in the tool behavior provider.
If you didn’t do so already you must first create a tool behavior provider and add it to the diagram type provider as described here.
There is one method of the tool behavior provider to overwrite:
The method getPalette has to return the palette entries (which implement IPaletteCompartmentEntry)
The default implementation of this method in the class DefaultToolBehaviorProvider builds a palette with two compartments. The first compartment contains a connection creation tool for each create connection feature and the second compartment contains a creation tool for each create feature you provide in your feature provider.
In this example we want create one additional compartment at below the compartments of the default implementation. This compartment contains one stack entry, which allows to choose between all create features.
You can see the complete implementation of the palette here:
@Override
public IPaletteCompartmentEntry[]
getPalette() {
List<IPaletteCompartmentEntry> ret
=
new
ArrayList<IPaletteCompartmentEntry>();
// add compartments from super class
IPaletteCompartmentEntry[] superCompartments =
super.getPalette();
for (int i = 0;
i < superCompartments.length; i++)
ret.add(superCompartments[i]);
// add new compartment at the end of the existing
compartments
PaletteCompartmentEntry compartmentEntry
=
new
PaletteCompartmentEntry("Stacked",
null);
ret.add(compartmentEntry);
// add new stack entry to new compartment
StackEntry stackEntry = new StackEntry("EObject",
"EObject", null);
compartmentEntry.addToolEntry(stackEntry);
// add all create-features to the new stack-entry
IFeatureProvider featureProvider = getFeatureProvider();
ICreateFeature[] createFeatures = featureProvider.getCreateFeatures();
for (ICreateFeature cf : createFeatures) {
ObjectCreationToolEntry objectCreationToolEntry =
new ObjectCreationToolEntry(cf.getCreateName(),
cf.getCreateDescription(), cf.getCreateImageId(),
cf.getCreateLargeImageId(), cf);
stackEntry.addCreationToolEntry(objectCreationToolEntry);
}
// add all create-connection-features to the new stack-entry
ICreateConnectionFeature[] createConnectionFeatures =
featureProvider.getCreateConnectionFeatures();
for (ICreateConnectionFeature cf : createConnectionFeatures)
{
ConnectionCreationToolEntry
connectionCreationToolEntry =
new ConnectionCreationToolEntry(cf.getCreateName(),
cf
.getCreateDescription(), cf.getCreateImageId(),
cf.getCreateLargeImageId());
connectionCreationToolEntry.addCreateConnectionFeature(cf);
stackEntry.addCreationToolEntry(connectionCreationToolEntry);
}
return ret.toArray(new
IPaletteCompartmentEntry[ret.size()]);
}
Now start the editor and verify that the palette has a new stack entry providing all objects, which can be created (same as the simple create object entries in the palette).
Use the stack entry to create an instance of the object, which is currently active in the stack entry. Afterwards change the active object of the stack entry (if several objects are available) and use the stack entry to create an instance of that object.