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.Dimension;
019 import java.awt.Insets;
020
021 import javax.swing.DefaultDesktopManager;
022 import javax.swing.JComponent;
023 import javax.swing.JInternalFrame;
024 import javax.swing.JScrollPane;
025 import javax.swing.JViewport;
026
027 /**
028 * Replaces the standard DesktopManager for JDesktopPane. Used to provide
029 * scrollbar functionality.
030 */
031 public class ScrollingDesktopManager extends DefaultDesktopManager {
032 private ScrollingDesktopPane desktopPane;
033
034 public ScrollingDesktopManager(ScrollingDesktopPane pane) {
035 this.desktopPane = pane;
036 }
037
038 public void endResizingFrame(JComponent f) {
039 super.endResizingFrame(f);
040 resizeDesktop();
041 }
042
043 public void endDraggingFrame(JComponent f) {
044 super.endDraggingFrame(f);
045 resizeDesktop();
046 }
047
048 public void setNormalSize() {
049 JScrollPane scrollPane = getScrollPane();
050
051 if (scrollPane != null) {
052 int x = 0;
053 int y = 0;
054 Insets scrollInsets = getInsets(scrollPane);
055 Dimension d = scrollPane.getVisibleRect().getSize();
056 if (scrollPane.getBorder() != null) {
057 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, d.getHeight() - scrollInsets.top
058 - scrollInsets.bottom);
059 }
060
061 d.setSize(d.getWidth() - 20, d.getHeight() - 20);
062 setAllSize(x, y);
063 scrollPane.invalidate();
064 scrollPane.validate();
065 }
066 }
067
068 private Insets getInsets(JScrollPane scrollPane) {
069 if (scrollPane == null) {
070 return new Insets(0, 0, 0, 0);
071 }
072
073 return scrollPane.getBorder().getBorderInsets(scrollPane);
074 }
075
076 private JScrollPane getScrollPane() {
077 if (desktopPane.getParent() instanceof JViewport) {
078 JViewport viewPort = (JViewport) desktopPane.getParent();
079 if (viewPort.getParent() instanceof JScrollPane)
080 return (JScrollPane) viewPort.getParent();
081 }
082 return null;
083 }
084
085 void resizeDesktop() {
086 JScrollPane scrollPane = getScrollPane();
087
088 if (scrollPane != null) {
089 int x = 0;
090 int y = 0;
091 Insets scrollInsets = getInsets(scrollPane);
092 JInternalFrame allFrames[] = desktopPane.getAllFrames();
093 for (int i = 0; i < allFrames.length; i++) {
094 if (allFrames[i].getX() + allFrames[i].getWidth() > x) {
095 x = allFrames[i].getX() + allFrames[i].getWidth();
096 }
097 if (allFrames[i].getY() + allFrames[i].getHeight() > y) {
098 y = allFrames[i].getY() + allFrames[i].getHeight();
099 }
100 }
101 Dimension d = scrollPane.getVisibleRect().getSize();
102 if (scrollPane.getBorder() != null) {
103 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, d.getHeight() - scrollInsets.top
104 - scrollInsets.bottom);
105 }
106
107 if (x <= d.getWidth())
108 x = ((int) d.getWidth()) - 20;
109 if (y <= d.getHeight())
110 y = ((int) d.getHeight()) - 20;
111 setAllSize(x, y);
112 scrollPane.invalidate();
113 scrollPane.validate();
114 }
115 }
116
117 private void setAllSize(int width, int height) {
118 Dimension d = new Dimension(width, height);
119
120 desktopPane.setMinimumSize(d);
121 desktopPane.setMaximumSize(d);
122 desktopPane.setPreferredSize(d);
123 }
124 }