On Dec 17, 2007 8:25 PM, Eric Johnson <eric@...> wrote:
> Is there a good Pythonic way to write the propertyChange method for a
> Writing code like:
> def propertyChange(self, e):
> if e.propertyName == "progress":
> self.progressBar.value = e.newValue
> elif e.propertyName == swing.SwingWorker.StateValue.PENDING:
> seems to be a very unsatisfactory.
> In the case of ActionListeners being able to use actionPerformed is greaqt
> but I don't really understand the reflection magic that Jython perfoms
> behind the scenes to know if that can be applied here.
As astigmatik pointed out, you can set a propertyChange function in
the constructor to an object that has an addPropertyChangeListener
method, but that still leaves you writing that propertyChange function
which is just a bunch of if else statements. I attempted to whip up a
little module that would relieve some of that tedium, but it ended up
being overkill for the task at hand. I'm still sending it on in the
hopes that someone can simplify it to something reasonable.
from java.awt import Color
from java.beans import PropertyChangeListener, PropertyChangeEvent
from javax.swing import JPanel
def __init__(self, action, name=None, newVal=None, oldVal=None):
Takes an action to execute if this matches, and optional values for
each of the fields on PropertyChangeListener to check for equality.
self.action = action
self.checkers = [
def __call__(self, e):
def match(self, e):
# If e passes all the checkers, or we don't have a checker for a field,
# invoke the action with e and return true. Otherwise return false.
for checker, val in self.checkers:
if checker and not checker == val(e):
'''Calls the first PropertyMatcher it has that matches a change'''
def __init__(self, *matchers):
'''Takes a list of PropertyMatchers to check in order'''
self.matchers = matchers
def __call__(self, e):
for matcher in self.matchers:
if __name__ == '__main__':
matcher = MultiplePropertyMatcher(
PropertyMatcher(printer('background to black'),
PropertyMatcher(printer('something is black'), newVal=Color.BLACK),
PropertyMatcher(printer('background changed'), 'background'),
p = JPanel(propertyChange=matcher)
Running that module prints four lines:
background to black
something is black
As I said earlier, it barely feels better to me than just writing the
if elses directly, but it *almost* feels elegant. Any takers on
making it suck less?