Filling the palette dynamically [message #556651] |
Thu, 02 September 2010 08:41 |
Cedric Moonen Messages: 274 Registered: August 2009 |
Senior Member |
|
|
I am currently designing an editor in which you can create blocks with predefined input and output ports. So, I have a certain number of blocks which are all of the same type but the difference between them is their input and output ports.
I would like to be able to create the buttons in the palette at runtime: all the blocks information (name, ports, ...) would be loaded from a configuration file when the program starts. The reason why I would like something like that is my users would like to add their own blocks to the program and if they have to change only the configuration file, this becomes MUCH easier (I can even write a small program to edit the file). Otherwise, I will need to edit my model, regenerate the model code and recreate the full GMF code, which is obviously not an option.
The way to solve this would be to be able to add a button to the palette that "contains" some information about the button to create (typically just the type is enough, because all the information can be retrieved based on the name).
I've looked in the generated code but I can't see nowhere code related to the palette. Any slightest hint on how can achieve this would be welcome, because I have no clue how to do this (and I don't even know if this is possible).
|
|
|
|
Re: Filling the palette dynamically [message #556735 is a reply to message #556651] |
Thu, 02 September 2010 13:15 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
Hi, I have done something similar.
1) Using a palette provider, but this wouldn't be dynamic. (See 2)
(Note: The GMF generated code doesn't use the paletteProvider!)
You can actually create your own custom paletteProvider service.
look for extension:
org.eclipse.gmf.runtime.diagram.ui.paletterProviders
The extension point description should get you going!
2) Hooking to the generated code.
A factory is generated, you'll find it here:
xxx.part/xxx.PaletteFactory
Simply add Tool (Link, Node) entries to your palette in fillPalette()
The DiagramEditor calls this:
protected PaletteRoot createPaletteRoot(PaletteRoot existingPaletteRoot) {
PaletteRoot root = super.createPaletteRoot(existingPaletteRoot);
new xxxPaletteFactory().fillPalette(root);
return root;
}
I can imagine your app would have code here which would read your config
file and populate the palette. My approach was to generated Tool entries
from an EMF model which could be populated from the app itself. (Pretty
cool self-extending GMF editor).
Also there is a way that if you use multiple creationtool for the same
semantic object, you can act on the created object depending on the
custom Palette tool. (Of course this would assume the dynamic aspect, as
otherwise you have declared this as another element type when building
your .gmfgraph / .gmfmap / .gmftool statically)
First you would need to create a custom CreationTool which would put a
parameter in the create request.
public class CustomCreationTool extends CreationTool {
protected String node;
public CustomCreationTool(List elementTypes, String node) {
super();
this.node = node;
this.elementTypes = elementTypes;
}
protected Request createTargetRequest() {
CreateUnspecifiedTypeRequest req= new
CreateUnspecifiedTypeRequest(elementTypes, getPreferencesHint());
req.getExtendedData().put("name of tool clicked", node);
return req;
}
/**
* List of element types of which one will be created (of type
* <code>IElementType</code>).
*/
private List elementTypes;
}
Next you pick up the request in the edit helpers, by using
xxx.edit.Helpers classes. So when an object is created you could derive
which tool did it and set some features on the semantic model.
So you would override i.e. the getConfigureCommand() like below.
@Override
protected ICommand getConfigureCommand(final ConfigureRequest req) {
return new ConfigureNetworkElementCommand(req,
xxxPackage.eINSTANCE.getMyObject()) {
protected CommandResult doExecute(IProgressMonitor progressMonitor) {
Object o = req.getElementToConfigure();
return this.getCommandResult();
}
protected CommandResult doExecuteWithResult(
IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
Object o = req.getElementToConfigure();
Map params = req.getParameters();
String value = (String) params.get("name of tool clicked");
if (value != null) {
// do something with your object.
}
return this.getCommandResult();
}
};
}
On 02-09-10 10:41, Cedric wrote:
> I am currently designing an editor in which you can create blocks with
> predefined input and output ports. So, I have a certain number of blocks
> which are all of the same type but the difference between them is their
> input and output ports.
> I would like to be able to create the buttons in the palette at runtime:
> all the blocks information (name, ports, ...) would be loaded from a
> configuration file when the program starts. The reason why I would like
> something like that is my users would like to add their own blocks to
> the program and if they have to change only the configuration file, this
> becomes MUCH easier (I can even write a small program to edit the file).
> Otherwise, I will need to edit my model, regenerate the model code and
> recreate the full GMF code, which is obviously not an option.
>
> The way to solve this would be to be able to add a button to the palette
> that "contains" some information about the button to create (typically
> just the type is enough, because all the information can be retrieved
> based on the name). I've looked in the generated code but I can't see
> nowhere code related to the palette. Any slightest hint on how can
> achieve this would be welcome, because I have no clue how to do this
> (and I don't even know if this is possible).
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03171 seconds