| Advanced Features > Link Shape Policies > Defining Your Own Link Shape Policy > Example of a User-Defined Link Shape Policy |
Example of a User-Defined Link Shape Policy |
INDEX
PREVIOUS
NEXT
|
This section uses an example to show how you can implement your own link shape policy. You will create a link shape policy that allows links to have 0, 1 or 2 bend points. With 1 or 2 bends, the link will have an orthogonal shape. The link shape policy will prohibit the creation of more than 2 bends.
In principle, this link shape policy is a combination of the functionality of IlvOneLinkImage and IlvDoubleLinkImage (see Basic Grapher Link Class of The Essential JViews Framework). However, using a link shape policy is more flexible, because the link shape policy can be enabled and disabled on the link, and can change from a 1-bend image to a 2-bend image.
The class IlvAbstractLinkShapePolicy is a suitable base class for our new link shape policy because it defines all the methods of the interface IlvLinkShapePolicy as empty methods.
Hence, you can concentrate on the few methods that you need to override:
public class MyLinkShapePolicy extends IlvAbstractLinkShapePolicy { ... } |
verifyLinkPoints inside these corresponding callback methods of the link shape policy.
afterAdd is called when the link is already in the grapher. Since the link shape policy will reshape the link and might change the bounding box of the link, it has to be performed via the method applyToObject:applyToObject, therefore you do not need to use this method again inside the link shape policy. Hence, the code of the method onInstall is simpler:public void onInstall(IlvLinkImage link) { verifyLinkPoints(link); super.onInstall(link); } |
public boolean allowInsertPoint(IlvLinkImage link, int index, float x, float y, IlvTransformer t) { int n = link.getPointsCardinal(); if (n >= 4) return false; return true; } |
afterInsertPoint, afterRemovePoint, and afterSetIntermediateLinkPoints to call verifyLinkPoints. Note that all these methods are always called inside the method applyToObject. Thus, the code can omit an additional call to applyToObject similar to onInstall:public void afterApplyTransform(IlvLinkImage link, IlvTransformer t) { verifyLinkPoints(link); super.afterApplyTransform(link, t); } |
public boolean allowMovePoint(IlvLinkImage link, int index, float x, float y, IlvTransformer t) { return false; } |
The callback method afterMovePoint of the link shape policy could be used to adjust the link shape after moving points. However, since this link shape policy does not allow point movement, the method afterMovePoint need not be overridden because point movements will never be executed.
The new link shape policy works for all subclasses of IlvPolicyAwareLinkImage. It can be set to a link in the following way:
link.setLinkShapePolicy(new MyLinkShapePolicy());
| Note |
For simplicity, this example class MyLinkShapePolicy is designed for zoomable, rectangular end nodes that have no link connector or a free link connector (IlvFreeLinkConnector). If this is not the case, the method verifyLinkPoints needs to be adjusted to analyze the current transformer, the shape of the end nodes, and the link connector.
|
| Copyright © 1987-2007 ILOG S.A. All rights reserved. Documentation homepage. Legal terms. | PREVIOUS NEXT |