I'm trying to make an easy-to-use TextArea-like component that does
some of the things that someone coming from Delphi would expect to
be there, such as managing its scrollbars and keeping track of
whether it's modified, using composition of a JTextArea and a
simple DocumentListener into a JScrollPane, the idea is to delegate
almost everything that the JScrollPane can't do to the JTextArea,
the fun part of the code is:
class ScrollTextArea(JScrollPane)
...
def __getattr__(self, attr):
funcname = 'get%s'%capitalizeFirst(attr)
try:
func = getattr(JScrollPane, funcname)
return apply(func, (self,))
except AttributeError:
pass
func = getattr(JTextArea, funcname)
return apply(func, (self.edit,))
def __setattr__(self, attr, value):
funcname = 'set%s'%capitalizeFirst(attr)
try:
func = getattr(JScrollPane, funcname)
return apply(func, (self, value))
except AttributeError:
pass
try:
func = getattr(JTextArea, funcname)
return apply(func, (self.edit, value))
except AttributeError:
self.__dict__[attr]=value
So question 1 is whether there is some easier and better way to do
the delegation, question 2 is whether there's some way to make
Jython's automatic conversions work; I find that
editor.preferredSize=(600,200)
will fail, although
editor.preferredSize=awt.Dimension(600,200)
works. Obviously it would be possible to intercept and convert
some specific instances such as this one, but is there a more
general way to do it?
|