| 1 | /** |
| 2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.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 | */ |
| 16 | package org.deduced.viewer.web.shared; |
| 17 | |
| 18 | import java.io.Serializable; |
| 19 | |
| 20 | /** |
| 21 | * an Ordered Model always contains a sorting value. This sorting value is |
| 22 | * derived from the sorting instance stored on the server side. Since ordering |
| 23 | * might change, Ordered models always notify their containers when their |
| 24 | * content change so that they might adapt to the new potential order. |
| 25 | * |
| 26 | * @author Steve McDuff |
| 27 | * |
| 28 | */ |
| 29 | public abstract class OrderedModel extends Model implements |
| 30 | Comparable<OrderedModel> |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * serialVersionUID |
| 35 | */ |
| 36 | private static final long serialVersionUID = 3404776290823513380L; |
| 37 | |
| 38 | /** |
| 39 | * value used to sort ordered models relative to each other. Matches the |
| 40 | * sorting instance stored in the ordered list on the server side. |
| 41 | */ |
| 42 | private SerializedValue sortingValue; |
| 43 | |
| 44 | /** |
| 45 | * (non-JSDoc) |
| 46 | * |
| 47 | * @see org.deduced.viewer.web.shared.Model#propertyChanged(org.deduced.viewer.web.shared.ChangeEvent) |
| 48 | */ |
| 49 | @Override |
| 50 | public void propertyChanged( |
| 51 | ChangeEvent event) |
| 52 | { |
| 53 | if (event.isSortingInstance()) |
| 54 | { |
| 55 | SerializedValue value = event.getValue(); |
| 56 | updateSortingValue(value); |
| 57 | } |
| 58 | |
| 59 | super.propertyChanged(event); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * update Sorting Value |
| 64 | * |
| 65 | * @param newValue the new sorting value |
| 66 | */ |
| 67 | public void updateSortingValue( |
| 68 | SerializedValue newValue) |
| 69 | { |
| 70 | sortingValue = newValue; |
| 71 | |
| 72 | notifyParentThatModelChanged(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * (non-JSDoc) |
| 77 | * |
| 78 | * @see java.lang.Comparable#compareTo(java.lang.Object) |
| 79 | */ |
| 80 | @Override |
| 81 | public int compareTo( |
| 82 | OrderedModel o) |
| 83 | { |
| 84 | if (o == null) |
| 85 | { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | if (getSerializableSortingValue() instanceof Comparable<?>) |
| 90 | { |
| 91 | if (o.getSerializableSortingValue() instanceof Comparable<?>) |
| 92 | { |
| 93 | return compareComparableObjects(getSerializableSortingValue(), |
| 94 | o.getSerializableSortingValue()); |
| 95 | } |
| 96 | } |
| 97 | return 0; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * compare Comparable Objects |
| 103 | * |
| 104 | * @param propertyValue1 first comparable value |
| 105 | * @param propertyValue2 second comparable value |
| 106 | * @return the comparison delta |
| 107 | */ |
| 108 | @SuppressWarnings("unchecked") |
| 109 | public int compareComparableObjects( |
| 110 | Object propertyValue1, Object propertyValue2) |
| 111 | { |
| 112 | int returnValue = 0; |
| 113 | try |
| 114 | { |
| 115 | Comparable comparable1 = (Comparable) propertyValue1; |
| 116 | returnValue = comparable1.compareTo(propertyValue2); |
| 117 | } |
| 118 | catch (Exception ex) |
| 119 | { |
| 120 | getModelRegistry().showErrorException( |
| 121 | "caught error while comparing 2 objects.", ex); |
| 122 | } |
| 123 | return returnValue; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * get Sorting Value |
| 128 | * |
| 129 | * @return the current sorting value |
| 130 | */ |
| 131 | public SerializedValue getSortingValue() |
| 132 | { |
| 133 | return sortingValue; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * set Sorting Value |
| 138 | * |
| 139 | * @param setSortingValue new sorting value |
| 140 | */ |
| 141 | public void setSortingValue( |
| 142 | SerializedValue setSortingValue) |
| 143 | { |
| 144 | sortingValue = setSortingValue; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param setSortingValue the sortingValue to set |
| 149 | */ |
| 150 | public void setSerializableSortingValue( |
| 151 | Serializable setSortingValue) |
| 152 | { |
| 153 | sortingValue = Serializer.serialize(setSortingValue); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return the sortingValue in serializable form |
| 158 | */ |
| 159 | public Serializable getSerializableSortingValue() |
| 160 | { |
| 161 | if (sortingValue != null) |
| 162 | { |
| 163 | return sortingValue.getValue(); |
| 164 | } |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * get the Ordered Value |
| 170 | * |
| 171 | * @return the ordered value, based on the type being ordered |
| 172 | */ |
| 173 | public abstract Object getOrderedValue(); |
| 174 | } |