Package muntjac :: Package ui :: Module combo_box
[hide private]
[frames] | no frames]

Source Code for Module muntjac.ui.combo_box

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  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   
 16  """Defines a filtering drop-down single-select.""" 
 17   
 18  from muntjac.ui.select import Select 
 19  from muntjac.data.container import IContainer 
 20   
 21  from muntjac.terminal.gwt.client.ui.v_filter_select import VFilterSelect 
 22   
 23   
24 -class ComboBox(Select):
25 """A filtering dropdown single-select. Suitable for newItemsAllowed, but 26 it's turned of by default to avoid mistakes. Items are filtered based on 27 user input, and loaded dynamically ("lazy-loading") from the server. You 28 can turn on newItemsAllowed and change filtering mode (and also turn it 29 off), but you can not turn on multi-select mode. 30 """ 31 32 CLIENT_WIDGET = None #ClientWidget(VFilterSelect) 33
34 - def __init__(self, *args):
35 self._inputPrompt = None 36 37 #: If text input is not allowed, the ComboBox behaves like a pretty 38 # NativeSelect - the user can not enter any text and clicking the 39 # text field opens the drop down with options 40 self._textInputAllowed = True 41 42 nargs = len(args) 43 if nargs == 0: 44 super(ComboBox, self).__init__() 45 self.setMultiSelect(False) 46 self.setNewItemsAllowed(False) 47 elif nargs == 1: 48 caption, = args 49 super(ComboBox, self).__init__(caption) 50 self.setMultiSelect(False) 51 self.setNewItemsAllowed(False) 52 elif nargs == 2: 53 if isinstance(args[1], IContainer): 54 caption, dataSource = args 55 super(ComboBox, self).__init__(caption, dataSource) 56 self.setMultiSelect(False) 57 self.setNewItemsAllowed(False) 58 else: 59 caption, options = args 60 super(ComboBox, self).__init__(caption, options) 61 self.setMultiSelect(False) 62 self.setNewItemsAllowed(False) 63 else: 64 raise ValueError, 'too many arguments'
65 66
67 - def setMultiSelect(self, multiSelect):
68 if multiSelect and not self.isMultiSelect(): 69 raise NotImplementedError, 'Multiselect not supported' 70 71 super(ComboBox, self).setMultiSelect(multiSelect)
72 73
74 - def getInputPrompt(self):
75 """Gets the current input prompt. 76 77 @see: L{setInputPrompt} 78 @return: the current input prompt, or null if not enabled 79 """ 80 return self._inputPrompt
81 82
83 - def setInputPrompt(self, inputPrompt):
84 """Sets the input prompt - a textual prompt that is displayed when 85 the select would otherwise be empty, to prompt the user for input. 86 87 @param inputPrompt: 88 the desired input prompt, or null to disable 89 """ 90 self._inputPrompt = inputPrompt 91 self.requestRepaint()
92 93
94 - def paintContent(self, target):
95 if self._inputPrompt is not None: 96 target.addAttribute('prompt', self._inputPrompt) 97 98 super(ComboBox, self).paintContent(target) 99 100 if not self._textInputAllowed: 101 target.addAttribute(VFilterSelect.ATTR_NO_TEXT_INPUT, True)
102 103
104 - def setTextInputAllowed(self, textInputAllowed):
105 """Sets whether it is possible to input text into the field or whether 106 the field area of the component is just used to show what is selected. 107 By disabling text input, the comboBox will work in the same way as a 108 L{NativeSelect} 109 110 @see L{isTextInputAllowed} 111 112 @param textInputAllowed: 113 true to allow entering text, false to just show the current 114 selection 115 """ 116 self._textInputAllowed = textInputAllowed 117 self.requestRepaint()
118 119
120 - def isTextInputAllowed(self):
121 """Returns true if the user can enter text into the field to either 122 filter the selections or enter a new value if :{isNewItemsAllowed} 123 returns true. If text input is disabled, the comboBox will work in the 124 same way as a L{NativeSelect}. 125 """ 126 return self._textInputAllowed
127