// 'center' must be in world coordinates private void Insert(object insertingObject, Point center, Statechart insertionPoint) { IDataObject dataObject = (IDataObject)insertingObject; object[] items = dataObject.GetData(typeof(object[])) as object[]; if (items == null) { return; } object[] itemCopies = DomNode.Copy(Adapters.AsIEnumerable <DomNode>(items)); foreach (Annotation annotation in Adapters.AsIEnumerable <Annotation>(itemCopies)) { this.As <Document>().Annotations.Add(annotation); } IEnumerable <StateBase> states = Adapters.AsIEnumerable <StateBase>(itemCopies); foreach (StateBase state in states) { insertionPoint.States.Add(state); } foreach (Transition transition in Adapters.AsIEnumerable <Transition>(itemCopies)) { m_transitions.Add(transition); } // centering hierarchical states requires some special code Center(itemCopies, center); Selection.SetRange(itemCopies); }
/// <summary> /// Adds new objects of given type to statechart using a transaction. /// Called by automated scripts during testing.</summary> /// <typeparam name="T">Type of objects to add</typeparam> /// <param name="domNode">DomNode that contains added objects</param> /// <param name="xPos">X-coordinate at center of insertion position</param> /// <param name="yPos">Y-coordinate at center of insertion position</param> /// <param name="parentState">Insertion point for added objects</param> /// <returns>Last selected item</returns> public T Insert <T>(DomNode domNode, int xPos, int yPos, State parentState) where T : class { //Use a default name of the type of object. Annotations don't have the nameAttribute though if (domNode.Type != Schema.annotationType.Type) { domNode.SetAttribute(Schema.stateBaseType.nameAttribute, typeof(T).Name); } DataObject dataObject = new DataObject(new object[] { domNode }); Statechart insertionPoint = m_statechart; if (parentState != null) { insertionPoint = GetStatechart(parentState); } ITransactionContext transactionContext = this.As <ITransactionContext>(); transactionContext.DoTransaction( delegate { Insert(dataObject, new Point(xPos, yPos), insertionPoint); }, "Scripted Insert Object"); return(Selection.GetLastSelected <T>()); }
private void DomNode_ChildInserting(object sender, ChildEventArgs e) { // check pseudo-state constraints StateBase state = e.Child.As <StateBase>(); if (state != null && state.IsPseudoState) { Statechart statechart = e.Parent.As <Statechart>(); CheckUniqueness(statechart, state.Type); } else { // check state transition constraints Transition transition = e.Child.As <Transition>(); if (transition != null) { if (transition.FromState.IsPseudoState) { if (transition.FromState.Type == StateType.Final) { throw new InvalidTransactionException( "Can't have a transition from the final state".Localize()); } } if (transition.ToState.IsPseudoState) { if (transition.ToState.Type == StateType.Start) { throw new InvalidTransactionException( "Can't have a transition to the start state".Localize()); } } } } }
private void CheckUniqueness(Statechart statechart, StateType type) { foreach (StateBase state in statechart.States) { if (state.Type == type) { string typeName = Enum.GetName(typeof(StateType), type); throw new InvalidTransactionException( "Each statechart cannot have more than one psuedo-state of type: ".Localize() + typeName); } } }
private Rectangle Layout(Statechart statechart) { Rectangle bounds = new Rectangle(); foreach (StateBase state in statechart.States) { Rectangle stateBounds = Layout(state); if (bounds.IsEmpty) { bounds = stateBounds; } else { bounds = Rectangle.Union(bounds, stateBounds); } } statechart.Bounds = bounds; return(bounds); }
/// <summary> /// Inserts object into statechart at the center of the canvas or last selected state if there was one</summary> /// <param name="insertingObject">Object to insert</param> public void Insert(object insertingObject) { Statechart insertionPoint = m_statechart; // default is root statechart AdaptableControl control = m_viewingContext.Control; DragDropAdapter dragDropAdapter = control.As <DragDropAdapter>(); Matrix transform = control.As <ITransformAdapter>().Transform; Point center; // in world coordinates if (dragDropAdapter != null && dragDropAdapter.IsDropping) { insertionPoint = FindStatechartUnder(dragDropAdapter.MousePosition); center = GdiUtil.InverseTransform(transform, dragDropAdapter.MousePosition); } else // paste into last selected state { State state = Selection.GetLastSelected <State>(); if (state != null) { insertionPoint = GetStatechart(state); Rectangle stateBounds = m_viewingContext.GetBounds(state); center = new Point( stateBounds.X + stateBounds.Width / 2, stateBounds.Y + stateBounds.Height / 2); center = GdiUtil.InverseTransform(transform, center); } else { center = GdiUtil.InverseTransform(transform, new Point( control.Width / 2, control.Height / 2)); } } Insert(insertingObject, center, insertionPoint); }
private Rectangle Layout(Statechart statechart) { Rectangle bounds = new Rectangle(); foreach (StateBase state in statechart.States) { Rectangle stateBounds = Layout(state); if (bounds.IsEmpty) { bounds = stateBounds; } else { bounds = Rectangle.Union(bounds, stateBounds); } } statechart.Bounds = bounds; return bounds; }