file Problem extending GenericLinkTool

2 months 2 weeks ago #6501 by bit
Hi,
I’m facing some problems extending the class GenericLinkTool.
My purpose is to create a custom AssociationEnd in the command palette. I created a class “ALTableRelationLinkTool” that extends “GenericLinkTool” and it works good with the override of acceptFirstElement and acceptSecondElement but it looks like the override of postConfigure it does not hook nothing.
I configured the tool in the module.xml in this way:
<Tool id="CreateTableRelation" label="TableRelation" tooltip="%CreateTableRelationTooltip" image="res/icons/AL_ext_logo_16.png">
                <Scope-source metaclass="ModelTree"/>
                <Scope-target metaclass="ModelTree"/>
                <Handler class="aldesigner.command.ALTableRelationLinkTool">
                    <HParameter name="name" value="TableRelation"/>
                    <HParameter name="metaclass" value="AssociationEnd"/>
                    <HParameter name="stereotype" value="TableRelation"/>
                </Handler>
            </Tool>

And this is my class:
public class ALTableRelationLinkTool extends GenericLinkTool {

	@Override
	public boolean acceptFirstElement(IDiagramHandle diagramHandle, IDiagramGraphic targetNode) {
		if (targetNode.getElement() instanceof Class)
			return ((Class) targetNode.getElement()).isStereotyped(ALModule.NAME, ALObjectType.TABLE.toString());
		else
			return false;
	}

	@Override
	public boolean acceptSecondElement(IDiagramHandle diagramHandle, IDiagramGraphic originNode,
			IDiagramGraphic targetNode) {
		if (targetNode.getElement() instanceof Class)
			return ((Class) targetNode.getElement()).isStereotyped(ALModule.NAME, ALObjectType.TABLE.toString());
		else
			return false;
	}
	
	@Override
	protected void postConfigure(IDiagramHandle diagramHandle, IDiagramGraphic originNode, IDiagramGraphic targetNode,
			ModelElement source, ModelElement target, MObject newElement, IDiagramLink newLink,
			List<IDiagramGraphic> newGraphics, LinkRouterKind routerType, ILinkRoute path) {

		MessageDialog.openError(null, "Error", "Cannot execute this line of code");			
	}	
}

Do you have any suggestion?
I did the same extending the GenericBoxTool with success. I don’t know why I have this problem with GenericLinkTool.

Thank you

Please Log in or Create an account to join the conversation.

2 months 2 weeks ago #6502 by chm
Hi bit,

Just did a test module doing the same thing as you, this seems to be working fine with Modelio 4.1.

My tool definition is slightly different:
<Tool id="CreateTableRelation" label="TableRelation" tooltip="%tool.TableRelation.tooltip" image="res/icons/AL_ext_logo_16.png">
    <Scope-source metaclass="Standard.Class" stereotype="MyModule#TABLE"/>
    <Scope-target metaclass="Standard.Class" stereotype="MyModule#TABLE"/>
    <Handler class="aldesigner.command.ALTableRelationLinkTool">
        <HParameter name="name" value="TableRelation"/>
        <HParameter name="metaclass" value="Standard.AssociationEnd"/>
        <HParameter name="stereotype" value="MyModule#TableRelation"/>
    </Handler>
</Tool>
It's usually a good practice to use qualified metaclass names, we have a few homonyms in the UML and ArchiMate metamodels. (not this metaclass though)
I included the source/target checks directly in the tool definition, which should be strictlty equivalent to what you did in Java.

Maybe something is wrong with your Stereoype definition? Mine looks like this:
<Stereotype name="TableRelation" metaclass="Standard.AssociationEnd" label="TableRelation" ...>
    <Icon path="res/icons/AL_ext_logo_16.png"/>
</Stereotype>

My java tool does absolutly nothing, the GenericLinkTool's behavior looks OK to me:
public class ALTableRelationLinkToolextends GenericLinkTool {
    @Override
    public boolean acceptFirstElement(final IDiagramHandle diagramHandle, final IDiagramGraphic targetNode) {
        return super.acceptFirstElement(diagramHandle, targetNode);
    }

    @Override
    public boolean acceptSecondElement(final IDiagramHandle diagramHandle, final IDiagramGraphic originNode, final IDiagramGraphic targetNode) {
        return super.acceptSecondElement(diagramHandle, originNode, targetNode);
    }

    @Override
    public void actionPerformed(final IDiagramHandle diagramHandle, final IDiagramGraphic originNode, final IDiagramGraphic targetNode, final LinkRouterKind routerType, final ILinkPath path) {
        super.actionPerformed(diagramHandle, originNode, targetNode, routerType, path);
    }

    @Override
    protected void postConfigure(IDiagramHandle diagramHandle, IDiagramGraphic originNode, IDiagramGraphic targetNode, ModelElement source, ModelElement target, MObject newElement, IDiagramLink newLink, List<IDiagramGraphic> newGraphics, LinkRouterKind routerType, ILinkPath path) {
        super.postConfigure(diagramHandle, originNode, targetNode, source, target, newElement, newLink, newGraphics, routerType, path);
    }

}

For reference, you can see the code of GenericLinkTool on GitHub .

Hope this helps!

Please Log in or Create an account to join the conversation.

2 months 2 weeks ago #6503 by bit
Hi chm,
thanks for the valuable advice. I changed the definition of the tools and saved myself a lines of code.

I found the problem due to a very serious error of mine. When I created the new project I downloaded the Modelio api in version 5.1 and not 4.1. I didn't notice it because for now everything was working correctly. It made me reflect the signature of the postConfigure method which has an ILinkPath interface as its last parameter while on version 5.1 it is an ILinkRoute interface.

Thanks for the support!

Please Log in or Create an account to join the conversation.

Moderators: tmachmaab
Time to create page: 0.037 seconds
^ Back to Top