001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.list;
017    
018    import java.awt.Component;
019    import java.util.Locale;
020    
021    import javax.swing.DefaultListCellRenderer;
022    import javax.swing.JList;
023    
024    import org.springframework.context.MessageSource;
025    import org.springframework.context.MessageSourceResolvable;
026    import org.springframework.core.enums.LabeledEnum;
027    
028    /**
029     * Renders a enumeration in a list.
030     * 
031     * @author Keith Donald
032     */
033    public class LabeledEnumListRenderer extends DefaultListCellRenderer {
034            private MessageSource messages;
035    
036            public LabeledEnumListRenderer() {
037    
038            }
039    
040            public LabeledEnumListRenderer(MessageSource messages) {
041                    this.messages = messages;
042            }
043    
044            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
045                            boolean cellHasFocus) {
046                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
047                    if (value == null) {
048                            return this;
049                    }
050                    if (messages != null && value instanceof MessageSourceResolvable) {
051                            setText(messages.getMessage((MessageSourceResolvable)value, Locale.getDefault()));
052                    }
053                    else {
054                            setText(((LabeledEnum)value).getLabel());
055                    }
056                    return this;
057            }
058    
059    }