comparison browser_side/dialog.py @ 52:4419ef07bb2b

browser side: adding contact, first draft
author Goffi <goffi@goffi.org>
date Fri, 27 May 2011 01:52:29 +0200
parents f1d2eb9b2523
children f25c4077f6b9
comparison
equal deleted inserted replaced
51:9f19e16187ff 52:4419ef07bb2b
23 from pyjamas.ui.HorizontalPanel import HorizontalPanel 23 from pyjamas.ui.HorizontalPanel import HorizontalPanel
24 from pyjamas.ui.PopupPanel import PopupPanel 24 from pyjamas.ui.PopupPanel import PopupPanel
25 from pyjamas.ui.DialogBox import DialogBox 25 from pyjamas.ui.DialogBox import DialogBox
26 from pyjamas.ui.ListBox import ListBox 26 from pyjamas.ui.ListBox import ListBox
27 from pyjamas.ui.Button import Button 27 from pyjamas.ui.Button import Button
28 from pyjamas.ui.TextBox import TextBox
29 from pyjamas.ui.Label import Label
28 from pyjamas.ui.HTML import HTML 30 from pyjamas.ui.HTML import HTML
29 from pyjamas.ui import HasAlignment 31 from pyjamas.ui import HasAlignment
30 from pyjamas.ui.KeyboardListener import KEY_ESCAPE 32 from pyjamas.ui.KeyboardListener import KEY_ESCAPE, KEY_ENTER
31 33
32 class ContactsChooser(DialogBox): 34 class ContactsChooser(DialogBox):
33 35
34 def __init__(self, host, callback, nb_contact=None, text='Please select contacts'): 36 def __init__(self, host, callback, nb_contact=None, text='Please select contacts'):
35 """ 37 """
150 def onEventPreview(self, event): 152 def onEventPreview(self, event):
151 if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE: 153 if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE:
152 #needed to prevent request cancellation in Firefox 154 #needed to prevent request cancellation in Firefox
153 event.preventDefault() 155 event.preventDefault()
154 return PopupPanel.onEventPreview(self, event) 156 return PopupPanel.onEventPreview(self, event)
157
158 class ExtTextBox(TextBox):
159 """Extended TextBox"""
160
161 def __init__(self, *args, **kwargs):
162 if 'enter_cb' in kwargs:
163 self.enter_cb = kwargs['enter_cb']
164 del kwargs['enter_cb']
165 TextBox.__init__(self, *args, **kwargs)
166 self.addKeyboardListener(self)
167
168 def onKeyUp(self, sender, keycode, modifiers):
169 pass
170
171 def onKeyDown(self, sender, keycode, modifiers):
172 pass
173
174 def onKeyPress(self, sender, keycode, modifiers):
175 if self.enter_cb and keycode == KEY_ENTER:
176 self.enter_cb(self)
177
178 class GroupSelector(DialogBox):
179
180 def __init__(self, top_widgets, initial_groups, selected_groups, ok_cb = None, cancel_cb = None):
181 DialogBox.__init__(self, centered = True)
182 main_panel = VerticalPanel()
183 self.ok_cb = ok_cb
184 self.cancel_cb = cancel_cb
185
186 for wid in top_widgets:
187 main_panel.add(wid)
188
189 main_panel.add(Label('Select in which groups your contact is:'))
190 self.list_box = ListBox()
191 self.list_box.setMultipleSelect(True)
192 self.list_box.setVisibleItemCount(5)
193 self.setAvailableGroups(initial_groups)
194 self.setGroupsSelected(selected_groups)
195 main_panel.add(self.list_box)
196
197 add_group_panel = HorizontalPanel()
198 add_group_lb = Label('Add group:')
199 add_group_tb = ExtTextBox(enter_cb = self.onGroupInput)
200 add_group_panel.add(add_group_lb)
201 add_group_panel.add(add_group_tb)
202 main_panel.add(add_group_panel)
203
204 button_panel = HorizontalPanel()
205 button_panel.add(Button("Add", self.onOK))
206 button_panel.add(Button("Cancel", self.onCancel))
207 main_panel.add(button_panel)
208
209 self.setWidget(main_panel)
210
211 def setAvailableGroups(self, groups):
212 _groups = list(set(groups))
213 _groups.sort()
214 self.list_box.clear()
215 for group in _groups:
216 self.list_box.addItem(group)
217
218 def setGroupsSelected(self, groups):
219 if groups:
220 self.list_box.setItemTextSelection(selected_groups)
221
222 def onOK(self, sender):
223 self.hide()
224 if self.ok_cb:
225 self.ok_cb(self)
226
227 def onCancel(self, sender):
228 self.hide()
229 if self.cancel_cb:
230 self.cancel_cb(self)
231
232 def onGroupInput(self, sender):
233 self.list_box.addItem(sender.getText())
234 sender.setText('')
235 self.list_box.setItemSelected(self.list_box.getItemCount()-1, "selected")
236