001 package org.springframework.richclient.list; 002 003 import java.awt.Component; 004 import java.awt.event.ActionListener; 005 import java.util.Locale; 006 007 import javax.swing.ComboBoxEditor; 008 009 import org.springframework.context.MessageSource; 010 import org.springframework.context.MessageSourceResolvable; 011 import org.springframework.core.enums.LabeledEnum; 012 import org.springframework.util.Assert; 013 014 /** 015 * <code>ComboBoxEditor</code> that wraps another editor, but performs 016 * conversion between <code>CodedEnum</code> s and <code>String</code>s. 017 * <br/>It wraps another <code>ComboBoxEditor</code> to avoid visual 018 * differences between the default editor and this editor. 019 * 020 * @author peter.de.bruycker 021 */ 022 public class LabeledEnumComboBoxEditor implements ComboBoxEditor { 023 024 private Object current; 025 026 private MessageSource messages; 027 028 private ComboBoxEditor inner; 029 030 /** 031 * Constructs a new <code>CodedEnumComboBoxEditor</code> instance. 032 * 033 * @param messageSource the <code>MessageSource</code> to use for 034 * conversion 035 * @param editor the <code>ComboBoxEditor</code> to use internally 036 */ 037 public LabeledEnumComboBoxEditor(MessageSource messageSource, ComboBoxEditor editor) { 038 Assert.notNull(editor, "Editor cannot be null"); 039 this.inner = editor; 040 messages = messageSource; 041 } 042 043 /** 044 * @see javax.swing.ComboBoxEditor#selectAll() 045 */ 046 public void selectAll() { 047 inner.selectAll(); 048 } 049 050 /** 051 * @see javax.swing.ComboBoxEditor#getEditorComponent() 052 */ 053 public Component getEditorComponent() { 054 return inner.getEditorComponent(); 055 } 056 057 /** 058 * @see javax.swing.ComboBoxEditor#addActionListener(java.awt.event.ActionListener) 059 */ 060 public void addActionListener(ActionListener l) { 061 inner.addActionListener(l); 062 } 063 064 /** 065 * @see javax.swing.ComboBoxEditor#removeActionListener(java.awt.event.ActionListener) 066 */ 067 public void removeActionListener(ActionListener l) { 068 inner.removeActionListener(l); 069 } 070 071 /** 072 * @see javax.swing.ComboBoxEditor#getItem() 073 */ 074 public Object getItem() { 075 return current; 076 } 077 078 /** 079 * @see javax.swing.ComboBoxEditor#setItem(java.lang.Object) 080 */ 081 public void setItem(Object anObject) { 082 current = anObject; 083 if (anObject != null) { 084 if (messages != null && anObject instanceof MessageSourceResolvable) { 085 inner.setItem(messages.getMessage((MessageSourceResolvable)anObject, Locale.getDefault())); 086 } 087 else { 088 inner.setItem(((LabeledEnum)anObject).getLabel()); 089 } 090 } 091 else { 092 inner.setItem(null); 093 } 094 } 095 }