001 package org.springframework.richclient.list;
002
003 import java.awt.Component;
004 import java.awt.event.ActionListener;
005
006 import javax.swing.ComboBoxEditor;
007
008 import org.springframework.beans.BeanWrapper;
009 import org.springframework.beans.BeanWrapperImpl;
010
011 /**
012 * @author Geoffrey De Smet
013 */
014 public class BeanPropertyValueComboBoxEditor implements ComboBoxEditor {
015
016 private final BeanWrapper beanWrapper = new BeanWrapperImpl();
017
018 private Object current;
019
020 private final ComboBoxEditor innerEditor;
021
022 private final String renderedProperty;
023
024 /**
025 * Constructs a new <code>BeanPropertyValueComboBoxEditor</code>
026 * instance. The <code>toString</code> method is used to render
027 * the items.
028 *
029 * @param editor
030 * the <code>ComboBoxEditor</code> to use internally
031 */
032 public BeanPropertyValueComboBoxEditor(ComboBoxEditor editor) {
033 this(editor, null);
034 }
035
036 /**
037 * Constructs a new <code>BeanPropertyValueComboBoxEditor</code>
038 * instance.
039 *
040 * @param innerEditor
041 * the <code>ComboBoxEditor</code> which is used to render the value of the property
042 * @param renderedProperty
043 * the property used to render the items
044 */
045 public BeanPropertyValueComboBoxEditor(ComboBoxEditor innerEditor, String renderedProperty) {
046 this.innerEditor = innerEditor;
047 this.renderedProperty = renderedProperty;
048 }
049
050 /**
051 * Should only be used if the innerEditor will be set later
052 *
053 * @param renderedProperty
054 */
055 public BeanPropertyValueComboBoxEditor(String renderedProperty) {
056 this(null, renderedProperty);
057 }
058
059 /**
060 * @see javax.swing.ComboBoxEditor#addActionListener(java.awt.event.ActionListener)
061 */
062 public void addActionListener(ActionListener l) {
063 innerEditor.addActionListener(l);
064 }
065
066 /**
067 * @see javax.swing.ComboBoxEditor#getEditorComponent()
068 */
069 public Component getEditorComponent() {
070 return innerEditor.getEditorComponent();
071 }
072
073 /**
074 * @see javax.swing.ComboBoxEditor#getItem()
075 */
076 public Object getItem() {
077 return current;
078 }
079
080 /**
081 * @see javax.swing.ComboBoxEditor#removeActionListener(java.awt.event.ActionListener)
082 */
083 public void removeActionListener(ActionListener l) {
084 innerEditor.removeActionListener(l);
085 }
086
087 /**
088 * @see javax.swing.ComboBoxEditor#selectAll()
089 */
090 public void selectAll() {
091 innerEditor.selectAll();
092 }
093
094 /**
095 * @see javax.swing.ComboBoxEditor#setItem(Object)
096 */
097 public void setItem(Object item) {
098 current = item;
099 if (item == null) {
100 innerEditor.setItem("");
101 } else {
102 beanWrapper.setWrappedInstance(item);
103 if (renderedProperty != null) {
104 innerEditor.setItem(String.valueOf(beanWrapper.getPropertyValue(renderedProperty)));
105 } else {
106 innerEditor.setItem(String.valueOf(item));
107 }
108 }
109 }
110
111 /**
112 * @return the property name
113 */
114 public String getPropertyName() {
115 return this.renderedProperty;
116 }
117 }