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 java.util.Collection;
019    import java.util.Map;
020    
021    import javax.swing.JComboBox;
022    import javax.swing.JComponent;
023    
024    import org.springframework.binding.form.FormModel;
025    import org.springframework.core.enums.LabeledEnumResolver;
026    import org.springframework.richclient.application.ApplicationServicesLocator;
027    import org.springframework.util.Assert;
028    
029    /**
030     * @author Oliver Hutchison
031     */
032    public class LabeledEnumComboBoxBinder extends ComboBoxBinder {
033    
034        private LabeledEnumResolver enumResolver;
035    
036        public LabeledEnumComboBoxBinder() {
037            super(new String[] { COMPARATOR_KEY, RENDERER_KEY, EDITOR_KEY, FILTER_KEY });
038        }
039    
040        protected AbstractListBinding createListBinding(JComponent control, FormModel formModel, String formPropertyPath) {
041            Assert.isInstanceOf(JComboBox.class, control, formPropertyPath);
042            return new LabeledEnumComboBoxBinding((JComboBox) control, formModel, formPropertyPath);
043        }
044    
045        protected void applyContext(AbstractListBinding binding, Map context) {
046            super.applyContext(binding, context);
047            binding.setSelectableItems(createEnumSelectableItemsHolder(binding.getFormModel(), binding.getProperty()));
048        }
049    
050        protected Collection createEnumSelectableItemsHolder(FormModel formModel, String formPropertyPath) {
051            Collection enumCollection = getLabeledEnumResolver().getLabeledEnumSet(
052                    getPropertyType(formModel, formPropertyPath));
053            Assert.notNull(enumCollection, "Unable to resolve enums for class '"
054                    + getPropertyType(formModel, formPropertyPath).getName() + "'.");
055            return enumCollection;
056        }
057    
058        public LabeledEnumResolver getLabeledEnumResolver() {
059            if (enumResolver == null) {
060                enumResolver = (LabeledEnumResolver) ApplicationServicesLocator.services().getService(
061                        LabeledEnumResolver.class);
062            }
063            return enumResolver;
064        }
065    
066        public void setEnumResolver(LabeledEnumResolver enumResolver) {
067            this.enumResolver = enumResolver;
068        }
069    }