KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > service > MDIDesktopPane


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.service;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Insets JavaDoc;
12 import java.awt.Point JavaDoc;
13 import java.awt.Rectangle JavaDoc;
14 import java.beans.PropertyVetoException JavaDoc;
15
16 import javax.swing.DefaultDesktopManager JavaDoc;
17 import javax.swing.JComponent JavaDoc;
18 import javax.swing.JDesktopPane JavaDoc;
19 import javax.swing.JInternalFrame JavaDoc;
20 import javax.swing.JScrollPane JavaDoc;
21 import javax.swing.JViewport JavaDoc;
22 import javax.swing.border.Border JavaDoc;
23
24 /**
25  * An extension of JDesktopPane that supports often used MDI functionality.
26  * This class also handles setting scroll bars for when windows move too far to the
27  * left or bottom, providing the MDIDesktopPane is in a ScrollPane.
28  *
29  * @author Laurent Etiemble
30  * @version $Revision: 1.6 $
31  * @todo Javadoc to complete
32  */

33 public class MDIDesktopPane extends JDesktopPane JavaDoc
34 {
35    /** Description of the Field */
36    protected MDIDesktopManager manager;
37    /** Description of the Field */
38    protected static int FRAME_OFFSET = 30;
39
40
41    /** Constructor for the MDIDesktopPane object */
42    public MDIDesktopPane()
43    {
44       this.manager = new MDIDesktopManager(this);
45       this.setDesktopManager(manager);
46       this.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
47    }
48
49
50    /**
51     * Description of the Method
52     *
53     * @param frame Description of Parameter
54     * @return Description of the Returned Value
55     */

56    public Component JavaDoc add(JInternalFrame JavaDoc frame)
57    {
58       JInternalFrame JavaDoc[] frames = this.getAllFrames();
59       Point JavaDoc p;
60       int w;
61       int h;
62
63       Component JavaDoc retval = super.add(frame);
64       this.checkDesktopSize();
65
66       if (frames.length > 0)
67       {
68          p = frames[0].getLocation();
69          p.x = p.x + FRAME_OFFSET;
70          p.y = p.y + FRAME_OFFSET;
71       }
72       else
73       {
74          p = new Point JavaDoc(0, 0);
75       }
76       frame.setLocation(p.x, p.y);
77       if (frame.isResizable())
78       {
79          w = getWidth() - (getWidth() / 3);
80          h = getHeight() - (getHeight() / 3);
81          if (w < frame.getMinimumSize().getWidth())
82          {
83             w = (int) frame.getMinimumSize().getWidth();
84          }
85          if (h < frame.getMinimumSize().getHeight())
86          {
87             h = (int) frame.getMinimumSize().getHeight();
88          }
89          frame.setSize(w, h);
90       }
91
92       this.moveToFront(frame);
93       frame.setVisible(true);
94       try
95       {
96          frame.setSelected(true);
97       }
98       catch (PropertyVetoException JavaDoc e)
99       {
100          frame.toBack();
101       }
102
103       return retval;
104    }
105
106
107    /** Cascade all internal frames */
108    public void cascadeFrames()
109    {
110       DesktopArranger.setCurrent(DesktopArranger.CASCADE);
111       this.arrangeFrames();
112    }
113
114
115    /**
116     * Description of the Method
117     *
118     * @param c Description of Parameter
119     */

120    public void remove(Component JavaDoc c)
121    {
122       super.remove(c);
123       this.checkDesktopSize();
124 // this.arrangeFrames();
125
}
126
127
128    /**
129     * Sets all component size properties ( maximum, minimum, preferred) to the given dimension.
130     *
131     * @param d The new AllSize value
132     */

133    public void setAllSize(Dimension JavaDoc d)
134    {
135       this.setMinimumSize(d);
136       this.setMaximumSize(d);
137       this.setPreferredSize(d);
138    }
139
140
141    /**
142     * Sets all component size properties ( maximum, minimum, preferred) to the given width and height.
143     *
144     * @param width The new AllSize value
145     * @param height The new AllSize value
146     */

147    public void setAllSize(int width, int height)
148    {
149       this.setAllSize(new Dimension JavaDoc(width, height));
150    }
151
152
153    /**
154     * Sets the Bounds attribute of the MDIDesktopPane object
155     *
156     * @param x The new Bounds value
157     * @param y The new Bounds value
158     * @param w The new Bounds value
159     * @param h The new Bounds value
160     */

161    public void setBounds(int x, int y, int w, int h)
162    {
163       super.setBounds(x, y, w, h);
164       this.checkDesktopSize();
165    }
166
167
168    /** Tile all internal frames */
169    public void tileFrames()
170    {
171       DesktopArranger.setCurrent(DesktopArranger.TILES);
172       this.arrangeFrames();
173    }
174
175
176    /** Description of the Method */
177    private void arrangeFrames()
178    {
179       JInternalFrame JavaDoc frames[] = getAllFrames();
180       DesktopArranger.getCurrent().arrange(this.manager, frames, this.getBounds());
181    }
182
183
184    /** Description of the Method */
185    private void checkDesktopSize()
186    {
187       if (this.getParent() != null && this.isVisible())
188       {
189          this.manager.resizeDesktop();
190       }
191    }
192
193
194    /**
195     * Private class used to replace the standard DesktopManager for JDesktopPane. Used to provide scrollbar functionality.
196     *
197     * @author laurent
198     * @version $Revision: 1.6 $
199     */

200    protected class MDIDesktopManager extends DefaultDesktopManager JavaDoc
201    {
202       /** Description of the Field */
203       protected MDIDesktopPane desktop;
204
205
206       /**
207        * Constructor for the MDIDesktopManager object
208        *
209        * @param desktop Description of Parameter
210        */

211       public MDIDesktopManager(MDIDesktopPane desktop)
212       {
213          this.desktop = desktop;
214       }
215
216
217       /**
218        * Description of the Method
219        *
220        * @param f Description of Parameter
221        */

222       public void endDraggingFrame(JComponent JavaDoc f)
223       {
224          super.endDraggingFrame(f);
225          this.resizeDesktop();
226       }
227
228
229       /**
230        * Description of the Method
231        *
232        * @param f Description of Parameter
233        */

234       public void endResizingFrame(JComponent JavaDoc f)
235       {
236          super.endResizingFrame(f);
237          this.resizeDesktop();
238       }
239
240
241       /** Sets the NormalSize attribute of the MDIDesktopManager object */
242       public void setNormalSize()
243       {
244          JScrollPane JavaDoc scrollPane = this.getScrollPane();
245          int x = 0;
246          int y = 0;
247          Insets JavaDoc scrollInsets = this.getScrollPaneInsets();
248
249          if (scrollPane != null)
250          {
251             Dimension JavaDoc d = scrollPane.getVisibleRect().getSize();
252             if (scrollPane.getBorder() != null)
253             {
254                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
255                   d.getHeight() - scrollInsets.top - scrollInsets.bottom);
256             }
257
258             d.setSize(d.getWidth() - 20, d.getHeight() - 20);
259             this.desktop.setAllSize(x, y);
260             scrollPane.invalidate();
261             scrollPane.validate();
262          }
263       }
264
265
266       /** Description of the Method */
267       protected void resizeDesktop()
268       {
269          int x = 0;
270          int y = 0;
271          JScrollPane JavaDoc scrollPane = this.getScrollPane();
272          Insets JavaDoc scrollInsets = this.getScrollPaneInsets();
273
274          if (scrollPane != null)
275          {
276             JInternalFrame JavaDoc frames[] = this.desktop.getAllFrames();
277             for (int i = 0; i < frames.length; i++)
278             {
279                if (frames[i].getX() + frames[i].getWidth() > x)
280                {
281                   x = frames[i].getX() + frames[i].getWidth();
282                }
283                if (frames[i].getY() + frames[i].getHeight() > y)
284                {
285                   y = frames[i].getY() + frames[i].getHeight();
286                }
287             }
288             Dimension JavaDoc d = scrollPane.getVisibleRect().getSize();
289             if (scrollPane.getBorder() != null)
290             {
291                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
292                   d.getHeight() - scrollInsets.top - scrollInsets.bottom);
293             }
294
295             if (x <= d.getWidth())
296             {
297                x = ((int) d.getWidth()) - 20;
298             }
299             if (y <= d.getHeight())
300             {
301                y = ((int) d.getHeight()) - 20;
302             }
303             this.desktop.setAllSize(x, y);
304             scrollPane.invalidate();
305             scrollPane.validate();
306          }
307       }
308
309
310       /**
311        * Gets the ScrollPane attribute of the MDIDesktopManager object
312        *
313        * @return The ScrollPane value
314        */

315       private JScrollPane JavaDoc getScrollPane()
316       {
317          if (this.desktop.getParent() instanceof JViewport JavaDoc)
318          {
319             JViewport JavaDoc viewPort = (JViewport JavaDoc) this.desktop.getParent();
320             if (viewPort.getParent() instanceof JScrollPane JavaDoc)
321             {
322                return (JScrollPane JavaDoc) viewPort.getParent();
323             }
324          }
325          return null;
326       }
327
328
329       /**
330        * Gets the ScrollPaneInsets attribute of the MDIDesktopManager object
331        *
332        * @return The ScrollPaneInsets value
333        */

334       private Insets JavaDoc getScrollPaneInsets()
335       {
336          JScrollPane JavaDoc scrollPane = this.getScrollPane();
337          if (scrollPane != null)
338          {
339             Border JavaDoc border = this.getScrollPane().getBorder();
340             if (border != null)
341             {
342                return border.getBorderInsets(scrollPane);
343             }
344          }
345          return new Insets JavaDoc(0, 0, 0, 0);
346       }
347    }
348
349
350    /**
351     * Description of the Class
352     *
353     * @author Administrator
354     * @version $Revision: 1.6 $
355     */

356    protected static class CascadeArranger extends DesktopArranger
357    {
358       /** Description of the Field */
359       private static int FRAME_OFFSET = 30;
360
361
362       /** Constructor for CascadeArranger. */
363       protected CascadeArranger() { }
364
365
366       /**
367        * @param frames Description of the Parameter
368        * @param bounds Description of the Parameter
369        * @param manager Description of the Parameter
370        */

371       public void arrange(MDIDesktopManager manager, JInternalFrame JavaDoc[] frames, Rectangle JavaDoc bounds)
372       {
373          manager.setNormalSize();
374
375          int x = 0;
376          int y = 0;
377
378          int frameWidth = bounds.width - frames.length * FRAME_OFFSET;
379          if (frameWidth < (bounds.width / 2))
380          {
381             frameWidth = bounds.width / 2;
382          }
383
384          int frameHeight = bounds.height - frames.length * FRAME_OFFSET;
385          if (frameHeight < (bounds.height / 2))
386          {
387             frameHeight = bounds.height / 2;
388          }
389
390          for (int i = frames.length - 1; i >= 0; i--)
391          {
392             if (!frames[i].isIcon())
393             {
394                frames[i].setSize(frameWidth, frameHeight);
395                frames[i].setLocation(x, y);
396                x = x + FRAME_OFFSET;
397                y = y + FRAME_OFFSET;
398                if ((x > (bounds.width - frameWidth)) || (y > (bounds.height - frameHeight)))
399                {
400                   x = 0;
401                   y = 0;
402                }
403             }
404          }
405       }
406    }
407
408
409    /**
410     * Description of the Interface
411     *
412     * @author Administrator
413     * @version $Revision: 1.6 $
414     */

415    protected abstract static class DesktopArranger
416    {
417       /** Description of the Field */
418       private static DesktopArranger current;
419       /** Description of the Field */
420       public final static DesktopArranger CASCADE = new CascadeArranger();
421       /** Description of the Field */
422       public final static DesktopArranger MAXIMIZE = new MaximizeArranger();
423       /** Description of the Field */
424       public final static DesktopArranger TILES = new TileArranger();
425
426
427       /**Constructor for the DesktopArranger object */
428       protected DesktopArranger() { }
429
430
431       /**
432        * Description of the Method
433        *
434        * @param frames Description of the Parameter
435        * @param bounds Description of the Parameter
436        * @param manager Description of the Parameter
437        */

438       public abstract void arrange(MDIDesktopManager manager, JInternalFrame JavaDoc[] frames, Rectangle JavaDoc bounds);
439
440
441       /**
442        * Gets the current attribute of the DesktopArranger class
443        *
444        * @return The current value
445        */

446       public static DesktopArranger getCurrent()
447       {
448          if (DesktopArranger.current == null)
449          {
450             return DesktopArranger.CASCADE;
451          }
452          return current;
453       }
454
455
456       /**
457        * Sets the current attribute of the DesktopArranger class
458        *
459        * @param arranger The new current value
460        */

461       public static void setCurrent(DesktopArranger arranger)
462       {
463          DesktopArranger.current = arranger;
464       }
465    }
466
467
468    /**
469     * Description of the Class
470     *
471     * @author Administrator
472     * @version $Revision: 1.6 $
473     */

474    protected static class MaximizeArranger extends DesktopArranger
475    {
476       /** Constructor for MaximizeArranger. */
477       protected MaximizeArranger() { }
478
479
480       /**
481        * @param manager Description of the Parameter
482        * @param frames Description of the Parameter
483        * @param bounds Description of the Parameter
484        */

485       public void arrange(MDIDesktopManager manager, JInternalFrame JavaDoc[] frames, Rectangle JavaDoc bounds)
486       {
487          manager.setNormalSize();
488          try
489          {
490             for (int i = 0; i < frames.length; i++)
491             {
492                frames[i].setMaximum(true);
493             }
494          }
495          catch (PropertyVetoException JavaDoc pve)
496          {
497             // Do nothing
498
}
499       }
500    }
501
502
503    /**
504     * Description of the Class
505     *
506     * @author Administrator
507     * @version $Revision: 1.6 $
508     */

509    protected static class TileArranger extends DesktopArranger
510    {
511       /** Constructor for TileArranger. */
512       protected TileArranger() { }
513
514
515       /**
516        * @param frames Description of the Parameter
517        * @param bounds Description of the Parameter
518        * @param manager Description of the Parameter
519        */

520       public void arrange(MDIDesktopManager manager, JInternalFrame JavaDoc[] frames, Rectangle JavaDoc bounds)
521       {
522          int totalNonIconFrames = 0;
523
524          for (int i = 0; i < frames.length; i++)
525          {
526             // don't include iconified frames...
527
if (!frames[i].isIcon())
528             {
529                totalNonIconFrames++;
530             }
531          }
532
533          int curCol = 0;
534          int curRow = 0;
535          int i = 0;
536
537          if (totalNonIconFrames > 0)
538          {
539             // compute number of columns and rows then tile the frames
540
int numCols = (int) Math.sqrt(totalNonIconFrames);
541
542             int frameWidth = bounds.width / numCols;
543             for (curCol = 0; curCol < numCols; curCol++)
544             {
545                int numRows = totalNonIconFrames / numCols;
546                int remainder = totalNonIconFrames % numCols;
547
548                if ((numCols - curCol) <= remainder)
549                {
550                   numRows++;
551                   // add an extra row for this guy
552
}
553
554                int frameHeight = bounds.height / numRows;
555                for (curRow = 0; curRow < numRows; curRow++)
556                {
557                   while (frames[i].isIcon())
558                   {
559                      // find the next visible frame
560
i++;
561                   }
562
563                   frames[i].setBounds(curCol * frameWidth, curRow * frameHeight, frameWidth, frameHeight);
564                   i++;
565                }
566             }
567          }
568       }
569    }
570
571 }
572
573
Popular Tags