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 javax.swing.JFrame;
019    
020    import org.springframework.richclient.application.Application;
021    import org.springframework.richclient.application.ApplicationWindow;
022    import org.springframework.richclient.application.config.ApplicationWindowAware;
023    import org.springframework.richclient.command.ActionCommand;
024    
025    /**
026     * A skeleton implementation of an action command that needs to be aware of the
027     * {@link ApplicationWindow} in which it resides. 
028     * 
029     * @author Keith Donald
030     */
031    public abstract class ApplicationWindowAwareCommand extends ActionCommand implements ApplicationWindowAware {
032    
033        private ApplicationWindow window;
034    
035        /**
036         * Creates a new uninitialized {@code ApplicationWindowAwareCommand}.
037         *
038         */
039        protected ApplicationWindowAwareCommand() {
040            //do nothing
041        }
042    
043        /**
044         * Creates a new {@code ApplicationWindowAwareCommand} with the given command identifier.
045         *
046         * @param commandId The identifier of this command instance. This should be unique amongst
047         * all comands within the application.
048         */
049        protected ApplicationWindowAwareCommand(String commandId) {
050            super(commandId);
051        }
052    
053        /**
054         * {@inheritDoc}
055         */
056        public void setApplicationWindow(ApplicationWindow window) {
057            this.window = window;
058        }
059    
060        /**
061         * Returns the application window that this component was created within.
062         * @return The application window, or null if this property has not yet been initialized.
063         */
064        protected ApplicationWindow getApplicationWindow() {
065            return window;
066        }
067    
068        /**
069         * Returns the {@link JFrame} of the application window that this command belongs to.
070         *
071         * @return The control component of the application window, never null.
072         */
073        protected JFrame getParentWindowControl() {
074            // allow subclasses to derive where the application window comes from
075            final ApplicationWindow applicationWindow = getApplicationWindow();
076            if (applicationWindow == null) {
077                return Application.instance().getActiveWindow().getControl();
078            }
079            return applicationWindow.getControl();
080        }
081        
082    }