#!/usr/bin/env python
import coral
import traceback

CASE = 0

def observer(dir):
	global expectedDir,d, CASE

	print "Diraction  =",dir
	assert expectedDir == dir
	global tm
	if CASE == 0:
		aom = tm.getCurrentTransaction()
		print "aom", aom
		assert aom
		print aom.size()
		cmd = aom[0]
		print "cmd"
		print cmd.__class__
		assert(cmd.__class__ == coral.ElementNewCommandPtr)
		print "We have a new element, ", cmd.getNewElement()
		
		print "---"
		
		cmd = aom[2]
		print cmd.__class__
		print cmd
		assert(cmd.__class__ == coral.ElementChangeSlotCommandPtr)
		print "We can retrieve the old value"
		a = cmd.getAtom()
		print a, a.__class__
		s = cmd.getSlot()
		print s, s.__class__
		print "old '", s.getProperty().getName(), "':",  s[0], s[0].__class__
		
		print "---"
		
		cmd = aom[3]
		print cmd.__class__
		print cmd
		assert(cmd.__class__ == coral.ElementChangeSlotCommandPtr)
		print "We can retrieve the old value"
		a = cmd.getAtom()
		print a, a.__class__
		s = cmd.getSlot()
		print s, s.__class__
		print "old '", s.getProperty().getName(), "':",  s[0], s[0].__class__

	if dir == coral.TRANSACTION_ORDINARY:
		print "Changing the model inside a transaction"
		d.width = d.width * 2
	print ">>>> Observer DONE!"
        
xmidi=coral.lang.XMIDI20

tm = coral.getTransactionManager()
tm.addObserver(observer)

expectedDir=coral.TRANSACTION_ORDINARY
s = coral.Project()
tm.beginModifyCmd()
try:
	d = xmidi.Dimension()
	d.__setProject__(s)
	d.width = 200
	d.height = 150
finally:
	tm.endModifyCmd()

CASE += 1

assert(d.width==400)
expectedDir=coral.TRANSACTION_UNDO
tm.undo()
assert(d.width==0)
expectedDir=coral.TRANSACTION_REDO
tm.redo()
assert(d.width==400)

expectedDir=coral.TRANSACTION_ORDINARY
tm.beginModifyCmd()
try:
	d.width = 20
	d.height = 15
finally:
	tm.endModifyCmd()

assert(d.width==40)
expectedDir=coral.TRANSACTION_UNDO
tm.undo()
assert(d.width==400)
expectedDir=coral.TRANSACTION_REDO
tm.redo()
assert(d.width==40)
