As explained in Chapter 6, Using yFiles FLEX with a yFiles Server, the yFiles FLEX client uses a different
concept for defining the visual properties of graph items than yFiles Java.
Therefore, the yFiles FLEX server API contains a framework for reading and writing
the yFiles FLEX GraphML format along with comptatiblity classes that model the
client's styles, labels and label models. The server API that provides this
functionality is contained in package
com.yworks.yfiles.server.graphml.flexio
and its sub-packages.
Please see the section called “Using the yFiles FLEX Native GraphML Extension with a yFiles for Java Server” for the required configuration to use the yFiles FLEX format compatibility layer on the server.
In order to read, manipulate, and write custom styles on the server, three classes are required:
In the following, examples for all of these three classes are provided. The example implementations are taken from the custom style demo application that comes with yFiles FLEX. The custom style has fill, stroke and "percentage" properties, all of which influence the way the style is rendered on the client. The following GraphML serialization format is used for the custom style:
<y:CustomStyle> <y:Percentage value="0.8" /> <y:Pen .. /> <y:Brush.. /> </y:CustomStyle>
A data object for a custom style is a very simple data class that provides getters and setters for all properties of the custom style. Instances of the data object class are created by the corresponding deserializer implementation.
Example 7.1. Custom style data object
/** * A simple data object that holds the state information of * the custom style. */ public class CustomStyle implements INodeStyle { private IStroke stroke; private IFill fill; private double percentage; public IStroke getStroke() { return stroke; } public void setStroke(IStroke stroke) { this.stroke = stroke; } public IFill getFill() { return fill; } public void setFill(IFill fill) { this.fill = fill; } public double getPercentage() { return percentage; } public void setPercentage(double percentage) { this.percentage = percentage; } }
A custom deserializer should extend AbstractDeserializer and override deserializeElementContent. It's important not to override deserialize instead, because this would prevent reference sharing from working correctly. The deserializer creates an instance of the corresponding data object and sets its properties by reading the GraphML data.
Example 7.2. Custom style deserializer
// Simple deserializer implementation that reads the custom style // and creates a corresponding CustomStyle data object that can be used to manipulate // the style properties. // // Because this deserializer extends AbstractDeserializer, we don't need // to implement canHandle. The default implementation will return true, if // the element name and namespace of an xml element match // the values returned by getElementName and getXmlNamespaceURI. // // The framework already contains deserializers for fill and strokes instances. // Therefore, we can just delegate the deserialization of the fill and stroke using // FlexIOTools.deserialize public class CustomStyleDeserializer extends AbstractDeserializer { public Object deserializeElementContent(GraphMLParseContext context, Node xmlNode) { // Create a new instance of the data object that will keep the state // of the deserialized custom style CustomStyle style = new CustomStyle(); // Deserialize the fill using // FlexIOTools.deserialize, which will delegate the deserialization to // an IDeserializer that can deserialize fills. Node fillNode = XmlSupport.getChildNode(xmlNode, Constants.Y_BRUSH, GraphMLConstants.YWORKS_EXT_NS_URI); IFill fill = (IFill) FlexIOTools.deserialize(context, fillNode); if (null != fill) { style.setFill(fill); } // Deserialize the stroke using // FlexIOTools.deserialize, which will delegate the deserialization to // an IDeserializer that can deserialize strokes Node strokeNode = XmlSupport.getChildNode(xmlNode, Constants.Y_PEN, GraphMLConstants.YWORKS_EXT_NS_URI); IStroke stroke = (IStroke) FlexIOTools.deserialize(context, strokeNode); if (null != stroke) { style.setStroke(stroke); } // Read the percentage attribute Node percentageNode = XmlSupport.getChildNode(xmlNode, "Percentage", GraphMLConstants.YWORKS_EXT_NS_URI); if (null != percentageNode) { String value = XmlSupport.getAttributeValue(percentageNode, "value"); if (null != value) { style.setPercentage(Double.parseDouble(value)); } } return style; } public String getElementName(GraphMLParseContext context) { return "CustomStyle"; } public String getXmlNamespaceURI(GraphMLParseContext context) { return GraphMLConstants.YWORKS_EXT_NS_URI; } }
All ISerializer and IDeserializer instances have to be registered with the
SerializerRegistry
that is available to the GraphML (de)serialization process.
Custom serializers and deserializers can also be added directly to the
GraphRoundtripSupport instance that is used for client-server communication.
Each time the graph is read or written with the roundtrip support, a
new SerializerRegistry is created and configured in
createSerializerRegistry()
.
The serializers and deserializers that have been added using
addSerializer(com.yworks.yfiles.server.graphml.flexio.ISerializer)
and
addDeserializer(com.yworks.yfiles.server.graphml.flexio.IDeserializer)
are added to the registry when it is created.
Example 7.3. Registering custom serializer and deserializer instances
// Register a serializer and a deserializer // for a custom style with the // this.support = new GraphRoundtripSupport(); support.addSerializer(new CustomStyleSerializer()); support.addDeserializer(new CustomStyleDeserializer());
Class FlexIOTools
provides static utility methods for working with yFiles FLEX styles and labels
on the server.
When the graph is read using a default GraphRoundtripSupport instance, the style and label data is parsed into data providers that are registered with the graph. Class FlexIOTools allows to easily retrieve and set style instances and labels without having to deal with the underlying data providers. API Excerpt 7.7, “Utility methods for accessing yFiles FLEX styles and labels” lists the methods provided by FlexIOTools for accessing styles and labels.
API Excerpt 7.7. Utility methods for accessing yFiles FLEX styles and labels
// Get a node's style static INodeStyle getStyle(Node node) // Set the style for a node instance static void setStyle(Node node,INodeStyle style) // Get the style of an edge static IEdgeStyle getStyle(Edge edge) // Set the style for an edge instance static void setStyle(Edge edge,IEdgeStyle style); // Add a label to a node static void addLabel(Node node, Label label) // Add a label to an edge static void addLabel(Edge edge, Label label) // Get all labels that belong to a node static List getLabels(Node node) // Get all labels that belong to an edge static List getLabels(Edge edge) // Get the first label of a node staic Label getFirstLabel(Node node) // Get the first label of an edge staic Label getFirstLabel(Edge edge)
Please see the section called “Working with Labels on the Server” for specifics on working with labels on the server.
FlexIOTools also offers some convenience methods that facilitate the implementation of custom serializers and deserializers. API Excerpt 7.8, “Utility methods for accessing yFiles FLEX styles and labels” lists these utility methods.
API Excerpt 7.8. Utility methods for accessing yFiles FLEX styles and labels
// Serialize the given object by delegation to an // appropriate serializer found in the context static void serialize(GraphMLWriteContext context, Object subject, XmlWriter writer) // Deserialize the given xml element by delegation to an // appropriate deserializer found in the context static Object deserialize(GraphMLParseContext context, Node xmlNode) // Returns the ARGB string (e.g. #FFCC00FF) of a // java.awt.Color instance static String getARGBString(Color c) // Parse the given argb string (e.g. #FFCC00FF) // or named color (e.g. "Lime") into a // java.awt.Color. static Color parseNamedOrARGBColor(String colorStr)
|
Copyright ©2007-2008, yWorks GmbH. All rights reserved. |