Some operations require more sophisticated controls than the binary permission flags discussed in the previous section. When the user tries to draw a new link or reconnect an existing link, your application may want to restrict which links may be made, depending on the data. When the user tries to add a node to a group, your application may want to control whether it is permitted for that particular node in that particular group. When the user edits some text, your application may want to limit the kinds of strings that they enter.
Although not exactly "validation", you can also limit how users drag (move or copy) parts by setting several properties on Part and customizing the DraggingTool.
There are a number of GraphObject properties that let you control what links the user may draw or reconnect. These properties apply to each port element and affect the links that may connect with that port.
The primary properties are GraphObject.FromLinkable and GraphObject.ToLinkable. If you do not have a Node containing an element with FromLinkable = true and another node with ToLinkable = true, the user will not be able to draw a new link between the nodes.
diagram.NodeTemplate =
new Node("Auto")
.Bind("Location", "Loc", Point.Parse)
.Add(
new Shape("Ellipse") {
Fill = "green", PortId = "", Cursor = "pointer"
}
.Bind("FromLinkable", "From")
.Bind("ToLinkable", "To"),
new TextBlock { Stroke = "white", Margin = 3 }
.Bind("Text", "Key")
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "From1", Loc = "0 0", From = true },
new NodeData { Key = "From2", Loc = "0 100", From = true },
new NodeData { Key = "To1", Loc = "150 0", To = true },
new NodeData { Key = "To2", Loc = "150 100", To = true }
},
LinkDataSource = new List<LinkData> {
// initially no links
}
};
Note how the only permitted links are those going from a "From" node to a "To" node. This is true even if you start the linking gesture on a "To" node.
Because the TextBlock in the above example is not declared to be a port (i.e. there is no value for GraphObject.PortId), mouse events on the TextBlock do not start the LinkingTool. This allows users the ability to select and move the node as well as any number of other operations.
You can certainly declare a Panel to have GraphObject.FromLinkable or GraphObject.ToLinkable be true. This will cause all elements inside that panel to behave as part of the port, including starting a linking operation. Sometimes you will want to make the whole Node linkable. If you still want the user to be able to select and drag the node, you will need to make some easy-to-click elements not-"linkable" within the node. You can do that by explicitly setting GraphObject.FromLinkable and/or GraphObject.ToLinkable to false. The default value for those two properties is null, which means the "linkable"-ness is inherited from the containing panel.
Just because you have set GraphObject.FromLinkable and GraphObject.ToLinkable to true on the desired port objects does not mean that you want to allow users to create a link from every such port/node to every other port/node. There are other GraphObject properties governing linkability for both the "from" and the "to" ends.
One restriction that you may have noticed before is that the user cannot draw a second link between the same pair of nodes in the same direction. This example sets GraphObject.FromLinkableDuplicates or GraphObject.ToLinkableDuplicates to true, in order to permit such duplicate links between nodes.
diagram.NodeTemplate =
new Node("Auto")
.Bind("Location", "Loc", Point.Parse)
.Add(
new Shape("Ellipse") {
Fill = "green", PortId = "", Cursor = "pointer",
FromLinkableDuplicates = true, ToLinkableDuplicates = true
}
.Bind("FromLinkable", "From")
.Bind("ToLinkable", "To"),
new TextBlock { Stroke = "white", Margin = 3 }
.Bind("Text", "Key")
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "From1", Loc = "0 0", From = true },
new NodeData { Key = "From2", Loc = "0 100", From = true },
new NodeData { Key = "To1", Loc = "150 0", To = true },
new NodeData { Key = "To2", Loc = "150 100", To = true }
},
LinkDataSource = new List<LinkData> {
// initially no links
}
};
Drawing multiple links between "From1" and "To1", you can see how the links are automatically spread apart. Dragging one of the nodes, you can see what happens with the link routing. A similar effect occurs also when the link's Link.Curve is LinkCurve.Bezier.
Another standard restriction is that the user cannot draw a link from a node to itself. Again it is easy to remove that restriction: just set GraphObject.FromLinkableSelfNode and GraphObject.ToLinkableSelfNode to true. Note though that each node has to be both GraphObject.FromLinkable and GraphObject.ToLinkable.
diagram.NodeTemplate =
new Node("Auto")
.Bind("Location", "Loc", Point.Parse)
.Add(
new Shape("Ellipse") {
Fill = "green", PortId = "", Cursor = "pointer",
FromLinkable = true, ToLinkable = true,
FromLinkableDuplicates = true, ToLinkableDuplicates = true,
FromLinkableSelfNode = true, ToLinkableSelfNode = true
},
new TextBlock { Stroke = "white", Margin = 3 }
.Bind("Text", "Key")
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "Node1", Loc = "0 0" },
new NodeData { Key = "Node2", Loc = "150 50" }
},
LinkDataSource = new List<LinkData> {
// initially no links
}
};
To draw a reflexive link, start drawing a new link but stay near the node when you release the mouse button. This example also sets the "Duplicates" properties to true, so that you can draw multiple reflexive links.
In these examples there is only one port per node. When there are multiple ports in a node, the restrictions actually apply per port, not per node. But the restrictions of the "LinkableSelfNode" properties do span the whole node, so they must be applied to both ports within a node for a link to connect to its own node.
The final linking restriction properties control how many links may connect to a node/port. This example sets the GraphObject.ToMaxLinks property to 2, even though GraphObject.ToLinkableDuplicates is true, to limit how many links may go into "to" nodes.
diagram.NodeTemplate =
new Node("Auto")
.Bind("Location", "Loc", Point.Parse)
.Add(
new Shape("Ellipse") {
Fill = "green", PortId = "", Cursor = "pointer",
FromLinkableDuplicates = true, ToLinkableDuplicates = true,
ToMaxLinks = 2 // at most TWO links can come into this node
}
.Bind("FromLinkable", "From")
.Bind("ToLinkable", "To"),
new TextBlock { Stroke = "white", Margin = 3 }
.Bind("Text", "Key")
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "From1", Loc = "0 0", From = true },
new NodeData { Key = "From2", Loc = "0 100", From = true },
new NodeData { Key = "To1", Loc = "150 0", To = true },
new NodeData { Key = "To2", Loc = "150 100", To = true }
},
LinkDataSource = new List<LinkData> {
// initially no links
}
};
This example has no limit on the number of links that may come out of "from" nodes.
If this property is set, it is most commonly set to one. Of course it depends on the nature of the application.
Note that the GraphObject.ToMaxLinks and GraphObject.FromMaxLinks properties are independent of each other. If you want to control the total number of links connecting with a port, not only "to" or "from" but both directions, then you cannot use those two properties and instead must implement your own link validation predicate, as discussed below.
If you want to make sure that the graph structure that your users create never have any cycles of links, or that the graph is always tree-structured, GoDiagram makes that easy to enforce. Just set Diagram.ValidCycle to CycleMode.NotDirected or CycleMode.DestinationTree. The default value is CycleMode.All, which imposes no restrictions -- all kinds of link cycles are allowed.
This example has nodes that allow links both to and from each node. However the assignment of Diagram.ValidCycle will prevent the user from drawing a second incoming link to any node and also ensures that the user draw no cycles in the graph.
diagram.NodeTemplate =
new Node("Auto")
.Add(
new Shape("Ellipse") {
Fill = "green", PortId = "", Cursor = "pointer",
FromLinkable = true, ToLinkable = true
},
new TextBlock { Stroke = "white", Margin = 3 }
.Bind("Text", "Key")
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "Node1" }, new NodeData { Key = "Node2" },
new NodeData { Key = "Node3" }, new NodeData { Key = "Node4" },
new NodeData { Key = "Node5" }, new NodeData { Key = "Node6" },
new NodeData { Key = "Node7" }, new NodeData { Key = "Node8" },
new NodeData { Key = "Node9" }
},
LinkDataSource = new List<LinkData> {
// initially no links
}
};
// only allow links that maintain tree-structure
diagram.ValidCycle = CycleMode.DestinationTree;
As you draw more links you can see how the set of potential linking destinations keeps getting smaller.
It may be the case that the semantics of your application will cause the set of valid link destinations to depend on the node data (i.e. at the node and port at which the link started from and at the possible destination node/port) in a manner that can only be implemented using code: a predicate function.
You can implement such domain-specific validation by setting LinkingBaseTool.LinkValidation or Node.LinkValidation. These predicates, if supplied, are called for each pair of ports that the linking tool considers. If the predicate returns false, the link may not be made. Setting the property on the LinkingTool or RelinkingToolcauses the predicate to be applied to all linking operations, whereas setting the property on the Node only applies to linking operations involving that node. The predicates are called only if all of the standard link checks pass, based on the properties discussed above.
In this example there are nodes of three different colors.
The LinkingTool and RelinkingTool are customized to use a function, sameColor
,
to make sure the links only connect nodes of the same color.
Mouse-down and drag on the ellipses (where the cursor changes to a "pointer") to start drawing a new link.
You will see that the only permitted link destinations are nodes of the same color that do not already have a link to it from the same node.
diagram.NodeTemplate =
new Node("Auto")
.Add(
new Shape("Ellipse") {
PortId = "", Cursor = "pointer",
FromLinkable = true, ToLinkable = true
}
.Bind("Fill", "Color"),
new TextBlock { Stroke = "white", Margin = 3 }
.Bind("Text", "Key")
);
diagram.LinkTemplate =
new Link {
Curve = LinkCurve.Bezier, RelinkableFrom = true, RelinkableTo = true
}
.Add(
new Shape { StrokeWidth = 2 }
.Bind(new Binding("Stroke", "FromNode", n => ((n as Node).Data as NodeData).Color).OfElement()),
new Shape { ToArrow = "Standard", Stroke = null }
.Bind(new Binding("Fill", "FromNode", n => ((n as Node).Data as NodeData).Color).OfElement())
);
static bool sameColor(Node fromnode, GraphObject fromport, Node tonode, GraphObject toport, Link l) {
return (fromnode.Data as NodeData).Color == (tonode.Data as NodeData).Color;
// this could look at the fromport.Fill and toport.Fill instead,
// assuming that the ports are Shapes, which they are because PortId was set on them,
// and that there is a data Binding on the Shape.Fill
}
// only allow new links between ports of the same color
diagram.ToolManager.LinkingTool.LinkValidation = sameColor;
// only allow reconnecting an existing link to a port of the same color
diagram.ToolManager.RelinkingTool.LinkValidation = sameColor;
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "Red1", Color = "red" },
new NodeData { Key = "Blue1", Color = "blue" },
new NodeData { Key = "Green1", Color = "green" },
new NodeData { Key = "Green2", Color = "green" },
new NodeData { Key = "Red2", Color = "red" },
new NodeData { Key = "Blue2", Color = "blue" },
new NodeData { Key = "Red3", Color = "red" },
new NodeData { Key = "Green3", Color = "green" },
new NodeData { Key = "Blue3", Color = "blue" }
},
LinkDataSource = new List<LinkData> {
// initially no links
}
};
To emphasize the color restriction, links have their colors bound to the "From" node data.
One can limit the number of links coming into a port by setting GraphObject.ToMaxLinks. Similarly, one can limit the number of links coming out of a port by setting GraphObject.FromMaxLinks. But what if you want to limit the total number of links connecting with a port regardless of whether they are coming into or going out of a port? Such constraints can only be implemented by a link validation predicate.
When wanting to limit the total number of links in either direction, connecting with each port, one can use this Node.LinkValidation predicate:
new Node() {
...,
LinkValidation = (fromnode, fromport, tonode, toport, link) => {
// total number of links connecting with a port is limited to 1:
return fromnode.FindLinksConnected(fromport.PortId).Count() +
tonode.FindLinksConnected(toport.PortId).Count() < 1;
}
}
When wanting to limit the total number of links in either direction, connecting with a node for all of its ports, one can use this Node.LinkValidation predicate:
new Node() {
...,
LinkValidation = (fromnode, fromport, tonode, toport, link) => {
// total number of links connecting with all ports of a node is limited to 1:
return fromnode.LinksConnected.Count() + tonode.LinksConnected.Count() < 1;
}
}
When you want to limit the kinds of nodes that the user may add to a particular group, you can implement a predicate as the CommandHandler.MemberValidation or Group.MemberValidation property. Setting the property on the CommandHandler causes the predicate to be applied to all Groups, whereas setting the property on the Group only applies to that group.
In this example the samePrefix
predicate is used to determine if a Node
may be dropped into a Group.
Try dragging the simple textual nodes on the left side into either of the groups on the right side.
Only when dropping the node onto a group that is highlit "green" will the node be added as a member of the group.
You can verify that by moving the group to see if the textual node moves too.
// this predicate is true if both node data keys start with the same letter
static bool samePrefix(Group group, Part node) {
if (group == null) return true; // when maybe dropping a node in the background
if (node is Group) return false; // don't add Groups to Groups
return ((string)group.Key).ElementAt(0) == ((string)node.Key).ElementAt(0);
}
diagram.NodeTemplate =
new Node()
.Bind("Location", "Loc", Point.Parse)
.Add(new TextBlock().Bind("Text", "Key"));
diagram.GroupTemplate =
new Group("Vertical") {
// only allow those simple nodes that have the same data key prefix:
MemberValidation = samePrefix,
// don't need to define handlers on member Nodes and Links
HandlesDragDropForMembers = true,
// support highlighting of Groups when allowing a drop to add a member
MouseDragEnter = (e, obj, prev) => {
var grp = obj as Group;
// this will call samePrefix; it is true if any node has the same key prefix
if (grp.CanAddMembers(grp.Diagram.Selection)) {
if (grp.FindElement("SHAPE") is Shape shape) shape.Fill = "green";
grp.Diagram.CurrentCursor = "";
} else {
grp.Diagram.CurrentCursor = "not-allowed";
}
},
MouseDragLeave = (e, obj, next) => {
var grp = obj as Group;
if (grp.FindElement("SHAPE") is Shape shape) shape.Fill = "rgba(128,128,128,0.33)";
grp.Diagram.CurrentCursor = "";
},
// actually add permitted new members when a drop occurs
MouseDrop = (e, obj) => {
var grp = obj as Group;
if (grp.CanAddMembers(grp.Diagram.Selection)) {
// this will only add nodes with the same key prefix
grp.AddMembers(grp.Diagram.Selection, true);
} else { // and otherwise cancel the drop
grp.Diagram.CurrentTool.DoCancel();
}
},
// make sure all Groups are behind all regular Nodes
LayerName = "Background"
}
.Bind("Location", "Loc", Point.Parse)
.Add(
new TextBlock { Alignment = Spot.Left, Font = new Font("Segoe UI", 15, FontWeight.Bold) }
.Bind("Text", "Key"),
new Shape {
Name = "SHAPE", Width = 100, Height = 100,
Fill = "rgba(128,128,128,0.33)"
}
);
diagram.MouseDrop = (e) => {
// dropping in diagram background removes nodes from any group
e.Diagram.CommandHandler.AddTopLevelParts(e.Diagram.Selection, true);
};
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "A group", IsGroup = true, Loc = "100 10" },
new NodeData { Key = "B group", IsGroup = true, Loc = "100 140" },
new NodeData { Key = "A1", Loc = "10 30" }, // can be added to "A" group
new NodeData { Key = "A2", Loc = "10 60" },
new NodeData { Key = "B1", Loc = "10 90" }, // can be added to "B" group
new NodeData { Key = "B2", Loc = "10 120" },
new NodeData { Key = "C1", Loc = "10 150" } // cannot be added to either group
}
};
These groups are fixed size groups -- they do not use Placeholders. So when a node is dropped into them the group does not automatically resize itself to surround its member nodes. But that is also a benefit when dragging a node out of a group.
The validation predicate is also called when dragging a node that is already a member of a group. You can see how it is acceptable to drop the node into its existing containing group. And when it is dragged outside of the group into the diagram's background, the predicate is called with null as the "group" argument.
In this example it is always OK to drop a node in the background of the diagram rather than into a group.
If you want to disallow dropping in the background, you can call diagram.CurrentTool.DoCancel()
in the Diagram.MouseDrop event handler.
If you want to show feedback during the drag in the background, you can implement a Diagram.MouseDragOver event handler that sets
diagram.CurrentCursor = "not-allowed"
.
This would be behavior similar to that implemented above when dragging inside a Group.
You can also limit what text the user enters when they do in-place text editing of a TextBlock. First, to enable any editing at all, you will need to set TextBlock.Editable to true. There may be many TextBlocks within a Part, but you might want to limit text editing to particular TextBlocks.
Normally there is no limitation on what text the user may enter. If you want to provide a predicate to approve the input when the user finishes editing, set the TextEditingTool.TextValidation or TextBlock.TextValidation property. Setting the property on the TextEditingTool causes the predicate to be applied to all TextBlocks, whereas setting the property on the TextBlock only applies to that text object.
// this predicate is true if the new string has at least three characters
// and has a vowel in it
static bool okName(TextBlock tb, string oldstr, string newstr) {
var re = new System.Text.RegularExpressions.Regex(@"[aeiouy]",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return newstr.Length >= 3 && re.IsMatch(newstr);
}
diagram.NodeTemplate =
new Node("Auto")
.Add(
new Shape { Fill = "lightyellow" },
new Panel("Vertical") { Margin = 3 }
.Add(
new TextBlock { Editable = true } // no validation predicate
.Bind("Text", "Text1"),
new TextBlock {
Editable = true,
IsMultiline = false, // don't allow embedded newlines
TextValidation = okName // new string must be an OK name
}
.Bind("Text", "Text2")
)
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "A", Text1 = "Hello", Text2 = "Dolly!" },
new NodeData { Key = "B", Text1 = "Goodbye", Text2 = "Mr. Chips" }
}
};
Note how editing the top TextBlock accepts text without any vowels, but the bottom one does not accept it and instead leaves the text editor open.
If you want to execute code after a text edit completes, implement a "TextEdited" DiagramEvent listener.
If you would like to show a custom error message when text validation fails, one way is to show a tooltip Adornment. Here is an example where a valid string must contain the letter "W".
diagram.NodeTemplate =
new Node("Auto")
.Add(
new Shape { Fill = "white" },
new TextBlock {
Margin = 8,
Editable = true,
IsMultiline = false,
TextValidation = (tb, oldstr, newstr) => {
return newstr.Contains("W"); // new string must contain a "W"
},
ErrorFunction = (tool, oldstr, newstr) => {
// create and show tooltip about why editing failed for this textblock
var mgr = tool.Diagram.ToolManager;
mgr.HideToolTip(); // hide any currently showing tooltip
var node = tool.TextBlock.Part;
// create a GoDiagram tooltip, which is an Adornment
var tt =
Builder.Make<Adornment>("ToolTip")
.Set(new {
Border_Fill = "pink",
Border_Stroke = "red",
Border_StrokeWidth = 2
})
.Add(
new TextBlock($"Unable to replace the string '{oldstr}' with '{newstr}'" +
$" on node '{node.Key}'" +
$"\nbecause the new string does not contain the capital letter 'W'.")
);
mgr.ShowToolTip(tt, node);
},
TextEdited = (tb, oldstr, newstr) => {
var mgr = tb.Diagram.ToolManager;
mgr.HideToolTip();
}
}
.Bind(new Binding("Text").MakeTwoWay())
);
diagram.Model =
new MyModel {
NodeDataSource = new List<NodeData> {
new NodeData { Key = "A", Text = "Alpha" },
new NodeData { Key = "B", Text = "Beta" }
}
};
If the string does not have the letter "W" in it, it will show an error message describing the problem.