| 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 com.google.gwt.event.logical.shared.ValueChangeEvent; |
| 19 | import com.google.gwt.event.logical.shared.ValueChangeHandler; |
| 20 | import com.google.gwt.user.client.rpc.AsyncCallback; |
| 21 | import com.google.gwt.user.client.ui.CheckBox; |
| 22 | import com.google.gwt.user.client.ui.UIObject; |
| 23 | |
| 24 | /** |
| 25 | * CheckBoxModel |
| 26 | * |
| 27 | * @author Steve McDuff |
| 28 | * |
| 29 | */ |
| 30 | public class CheckBoxModel extends ButtonBaseModel implements |
| 31 | ValueChangeHandler<Boolean> |
| 32 | { |
| 33 | |
| 34 | /** |
| 35 | * serialVersionUID |
| 36 | */ |
| 37 | private static final long serialVersionUID = -844003199493729952L; |
| 38 | |
| 39 | /** |
| 40 | * is checked |
| 41 | */ |
| 42 | private boolean isChecked; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * CheckBoxModel constructor |
| 47 | */ |
| 48 | public CheckBoxModel() |
| 49 | { |
| 50 | |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * (non-JSDoc) |
| 55 | * |
| 56 | * @see org.deduced.viewer.web.shared.WidgetModel#createUIObject() |
| 57 | */ |
| 58 | @Override |
| 59 | public UIObject createUIObject() |
| 60 | { |
| 61 | CheckBox checkBox = new CheckBox(); |
| 62 | checkBox.addValueChangeHandler(this); |
| 63 | return checkBox; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * (non-JSDoc) |
| 68 | * |
| 69 | * @see org.deduced.viewer.web.shared.ButtonBaseModel#internalSynchronizeWithUI() |
| 70 | */ |
| 71 | @Override |
| 72 | public void internalSynchronizeWithUI() |
| 73 | { |
| 74 | CheckBox checkBox = getCheckBox(); |
| 75 | checkBox.setValue(Boolean.valueOf(isChecked), false); |
| 76 | |
| 77 | super.internalSynchronizeWithUI(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * get Check Box |
| 82 | * |
| 83 | * @return the check box |
| 84 | */ |
| 85 | public CheckBox getCheckBox() |
| 86 | { |
| 87 | return (CheckBox) getUIObject(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param setChecked the isChecked to set |
| 92 | */ |
| 93 | public void setIsChecked( |
| 94 | boolean setChecked) |
| 95 | { |
| 96 | isChecked = setChecked; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return the isChecked |
| 101 | */ |
| 102 | public boolean isChecked() |
| 103 | { |
| 104 | return isChecked; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * (non-JSDoc) |
| 109 | * |
| 110 | * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent) |
| 111 | */ |
| 112 | @Override |
| 113 | public void onValueChange( |
| 114 | ValueChangeEvent<Boolean> event) |
| 115 | { |
| 116 | Boolean newValue = event.getValue(); |
| 117 | propagateValueChanged(newValue); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * propagate Value Changed |
| 122 | * |
| 123 | * @param newValue the new value |
| 124 | */ |
| 125 | protected void propagateValueChanged( |
| 126 | Boolean newValue) |
| 127 | { |
| 128 | ModelRegistry modelRegistry = getModelRegistry(); |
| 129 | AsyncCallback<Void> callBack = modelRegistry.getDefaultVoidCallback(); |
| 130 | String id = getId(); |
| 131 | isChecked = newValue.booleanValue(); |
| 132 | modelRegistry.updateProperty(id, "is checked", newValue, callBack); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * (non-JSDoc) |
| 137 | * |
| 138 | * @see org.deduced.viewer.web.shared.ButtonBaseModel#propertyChanged(org.deduced.viewer.web.shared.ChangeEvent) |
| 139 | */ |
| 140 | @Override |
| 141 | public void propertyChanged( |
| 142 | ChangeEvent event) |
| 143 | { |
| 144 | if (Utilities.equals(event.getName(), "is checked")) |
| 145 | { |
| 146 | setIsChecked(((Boolean) event.getSerializableValue()) |
| 147 | .booleanValue()); |
| 148 | synchronizeWithUI(); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | super.propertyChanged(event); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } |