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;
017    
018    import java.awt.Component;
019    import java.awt.Point;
020    import java.beans.PropertyVetoException;
021    
022    import javax.swing.JDesktopPane;
023    import javax.swing.JInternalFrame;
024    
025    /**
026     * An extension of WDesktopPane that supports often used MDI functionality. This
027     * class also handles setting scroll bars for when windows move too far to the
028     * left or bottom, providing the MDIDesktopPane is in a ScrollPane.
029     */
030    public class ScrollingDesktopPane extends JDesktopPane {
031            private static int FRAME_OFFSET = 20;
032    
033        private ScrollingDesktopManager manager;
034    
035        public ScrollingDesktopPane() {
036            manager = new ScrollingDesktopManager(this);
037            setDesktopManager(manager);
038        }
039    
040        public void setBounds(int x, int y, int w, int h) {
041            super.setBounds(x, y, w, h);
042            checkDesktopSize();
043        }
044    
045        public Component add(JInternalFrame frame) {
046            JInternalFrame[] array = getAllFrames();
047            Point p;
048    
049            Component retval = super.add(frame);
050            checkDesktopSize();
051            if (array.length > 0) {
052                p = array[0].getLocation();
053                p.x = p.x + FRAME_OFFSET;
054                p.y = p.y + FRAME_OFFSET;
055            }
056            else {
057                p = new Point(0, 0);
058            }
059            frame.setLocation(p.x, p.y);
060    
061            moveToFront(frame);
062            frame.setVisible(true);
063            try {
064                frame.setSelected(true);
065            }
066            catch (PropertyVetoException e) {
067                frame.toBack();
068            }
069            return retval;
070        }
071    
072        public void remove(Component c) {
073            super.remove(c);
074            checkDesktopSize();
075        }
076    
077        private void checkDesktopSize() {
078            if (getParent() != null && isVisible()) {
079                manager.resizeDesktop();
080            }
081        }
082    }