■
事例:
>>> dir(list)
- ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
$ jython2.5b3 -i exJList.py from javax.swing import BoxLayout from javax.swing import DefaultListModel from javax.swing import JFrame from javax.swing import JList from javax.swing import JPanel from javax.swing import JScrollPane from javax.swing import JTextArea from java.awt import BorderLayout ## ---------------------------------------- class Model(DefaultListModel): @classmethod def open(self, target): frame = JFrame( title = target.__class__.__name__, size = (250,150), defaultCloseOperation = JFrame.EXIT_ON_CLOSE, ) model = self(target) frame.contentPane.add(model.view) frame.visible = True return model def __init__(self, target): self._list = list(target) self.target = target self.view = XListView(self) self.addElement("self") for e in self.target: self.addElement(e) def __getitem__(self, key): return self._list[key] def __getslice__(self, i, j): """ >>> print list.__getslice__.__doc__ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ s = self._list[i:j] model = self.__class__.open(s) return model ## ---------------------------------------- def pop(self, index=None): """ >>> print list.pop.__doc__ L.pop([index]) -> item -- remove and return item at index (default last) """ if index == None: index = len(self._list)-1 if index < 0: index += len(self._list) item = self._list.pop(index) # public Object remove(int index) self.remove(index+1) self._update() return item def insert(self, index, object): """ >>> print list.insert.__doc__ L.insert(index, object) -- insert object before index """ if index < 0: index += len(self._list) self._list.insert(index, object) # public void add(int index, Object element) self.add(index+1, object) self._update() pass def index(self, value, start=None, stop=None): """ >>> print list.index.__doc__ L.index(value, [start, [stop]]) -> integer -- return first index of value >>> s ['A', 'B', 'C'] >>> s.index("@") ... ValueError: list.index(x): x not in list """ # public int indexOf(Object elem) integer = self.indexOf(value) if integer == -1: raise ValueError, "list.index(x): x not in list" else: return integer-1 def reverse(self): """ >>> print list.reverse.__doc__ L.reverse() -- reverse *IN PLACE* """ self._list.reverse() self._changed() pass def sort(self): """ >>> print list.sort.__doc__ L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 """ self._list.sort() self._changed() pass ## ---------------------------------------- def _changed(self): # public Object set(int index, Object element) for index, e in enumerate(self._list): self.set(index+1, e) self._update() pass def _update(self): view = self.view.list if view.selectedIndex == 0: try: view.clearSelection() except: pass view.selectedIndex = 0 @classmethod def help(self): """ >>> Model.help() """ s1 = dir(Model) s2 = dir(DefaultListModel) ss = set(s1)-set(s2) s = [] for e in ss: t = eval("Model.%s.__doc__"%e) if t: s.append("\n%s:"%e) s.append(t) print "".join(s) ## ---------------------------------------- class XListView(JPanel): def __init__(self, model): self.model = model self.layout = BorderLayout() self.list = JList( model = model, valueChanged = self.valueChanged, fixedCellWidth = 80, ) self.textArea = JTextArea( lineWrap=True, wrapStyleWord=True, ) for e, constraints in [ (self.list, BorderLayout.WEST ), (self.textArea, BorderLayout.CENTER), ]: self.add(JScrollPane(e), constraints) def valueChanged(self, e): """ public Object get(int index) """ index = self.list.selectedIndex if index == None: return if index == 0: s = self.model._list else: s = self.model.get(index) self.textArea.text = str(s) ## ---------------------------------------- def ex(target): pass ;global Xmodel frame = JFrame( title = target.__class__.__name__, size = (250,150), defaultCloseOperation = JFrame.EXIT_ON_CLOSE, ) model = Model(target) ;Xmodel = model frame.contentPane.add(model.view) frame.visible = True return frame ## ---------------------------------------- Xmodel = None if __name__=='__main__': pass ;print "... #1.0b" ex("ABC") ## ========================================