1 30 31 package com.nightlabs.util.bean.propertyeditor; 32 33 import java.awt.*; 34 import java.lang.reflect.*; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.beans.*; 38 39 import javax.swing.*; 40 import com.nightlabs.util.bean.BeanUtil; 41 import com.nightlabs.util.bean.DefaultPropertyEditorContext; 42 import com.nightlabs.util.bean.PropertyEditorMan; 43 44 54 public class PropertyEditorUI 55 extends JPanel 56 { 57 private boolean differentValues = false; 58 public boolean isDifferentValues() { 59 return differentValues; 60 } 61 62 private boolean saveAllValues = false; 63 public boolean isSaveAllValues() { 64 return saveAllValues; 65 } 66 public void setSaveAllValues(boolean b) { 67 saveAllValues = b; 68 } 69 70 private boolean readOnly = false; 71 public boolean isReadOnly() { 72 return readOnly; 73 } 74 75 private Object firstBean; 76 private Object firstValue; 77 78 81 private Method getMethod, setMethod; 82 83 84 private PropertyEditor editor; 85 public PropertyEditor getPropertyEditor() { 86 return editor; 87 } 88 89 90 private int editorType; 91 92 93 private Component component; 94 public Component getComponent() { 95 return component; 96 } 97 98 private String propertyName; 99 public String getPropertyName() { 100 return propertyName; 101 } 102 103 108 public String getPropertyName(String methodName) 109 { 110 return BeanUtil.getPropertyName(methodName, false); 111 } 112 113 116 static final int EDITOR_CUSTOM = 0; 117 static final int EDITOR_PAINTABLE = 1; 118 static final int EDITOR_STRING = 2; 119 120 public PropertyEditorUI(PropertyDescriptor pd) 121 { 122 this.editor = PropertyEditorMan.getPropertyEditor(DefaultPropertyEditorContext.class, pd.getPropertyType()); 124 this.getMethod = pd.getReadMethod(); 125 this.setMethod = pd.getWriteMethod(); 126 127 this.propertyName = pd.getName(); 128 129 init(); 130 } 131 132 137 public PropertyEditorUI(PropertyEditor editor, Method getMethod, Method setMethod) 138 { 139 if(editor==null || getMethod==null) 140 throw new IllegalArgumentException (); 141 142 this.editor = editor; 143 this.getMethod = getMethod; 144 this.setMethod = setMethod; 145 146 this.propertyName = getPropertyName(getMethod.getName()); 147 148 init(); 149 } 150 151 private void init() 152 { 153 if(editor.supportsCustomEditor()) 154 editorType = EDITOR_CUSTOM; 155 else if(editor.isPaintable()) 156 editorType = EDITOR_PAINTABLE; 157 else 158 editorType = EDITOR_STRING; 159 160 if (setMethod == null) 161 readOnly = true; 162 163 switch(editorType){ 164 case EDITOR_CUSTOM: 165 component = editor.getCustomEditor(); 166 if (readOnly) 167 component.setEnabled(false); 168 break; 169 case EDITOR_PAINTABLE: 170 component = new PaintableEditor(editor); 171 if (readOnly) 172 component.setEnabled(false); 173 break; 174 case EDITOR_STRING: 175 component = new JTextField(); 176 if (readOnly) 177 component.setEnabled(false); 178 break; 179 } 180 181 setLayout(new BorderLayout()); 182 add(component); 183 } 184 185 public int getEditorType(){ 186 return editorType; 187 } 188 189 193 public void load(List beans) 194 throws PropertyEditorUIException 195 { 196 for (int i=0; i<beans.size(); i++) 198 { 199 Object bean = beans.get(i); 200 201 try 202 { 203 Object value = getMethod.invoke(bean, null); 204 205 if (firstValue == null) 208 { 209 firstValue = value; 210 firstBean = bean; 211 editor.setValue(firstValue); 212 213 if(editorType==EDITOR_STRING){ 214 ((JTextField)component).setText(editor.getAsText()); 215 } 216 217 component.repaint(); 218 } 219 else if (!firstValue.equals(value)) 222 differentValues = true; 223 224 }catch(IllegalAccessException e1){ 225 throw new PropertyEditorUIException(e1); 226 }catch(IllegalArgumentException e2){ 227 throw new PropertyEditorUIException(e2); 228 }catch(InvocationTargetException e3){ 229 throw new PropertyEditorUIException(e3); 230 } 231 } 233 } 234 235 238 public void load(Object bean) 239 throws PropertyEditorUIException 240 { 241 try 242 { 243 Object value = getMethod.invoke(bean, null); 244 if (firstValue == null) 245 { 246 firstValue = value; 247 firstBean = bean; 248 editor.setValue(firstValue); 249 250 if(editorType==EDITOR_STRING){ 251 ((JTextField)component).setText(editor.getAsText()); 252 } 253 254 component.repaint(); 255 } 256 }catch(IllegalAccessException e1){ 257 throw new PropertyEditorUIException(e1); 258 }catch(IllegalArgumentException e2){ 259 throw new PropertyEditorUIException(e2); 260 }catch(InvocationTargetException e3){ 261 throw new PropertyEditorUIException(e3); 262 } 263 } 264 265 269 public void save(List beans) 270 throws PropertyEditorUIException 271 { 272 boolean commitValue = false; 273 if(editorType==EDITOR_STRING) 274 editor.setAsText(((JTextField)component).getText()); 275 276 Object value = editor.getValue(); 277 278 if (firstValue == null) 279 throw new IllegalStateException ("you must first load values before you can save!"); 280 281 if (firstValue.equals(value)) 282 commitValue = false; 283 else 284 commitValue = true; 285 286 try 287 { 288 if (commitValue) 289 { 290 if (saveAllValues) 291 { 292 for (Iterator it = beans.iterator(); it.hasNext(); ) 293 { 294 Object bean = it.next(); 295 if (setMethod != null) 296 { 297 setMethod.invoke(bean, new Object []{value}); 298 } 299 } 300 } else 302 { 303 if (setMethod != null) 304 setMethod.invoke(firstBean, new Object []{value}); 305 } 306 } 307 }catch(IllegalAccessException e1){ 308 throw new PropertyEditorUIException(e1); 309 }catch(IllegalArgumentException e2){ 310 throw new PropertyEditorUIException(e2); 311 }catch(InvocationTargetException e3){ 312 throw new PropertyEditorUIException(e3); 313 } 314 } 315 316 320 public void save(Object bean) 321 throws PropertyEditorUIException 322 { 323 if(editorType==EDITOR_STRING) 324 editor.setAsText(((JTextField)component).getText()); 325 326 Object value = editor.getValue(); 327 328 boolean commitValue = false; 329 330 if (firstValue == null) 331 throw new IllegalStateException ("you must first load values before you can save!"); 332 333 if (firstValue.equals(value)) 334 commitValue = false; 335 else 336 commitValue = true; 337 338 try 339 { 340 if (commitValue) 341 { 342 if (setMethod != null) 343 { 344 setMethod.invoke(bean, new Object []{value}); 345 } 346 } 347 }catch(IllegalAccessException e1){ 348 throw new PropertyEditorUIException(e1); 349 }catch(IllegalArgumentException e2){ 350 throw new PropertyEditorUIException(e2); 351 }catch(InvocationTargetException e3){ 352 throw new PropertyEditorUIException(e3); 353 } 354 } 355 } 356 | Popular Tags |