001    /*
002     * Copyright 2002-2007 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.application.mdi.contextmenu;
017    
018    import java.beans.PropertyVetoException;
019    
020    import javax.swing.JDesktopPane;
021    import javax.swing.JInternalFrame;
022    
023    import org.springframework.richclient.command.ActionCommand;
024    
025    /**
026     * Cascades all <code>JInternalFrame</code>s in a given <code>JDesktopPane</code>.
027     * 
028     * @author Peter De Bruycker
029     */
030    public class CascadeCommand extends ActionCommand {
031        private static final String ID = "cascadeCommand";
032    
033        private JDesktopPane desktop;
034        private int offset;
035        private boolean resizeFrames;
036    
037        public CascadeCommand( JDesktopPane desktopPane ) {
038            this( desktopPane, true );
039        }
040    
041        public CascadeCommand( JDesktopPane desktopPane, int offset ) {
042            this( desktopPane, offset, true );
043        }
044    
045        public CascadeCommand( JDesktopPane desktopPane, boolean resizeFrames ) {
046            this( desktopPane, 20, resizeFrames );
047        }
048    
049        public CascadeCommand( JDesktopPane desktopPane, int offset, boolean resizeFrames ) {
050            super( ID );
051            desktop = desktopPane;
052            this.offset = offset;
053            this.resizeFrames = resizeFrames;
054        }
055    
056        protected void doExecuteCommand() {
057            int x = 0;
058            int y = 0;
059            JInternalFrame allFrames[] = desktop.getAllFrames();
060    
061            // manager.setNormalSize();
062            int frameHeight = (desktop.getBounds().height - 5) - allFrames.length * offset;
063            int frameWidth = (desktop.getBounds().width - 5) - allFrames.length * offset;
064            for( int i = allFrames.length - 1; i >= 0; i-- ) {
065                JInternalFrame frame = allFrames[i];
066                if( frame.isIcon() ) {
067                    try {
068                        frame.setIcon( false );
069                    } catch( PropertyVetoException e ) {
070                        // ignore
071                    }
072                }
073    
074                if( resizeFrames ) {
075                    frame.setSize( frameWidth, frameHeight );
076                }
077                frame.setLocation( x, y );
078                x = x + offset;
079                y = y + offset;
080            }
081        }
082    }