1 30 package com.genimen.djeneric.tools.specifier.components; 31 32 import java.awt.event.ActionListener ; 33 import java.awt.event.FocusEvent ; 34 import java.awt.event.FocusListener ; 35 36 import javax.swing.JComponent ; 37 38 import com.genimen.djeneric.repository.DjProperty; 39 import com.genimen.djeneric.repository.exceptions.DjenericException; 40 import com.genimen.djeneric.structure.PropertyUsage; 41 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 42 import com.genimen.djeneric.ui.DateChooser; 43 import com.genimen.djeneric.util.DjLogger; 44 45 public class DjDateChooser extends DateChooser implements DjBindable, FocusListener 46 { 47 private static final long serialVersionUID = 1L; 48 private BindingMediator _mediator; 49 private ObjectViewer _viewer; 50 private String _propertyName; 51 private boolean _required = true; 52 53 public DjDateChooser(ObjectViewer viewer, String propertyName) throws DjenericException 54 { 55 super(); 56 getFocussableComponent().addFocusListener(this); 57 _mediator = new BindingMediator(this, viewer, propertyName); 58 _viewer = viewer; 59 _propertyName = propertyName; 60 setDateFormat(propertyName); 61 } 62 63 public DjDateChooser(DjProperty p) 64 { 65 super(); 66 _propertyName = p.getName(); 67 setDateFormatPattern(p.getType().getFormatMask()); 68 } 69 70 private void setDateFormat(String propertyName) throws DjenericException 71 { 72 PropertyUsage pu = _viewer.getPropertyUsageByName(propertyName); 73 DjProperty p = pu.getProperty(); 74 setDateFormatPattern(p.getType().getFormatMask()); 75 } 76 77 public boolean isComponentWritable() 78 { 79 return _mediator.isComponentWritable(); 80 } 81 82 public void clear() 83 { 84 super.clear(); 85 } 86 87 public void apply() throws DjenericException 88 { 89 _mediator.setValue(getTextField().getText()); 90 } 91 92 public void focusGained(FocusEvent e) 93 { 94 super.selectAll(); 95 } 96 97 public void focusLost(FocusEvent e) 98 { 99 try 100 { 101 apply(); 102 synchronize(); 103 } 104 catch (Exception x) 105 { 106 DjLogger.log(x); 107 } 108 } 109 110 public void setPropertyName(String propertyName) throws DjenericException 111 { 112 _mediator.setPropertyName(propertyName); 113 } 114 115 public void setViewer(ObjectViewer viewer) throws DjenericException 116 { 117 if (_viewer == null) 118 { 119 _viewer = viewer; 120 _mediator = new BindingMediator(this, _viewer, _propertyName); 121 setDateFormat(_propertyName); 122 } 123 } 124 125 public void synchronize() throws DjenericException 126 { 127 try 128 { 129 String dateString = _mediator.getPropertyValueString(); 130 if (dateString != null && dateString.trim().length() > 0) setDate(getDateFormat().parse(dateString)); 131 } 132 catch (java.text.ParseException e) 133 { 134 DjLogger.log(e); 135 } 136 } 137 138 public Object getDisplayedValue() 139 { 140 if (getDate() == null) 141 { 142 return ""; 143 } 144 else 145 { 146 return getDateFormat().format(getDate()); 147 } 148 } 149 150 public void setDisplayedValue(Object value) 151 { 152 try 153 { 154 if (value != null) 155 { 156 setDate(getDateFormat().parse(value.toString())); 157 } 158 } 159 catch (java.text.ParseException e) 160 { 161 DjLogger.log(e); 162 } 163 } 164 165 public String getPropertyName() 166 { 167 return _mediator.getPropertyName(); 168 } 169 170 public void addActionListener(ActionListener l) 171 { 172 173 } 174 175 public void removeActionListener(ActionListener l) 176 { 177 178 } 179 180 public boolean isRequired() 181 { 182 return _required; 183 } 184 185 public void setRequired(boolean b) 186 { 187 _required = b; 188 } 189 190 public JComponent getFocussableComponent() 191 { 192 return getTextField(); 193 } 194 } | Popular Tags |