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.command.support;
017    
018    import java.awt.Component;
019    import java.awt.Container;
020    
021    import javax.swing.JMenu;
022    import javax.swing.JPopupMenu;
023    import javax.swing.JSeparator;
024    import javax.swing.JToolBar;
025    import javax.swing.SwingConstants;
026    
027    import org.springframework.richclient.command.GroupContainerPopulator;
028    import org.springframework.richclient.util.Assert;
029    
030    /**
031     * A default implementation of the {@link GroupContainerPopulator} interface.
032     *
033     */
034    public class SimpleGroupContainerPopulator implements GroupContainerPopulator {
035        
036        private final Container container;
037    
038        /**
039         * Creates a new {@code SimpleGroupContainerPopulator} that will populate the given container.
040         *
041         * @param container The container to be populated. Must not be null.
042         * 
043         * @throws IllegalArgumentException if {@code container} is null.
044         */
045        public SimpleGroupContainerPopulator(Container container) {
046            Assert.required(container, "container");
047            this.container = container;
048        }
049    
050        /**
051         * {@inheritDoc}
052         */
053        public Container getContainer() {
054            return container;
055        }
056    
057        /**
058         * {@inheritDoc}
059         */
060        public void add(Component c) {
061            container.add(c);
062        }
063    
064        /**
065         * {@inheritDoc}
066         */
067        public void addSeparator() {
068            if (container instanceof JMenu) {
069                ((JMenu)container).addSeparator();
070            }
071            else if (container instanceof JPopupMenu) {
072                ((JPopupMenu)container).addSeparator();
073            }
074            else if (container instanceof JToolBar) {
075                ((JToolBar)container).addSeparator();
076            }
077            else {
078                container.add(new JSeparator(SwingConstants.VERTICAL));
079            }
080        }
081    
082        /**
083         * Default implementation, no action is performed.
084         */
085        public void onPopulated() {
086            //do nothing
087        }
088        
089    }