You are not logged in Log in Join
You are here: Home » Resources » Mozilla » ZWiki » SampleUse » wikipage_view

Log in
Name

Password

 
 

SampleUse


#! /usr/bin/env python

import Model import View import Controller import Reason import Event

class ModelChanged(Reason.Reason): def __init__(self): """Construct the instance."""

Reason.Reason.__init__(self, "ModelChanged") return

class NotUsed(Reason.Reason): def __init__(self): """Construct the instance."""

Reason.Reason.__init__(self, "NotUsed") return

class Model1(Model.Model): def __init__(self, aValue): Model.Model.__init__(self) self.__value = aValue return

def __del__(self): print "Model1.__del__" Model.Model.__del__(self) return

def getValue(self): result = self.__value

return result

def setValue(self, aValue): self.__value = aValue self.notifyViews(ModelChanged())

return

class View1(View.View):

def __init__(self, aModel): self.notUsedEvent = Event.Event(NotUsed(), self.Callback(self.modelChanged))

View.View.__init__(self, aModel) return

def __del__(self): print "View1.__del__" View.View.__del__(self) return

def registerEvents(self): eventList = View.View.registerEvents(self)

eventList.append(self.notUsedEvent)

eventList.append(Event.Event(ModelChanged(), self.Callback(self.modelChanged)))

return eventList

def modelChanged(self, reason): print "View1.modelChanged" print self.getValue() return

def getValue(self): return self.getModel().getValue()

def modelDeleted(self, aReason): """Method that is invoked when the model is deleted."""

print "View1.modelDeleted"

View.View.modelDeleted(self, aReason) return

class Controller1(Controller.Controller): def __del__(self): print "Controller1.__del__" Controller.Controller.__del__(self) return

def setValue(self, aValue): self.getModel().setValue(aValue) return

def modelDeleted(self, aReason): """Method that is invoked when the model is deleted."""

print "Controller1.modelDeleted"

Controller.Controller.modelDeleted(self, aReason) return

def main(argv): model = Model1(41) v = View1(model) c = Controller1(model)

print v.getValue() c.setValue(42)

model.removeViewEvent(v, v.notUsedEvent) model.finished() return

if __name__ == "__main__": import sys sys.exit(main(sys.argv) or 0)