1 30 package com.genimen.djeneric.tools.specifier.editor; 31 32 import java.util.ArrayList ; 33 34 import javax.swing.DefaultComboBoxModel ; 35 import javax.swing.JComboBox ; 36 import javax.swing.JComponent ; 37 import javax.swing.JList ; 38 import javax.swing.JTextField ; 39 40 import com.genimen.djeneric.language.Messages; 41 import com.genimen.djeneric.repository.DjDomainValue; 42 import com.genimen.djeneric.repository.DjObject; 43 import com.genimen.djeneric.repository.DjProperty; 44 import com.genimen.djeneric.repository.DjSession; 45 import com.genimen.djeneric.repository.exceptions.DjenericException; 46 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 47 import com.genimen.djeneric.tools.specifier.components.DjBindable; 48 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 49 50 public class DjenericObjectEditorProxy 51 { 52 private ObjectViewer _viewer; 53 54 private ArrayList _allBinders = new ArrayList (); 55 56 public DjenericObjectEditorProxy(ObjectViewer forViewer) 57 { 58 _viewer = forViewer; 59 } 60 61 public void setEnabled(boolean b) 62 { 63 for (int i = 0; i < _allBinders.size(); i++) 64 { 65 DjBindable pb = (DjBindable) _allBinders.get(i); 66 JComponent comp = pb.getFocussableComponent(); 67 if (comp instanceof JTextField ) 68 { 69 JTextField fld = (JTextField ) comp; 70 fld.setEditable(b); 71 } 72 else 73 { 74 comp.setEnabled(b && pb.isComponentWritable()); 75 } 76 } 77 78 } 79 80 public boolean isEnabled() 81 { 82 for (int i = 0; i < _allBinders.size(); i++) 83 { 84 DjBindable pb = (DjBindable) _allBinders.get(i); 85 if (pb.getFocussableComponent().isEnabled()) 86 { 87 return true; 88 } 89 } 90 return false; 91 } 92 93 public void synchronize() 94 { 95 for (int i = 0; i < _allBinders.size(); i++) 96 { 97 DjBindable pb = (DjBindable) _allBinders.get(i); 98 try 99 { 100 pb.synchronize(); 101 } 102 catch (DjenericException x) 103 { 104 _viewer.setStatusMessage(x); 105 } 106 } 107 } 108 109 public void clear() 110 { 111 for (int i = 0; i < _allBinders.size(); i++) 112 { 113 DjBindable pb = (DjBindable) _allBinders.get(i); 114 try 115 { 116 pb.clear(); 117 } 118 catch (DjenericException x) 119 { 120 _viewer.setStatusMessage(x); 121 } 122 } 123 synchronize(); 124 125 } 126 127 public void applyEditorsToProxy() throws DjenericException 128 { 129 for (int i = 0; i < _allBinders.size(); i++) 130 { 131 DjBindable pb = (DjBindable) _allBinders.get(i); 132 pb.apply(); 133 } 134 _viewer.setStatusMessage(Messages.getString("global.Ok")); 135 } 136 137 protected ArrayList getBinders() 138 { 139 return _allBinders; 140 } 141 142 public void unbindEditor(DjBindable comp) 143 { 144 getBinders().remove(comp); 145 } 146 147 public void unbindEditors() 148 { 149 getBinders().clear(); 150 } 151 152 public void updateLookupModel(DjSession session, String propertyName) throws DjenericException 153 { 154 for (int i = 0; i < _allBinders.size(); i++) 155 { 156 DjBindable ppb = (DjBindable) (getBinders().get(i)); 157 158 if (ppb.getPropertyName().equals(propertyName)) updateModelForBinder(session, ppb); 159 } 160 } 161 162 public void updateAllLookupModels(DjSession session) throws DjenericException 163 { 164 if (session == null) return; 165 for (int i = 0; i < _allBinders.size(); i++) 166 { 167 DjBindable ppb = (DjBindable) (getBinders().get(i)); 168 updateModelForBinder(session, ppb); 169 } 170 } 171 172 public void updateAllRestrictedModels(DjSession session) throws DjenericException 173 { 174 for (int i = 0; i < _allBinders.size(); i++) 175 { 176 DjBindable ppb = (DjBindable) (getBinders().get(i)); 177 DjProperty prop = _viewer.getExtent().getProperty(ppb.getPropertyName()); 178 if (prop.isPartOfRestrictedPath()) updateModelForBinder(session, ppb); 179 } 180 } 181 182 protected void updateModelForBinder(DjSession session, DjBindable ppb) throws DjenericException, 183 ObjectNotDefinedException 184 { 185 DjDomainValue[] validValues = null; 186 JComponent comp = ppb.getFocussableComponent(); 187 188 if (comp instanceof JComboBox || comp instanceof JList ) 189 { 190 validValues = getValidValuesForProperty(session, ppb.getPropertyName()); 191 } 192 193 if (validValues != null) 194 { 195 if (comp instanceof JComboBox ) 196 { 197 JComboBox cbb = (JComboBox ) comp; 198 if (cbb.isEditable()) 199 { 200 Object obj = cbb.getEditor().getItem(); 201 cbb.setModel(new DefaultComboBoxModel (validValues)); 202 cbb.setSelectedIndex(-1); 203 cbb.getEditor().setItem(obj); 204 } 205 else 206 { 207 Object obj = cbb.getSelectedItem(); 208 cbb.setModel(new DefaultComboBoxModel (validValues)); 209 if (obj != null) 210 { 211 cbb.setSelectedIndex(-1); 212 cbb.setSelectedItem(obj); 213 } 214 else 215 { 216 cbb.setSelectedIndex(-1); 217 } 218 } 219 } 220 } 221 } 222 223 public void bindEditor(DjSession session, DjBindable comp, String propertyName) 224 { 225 unbindEditor(comp); 227 getBinders().add(comp); 228 } 229 230 private DjDomainValue[] getValidValuesForProperty(DjSession session, String propertyName) throws DjenericException, 231 ObjectNotDefinedException 232 { 233 DjDomainValue[] validValues; 234 DjObject obj = _viewer.getSelectedValue(); 235 if (obj != null) 236 { 237 validValues = obj.getValidValues(propertyName); 238 } 239 else 240 { 241 DjProperty prop = _viewer.getExtent().getProperty(propertyName); 242 validValues = prop.getValidValues(session); 243 } 244 return validValues; 245 } 246 247 public void requestFocus() 248 { 249 ArrayList binders = getBinders(); 250 if (binders.size() > 0) 251 { 252 DjBindable ppb = (DjBindable) (getBinders().get(0)); 253 ppb.getFocussableComponent().requestFocus(); 254 } 255 } 256 257 public void requestFocus(String propertyName) 258 { 259 ArrayList binders = getBinders(); 260 for (int i = 0; i < binders.size(); i++) 261 { 262 DjBindable ppb = (DjBindable) (getBinders().get(i)); 263 if (ppb.getPropertyName().equals(propertyName)) 264 { 265 ppb.getFocussableComponent().requestFocus(); 266 break; 267 } 268 } 269 270 } 271 272 } | Popular Tags |