Package muntjac :: Package data :: Package util :: Module default_item_sorter
[hide private]
[frames] | no frames]

Source Code for Module muntjac.data.util.default_item_sorter

  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  """Provides a default implementation of an IItemSorter.""" 
 17   
 18  from muntjac.data.util.item_sorter import IItemSorter 
 19   
 20   
21 -class DefaultItemSorter(IItemSorter):
22 """Provides a default implementation of an IItemSorter. The 23 C{DefaultItemSorter} adheres to the L{ISortable.sort} rules and sorts 24 the container according to the properties given using L{setSortProperties}. 25 26 A Comparator is used for comparing the individual C{Property} 27 values. The comparator can be set using the constructor. If no comparator 28 is provided a default comparator is used. 29 """ 30
31 - def __init__(self, propertyValueComparator=None):
32 """Constructs a DefaultItemSorter which uses the C{Comparator} 33 indicated by the C{propertyValueComparator} parameter for 34 comparing C{Property} values. Uses the default C{Comparator} 35 for comparing C{Property} values if propertyValueComparator is None. 36 37 @param propertyValueComparator: 38 The comparator to use when comparing individual 39 C{Property} values 40 """ 41 self._sortPropertyIds = None 42 self._sortDirections = None 43 self._container = None 44 self._propertyValueComparator = None 45 46 if propertyValueComparator is None: 47 DefaultItemSorter.__init__(self, DefaultPropertyValueComparator()) 48 else: 49 self._propertyValueComparator = propertyValueComparator
50 51
52 - def __call__(self, o1, o2):
53 return self.compare(o1, o2)
54 55
56 - def compare(self, o1, o2):
57 item1 = self._container.getItem(o1) 58 item2 = self._container.getItem(o2) 59 60 # Items can be null if the container is filtered. Null is considered 61 # "less" than not-null. 62 if item1 is None: 63 if item2 is None: 64 return 0 65 else: 66 return 1 67 elif item2 is None: 68 return -1 69 70 for i in range(len(self._sortPropertyIds)): 71 result = self.compareProperty(self._sortPropertyIds[i], 72 self._sortDirections[i], item1, item2) 73 74 # If order can be decided 75 if result != 0: 76 return result 77 78 return 0
79 80
81 - def compareProperty(self, propertyId, sortDirection, item1, item2):
82 """Compares the property indicated by C{propertyId} in the items 83 indicated by C{item1} and C{item2} for order. Returns a negative 84 integer, zero, or a positive integer as the property value in 85 the first item is less than, equal to, or greater than the property 86 value in the second item. If the C{sortDirection} is false the 87 returned value is negated. 88 89 The comparator set for this C{DefaultItemSorter} is used for 90 comparing the two property values. 91 92 @param propertyId: 93 The property id for the property that is used for comparison. 94 @param sortDirection: 95 The direction of the sort. A false value negates the result. 96 @param item1: 97 The first item to compare. 98 @param item2: 99 The second item to compare. 100 @return: a negative, zero, or positive integer if the property value in 101 the first item is less than, equal to, or greater than the 102 property value in the second item. Negated if 103 C{sortDirection} is false. 104 """ 105 property1 = item1.getItemProperty(propertyId) 106 property2 = item2.getItemProperty(propertyId) 107 108 # Get the values to compare 109 value1 = None if property1 is None else property1.getValue() 110 value2 = None if property2 is None else property2.getValue() 111 112 # Result of the comparison 113 r = 0 114 if sortDirection: 115 r = self._propertyValueComparator.compare(value1, value2) 116 else: 117 r = self._propertyValueComparator.compare(value2, value1) 118 119 return r
120 121
122 - def setSortProperties(self, container, propertyId, ascending):
123 self._container = container 124 125 # Removes any non-sortable property ids 126 ids = list() 127 orders = list() 128 sortable = container.getSortableContainerPropertyIds() 129 130 for i in range(len(propertyId)): 131 if propertyId[i] in sortable: 132 ids.append(propertyId[i]) 133 order = bool(ascending[i]) if i < len(ascending) else True 134 orders.append(order) 135 136 self._sortPropertyIds = list(ids) 137 self._sortDirections = [None] * len(orders) 138 139 for i in range(len(self._sortDirections)): 140 self._sortDirections[i] = bool( orders[i] )
141 142
143 -class DefaultPropertyValueComparator(object):
144 """Provides a default comparator used for comparing L{Property} values. 145 The C{DefaultPropertyValueComparator} assumes all objects it compares 146 can be cast to Comparable. 147 """ 148
149 - def __call__(self, o1, o2):
150 return self.compare(o1, o1)
151 152
153 - def compare(self, o1, o2):
154 r = 0 155 # Normal non-null comparison 156 if o1 is not None and o2 is not None: 157 # Assume the objects to be comparable 158 r = cmp(o1, o2) 159 elif o1 == o2: 160 # Objects are equal if both are null 161 r = 0 162 elif o1 is None: 163 # null is less than non-null 164 r = -1 165 else: 166 # non-null is greater than null 167 r = 1 168 return r
169