public static void AssertIsGraphElementAsExpected(IGraphElementModel expected, IGraphElementModel actual, string elementId) { Assert.AreSame(expected.GetType(), actual.GetType(), UnexpectedValueMessage("Type", elementId)); Assert.AreEqual(expected.Capabilities, actual.Capabilities, UnexpectedValueMessage(nameof(expected.Capabilities), elementId)); Assert.AreEqual(expected.Color, actual.Color, UnexpectedValueMessage(nameof(expected.Color), elementId)); Assert.AreEqual(expected.HasUserColor, actual.HasUserColor, UnexpectedValueMessage(nameof(expected.HasUserColor), elementId)); if (expected is IHasTitle expectedTitle) { var actualTitle = actual as IHasTitle; Assert.IsNotNull(actualTitle, elementId); Assert.AreEqual(expectedTitle.Title, actualTitle.Title, UnexpectedValueMessage(nameof(expectedTitle.Title), elementId)); } if (expected is IHasProgress expectedProgress) { var actualProgress = actual as IHasProgress; Assert.IsNotNull(actualProgress, elementId); Assert.AreEqual(expectedProgress.HasProgress, actualProgress.HasProgress, UnexpectedValueMessage(nameof(expectedProgress.HasProgress), elementId)); } if (expected is ICollapsible expectedCollapsible) { var actualCollapsible = actual as ICollapsible; Assert.IsNotNull(actualCollapsible, elementId); Assert.AreEqual(expectedCollapsible.Collapsed, actualCollapsible.Collapsed, UnexpectedValueMessage(nameof(expectedCollapsible.Collapsed), elementId)); } if (expected is IResizable expectedResizable) { var actualResizable = actual as IResizable; Assert.IsNotNull(actualResizable, elementId); Assert.AreEqual(expectedResizable.PositionAndSize, actualResizable.PositionAndSize, UnexpectedValueMessage(nameof(expectedResizable.PositionAndSize), elementId)); } if (expected is IMovable expectedMovable) { var actualMovable = actual as IMovable; Assert.IsNotNull(actualMovable, elementId); Assert.AreEqual(expectedMovable.Position, actualMovable.Position, UnexpectedValueMessage(nameof(expectedMovable.Position), elementId)); } if (expected is IHasDeclarationModel expectedDeclarationModel) { var actualDeclarationModel = actual as IHasDeclarationModel; Assert.IsNotNull(actualDeclarationModel, elementId); Assert.AreEqual(expectedDeclarationModel.DeclarationModel.Guid, actualDeclarationModel.DeclarationModel.Guid, UnexpectedValueMessage(nameof(expectedDeclarationModel.DeclarationModel), elementId)); } }
public static GraphElement CreateUI(GraphView graphView, Store store, IGraphElementModel model) { if (model == null) { Debug.LogError("GraphElementFactory could not create node because of a null reference model."); return(null); } var ext = ModelUtility.ExtensionMethodCache <INodeBuilder> .GetExtensionMethod( model.GetType(), NodeBuilder.FilterMethods, NodeBuilder.KeySelector ); GraphElement newElem = null; if (ext != null) { var nodeBuilder = new NodeBuilder { GraphView = graphView }; newElem = (GraphElement)ext.Invoke(null, new object[] { nodeBuilder, store, model }); } else if (model is INodeModel nodeModel) { newElem = new Node(nodeModel, store, graphView); } if (newElem == null) { Debug.LogError($"GraphElementFactory doesn't know how to create a node of type: {model.GetType()}"); } else if (model is INodeModel nodeModel) { if (nodeModel.HasUserColor) { (newElem as ICustomColor)?.SetColor(nodeModel.Color); } if (newElem is INodeState nodeState) { if (nodeModel.State == ModelState.Disabled) { nodeState.UIState = NodeUIState.Disabled; } else { nodeState.UIState = NodeUIState.Enabled; } nodeState.ApplyNodeState(); nodeState.AddOverlay(); } } return(newElem); }
static Func <TValue> MakePropertyValueGetter(IGraphElementModel model, string propertyName) { var getterInfo = model?.GetType().GetProperty(propertyName)?.GetGetMethod(); if (getterInfo != null) { Debug.Assert(typeof(TValue) == getterInfo.ReturnType); var del = Delegate.CreateDelegate(typeof(Func <TValue>), model, getterInfo); return(del as Func <TValue>); } return(null); }
/// <summary> /// Creates an instance of a class implementing <see cref="IModelUI"/> to display <paramref name="model"/>. /// </summary> /// <param name="view">The view in which to put the UI.</param> /// <param name="commandDispatcher">The command dispatcher.</param> /// <param name="model">The model.</param> /// <param name="context">A context creation string. When a model needs different UI in /// different contexts, use this parameter to differentiate between contexts.</param> /// <typeparam name="T">The type of the returned object.</typeparam> /// <returns>An instance of <see cref="IModelUI"/> that display <paramref name="model"/>.</returns> public static T CreateUI <T>(IModelView view, CommandDispatcher commandDispatcher, IGraphElementModel model, string context) where T : class, IModelUI { if (view == null) { Debug.LogError("GraphElementFactory could not create element because view is null."); return(null); } if (model == null) { Debug.LogError("GraphElementFactory could not create element because model is null."); return(null); } var ext = ExtensionMethodCache <ElementBuilder> .GetExtensionMethod( view.GetType(), model.GetType(), FilterMethods, KeySelector ); T newElem = null; if (ext != null) { var nodeBuilder = new ElementBuilder { View = view, Context = context }; newElem = ext.Invoke(null, new object[] { nodeBuilder, commandDispatcher, model }) as T; } if (newElem == null) { Debug.LogError($"GraphElementFactory doesn't know how to create a UI of type {typeof(T)} for model of type: {model.GetType()}"); return(null); } return(newElem); }