Package library :: Package pyjamas :: Package ui :: Module Panel
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.ui.Panel

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from pyjamas import Factory 
 16  from pyjamas import DOM 
 17   
 18  from pyjamas.ui.Widget import Widget 
 19   
20 -class PanelBase(object):
21
22 - def clear(self):
23 """ use this method, due to list changing as it's being iterated. 24 also, it's possible to use this method even 25 """ 26 children = [] 27 for child in self.__iter__(): 28 children.append(child) 29 30 for child in children: 31 self.remove(child)
32
33 - def doAttachChildren(self):
34 for child in self: 35 child.onAttach()
36
37 - def doDetachChildren(self):
38 for child in self: 39 child.onDetach()
40
41 - def getWidgetCount(self):
42 return len(self.getChildren())
43
44 - def getWidget(self, index):
45 return self.getChildren()[index]
46
47 - def getIndexedChild(self, index):
48 return self.getWidget(index)
49
50 - def addIndexedItem(self, index, child):
51 self.add(child)
52
53 - def getWidgetIndex(self, child):
54 return self.getChildren().index(child)
55
56 - def getChildren(self):
57 return self.children # assumes self.children: override if needed.
58
59 - def setWidget(self, index, widget):
60 """ Insert (or optionally replace) the widget at the given index 61 with a new one 62 """ 63 existing = self.getWidget(index) 64 if existing is not None: 65 self.remove(existing) 66 self.insert(widget, index)
67
68 - def append(self, widget):
69 return self.add(widget)
70
71 - def __setitem__(self, index, widget):
72 return self.setWidget(index, widget)
73
74 - def __getitem__(self, index):
75 return self.getWidget(index)
76
77 - def __len__(self):
78 return len(self.getChildren())
79
80 - def __nonzero__(self):
81 return self is not None
82
83 - def __iter__(self):
84 return self.getChildren().__iter__()
85 86
87 -class Panel(PanelBase, Widget):
88 - def __init__(self, **kwargs):
89 self.children = [] 90 PanelBase.__init__(self) 91 Widget.__init__(self, **kwargs)
92
93 - def disown(self, widget):
94 if widget.getParent() is not self: 95 raise Exception("widget %s is not a child of this panel %s" % \ 96 (str(widget), str(self))) 97 element = widget.getElement() 98 widget.setParent(None) 99 parentElement = DOM.getParent(element) 100 if parentElement is not None: 101 DOM.removeChild(parentElement, element)
102
103 - def adopt(self, widget, container):
104 if container is not None: 105 widget.removeFromParent() 106 DOM.appendChild(container, widget.getElement()) 107 widget.setParent(self)
108 109 110 Factory.registerClass('pyjamas.ui.Panel', 'Panel', Panel) 111