001    /*
002     * Copyright 2002-2006 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     */
017    
018    package org.springframework.richclient.form;
019    
020    import java.util.Collections;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.springframework.richclient.factory.AbstractControlFactory;
025    import org.springframework.richclient.form.binding.BindingFactory;
026    import org.springframework.util.Assert;
027    
028    /**
029     * Abstract <code>FormUIProvider</code> implementation. Extenders only need to implement
030     * the <code>createControl()</code> and <code>getComponent(String id)</code> methods.
031     * 
032     * @author Peter De Bruycker
033     */
034    public abstract class AbstractFormUIProvider extends AbstractControlFactory implements FormUIProvider {
035        private boolean bound = false;
036        private String[] properties;
037        private Map contextMap = new HashMap();
038    
039        public void bind(BindingFactory factory, Form form) {
040            Assert.state(properties != null && properties.length > 0, "Properties must be set");
041    
042            bound = true;
043    
044            for (int i = 0; i < properties.length; i++) {
045                factory.bindControl(getComponent(properties[i]), properties[i], getContext(properties[i]));
046            }
047        }
048    
049        public Map getContext(String propertyPath) {
050            return contextMap.containsKey(propertyPath) ? (Map)contextMap.get(propertyPath) : Collections.EMPTY_MAP;
051        }
052    
053        public void setContext(String propertyPath, Map context) {
054            contextMap.put(propertyPath, context);
055        }
056    
057        public void setProperties(String[] properties) {
058            Assert.state(!bound, "You cannot set the form properties after the binding");
059    
060            this.properties = properties;
061        }
062    
063        public String[] getProperties() {
064            return properties;
065        }
066    
067    }