1 30 package com.genimen.djeneric.tools.specifier.components; 31 32 import java.awt.Component ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.event.KeyEvent ; 35 36 import javax.swing.AbstractAction ; 37 import javax.swing.JButton ; 38 import javax.swing.JComboBox ; 39 import javax.swing.JComponent ; 40 import javax.swing.JTextArea ; 41 import javax.swing.JTextField ; 42 import javax.swing.KeyStroke ; 43 44 import com.genimen.djeneric.language.Messages; 45 import com.genimen.djeneric.repository.exceptions.DjenericException; 46 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 47 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 48 import com.genimen.djeneric.ui.DjFocusMeLater; 49 import com.genimen.djeneric.util.DjLogger; 50 51 57 public class BindingMediator 58 { 59 protected String _propertyName = null; 60 protected ObjectViewer _viewer; 61 protected int _cellIndex; 62 private boolean _updateable = false; 63 private boolean _clearStatusOnExit = false; 64 private DjBindable _comp; 65 private boolean wasSetup = false; 66 67 public BindingMediator(DjBindable comp) 68 { 69 _comp = comp; 70 } 71 72 public BindingMediator(DjBindable comp, ObjectViewer viewer, String propertyName) throws DjenericException 73 { 74 _comp = comp; 75 _viewer = viewer; 76 setPropertyName(propertyName); 77 } 78 79 protected void setupBinder() 80 { 81 try 82 { 83 if (!wasSetup && _viewer != null && _propertyName != null) 84 { 85 _cellIndex = _viewer.getCellIndex(_propertyName); 86 _updateable = _viewer.isCellUpdateable(_cellIndex); 87 bindKeyboardActions(this, _viewer, _cellIndex, _comp, true); 88 wasSetup = true; 89 } 90 } 91 catch (ObjectNotDefinedException e) 92 { 93 _viewer.setStatusMessage(e); 94 } 95 } 96 97 public DjBindable getBindable() 98 { 99 return _comp; 100 } 101 102 public void setPropertyName(String propertyName) throws DjenericException 103 { 104 _propertyName = propertyName; 105 setupBinder(); 106 107 } 108 109 public void setViewer(ObjectViewer viewer) 110 { 111 _viewer = viewer; 112 setupBinder(); 113 } 114 115 public void synchronizeComponent() throws DjenericException 116 { 117 _comp.synchronize(); 118 } 119 120 public void setValue(Object value) throws DjenericException 121 { 122 if (_viewer == null) return; 123 124 if (!createIfNeeded(value)) 125 { 126 return; 127 } 128 129 try 130 { 131 boolean propertyChanged = false; 132 133 if (shouldUpdate(value)) 134 { 135 if (!isComponentWritable()) 136 { 137 _viewer.setStatusMessage(Messages.getString("BindingMediator.ValueNotUpdateable"), false); 138 synchronizeComponent(); 139 } 140 else 141 { 142 setPropertyValue(value); 143 propertyChanged = true; 144 } 145 } 146 147 if (propertyChanged || _clearStatusOnExit) 149 { 150 _viewer.setStatusMessage(Messages.getString("global.Ok"), true); 151 } 152 _clearStatusOnExit = false; 153 } 154 catch (DjenericException ce) 155 { 156 _viewer.setStatusMessage(DjenericException.getNiceMessage(ce), false); 157 _clearStatusOnExit = true; 158 new DjFocusMeLater((Component ) _comp); 160 } 161 } 162 163 private boolean shouldUpdate(Object value) throws DjenericException 164 { 165 String curr = getPropertyValueString(); 166 if (curr == null && value != null) return true; 167 if (curr != null && value == null) return true; 168 169 return !getPropertyValueString().equals(value.toString()); 170 } 171 172 public boolean isComponentWritable() 173 { 174 try 175 { 176 return (_viewer.isEditable() && _updateable && !_viewer.isRestricted(_cellIndex)) || _viewer.isNew(); 177 } 178 catch (Exception x) 179 { 180 _viewer.setStatusMessage(x); 181 } 182 return false; 183 } 184 185 public String getPropertyValueString() throws DjenericException 186 { 187 return _viewer.getString(_cellIndex); 188 } 189 190 public Object getPropertyValue() throws DjenericException 191 { 192 return _viewer.getValue(_cellIndex); 193 } 194 195 public void setPropertyValue(String strVal) throws DjenericException 196 { 197 _viewer.setString(_cellIndex, strVal); 198 } 199 200 public void setPropertyValue(Object obj) throws DjenericException 201 { 202 _viewer.setValue(_cellIndex, obj); 203 } 204 205 public boolean createIfNeeded(Object initialValue) throws DjenericException 206 { 207 if (_viewer != null && _viewer.getModel().isNoObjectsPresent()) 208 { 209 if (initialValue instanceof String && initialValue.toString().trim().length() == 0) 210 { 211 return false; 213 } 214 if (!_viewer.getModel().isInsertable()) 215 { 216 synchronizeComponent(); 217 throw new DjenericException(Messages.getString("BindingMediator.CannotCreate", _viewer.getTitle())); 218 } 219 _viewer.createNew(); 220 if (initialValue != null) 221 { 222 _viewer.setValue(_cellIndex, initialValue); 223 } 224 synchronizeComponent(); 225 } 226 return true; 227 } 228 229 public static void bindKeyboardActions(BindingMediator mediator, ObjectViewer obj, int propertyIndex, 230 DjBindable comp, boolean isEditor) 231 { 232 JComponent target = comp.getFocussableComponent(); 233 234 if (comp instanceof JComboBox ) 235 { 236 JComboBox jco = (JComboBox ) comp; 237 238 if (jco.isEditable()) 239 { 240 for (int i = 0; i < jco.getComponentCount(); i++) 242 { 243 if (jco.getComponent(i) instanceof JTextField ) 244 { 245 target = (JComponent ) jco.getComponent(i); 246 } 247 } 248 } 249 else 250 { 251 for (int i = 0; i < jco.getComponentCount(); i++) 253 { 254 if (jco.getComponent(i) instanceof JButton ) 255 { 256 target = (JComponent ) jco.getComponent(i); 257 } 258 } 259 } 260 target.registerKeyboardAction(new ClearPropertyAction(obj, propertyIndex), KeyStroke 261 .getKeyStroke(KeyEvent.VK_DELETE, 0), JComponent.WHEN_FOCUSED); 262 263 } 264 265 if (target == null) 266 { 267 DjLogger.log(Messages.getString("BindingMediator.CannotBindTo", comp.getClass().getName())); 268 return; 269 } 270 271 target.registerKeyboardAction(new DeleteRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 272 ActionEvent.CTRL_MASK), 273 JComponent.WHEN_FOCUSED); 274 target.registerKeyboardAction(new InsertRowAction(mediator, obj), KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 275 ActionEvent.CTRL_MASK), 276 JComponent.WHEN_FOCUSED); 277 target.registerKeyboardAction(new ReloadAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK), 278 JComponent.WHEN_FOCUSED); 279 280 if (isEditor) 281 { 282 if (!(target instanceof JTextArea )) 283 { 284 target.registerKeyboardAction(new NextRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0), 285 JComponent.WHEN_FOCUSED); 286 287 target.registerKeyboardAction(new PrevRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0), 288 JComponent.WHEN_FOCUSED); 289 target.registerKeyboardAction(new NextRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 290 ActionEvent.SHIFT_MASK), 291 JComponent.WHEN_FOCUSED); 292 293 target.registerKeyboardAction(new PrevRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 294 ActionEvent.SHIFT_MASK), 295 JComponent.WHEN_FOCUSED); 296 } 297 target.registerKeyboardAction(new FirstRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 298 ActionEvent.CTRL_MASK), 299 JComponent.WHEN_FOCUSED); 300 301 target.registerKeyboardAction(new LastRowAction(obj), KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 302 ActionEvent.CTRL_MASK), 303 JComponent.WHEN_FOCUSED); 304 } 305 } 306 307 public String getPropertyName() 308 { 309 return _propertyName; 310 } 311 312 public ObjectViewer getViewer() 313 { 314 return _viewer; 315 } 316 317 } 318 319 321 class DeleteRowAction extends AbstractAction 322 { 323 324 private static final long serialVersionUID = 1L; 325 ObjectViewer _objectViewer; 326 327 333 public DeleteRowAction(ObjectViewer obj) 334 { 335 super("DeleteRowAction"); 336 _objectViewer = obj; 337 } 338 339 345 public void actionPerformed(ActionEvent e) 346 { 347 if (!_objectViewer.getModel().isNoObjectsPresent()) 348 { 349 try 350 { 351 if (!_objectViewer.isDeleteable() && !(_objectViewer.isNew() && _objectViewer.isInserteable())) 352 { 353 throw new Exception (Messages.getString("BindingMediator.DeletionOf", _objectViewer.getExtent() 354 .getNameSingular())); 355 } 356 357 _objectViewer.markForDelete(); 358 _objectViewer.tellRowPosition(); 359 } 360 catch (Exception x) 361 { 362 _objectViewer.setStatusMessage(x); 363 } 364 } 365 else 366 { 367 _objectViewer.setStatusMessage(Messages.getString("BindingMediator.NoMore", _objectViewer.getExtent() 368 .getNamePlural()), true); 369 } 370 371 } 372 } 373 374 class InsertRowAction extends AbstractAction 375 { 376 377 private static final long serialVersionUID = 1L; 378 ObjectViewer _objectViewer; 379 BindingMediator _mediator; 380 381 388 public InsertRowAction(BindingMediator mediator, ObjectViewer obj) 389 { 390 super("InsertRowAction"); 391 _objectViewer = obj; 392 _mediator = mediator; 393 394 } 395 396 402 public void actionPerformed(ActionEvent e) 403 { 404 try 405 { 406 if (!_objectViewer.isInserteable()) 407 { 408 throw new Exception (Messages.getString("BindingMediator.CreationOf", _objectViewer.getExtent().getNamePlural())); 409 } 410 411 if (_mediator.getBindable() != null) 417 { 418 if (!_mediator.createIfNeeded(_mediator.getBindable().getDisplayedValue())) 419 { 420 _objectViewer.createNew(); 421 } 422 else 423 { 424 _objectViewer.createNew(); 425 } 426 } 427 428 _objectViewer.tellRowPosition(); 429 } 430 catch (Exception x) 431 { 432 _objectViewer.setStatusMessage(x); 433 } 434 } 435 } 436 437 class NextRowAction extends AbstractAction 438 { 439 440 private static final long serialVersionUID = 1L; 441 ObjectViewer _objectViewer; 442 443 449 public NextRowAction(ObjectViewer obj) 450 { 451 super("NextRowAction"); 452 _objectViewer = obj; 453 } 454 455 461 public void actionPerformed(ActionEvent e) 462 { 463 try 464 { 465 _objectViewer.next(); 466 } 467 catch (Exception x) 468 { 469 _objectViewer.setStatusMessage(x); 470 } 471 } 472 } 473 474 class PrevRowAction extends AbstractAction 475 { 476 477 private static final long serialVersionUID = 1L; 478 ObjectViewer _objectViewer; 479 480 486 public PrevRowAction(ObjectViewer obj) 487 { 488 super("PrevRowAction"); 489 _objectViewer = obj; 490 } 491 492 498 public void actionPerformed(ActionEvent e) 499 { 500 try 501 { 502 _objectViewer.prev(); 503 } 504 catch (Exception x) 505 { 506 _objectViewer.setStatusMessage(x); 507 } 508 } 509 } 510 511 class FirstRowAction extends AbstractAction 512 { 513 514 private static final long serialVersionUID = 1L; 515 ObjectViewer _objectViewer; 516 517 523 public FirstRowAction(ObjectViewer obj) 524 { 525 super("FirstRowAction"); 526 _objectViewer = obj; 527 } 528 529 535 public void actionPerformed(ActionEvent e) 536 { 537 try 538 { 539 if (!_objectViewer.getModel().isNoObjectsPresent()) 540 { 541 _objectViewer.setSelectedIndex(0); 542 } 543 _objectViewer.tellRowPosition(); 544 } 545 catch (Exception x) 546 { 547 _objectViewer.setStatusMessage(x); 548 } 549 } 550 } 551 552 class LastRowAction extends AbstractAction 553 { 554 555 private static final long serialVersionUID = 1L; 556 ObjectViewer _objectViewer; 557 558 564 public LastRowAction(ObjectViewer obj) 565 { 566 super("LastRowAction"); 567 _objectViewer = obj; 568 } 569 570 576 public void actionPerformed(ActionEvent e) 577 { 578 try 579 { 580 if (!_objectViewer.getModel().isNoObjectsPresent()) 581 { 582 _objectViewer.setSelectedIndex(_objectViewer.getModel().getRowCount() - 1); 583 } 584 _objectViewer.tellRowPosition(); 585 } 586 catch (Exception x) 587 { 588 _objectViewer.setStatusMessage(x); 589 } 590 } 591 } 592 593 class ReloadAction extends AbstractAction 594 { 595 596 private static final long serialVersionUID = 1L; 597 ObjectViewer _objectViewer; 598 599 605 public ReloadAction(ObjectViewer obj) 606 { 607 super("ReloadAction"); 608 _objectViewer = obj; 609 } 610 611 617 public void actionPerformed(ActionEvent e) 618 { 619 try 620 { 621 _objectViewer.reload(); 622 } 623 catch (Exception x) 624 { 625 _objectViewer.setStatusMessage(x); 626 } 627 } 628 } 629 630 class ClearPropertyAction extends AbstractAction 631 { 632 633 private static final long serialVersionUID = 1L; 634 ObjectViewer _objectViewer; 635 int _cellIndex; 636 637 645 public ClearPropertyAction(ObjectViewer obj, int propertyIndex) 646 { 647 super("ClearPropertyAction"); 648 _objectViewer = obj; 649 _cellIndex = propertyIndex; 650 } 651 652 658 public void actionPerformed(ActionEvent e) 659 { 660 try 661 { 662 if (_cellIndex != -1) 663 { 664 _objectViewer.setString(_cellIndex, ""); 665 _objectViewer.synchronize(); 666 } 667 } 668 catch (Exception x) 669 { 670 _objectViewer.setStatusMessage(x); 671 } 672 } 673 } | Popular Tags |