| 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.allen_sauer.gwt.dnd.client.DragContext; |
| 19 | import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController; |
| 20 | import com.google.gwt.event.dom.client.KeyPressEvent; |
| 21 | import com.google.gwt.event.dom.client.KeyPressHandler; |
| 22 | import com.google.gwt.event.dom.client.KeyUpEvent; |
| 23 | import com.google.gwt.event.dom.client.KeyUpHandler; |
| 24 | import com.google.gwt.event.dom.client.MouseUpEvent; |
| 25 | import com.google.gwt.event.dom.client.MouseUpHandler; |
| 26 | import com.google.gwt.event.logical.shared.ValueChangeEvent; |
| 27 | import com.google.gwt.event.logical.shared.ValueChangeHandler; |
| 28 | import com.google.gwt.i18n.client.HasDirection.Direction; |
| 29 | import com.google.gwt.user.client.Timer; |
| 30 | import com.google.gwt.user.client.rpc.AsyncCallback; |
| 31 | import com.google.gwt.user.client.ui.TextBoxBase; |
| 32 | import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment; |
| 33 | import com.google.gwt.user.client.ui.Widget; |
| 34 | |
| 35 | /** |
| 36 | * |
| 37 | * TextBoxBase Model |
| 38 | * |
| 39 | * @author Steve McDuff |
| 40 | * |
| 41 | */ |
| 42 | public abstract class TextBoxBaseModel extends FocusWidgetModel implements |
| 43 | ValueChangeHandler<String>, KeyPressHandler, MouseUpHandler, KeyUpHandler |
| 44 | { |
| 45 | |
| 46 | /** |
| 47 | * how many milliseconds should the text box wait to detect changes and send |
| 48 | * it to the server |
| 49 | */ |
| 50 | private static final int TEXT_CHANGE_DETECTION_WAIT_TIME = 100; |
| 51 | |
| 52 | /** |
| 53 | * serialVersionUID |
| 54 | */ |
| 55 | private static final long serialVersionUID = -1003728423882821003L; |
| 56 | |
| 57 | /** |
| 58 | * text to display in the text box |
| 59 | */ |
| 60 | private String text; |
| 61 | |
| 62 | /** |
| 63 | * direction name |
| 64 | */ |
| 65 | private String direction; |
| 66 | |
| 67 | /** |
| 68 | * text alignment name |
| 69 | */ |
| 70 | private String textAlignment; |
| 71 | |
| 72 | /** |
| 73 | * is the text box read only |
| 74 | */ |
| 75 | private boolean readOnly; |
| 76 | |
| 77 | /** |
| 78 | * change timer |
| 79 | */ |
| 80 | private transient Timer timer; |
| 81 | |
| 82 | /** |
| 83 | * drop controller |
| 84 | */ |
| 85 | private transient SimpleDropController dropController; |
| 86 | |
| 87 | /** |
| 88 | * get TextBoxBase |
| 89 | * |
| 90 | * @return the text box base |
| 91 | */ |
| 92 | public TextBoxBase getTextBoxBase() |
| 93 | { |
| 94 | return (TextBoxBase) getUIObject(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * (non-JSDoc) |
| 99 | * |
| 100 | * @see org.deduced.viewer.web.shared.FocusWidgetModel#internalSynchronizeWithUI() |
| 101 | */ |
| 102 | @Override |
| 103 | public void internalSynchronizeWithUI() |
| 104 | { |
| 105 | TextBoxBase label = getTextBoxBase(); |
| 106 | label.setReadOnly(isReadOnly()); |
| 107 | label.setText(getText()); |
| 108 | label.setName(getName()); |
| 109 | |
| 110 | TextAlignment setTextAlignment = getTextAlignmentConstant(); |
| 111 | if (setTextAlignment != null) |
| 112 | { |
| 113 | label.setAlignment(setTextAlignment); |
| 114 | } |
| 115 | |
| 116 | super.internalSynchronizeWithUI(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * get Direction |
| 121 | * |
| 122 | * @return the direction used in the widget |
| 123 | */ |
| 124 | public Direction getDirectionConstant() |
| 125 | { |
| 126 | return getDirection(getDirection()); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * get the Text Alignment constant to use in the widget |
| 131 | * |
| 132 | * @return the Text Alignment constant to use in the widget |
| 133 | */ |
| 134 | public TextAlignment getTextAlignmentConstant() |
| 135 | { |
| 136 | return getTextAlignment(getTextAlignment()); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * (non-JSDoc) |
| 141 | * |
| 142 | * @see org.deduced.viewer.web.shared.FocusWidgetModel#propertyChanged(org.deduced.viewer.web.shared.ChangeEvent) |
| 143 | */ |
| 144 | @Override |
| 145 | public void propertyChanged( |
| 146 | ChangeEvent event) |
| 147 | { |
| 148 | if (Utilities.equals(event.getName(), "text")) |
| 149 | { |
| 150 | setText((String) event.getSerializableValue()); |
| 151 | synchronizeWithUI(); |
| 152 | } |
| 153 | else if (Utilities.equals(event.getName(), "read only")) |
| 154 | { |
| 155 | setReadOnly(((Boolean) event.getSerializableValue()).booleanValue()); |
| 156 | synchronizeWithUI(); |
| 157 | } |
| 158 | else if (Utilities.equals(event.getName(), "direction")) |
| 159 | { |
| 160 | setDirection((String) event.getSerializableValue()); |
| 161 | synchronizeWithUI(); |
| 162 | } |
| 163 | else if (Utilities.equals(event.getName(), "text alignment")) |
| 164 | { |
| 165 | setTextAlignment((String) event.getSerializableValue()); |
| 166 | synchronizeWithUI(); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | super.propertyChanged(event); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @param setDirection the direction to set |
| 176 | */ |
| 177 | public void setDirection( |
| 178 | String setDirection) |
| 179 | { |
| 180 | direction = setDirection; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return the direction |
| 185 | */ |
| 186 | public String getDirection() |
| 187 | { |
| 188 | return direction; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param setReadOnly the readOnly to set |
| 193 | */ |
| 194 | public void setReadOnly( |
| 195 | boolean setReadOnly) |
| 196 | { |
| 197 | readOnly = setReadOnly; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @return the readOnly |
| 202 | */ |
| 203 | public boolean isReadOnly() |
| 204 | { |
| 205 | return readOnly; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param setText the text to set |
| 210 | */ |
| 211 | public void setText( |
| 212 | String setText) |
| 213 | { |
| 214 | text = setText; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @return the text |
| 219 | */ |
| 220 | public String getText() |
| 221 | { |
| 222 | return text; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @param setTextAlignment the textAlignment to set |
| 227 | */ |
| 228 | public void setTextAlignment( |
| 229 | String setTextAlignment) |
| 230 | { |
| 231 | textAlignment = setTextAlignment; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @return the textAlignment |
| 236 | */ |
| 237 | public String getTextAlignment() |
| 238 | { |
| 239 | return textAlignment; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * (non-JSDoc) |
| 244 | * |
| 245 | * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent) |
| 246 | */ |
| 247 | @Override |
| 248 | public void onValueChange( |
| 249 | ValueChangeEvent<String> event) |
| 250 | { |
| 251 | detectTextChange(); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * fire Text Change Update |
| 256 | * |
| 257 | * @param newValue the new text value from the UI |
| 258 | */ |
| 259 | public void fireTextChangeUpdate( |
| 260 | String newValue) |
| 261 | { |
| 262 | ModelRegistry modelRegistry = getModelRegistry(); |
| 263 | AsyncCallback<Void> callback = modelRegistry.getDefaultVoidCallback(); |
| 264 | String id = getId(); |
| 265 | setText(newValue); |
| 266 | modelRegistry.updateProperty(id, "text", newValue, callback); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * (non-JSDoc) |
| 271 | * |
| 272 | * @see com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google.gwt.event.dom.client.KeyPressEvent) |
| 273 | */ |
| 274 | @Override |
| 275 | public void onKeyPress( |
| 276 | KeyPressEvent event) |
| 277 | { |
| 278 | startTextChangeTimer(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * (non-JSDoc) |
| 283 | * |
| 284 | * @see com.google.gwt.event.dom.client.MouseUpHandler#onMouseUp(com.google.gwt.event.dom.client.MouseUpEvent) |
| 285 | */ |
| 286 | @Override |
| 287 | public void onMouseUp( |
| 288 | MouseUpEvent event) |
| 289 | { |
| 290 | startTextChangeTimer(); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * (non-JSDoc) |
| 295 | * |
| 296 | * @see com.google.gwt.event.dom.client.KeyUpHandler#onKeyUp(com.google.gwt.event.dom.client.KeyUpEvent) |
| 297 | */ |
| 298 | @Override |
| 299 | public void onKeyUp( |
| 300 | KeyUpEvent event) |
| 301 | { |
| 302 | startTextChangeTimer(); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * detect Text Change and trigger an update if required |
| 307 | */ |
| 308 | public void detectTextChange() |
| 309 | { |
| 310 | if (timer != null) |
| 311 | { |
| 312 | timer.cancel(); |
| 313 | timer = null; |
| 314 | } |
| 315 | |
| 316 | TextBoxBase textBoxBase = getTextBoxBase(); |
| 317 | if (textBoxBase != null) |
| 318 | { |
| 319 | String currentDisplayText = textBoxBase.getText(); |
| 320 | if (!Utilities.equals(text, currentDisplayText)) |
| 321 | { |
| 322 | fireTextChangeUpdate(currentDisplayText); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * |
| 329 | * start Text Change Timer |
| 330 | */ |
| 331 | public void startTextChangeTimer() |
| 332 | { |
| 333 | if (timer == null) |
| 334 | { |
| 335 | timer = new Timer() |
| 336 | { |
| 337 | @Override |
| 338 | public void run() |
| 339 | { |
| 340 | detectTextChange(); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | timer.schedule(TEXT_CHANGE_DETECTION_WAIT_TIME); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * createTextBoxBaseDropController |
| 350 | * |
| 351 | * @param widget widget for which to create the drop controller |
| 352 | */ |
| 353 | public void createTextBoxBaseDropController( |
| 354 | Widget widget) |
| 355 | { |
| 356 | dropController = new SimpleDropController(widget) |
| 357 | { |
| 358 | @Override |
| 359 | public void onDrop( |
| 360 | DragContext context) |
| 361 | { |
| 362 | startTextChangeTimer(); |
| 363 | } |
| 364 | }; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * get Drop Controller |
| 369 | * |
| 370 | * @return the drop controller |
| 371 | */ |
| 372 | public SimpleDropController getDropController() |
| 373 | { |
| 374 | return dropController; |
| 375 | } |
| 376 | } |