KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > console > MDIDesktopPane


1 package org.sape.carbon.services.console;
2
3
4 import java.awt.Component JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Insets JavaDoc;
7 import java.awt.Point JavaDoc;
8 import java.beans.PropertyVetoException JavaDoc;
9
10 import javax.swing.DefaultDesktopManager JavaDoc;
11 import javax.swing.JComponent JavaDoc;
12 import javax.swing.JDesktopPane JavaDoc;
13 import javax.swing.JInternalFrame JavaDoc;
14 import javax.swing.JScrollPane JavaDoc;
15 import javax.swing.JViewport JavaDoc;
16
17
18 /**
19  * <p>An extension of WDesktopPane that supports often used MDI functionality. This
20  * class also handles setting scroll bars for when windows move too far to the left or
21  * bottom, providing the MDIDesktopPane is in a ScrollPane.</p>
22  *
23  *
24  * Copyright 2002 Sapient
25  * @since carbon 1.0
26  * @author Greg Hinkle, January 2002
27  * @version $Revision: 1.3 $($Author: ghinkl $ / $Date: 2003/04/04 01:11:49 $)
28  */

29 public class MDIDesktopPane extends JDesktopPane JavaDoc {
30     private static int FRAME_OFFSET=20;
31     private MDIDesktopManager manager;
32
33     public MDIDesktopPane() {
34         manager=new MDIDesktopManager(this);
35         setDesktopManager(manager);
36         setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
37     }
38
39     public void setBounds(int x, int y, int w, int h) {
40         super.setBounds(x,y,w,h);
41         checkDesktopSize();
42     }
43
44     public Component JavaDoc add(JInternalFrame JavaDoc frame) {
45         JInternalFrame JavaDoc[] array = getAllFrames();
46         Point JavaDoc p;
47         int w;
48         int h;
49
50         Component JavaDoc retval=super.add(frame);
51         checkDesktopSize();
52         if (array.length > 0) {
53             p = array[0].getLocation();
54             p.x = p.x + FRAME_OFFSET;
55             p.y = p.y + FRAME_OFFSET;
56         }
57         else {
58             p = new Point JavaDoc(0, 0);
59         }
60         frame.setLocation(p.x, p.y);
61         if (frame.isResizable()) {
62             w = getWidth() - (getWidth()/3);
63             h = getHeight() - (getHeight()/3);
64             if (w < frame.getMinimumSize().getWidth()) w = (int)frame.getMinimumSize().getWidth();
65             if (h < frame.getMinimumSize().getHeight()) h = (int)frame.getMinimumSize().getHeight();
66             frame.setSize(w, h);
67         }
68         moveToFront(frame);
69         frame.setVisible(true);
70         try {
71             frame.setSelected(true);
72         } catch (PropertyVetoException JavaDoc e) {
73             frame.toBack();
74         }
75         return retval;
76     }
77
78     public void remove(Component JavaDoc c) {
79         super.remove(c);
80         checkDesktopSize();
81     }
82
83     /**
84      * Cascade all internal frames
85      */

86     public void cascadeFrames() {
87         int x = 0;
88         int y = 0;
89         JInternalFrame JavaDoc allFrames[] = getAllFrames();
90
91         manager.setNormalSize();
92         int frameHeight = (getBounds().height - 5) - allFrames.length * FRAME_OFFSET;
93         int frameWidth = (getBounds().width - 5) - allFrames.length * FRAME_OFFSET;
94         for (int i = allFrames.length - 1; i >= 0; i--) {
95             allFrames[i].setSize(frameWidth,frameHeight);
96             allFrames[i].setLocation(x,y);
97             x = x + FRAME_OFFSET;
98             y = y + FRAME_OFFSET;
99         }
100     }
101
102     /**
103      * Tile all internal frames
104      */

105     public void tileFrames() {
106         java.awt.Component JavaDoc allFrames[] = getAllFrames();
107         manager.setNormalSize();
108         int frameHeight = getBounds().height/allFrames.length;
109         int y = 0;
110         for (int i = 0; i < allFrames.length; i++) {
111             allFrames[i].setSize(getBounds().width,frameHeight);
112             allFrames[i].setLocation(0,y);
113             y = y + frameHeight;
114         }
115     }
116
117     /**
118      * Sets all component size properties ( maximum, minimum, preferred)
119      * to the given dimension.
120      */

121     public void setAllSize(Dimension JavaDoc d){
122         setMinimumSize(d);
123         setMaximumSize(d);
124         setPreferredSize(d);
125     }
126
127     /**
128      * Sets all component size properties ( maximum, minimum, preferred)
129      * to the given width and height.
130      */

131     public void setAllSize(int width, int height){
132         setAllSize(new Dimension JavaDoc(width,height));
133     }
134
135     private void checkDesktopSize() {
136         if (getParent()!=null&&isVisible()) manager.resizeDesktop();
137     }
138 }
139
140 /**
141  * Private class used to replace the standard DesktopManager for JDesktopPane.
142  * Used to provide scrollbar functionality.
143  */

144 class MDIDesktopManager extends DefaultDesktopManager JavaDoc {
145     private MDIDesktopPane desktop;
146
147     public MDIDesktopManager(MDIDesktopPane desktop) {
148         this.desktop = desktop;
149     }
150
151     public void endResizingFrame(JComponent JavaDoc f) {
152         super.endResizingFrame(f);
153         resizeDesktop();
154     }
155
156     public void endDraggingFrame(JComponent JavaDoc f) {
157         super.endDraggingFrame(f);
158         resizeDesktop();
159     }
160
161     public void setNormalSize() {
162         JScrollPane JavaDoc scrollPane=getScrollPane();
163         int x = 0;
164         int y = 0;
165         Insets JavaDoc scrollInsets = getScrollPaneInsets();
166
167         if (scrollPane != null) {
168             Dimension JavaDoc d = scrollPane.getVisibleRect().getSize();
169             if (scrollPane.getBorder() != null) {
170                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
171                          d.getHeight() - scrollInsets.top - scrollInsets.bottom);
172             }
173
174             d.setSize(d.getWidth() - 20, d.getHeight() - 20);
175             desktop.setAllSize(x,y);
176             scrollPane.invalidate();
177             scrollPane.validate();
178         }
179     }
180
181     private Insets JavaDoc getScrollPaneInsets() {
182         JScrollPane JavaDoc scrollPane=getScrollPane();
183         if (scrollPane==null) return new Insets JavaDoc(0,0,0,0);
184         else return getScrollPane().getBorder().getBorderInsets(scrollPane);
185     }
186
187     private JScrollPane JavaDoc getScrollPane() {
188         if (desktop.getParent() instanceof JViewport JavaDoc) {
189             JViewport JavaDoc viewPort = (JViewport JavaDoc)desktop.getParent();
190             if (viewPort.getParent() instanceof JScrollPane JavaDoc)
191                 return (JScrollPane JavaDoc)viewPort.getParent();
192         }
193         return null;
194     }
195
196     protected void resizeDesktop() {
197         int x = 0;
198         int y = 0;
199         JScrollPane JavaDoc scrollPane = getScrollPane();
200         Insets JavaDoc scrollInsets = getScrollPaneInsets();
201
202         if (scrollPane != null) {
203             JInternalFrame JavaDoc allFrames[] = desktop.getAllFrames();
204             for (int i = 0; i < allFrames.length; i++) {
205                 if (allFrames[i].getX()+allFrames[i].getWidth()>x) {
206                     x = allFrames[i].getX() + allFrames[i].getWidth();
207                 }
208                 if (allFrames[i].getY()+allFrames[i].getHeight()>y) {
209                     y = allFrames[i].getY() + allFrames[i].getHeight();
210                 }
211             }
212             Dimension JavaDoc d=scrollPane.getVisibleRect().getSize();
213             if (scrollPane.getBorder() != null) {
214                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
215                          d.getHeight() - scrollInsets.top - scrollInsets.bottom);
216             }
217
218             if (x <= d.getWidth()) x = ((int)d.getWidth()) - 20;
219             if (y <= d.getHeight()) y = ((int)d.getHeight()) - 20;
220             desktop.setAllSize(x,y);
221             scrollPane.invalidate();
222             scrollPane.validate();
223         }
224     }
225 }
Popular Tags