| 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.serializer; |
| 17 | |
| 18 | import java.io.Serializable; |
| 19 | |
| 20 | import org.deduced.PropertyCollection; |
| 21 | import org.deduced.implementation.NamedPropertyCollectionImplementation; |
| 22 | import org.deduced.viewer.model.View; |
| 23 | import org.deduced.viewer.web.shared.Model; |
| 24 | import org.deduced.viewer.web.shared.ModelListModel; |
| 25 | import org.deduced.viewer.web.shared.PopupPanelModel; |
| 26 | import org.deduced.viewer.web.shared.ViewModel; |
| 27 | import org.deduced.viewer.web.shared.WidgetModel; |
| 28 | |
| 29 | /** |
| 30 | * View Model Serializer |
| 31 | * |
| 32 | * @author Steve McDuff |
| 33 | * |
| 34 | */ |
| 35 | public class ViewModelSerializer extends ModelSerializer |
| 36 | { |
| 37 | /** |
| 38 | * fill ViewModel |
| 39 | * |
| 40 | * @param model model to serialize |
| 41 | * @param viewModel model to fill |
| 42 | * @param masterSerializer master serializer used to serialize child |
| 43 | * objects. |
| 44 | */ |
| 45 | @SuppressWarnings({"unchecked", "boxing"}) |
| 46 | private static void fillViewModel( |
| 47 | PropertyCollection<?, ?> model, ViewModel viewModel, |
| 48 | MasterWebSerializer masterSerializer) |
| 49 | { |
| 50 | PropertyCollection<?, ?> content = |
| 51 | (PropertyCollection<?, ?>) model |
| 52 | .getPropertyValue(View.ROOT_COMPONENT_INSTANCE.getKey()); |
| 53 | |
| 54 | WidgetModel rootComponent = |
| 55 | (WidgetModel) masterSerializer.serialize(content); |
| 56 | viewModel.setRootComponent(rootComponent); |
| 57 | |
| 58 | PropertyCollection<?, ?> popupList = |
| 59 | (PropertyCollection<?, ?>) model |
| 60 | .getPropertyValue(View.POPUP_PANEL_LIST_INSTANCE.getKey()); |
| 61 | |
| 62 | ModelListModel<PopupPanelModel> serializedPopupList = |
| 63 | (ModelListModel<PopupPanelModel>) masterSerializer |
| 64 | .serialize(popupList); |
| 65 | viewModel.setPopupPanelList(serializedPopupList); |
| 66 | |
| 67 | String name = NamedPropertyCollectionImplementation.getName(model); |
| 68 | viewModel.setName(name); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * (non-JSDoc) |
| 74 | * |
| 75 | * @see org.deduced.viewer.web.serializer.PanelModelSerializer#createSerializedObject(org.deduced.PropertyCollection) |
| 76 | */ |
| 77 | @Override |
| 78 | public Model createSerializedObject( |
| 79 | PropertyCollection<?, ?> collection) |
| 80 | { |
| 81 | return new ViewModel(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * (non-JSDoc) |
| 86 | * |
| 87 | * @see org.deduced.viewer.web.serializer.CellPanelModelSerializer#fillSerializedObject(java.io.Serializable, |
| 88 | * org.deduced.PropertyCollection, |
| 89 | * org.deduced.viewer.web.serializer.MasterWebSerializer) |
| 90 | */ |
| 91 | @Override |
| 92 | public void fillSerializedObject( |
| 93 | Serializable serializedObject, PropertyCollection<?, ?> collection, |
| 94 | MasterWebSerializer masterSerializer) |
| 95 | { |
| 96 | fillViewModel(collection, (ViewModel) serializedObject, |
| 97 | masterSerializer); |
| 98 | |
| 99 | super.fillSerializedObject(serializedObject, collection, |
| 100 | masterSerializer); |
| 101 | } |
| 102 | } |