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.awt.Component;
021    import java.awt.Container;
022    
023    import javax.swing.JComponent;
024    
025    /**
026     * Default <code>FormUIProvider</code> implementation.
027     * 
028     * @author Peter De Bruycker
029     */
030    public class DefaultFormUIProvider extends AbstractFormUIProvider {
031        private JComponent formComponent;
032    
033        public DefaultFormUIProvider() {
034    
035        }
036    
037        public DefaultFormUIProvider(JComponent component) {
038            formComponent = component;
039        }
040    
041        public JComponent getComponent(String propertyPath) {
042            return getComponent(formComponent, propertyPath);
043        }
044    
045        private static JComponent getComponent(Container parent, String id) {
046            Component[] children = parent.getComponents();
047            for (int i = 0; i < children.length; i++) {
048                Component child = children[i];
049                if (child instanceof JComponent && id.equals(children[i].getName())) {
050                    return (JComponent)child;
051                }
052    
053                if (child instanceof Container) {
054                    Container container = (Container)child;
055                    JComponent result = getComponent(container, id);
056                    if (result != null) {
057                        return result;
058                    }
059                }
060            }
061    
062            return null;
063        }
064    
065        public void setControl(JComponent control) {
066            formComponent = control;
067        }
068    
069        protected JComponent createControl() {
070            return formComponent;
071        }
072    }