|
Re: Generating palette programmatically [message #733524 is a reply to message #731284] |
Tue, 04 October 2011 23:40 |
Michael Golubev Messages: 383 Registered: July 2009 |
Senior Member |
|
|
Hello,
I was sure Papyrus already has code to modify palette when the profile is defined -- for every stereotype you should get the additional tool that creates the instance with this stereotype applied.
I wrote something very similar 4 years ago but can't find it now.
Anyway, I would have it implemented by contributing the custom IPaletteProvider, as explained at the tutorial here.
This new provider in the contributeToPalette method would check whether it should add custom content by checking your specific properties of passed 'editor' and 'content' parameters.
I never used the PaletteToolEntry and subclasses before, so here is how I would do that without them:
//in the MyPaletteProvider class:
private ToolEntry createSubmachineState3CreationTool() {
Stereotype someStereo = myFindSomeStereotypeToApply();
List<IElementType> types = myComputeListOfApplicableElementTypes();
MyToolEntry entry = new MyToolEntry(types, someStereo);
entry.setId(...);
entry.setSmallIcon(...);
entry.setLargeIcon(...);
entry.setToolClass(CreateSubmachineStateTool.class);
return entry;
}
private static class MyToolEntry extends ToolEntry {
private final List<IElementType> myElementTypes;
private final Stereotype myStereo;
public MyToolEntry(List<IElementType> elementTypes, Stereotype stereo) {
super("Label", "Desc", null, null);
myElementTypes = elementTypes;
myStereo = stereo;
}
@Override
public Tool createTool() {
return new MyWithStereotypeTool(myElementTypes, myStereo);
}
}
private static class MyWithStereotypeTool extends UnspecifiedTypeCreationTool {
public static final String PROP_NAME = "StereotypeToAdd";
private final Stereotype myUmlStereo;
public MyWithStereotypeTool(List<IElementType> elementTypes, Stereotype umlStereo) {
super(elementTypes);
myUmlStereo = umlStereo;
}
@Override
protected Request createTargetRequest() {
CreateUnspecifiedTypeRequest request = (CreateUnspecifiedTypeRequest) super.createTargetRequest();
//below is the only way to propagate extended data into IEditCommandRequest#parameters
HashMap extendedData = new HashMap();
extendedData.putAll(request.getExtendedData());
extendedData.put(PROP_NAME, myUmlStereo);
request.setExtendedData(extendedData);
return request;
}
}
The code to handle the request from new custom tool looks like this:
//somewhere in XXXElementHelper
protected ICommand getConfigureCommand(final ConfigureRequest req) {
if (req.getParameter(MyWithStereotypeTool.PROP_NAME) instanceof Stereotype) {
return new AbstractTransactionalCommand(req.getEditingDomain(), "Applying the stereo", null) {
@Override
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
Stereotype applyMe = (Stereotype)req.getParameter(MyWithStereotypeTool.PROP_NAME);
customApplyUMLStereotype(req.getElementToConfigure(), applyMe);
return CommandResult.newOKCommandResult();
}
};
}
return null;
}
Also there should be a code in the editor that clears the old palete and creates a new one like this;
//in the diagram editor:
public void refreshPalette() {
PaletteRoot paletteRoot = getEditDomain().getPaletteViewer().getPaletteRoot();
cleanPaletteRoot(paletteRoot);
createPaletteRoot(paletteRoot);
}
private void cleanPaletteRoot(PaletteRoot paletteRoot) {
List<Object> entries = new ArrayList<Object>();
entries.addAll(paletteRoot.getChildren());
for (Object entry : entries) {
PaletteEntry paletteEntry = (PaletteEntry) entry;
// we don't repaint standard palette group
if (PaletteService.GROUP_STANDARD.equals(paletteEntry.getId())) {
continue;
}
paletteRoot.remove(paletteEntry);
}
}
and finally, somewhere in the place where you know that the palette need to be changed (e.g, inthe Profile application action):
XXXDiagramEditor editop = reachTheEditor(...);
editor.refreshPalette();
Hope it helps,
Regards,
Michael "Borlander" Golubev
Eclipse Committer (GMF, UML2Tools)
at Montages Think Tank, Prague, Czech Republic
Montages AG, Zürich, Switzerland
|
|
|
|
Powered by
FUDForum. Page generated in 0.02769 seconds