001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.form.binding.swing;
017    
018    import javax.swing.JComboBox;
019    import javax.swing.ListModel;
020    
021    import org.springframework.binding.form.FormModel;
022    import org.springframework.context.MessageSource;
023    import org.springframework.core.enums.LabeledEnum;
024    import org.springframework.richclient.application.ApplicationServicesLocator;
025    import org.springframework.richclient.list.LabeledEnumComboBoxEditor;
026    import org.springframework.richclient.list.LabeledEnumListRenderer;
027    import org.springframework.util.comparator.ComparableComparator;
028    import org.springframework.util.comparator.CompoundComparator;
029    
030    /**
031     * @author Oliver Hutchison
032     */
033    public class LabeledEnumComboBoxBinding extends ComboBoxBinding {
034    
035        private MessageSource messageSource;
036    
037        public LabeledEnumComboBoxBinding(JComboBox comboBox, FormModel formModel, String formPropertyPath) {
038            super(comboBox, formModel, formPropertyPath);
039        }
040    
041        protected void doBindControl(ListModel bindingModel) {
042            setRenderer(new LabeledEnumListRenderer(getMessageSource()));
043            setEditor(new LabeledEnumComboBoxEditor(getMessageSource(), getEditor()));
044            CompoundComparator comparator = new CompoundComparator();
045            comparator.addComparator(LabeledEnum.LABEL_ORDER);
046            comparator.addComparator(new ComparableComparator());
047            setComparator(comparator);
048            super.doBindControl(bindingModel);
049        }
050    
051        public void setMessageSource(MessageSource messageSource) {
052            this.messageSource = messageSource;
053        }
054    
055        protected MessageSource getMessageSource() {
056            if (messageSource == null) {
057                messageSource = (MessageSource) ApplicationServicesLocator.services().getService(MessageSource.class);
058            }
059            return messageSource;
060        }
061    }