Imported Upstream version 2008.1+svn1553
[opeanno-debian-packaging.git] / game / util / changelistener.py
blob22b2f9196725e387b0ccf14572113fe63e2bb906
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 #because of unclean inheritance, bad usage of parameters and bad use of super in most world object, i cant trust that the init constructor is called.
24 from weakmethod import WeakMethod
26 class Changelistener(object):
27 def __init__(self, *args, **kwargs):
28 super(Changelistener, self).__init__(**kwargs)
29 self.__listeners = []
31 def addChangeListener(self, listener):
32 if not hasattr(self, '_Changelistener__listeners'):
33 self.__listeners = []
34 self.__listeners.append(WeakMethod(listener))
36 def removeChangeListener(self, listener):
37 if not hasattr(self, '_Changelistener__listeners'):
38 self.__listeners = []
39 self.__listeners.remove(WeakMethod(listener))
41 def hasChangeListener(self, listener):
42 if WeakMethod(listener) in self.__listeners:
43 return True
44 else:
45 return False
47 def _changed(self):
48 if not hasattr(self, '_Changelistener__listeners'):
49 self.__listeners = []
50 for listener in self.__listeners:
51 listener()