/Users/lyon/j4p/src/j2d/gui/MDIDesktopPane.java
|
1 package j2d.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.beans.PropertyVetoException;
6
7 /**
8 * An extension of JDesktopPane that supports often used MDI functionality. This
9 * class also handles setting scroll bars for when windows move too far to the left or
10 * bottom, providing the MDIDesktopPane is in a ScrollPane.
11 * <p/>
12 * see http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-mdi.html
13 */
14 public class MDIDesktopPane extends JDesktopPane {
15 private static int FRAME_OFFSET = 20;
16 private MDIDesktopManager manager;
17
18 public MDIDesktopPane() {
19 manager = new MDIDesktopManager(this);
20 setDesktopManager(manager);
21 setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
22 }
23
24 public void setBounds(int x, int y, int w, int h) {
25 super.setBounds(x, y, w, h);
26 checkDesktopSize();
27 }
28
29 public Component add(JInternalFrame frame) {
30 JInternalFrame[] array = getAllFrames();
31 Point p;
32 int w;
33 int h;
34
35 Component retval = super.add(frame);
36 checkDesktopSize();
37 if (array.length > 0) {
38 p = array[0].getLocation();
39 p.x = p.x + FRAME_OFFSET;
40 p.y = p.y + FRAME_OFFSET;
41 } else {
42 p = new Point(0, 0);
43 }
44 frame.setLocation(p.x, p.y);
45 if (frame.isResizable()) {
46 w = getWidth() - (getWidth() / 3);
47 h = getHeight() - (getHeight() / 3);
48 if (w < frame.getMinimumSize().getWidth()) w = (int) frame.getMinimumSize().getWidth();
49 if (h < frame.getMinimumSize().getHeight()) h = (int) frame.getMinimumSize().getHeight();
50 frame.setSize(w, h);
51 }
52 moveToFront(frame);
53 frame.setVisible(true);
54 try {
55 frame.setSelected(true);
56 } catch (PropertyVetoException e) {
57 frame.toBack();
58 }
59 return retval;
60 }
61
62 public void remove(Component c) {
63 super.remove(c);
64 checkDesktopSize();
65 }
66
67 /**
68 * Return the topmost frame of the specified type
69 */
70 public JInternalFrame getTopmostFrame(Class soughtClass) {
71 JInternalFrame allFrames[] = getAllFrames();
72 for (int i = 0; i < allFrames.length; i++) {
73 if (allFrames[i].getClass().equals(soughtClass)) {
74 return allFrames[i];
75 }
76 }
77 return null;
78 }
79
80 /**
81 * Cascade all internal frames
82 */
83 public void cascadeFrames() {
84 int x = 0;
85 int y = 0;
86 JInternalFrame allFrames[] = getAllFrames();
87
88 manager.setNormalSize();
89 // int frameHeight = (getBounds().height - 5) - allFrames.length * FRAME_OFFSET;
90 // int frameWidth = (getBounds().width - 5) - allFrames.length * FRAME_OFFSET;
91 for (int i = allFrames.length - 1; i >= 0; i--) {
92 // for (int i = 0; i < allFrames.length; i++) {
93 // allFrames[i].setSize(frameWidth,frameHeight);
94 if (allFrames[i].isMaximum()) { // grj
95 try {
96 allFrames[i].setMaximum(false);
97 } catch (PropertyVetoException p) {
98 }
99 }
100 x = FRAME_OFFSET * ((allFrames.length - 1 - i) % 12);
101 y = FRAME_OFFSET * ((allFrames.length - 1 - i) % 6);
102
103 allFrames[i].setLocation(x, y);
104 // x = x + FRAME_OFFSET;
105 // y = y + FRAME_OFFSET;
106 }
107
108 // reset view so upper left corner is in focus -- grj
109 setLocation(0, 0); // grj
110 }
111
112 /**
113 * Tile all internal frames
114 */
115 /* public void tileFrames() {
116 java.awt.Component allFrames[] = getAllFrames();
117 manager.setNormalSize();
118 int frameHeight = getBounds().height/allFrames.length;
119 int y = 0;
120 for (int i = 0; i < allFrames.length; i++) {
121 allFrames[i].setSize(getBounds().width,frameHeight);
122 allFrames[i].setLocation(0,y);
123 y = y + frameHeight;
124 }
125 }
126 */
127 /**
128 * Sets all component size properties ( maximum, minimum, preferred)
129 * to the given dimension.
130 */
131 public void setAllSize(Dimension d) {
132 setMinimumSize(d);
133 setMaximumSize(d);
134 setPreferredSize(d);
135 }
136
137 /**
138 * Sets all component size properties ( maximum, minimum, preferred)
139 * to the given width and height.
140 */
141 public void setAllSize(int width, int height) {
142 setAllSize(new Dimension(width, height));
143 }
144
145 private void checkDesktopSize() {
146 if (getParent() != null && isVisible()) manager.resizeDesktop();
147 }
148
149 /**
150 * Private class used to replace the standard DesktopManager for JDesktopPane.
151 * Used to provide scrollbar functionality.
152 */
153 class MDIDesktopManager extends DefaultDesktopManager {
154 private MDIDesktopPane desktop;
155
156 public MDIDesktopManager(MDIDesktopPane desktop) {
157 this.desktop = desktop;
158 }
159
160 public void endResizingFrame(JComponent f) {
161 super.endResizingFrame(f);
162 resizeDesktop();
163 }
164
165 public void endDraggingFrame(JComponent f) {
166 super.endDraggingFrame(f);
167 resizeDesktop();
168 }
169
170 public void setNormalSize() {
171 JScrollPane scrollPane = getScrollPane();
172 int x = 0;
173 int y = 0;
174 Insets scrollInsets = getScrollPaneInsets();
175
176 if (scrollPane != null) {
177 Dimension d = scrollPane.getVisibleRect().getSize();
178 if (scrollPane.getBorder() != null) {
179 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
180 d.getHeight() - scrollInsets.top - scrollInsets.bottom);
181 }
182
183 d.setSize(d.getWidth() - 20, d.getHeight() - 20);
184 desktop.setAllSize(x, y);
185 scrollPane.invalidate();
186 scrollPane.validate();
187 }
188 }
189
190 private Insets getScrollPaneInsets() {
191 JScrollPane scrollPane = getScrollPane();
192 if (scrollPane == null)
193 return new Insets(0, 0, 0, 0);
194 else
195 return getScrollPane().getBorder().getBorderInsets(scrollPane);
196 }
197
198 private JScrollPane getScrollPane() {
199 if (desktop.getParent() instanceof JViewport) {
200 JViewport viewPort = (JViewport) desktop.getParent();
201 if (viewPort.getParent() instanceof JScrollPane)
202 return (JScrollPane) viewPort.getParent();
203 }
204 return null;
205 }
206
207 protected void resizeDesktop() {
208 int x = 0;
209 int y = 0;
210 JScrollPane scrollPane = getScrollPane();
211 Insets scrollInsets = getScrollPaneInsets();
212
213 if (scrollPane != null) {
214 JInternalFrame allFrames[] = desktop.getAllFrames();
215 for (int i = 0; i < allFrames.length; i++) {
216 if (allFrames[i].getX() + allFrames[i].getWidth() > x) {
217 x = allFrames[i].getX() + allFrames[i].getWidth();
218 }
219 if (allFrames[i].getY() + allFrames[i].getHeight() > y) {
220 y = allFrames[i].getY() + allFrames[i].getHeight();
221 }
222 }
223 Dimension d = scrollPane.getVisibleRect().getSize();
224 if (scrollPane.getBorder() != null) {
225 d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
226 d.getHeight() - scrollInsets.top - scrollInsets.bottom);
227 }
228
229 if (x <= d.getWidth()) x = ((int) d.getWidth()) - 20;
230 if (y <= d.getHeight()) y = ((int) d.getHeight()) - 20;
231 desktop.setAllSize(x, y);
232 scrollPane.invalidate();
233 scrollPane.validate();
234 }
235 }
236 }
237 }