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