Example #1
0
func (t *nodeType) treeInstObject(tree tr.TreeIf, cursor tr.Cursor, obj tr.TreeElementIf) (newCursor tr.Cursor) {
	switch obj.(type) {
	case bh.ImplementationIf:
		impl := obj.(bh.ImplementationIf)
		// update all instance nodes in the tree with new implementation
		for _, n := range t.Instances() {
			nCursor := tree.Cursor(n)
			tCursor := tree.CursorAt(nCursor, t)
			tCursor.Position = len(t.Implementation()) - 1
			newICursor := tree.Insert(tCursor)
			impl.AddToTree(tree, newICursor)
		}

	case bh.PortTypeIf:
		pt := obj.(bh.PortTypeIf)
		// update all instance nodes in the tree with new port
		for _, n := range t.Instances() {
			var p bh.PortIf
			var ok bool
			if pt.Direction() == gr.InPort {
				p, ok, _ = n.(*node).inPort.Find(n.Name(), pt.Name())
			} else {
				p, ok, _ = n.(*node).outPort.Find(n.Name(), pt.Name())
			}
			if !ok {
				log.Fatalf("nodeType.treeInstObject error: port %s not found.\n", pt.Name())
			}
			nCursor := tree.Cursor(n)
			// Insert new port at the same position as in the type:
			// Need to deal with implementations in type VS type in node
			if cursor.Position >= 0 {
				nCursor.Position = cursor.Position - len(t.Implementation()) + 1
			}
			newNCursor := tree.Insert(nCursor)
			p.AddToTree(tree, newNCursor)
			// Update mirrored type in node:
			tCursor := tree.CursorAt(nCursor, t)
			tCursor.Position = cursor.Position
			t.treeNewObject(tree, tCursor, obj)
		}

	default:
		log.Fatalf("nodeType.AddNewObject error: invalid type %T\n", obj)
	}
	return
}