Community
Participate
Working Groups
Add support for custom graph items so users can use their own figures a nodes and edges.
*** Bug 214990 has been marked as a duplicate of this bug. ***
*** Bug 214991 has been marked as a duplicate of this bug. ***
I have this same need. For example, I would like to create an oval figure (instead of a rounded rectangle) but still use the capabilities of GraphNode and GraphLabel; I don't want to re-implement everything those classes do so well. Unfortunately, GraphLabel and CachedLabel are internal, so I can't subclass them. Nor can I copy them into my source and subclass them because GraphNode#updateFigureForModel() returns if its figure isn't an instance of GraphLabel. initFigure() and createFigureForModel() are protected so they can't be overridden. If there's an easy way of implementing custom shapes, I can't find it, so please educate us.
*** Bug 214989 has been marked as a duplicate of this bug. ***
*** Bug 214988 has been marked as a duplicate of this bug. ***
Looking forward to seeing custom shapes in galileo. Good news, Ian.
Created attachment 115690 [details] AbstractStructuredGraphViewer: 6 methods private -> protected
I have a work around for this bug. I added the protected keyword to the following methods in org.eclipse.zest.core.viewers.internal.AbstractStructuredGraphViewer: #getNodesMap() #getConnectionsMap() #getGraphModelNode(Object obj) #getGraphModelConnection(Object obj) #addGraphModelNode(Object) #addGraphModelConnection(Object element, GraphNode source, GraphNode target) I then extend org.eclipse.zest.core.viewers.GraphViewer class and override adding of Nodes and Connections to the graph using the now exposed methods: @Override protected GraphNode addGraphModelNode(Object element) { GraphNode node = this.getGraphModelNode(element); if (node == null) { node = nodeDecorator.createGraphNode((Graph) getControl(), element); getNodesMap().put(element, node); } return node; } @Override protected GraphConnection addGraphModelConnection(Object element, GraphNode source, GraphNode target) { GraphConnection connection = this.getGraphModelConnection(element); if (connection == null) { connection = arcDecorator.createGraphConnection((Graph) getControl(), element, source, target); getConnectionsMap().put(element, connection); } return connection; } Class nodeDecorator creates a CustomGraphNode by extending GraphNode (as in the UMLExample by Ian Bull). The arcDecorator class creates a GraphConnection and has logic to create specific PolygonDecoration based on the arc type received.
Hey all- Here's another workaround that doesn't require rebuilding Zest. The strategy is to draw the figure to an image and then return that. Bonus is that you might actually see some performance improvements in case where the figures are complex and not mutable. Note that this version does assume that the actual figure is immutable -- that's a big assumption but if you want it updated every time just get rid of all of the image caching stuff. The big bummer with this approach is that there is no way that I can tell to get rid of the actual border, so your figure will appear inside of the regular node. I tried to play with colors to make the container gray, but that didn't work because of the auto highlighting within GraphNode. Obviously you want to turn off text etc.. -Miles public class MyStyleProvider implements IConnectionStyleProvider, IEntityStyleProvider, ILabelProvider { public IFigure createFigure(Object object) { //create any figure here -- note that layout etc will have to be performed manually //Its fine to return null when a figure isn't appropriate } Map<Object, Image> imageForObject = new HashMap<Object, Image>(); public Image getImage(Object object) { Image image = imageForObject.get(object); if (image == null) { IFigure figure = createFigure(object); if (figure != null) { image = new Image(PlatformUI.getWorkbench().getDisplay(), figure.getSize().width, figure.getSize().height); GC imageGC = new GC(image); SWTGraphics imageGraphics = new SWTGraphics(imageGC); figure.paint(imageGraphics); imageGraphics.drawImage(image, 0, 0); imageForObject.put(object, image); } } return image; } public String getText(Object entity) { return null; } //setup colors }
This bug is about adding Custom Graph Items (like CGraphNode). Bug 154579 is about adding this to the viewer. I have a basic version of both of these working. On the Graph API side I have added CGraphNode and then I added a IFigureProvider which can be used (as a lable provider) to provide a custom figure for your graph.
I have added the custom graph nodes as part of Bug 154579. See CGraphNode to add a custom figure.