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.command.support;
017    
018    import org.springframework.richclient.application.ApplicationServices;
019    import org.springframework.richclient.application.ApplicationServicesLocator;
020    import org.springframework.richclient.application.ApplicationWindow;
021    import org.springframework.richclient.application.ViewDescriptor;
022    import org.springframework.richclient.application.ViewDescriptorRegistry;
023    import org.springframework.richclient.application.config.ApplicationWindowAware;
024    import org.springframework.richclient.command.CommandGroup;
025    
026    /**
027     * A menu containing a collection of sub-menu items that each display a given view.
028     * 
029     * @author Keith Donald
030     */
031    public class ShowViewMenu extends CommandGroup implements ApplicationWindowAware {
032    
033        /** The identifier of this command. */
034        public static final String ID = "showViewMenu";
035    
036        private ApplicationWindow window;
037    
038        /**
039         * Creates a new {@code ShowViewMenu} with an id of {@value #ID}.
040         */
041        public ShowViewMenu() {
042            super(ID);
043        }
044    
045        /**
046         * {@inheritDoc}
047         */
048        public void setApplicationWindow(ApplicationWindow window) {
049            this.window = window;
050        }
051    
052        /**
053         * Called after dependencies have been set, populates this menu with action command objects 
054         * that will each show a given view when executed. The collection of 'show view' commands will
055         * be determined by querying the {@link ViewDescriptorRegistry} retrieved from 
056         * {@link ApplicationServices}. 
057         */
058        public void afterPropertiesSet() {
059            super.afterPropertiesSet();
060            //TODO should this be confirming that 'this.window' is not null?
061            populate();
062        }
063    
064        private void populate() {
065            ViewDescriptorRegistry viewDescriptorRegistry 
066                    = (ViewDescriptorRegistry) ApplicationServicesLocator
067                                               .services()
068                                               .getService(ViewDescriptorRegistry.class);
069            
070            ViewDescriptor[] views = viewDescriptorRegistry.getViewDescriptors();
071            for( int i = 0; i < views.length; i++ ) {
072                ViewDescriptor view = views[i];
073                addInternal(view.createShowViewCommand(window));
074            }
075        }
076        
077    }