Dynamic command handlers for diagram-selections [message #1804784] |
Mon, 01 April 2019 04:54  |
Eclipse User |
|
|
|
Hi,
I think this is a common issue, so not sure if Sirius has already a solution:
I have an org.eclipse.core.command that's enabled on a given domain element type. I want to be able to execute the command also when a diagram element that maps to that type is selected.
The current way is that handlers are just coupled to sirius:
1. their enablement must work for diagram elements with the corredt target type
2. the handler code itself must first translate the diagram selection to the domain selection
Whenever I face this, I hate it, and wonder if there's a better way, and maybe someone tried already? Maybe this could work:
If I have a list of command ids, I could dynamically register a handler for these commands that
1. is active, by checking if another handler for that command is active while replacing the selection of diagram elements with the selection of domain elements.
2. executes that other handler, again, replacing the selection
Yes, I would need to know the commands that should be treated that way, e.g. via extension, but still better than having to deal with 2 kinds of selections all the time no?
|
|
|
|
|
Re: Dynamic command handlers for diagram-selections [message #1804855 is a reply to message #1804845] |
Tue, 02 April 2019 07:37  |
Eclipse User |
|
|
|
Hi, yes, I will just add an adapter factory for the type IDiagramElementEditPart to the root type of the domain model type hierarchy and use 'adapt' in the handler expressions. The handler code must adapt the selection again but I can live with that.
public class IDiagramElementEditPartAdapterFactory implements IAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if (adapterType == MyDomainElement.class) {
Object model = ((IDiagramElementEditPart) adaptableObject).getModel();
if (model instanceof View) {
Object element = ((View)model).getElement();
if (element instanceof DSemanticDecorator && adapterType.isInstance(((DSemanticDecorator)element).getTarget())) {
return (T) ((DSemanticDecorator)element).getTarget();
}
}
}
return null;
}
@Override
public Class<?>[] getAdapterList() {
return new Class[] {
MyDomainElement.class
};
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.35397 seconds