1 30 package com.genimen.djeneric.tools.specifier.components; 31 32 import java.awt.event.FocusEvent ; 33 import java.awt.event.FocusListener ; 34 35 import javax.swing.JComponent ; 36 import javax.swing.JTextField ; 37 38 import com.genimen.djeneric.repository.exceptions.DjenericException; 39 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 40 import com.genimen.djeneric.util.DjLogger; 41 42 public class DjTextField extends JTextField implements DjBindable, FocusListener 43 { 44 private static final long serialVersionUID = 1L; 45 private BindingMediator _mediator; 46 47 public DjTextField(ObjectViewer viewer, String propertyName) throws DjenericException 48 { 49 super(); 50 addFocusListener(this); 51 _mediator = new BindingMediator(this, viewer, propertyName); 52 } 53 54 public boolean isComponentWritable() 55 { 56 return _mediator.isComponentWritable(); 57 } 58 59 public void clear() 60 { 61 setText(""); 62 } 63 64 public void apply() throws DjenericException 65 { 66 _mediator.setValue(getText()); 67 } 68 69 public void focusGained(FocusEvent e) 70 { 71 selectAll(); 72 } 73 74 public void focusLost(FocusEvent e) 75 { 76 try 77 { 78 apply(); 79 synchronize(); 80 } 81 catch (Exception x) 82 { 83 DjLogger.log(x); 84 } 85 } 86 87 public void setPropertyName(String propertyName) throws DjenericException 88 { 89 _mediator.setPropertyName(propertyName); 90 } 91 92 public void setViewer(ObjectViewer viewer) 93 { 94 _mediator.setViewer(viewer); 95 } 96 97 public void synchronize() throws DjenericException 98 { 99 setText(_mediator.getPropertyValueString()); 100 setCaretPosition(0); 101 } 102 103 public Object getDisplayedValue() 104 { 105 return getText(); 106 } 107 108 public String getPropertyName() 109 { 110 return _mediator.getPropertyName(); 111 } 112 113 public JComponent getFocussableComponent() 114 { 115 return this; 116 } 117 } | Popular Tags |