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 javax.swing.JDesktopPane;
019    import javax.swing.JInternalFrame;
020    
021    import org.springframework.richclient.command.CommandGroup;
022    import org.springframework.richclient.command.CommandManager;
023    
024    /**
025     * Helper class to create a context menu for a <code>JDesktopPane</code>. The
026     * context menu contains the following items:
027     * <ul>
028     * <li>tile command</li>
029     * <li>cascade command</li>
030     * <li>minimze command</li>
031     * <li>a separator</li>
032     * <li>a "show" command for each frame in the desktop pane</li>
033     * </ul>
034     * 
035     * @author Peter De Bruycker
036     */
037    public class DefaultDesktopCommandGroupFactory implements DesktopCommandGroupFactory {
038            private boolean cascadeResizesFrames = false;
039            private int cascadeOffset = 20;
040    
041            /**
042             * Create a window menu CommandGroup
043             * @return the window menu CommandGroup
044             */
045            public CommandGroup createWindowMenuCommandGroup(CommandManager commandManager, JDesktopPane desktopPane) {
046                    // TODO implement this
047                    return null;
048            }
049    
050            /**
051             * Create a desktop pane context menu CommandGroup.
052             * @return the context menu CommandGroup
053             */
054            public CommandGroup createContextMenuCommandGroup(CommandManager commandManager, JDesktopPane desktop) {
055                    CommandGroup commandGroup = new CommandGroup();
056    
057                    TileCommand tileCommand = new TileCommand(desktop);
058                    CascadeCommand cascadeCommand = new CascadeCommand(desktop, cascadeOffset, cascadeResizesFrames);
059                    MinimizeAllCommand minimizeAllCommand = new MinimizeAllCommand(desktop);
060    
061                    commandManager.configure(tileCommand);
062                    commandManager.configure(cascadeCommand);
063                    commandManager.configure(minimizeAllCommand);
064    
065                    commandGroup.add(tileCommand);
066                    commandGroup.add(cascadeCommand);
067                    commandGroup.add(minimizeAllCommand);
068    
069                    if (desktop.getAllFrames().length > 0) {
070                            commandGroup.addSeparator();
071                            // TODO try to get the frames in the order they've been added to the
072                            // desktop
073                            // pane instead of the current z-order.
074                            for (int i = 0; i < desktop.getAllFrames().length; i++) {
075                                    JInternalFrame frame = desktop.getAllFrames()[i];
076    
077                                    ShowFrameCommand showFrameCommand = new ShowFrameCommand(frame);
078                                    showFrameCommand.setIcon(frame.getFrameIcon());
079                                    showFrameCommand.setCaption("" + frame.getTitle());
080    
081                                    String label = i + " " + frame.getTitle();
082                                    if (i < 10) {
083                                            label = "&" + label;
084                                    }
085                                    showFrameCommand.setLabel(label);
086    
087                                    commandGroup.add(showFrameCommand);
088                            }
089                    }
090                    return commandGroup;
091            }
092    
093            public void setCascadeResizesFrames(boolean resizeFrames) {
094                    cascadeResizesFrames = resizeFrames;
095            }
096            
097            public void setCascadeOffset(int offset) {
098                    cascadeOffset = offset;
099            }
100    }