| 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 | import com.google.gwt.user.client.rpc.AsyncCallback; |
| 21 | |
| 22 | /** |
| 23 | * Model is the base class for client side objects that map to a property |
| 24 | * collection. Each model has a unique ID that matches the unique ID of the |
| 25 | * collection on the server side. A model is also registered on a model |
| 26 | * registry. This is necessary for change events on the server collection to be |
| 27 | * sent back to the Model. |
| 28 | * |
| 29 | * @author Steve McDuff |
| 30 | * |
| 31 | */ |
| 32 | public abstract class Model implements Serializable |
| 33 | { |
| 34 | /** |
| 35 | * remove event type name |
| 36 | */ |
| 37 | public static final String REMOVE_TYPE_NAME = "remove"; |
| 38 | |
| 39 | /** |
| 40 | * delete event type name |
| 41 | */ |
| 42 | public static final String DELETE_TYPE_NAME = "delete"; |
| 43 | |
| 44 | /** |
| 45 | * add event type name |
| 46 | */ |
| 47 | public static final String ADD_TYPE_NAME = "add"; |
| 48 | |
| 49 | /** |
| 50 | * update event type name |
| 51 | */ |
| 52 | public static final String UPDATE_TYPE_NAME = "update"; |
| 53 | |
| 54 | /** |
| 55 | * serialVersionUID |
| 56 | */ |
| 57 | private static final long serialVersionUID = 3880173030440089442L; |
| 58 | |
| 59 | /** |
| 60 | * model unique ID |
| 61 | */ |
| 62 | private String id; |
| 63 | |
| 64 | /** |
| 65 | * the model registry on which this model is registered |
| 66 | */ |
| 67 | private transient ModelRegistry modelRegistry; |
| 68 | |
| 69 | /** |
| 70 | * current parent component |
| 71 | */ |
| 72 | private transient ModelContainer parent; |
| 73 | |
| 74 | /** |
| 75 | * get Parent |
| 76 | * |
| 77 | * @return the parent |
| 78 | */ |
| 79 | public ModelContainer getParent() |
| 80 | { |
| 81 | return parent; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * set Parent |
| 86 | * |
| 87 | * @param setParent the current parent |
| 88 | */ |
| 89 | public void setParent( |
| 90 | ModelContainer setParent) |
| 91 | { |
| 92 | parent = setParent; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * notify Parent That Model Changed |
| 97 | */ |
| 98 | public void notifyParentThatModelChanged() |
| 99 | { |
| 100 | if (parent != null) |
| 101 | { |
| 102 | parent.modelChanged(this); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * handle a property change event. Override this method to handle events in |
| 108 | * child objects. |
| 109 | * |
| 110 | * @param event the change event |
| 111 | */ |
| 112 | public void propertyChanged( |
| 113 | @SuppressWarnings("unused") ChangeEvent event) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Delete the local object and remove it from the model registry. Override |
| 119 | * this method to also delete child objects, but calling super.delete() is |
| 120 | * mandatory. |
| 121 | */ |
| 122 | public void delete() |
| 123 | { |
| 124 | ModelRegistry currentRegistry = getModelRegistry(); |
| 125 | if (currentRegistry != null) |
| 126 | { |
| 127 | currentRegistry.unRegisterModel(this); |
| 128 | setModelRegistry(null); |
| 129 | } |
| 130 | |
| 131 | deleteChildModels(); |
| 132 | |
| 133 | setParent(null); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * delete Child Models : Override this method to implement this behavior |
| 138 | */ |
| 139 | protected void deleteChildModels() |
| 140 | { |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * register Model |
| 146 | * |
| 147 | * @param registry the model registry to use. |
| 148 | */ |
| 149 | public void registerModel( |
| 150 | ModelRegistry registry) |
| 151 | { |
| 152 | setModelRegistry(registry); |
| 153 | registry.registerModel(this); |
| 154 | registerChildModels(); |
| 155 | registrationComplete(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * registration Complete is called when the entire object structure |
| 160 | * registration is completed. Override as necessary. |
| 161 | */ |
| 162 | protected void registrationComplete() |
| 163 | { |
| 164 | |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * register Child Models. Override this method to register child models. |
| 169 | */ |
| 170 | protected void registerChildModels() |
| 171 | { |
| 172 | |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * check if the event name is equal to a specified name |
| 177 | * |
| 178 | * @param event the event to use |
| 179 | * @param name the name to match |
| 180 | * @return true if the event name matches the desired name |
| 181 | */ |
| 182 | public static boolean isEventNameEqual( |
| 183 | ChangeEvent event, String name) |
| 184 | { |
| 185 | return Utilities.equals(event.getName(), name); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * is Event delete |
| 190 | * |
| 191 | * @param event the event to analyze |
| 192 | * @return true if the event is a delete event |
| 193 | */ |
| 194 | public static boolean isEventDelete( |
| 195 | ChangeEvent event) |
| 196 | { |
| 197 | return Utilities.equals(event.getType(), DELETE_TYPE_NAME); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * is Event Update |
| 202 | * |
| 203 | * @param event the event to analyze |
| 204 | * @return true if the event is an update event |
| 205 | */ |
| 206 | public static boolean isEventUpdate( |
| 207 | ChangeEvent event) |
| 208 | { |
| 209 | return Utilities.equals(event.getType(), UPDATE_TYPE_NAME); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param event the event to analyze |
| 214 | * @return true if the event is an addition event |
| 215 | */ |
| 216 | public static boolean isEventAdd( |
| 217 | ChangeEvent event) |
| 218 | { |
| 219 | return Utilities.equals(event.getType(), ADD_TYPE_NAME); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @param event event to verify |
| 224 | * @return true if the event is a remove event |
| 225 | */ |
| 226 | public static boolean isEventRemove( |
| 227 | ChangeEvent event) |
| 228 | { |
| 229 | return Utilities.equals(event.getType(), REMOVE_TYPE_NAME); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * update a model by deleting the old model and replacing it with the new |
| 234 | * one |
| 235 | * |
| 236 | * @param <Y> the type of model to update |
| 237 | * @param newChildModel the new model |
| 238 | * @param oldModel the old model |
| 239 | * @param setParent the model parent |
| 240 | * @return the new child item list |
| 241 | */ |
| 242 | public <Y extends Model> Y updateModel( |
| 243 | Y newChildModel, Y oldModel, ModelContainer setParent) |
| 244 | { |
| 245 | if (oldModel != newChildModel) |
| 246 | { |
| 247 | if (oldModel != null) |
| 248 | { |
| 249 | oldModel.delete(); |
| 250 | } |
| 251 | |
| 252 | registerChildModel(newChildModel, setParent); |
| 253 | } |
| 254 | return newChildModel; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * register Child Model |
| 259 | * |
| 260 | * @param childItem the child item to register |
| 261 | * @param setParent model parent |
| 262 | */ |
| 263 | public void registerChildModel( |
| 264 | Model childItem, ModelContainer setParent) |
| 265 | { |
| 266 | if (childItem != null) |
| 267 | { |
| 268 | childItem.setParent(setParent); |
| 269 | childItem.registerModel(getModelRegistry()); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * delete Child Model |
| 275 | * |
| 276 | * @param childModel the child model to delete |
| 277 | */ |
| 278 | public void deleteChildModel( |
| 279 | Model childModel) |
| 280 | { |
| 281 | if (childModel != null) |
| 282 | { |
| 283 | childModel.delete(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @param setId the id to set |
| 289 | */ |
| 290 | public void setId( |
| 291 | String setId) |
| 292 | { |
| 293 | id = setId; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @return the id |
| 298 | */ |
| 299 | public String getId() |
| 300 | { |
| 301 | return id; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param setModelRegistry the modelRegistry to set |
| 306 | */ |
| 307 | public void setModelRegistry( |
| 308 | ModelRegistry setModelRegistry) |
| 309 | { |
| 310 | modelRegistry = setModelRegistry; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @return the modelRegistry |
| 315 | */ |
| 316 | public ModelRegistry getModelRegistry() |
| 317 | { |
| 318 | return modelRegistry; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * trigger Signal |
| 323 | * |
| 324 | * @param signalName signal name to trigger |
| 325 | */ |
| 326 | public void triggerSignal( |
| 327 | String signalName) |
| 328 | { |
| 329 | ModelRegistry currentModelRegistry = getModelRegistry(); |
| 330 | AsyncCallback<Void> defaultVoidCallback = |
| 331 | currentModelRegistry.getDefaultVoidCallback(); |
| 332 | String currentId = getId(); |
| 333 | currentModelRegistry.signalTriggered(currentId, signalName, |
| 334 | defaultVoidCallback); |
| 335 | } |
| 336 | } |