protected override void setItems() { base.setItems(); if (InstructionGroup.NodeInstructionChildren.Items.Count != 1) { throw new InvalidOperationException("An " + Extensions.Workbench.Documents.PageEditor_.InstructionGroupItems.LD + " instruction group can only have one instruction child."); } NodeInstruction nInstruction = InstructionGroup.NodeInstructionChildren.Items[0]; if (nInstruction.InstructionType != InstructionLDSeries.StaticInstructionType) { throw new InvalidOperationException("The first and only instruction in an " + Extensions.Workbench.Documents.PageEditor_.InstructionGroupItems.LD + " instruction group must be a " + InstructionLDSeries.StaticInstructionType.ToString() + " instruction."); } var newCollection = new ObservableCollection <INodeWrapper>(); IInstructionItem editorItem = FindItemByNodeId(nInstruction.ID) as InstructionLDSeries; if (editorItem == null) { editorItem = InstructionLDSeries.CreateForLD(this, nInstruction); HookupHandlers(editorItem); } newCollection.Add(editorItem); Items = newCollection; }
protected InstructionLDParallel(IEditorItem parent, NodeInstruction instruction) : base(parent, m_InstructionType) { if (instruction == null) { var newInstruction = NodeInstruction.BuildWith(InstructionType); newInstruction = newInstruction.NodeInstructionChildren.Append( InstructionLDSeries.EmptyRung()); newInstruction = newInstruction.NodeInstructionChildren.Append( InstructionLDSeries.EmptyRung()); Instruction = newInstruction; } else { if (instruction.InstructionType != InstructionType) { throw new InvalidOperationException("Tried to instantiate InstructionLDParallel but passed a different instruction type."); } Instruction = instruction; } // Build the context menu if (extensionService != null) { ContextMenu = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu); ContextMenuEnabled = true; } }
private static NodeInstructionGroup emptyNode() { var newInstructionGroup = NodeInstructionGroup.BuildWith( new FieldIdentifier(Extensions.Workbench.Documents.PageEditor_.InstructionGroupItems.LD)); newInstructionGroup = newInstructionGroup.NodeInstructionChildren.Append( InstructionLDSeries.EmptyRung()); // empty rungs are just a Series instruction return(newInstructionGroup); }
public void AppendBranch() { if (runtimeService.DisconnectDialog(this)) { var doActions = new List <Action>(); var undoActions = new List <Action>(); var origInstruction = Instruction; var newSeriesItem = InstructionLDSeries.CreateForLD(this, null); doActions.Add(() => Items.Add(newSeriesItem)); doActions.Add(() => Instruction = Instruction.NodeInstructionChildren.Append(newSeriesItem.Instruction)); undoActions.Add(() => Instruction = origInstruction); Do(new UndoMemento(this, ActionType.EDIT, Resources.Strings.Undo_Action_Instruction_Parallel_AppendBranch, undoActions, doActions)); } }
protected override void setItems() { if (Instruction.NodeInstructionChildren.Items.Count < 2) { throw new InvalidOperationException("An LD Parallel instruction must have at least 2 series children."); } foreach (var nodeWrapper in Items) { var instruc = nodeWrapper as ILDInstructionItem; if (instruc != null) { instruc.PropertyChanged -= new PropertyChangedEventHandler(editorItem_PropertyChanged); } } var newCollection = new ObservableCollection <INodeWrapper>(); foreach (var nInstruction in Instruction.NodeInstructionChildren.Items) { if (nInstruction.InstructionType != InstructionLDSeries.StaticInstructionType) { throw new InvalidOperationException("All children of the LD Parallel instruction must be " + InstructionLDSeries.StaticInstructionType.ToString() + " instructions."); } IInstructionItem editorItem = FindItemByNodeId(nInstruction.ID) as IInstructionItem; if (editorItem == null) { editorItem = InstructionLDSeries.CreateForLD(this, nInstruction); logger.Info("Created new series editor item as child of parallel instruction."); } else { editorItem.Parent = this; } editorItem.PropertyChanged += new PropertyChangedEventHandler(editorItem_PropertyChanged); newCollection.Add(editorItem); } foreach (var item in Items) { if (!newCollection.Contains(item)) { item.Parent = null; } } Items = newCollection; NotifyPropertyChanged(m_IsRightArgs); NotifyPropertyChanged(m_VerticalRungOffsetArgs); }
/// <summary> /// Inserts the given item *before* the given index. /// If the given index is one greater than the last index, /// it appends it to the items. Also takes care of removing the /// items from wherever they came from (i.e. for drag and drop) /// and builds the Do/Undo/Redo actions. /// </summary> internal void insert(IEnumerable <ILDInstructionItem> source, int index) { if (runtimeService.DisconnectDialog(this)) { NodeInstruction nInstruction = null; if (index < Items.Count) { nInstruction = Items[index].Node as NodeInstruction; } var doActions = new List <Action>(); var undoActions = new List <Action>(); var origInstruction = Instruction; var newInstruction = Instruction; undoActions.Add(() => Instruction = origInstruction); // Collect a list of the parents that we're taking these from, and state of each (there can only be one parent) InstructionLDSeries previousParent = null; NodeInstruction previousParentNodeOrig = null; NodeInstruction previousParentNodeNew = null; foreach (var ldInstructionItem in source) { if (ldInstructionItem.Parent != null) { var instructionParent = ldInstructionItem.Parent as InstructionLDSeries; if (instructionParent != null) { if (previousParent != null && previousParent != instructionParent) { throw new InvalidOperationException("Tried to drop items that were sourced from more than one series instruction."); } if (previousParent == null) { previousParent = instructionParent; previousParentNodeOrig = previousParent.Instruction; previousParentNodeNew = previousParent.Instruction; } previousParentNodeNew = previousParentNodeNew.NodeInstructionChildren.Remove(ldInstructionItem.Instruction); } } } bool destinationIsAncestor = false; bool sourceIsAncestor = false; // This block deals with where the items came from (if anywhere) if (previousParent != null) { if (previousParent == this) { // simple case - do nothing (we're just moving things around in the same series instruction } else { if (previousParent.FindAncestorByNodeId(this.Node.ID) != null) { destinationIsAncestor = true; } else if (FindAncestorByNodeId(previousParent.Node.ID) != null) { sourceIsAncestor = true; } if (!sourceIsAncestor && !destinationIsAncestor) { // They are from different branches, and one is not the ancestor of another // -------------- UNDO --------------------- foreach (var ldInstructionItem in source) { undoActions.Add(() => ldInstructionItem.Parent = null); undoActions.Add(() => previousParent.Items.Add(ldInstructionItem)); } // we also need to restore the parents' parent node, etc. INodeWrapper parentIterator = previousParent; while (parentIterator != null) { saveNodeState(parentIterator, undoActions); parentIterator = parentIterator.Parent; } undoActions.Add(() => previousParent.setItems()); // --------------- DO ---------------------- doActions.Add(() => previousParent.Instruction = previousParentNodeNew); } else { // Break it down into two steps previousParent.DeleteSelectedChildren(true); this.insert(source, index); } } } // This block deals with where the items are going (here) if (!sourceIsAncestor && !destinationIsAncestor) { foreach (var ldInstructionItem in source) { var instruc = ldInstructionItem.Instruction; doActions.Add(() => ldInstructionItem.Parent = null); doActions.Add(() => { if (!Items.Contains(ldInstructionItem)) { Items.Add(ldInstructionItem); } }); if (index >= Items.Count) { newInstruction = newInstruction.NodeInstructionChildren.Append(instruc); } else { if (nInstruction == null) { throw new ArgumentOutOfRangeException(); } if (newInstruction.NodeInstructionChildren.Contains(instruc)) // in case we're dragging and dropping from the same rung { newInstruction = newInstruction.NodeInstructionChildren.Remove(instruc); } newInstruction = newInstruction.NodeInstructionChildren.InsertBefore(nInstruction, instruc); } } doActions.Add(() => Instruction = newInstruction); string undoMessage; if (previousParent == null) { undoMessage = Resources.Strings.Undo_Action_Instruction_Series_Insert; } else { undoMessage = Resources.Strings.Undo_Action_Instruction_Series_Move; } Do(new UndoMemento(this, ActionType.EDIT, undoMessage, undoActions, doActions), previousParent); } } }