Clover coverage report - AdWT - 1.0.0
Coverage timestamp: sam. déc. 27 2003 15:14:10 CET
file stats: LOC: 573   Methods: 26
NCLOC: 331   Classes: 6
 
 Source file Conditionals Statements Methods TOTAL
MDIDesktopPane.java 0% 0% 0% 0%
coverage
 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;
 10   
 import java.awt.Dimension;
 11   
 import java.awt.Insets;
 12   
 import java.awt.Point;
 13   
 import java.awt.Rectangle;
 14   
 import java.beans.PropertyVetoException;
 15   
 
 16   
 import javax.swing.DefaultDesktopManager;
 17   
 import javax.swing.JComponent;
 18   
 import javax.swing.JDesktopPane;
 19   
 import javax.swing.JInternalFrame;
 20   
 import javax.swing.JScrollPane;
 21   
 import javax.swing.JViewport;
 22   
 import javax.swing.border.Border;
 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
 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  0
    public MDIDesktopPane()
 43   
    {
 44  0
       this.manager = new MDIDesktopManager(this);
 45  0
       this.setDesktopManager(manager);
 46  0
       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  0
    public Component add(JInternalFrame frame)
 57   
    {
 58  0
       JInternalFrame[] frames = this.getAllFrames();
 59  0
       Point p;
 60  0
       int w;
 61  0
       int h;
 62   
 
 63  0
       Component retval = super.add(frame);
 64  0
       this.checkDesktopSize();
 65   
 
 66  0
       if (frames.length > 0)
 67   
       {
 68  0
          p = frames[0].getLocation();
 69  0
          p.x = p.x + FRAME_OFFSET;
 70  0
          p.y = p.y + FRAME_OFFSET;
 71   
       }
 72   
       else
 73   
       {
 74  0
          p = new Point(0, 0);
 75   
       }
 76  0
       frame.setLocation(p.x, p.y);
 77  0
       if (frame.isResizable())
 78   
       {
 79  0
          w = getWidth() - (getWidth() / 3);
 80  0
          h = getHeight() - (getHeight() / 3);
 81  0
          if (w < frame.getMinimumSize().getWidth())
 82   
          {
 83  0
             w = (int) frame.getMinimumSize().getWidth();
 84   
          }
 85  0
          if (h < frame.getMinimumSize().getHeight())
 86   
          {
 87  0
             h = (int) frame.getMinimumSize().getHeight();
 88   
          }
 89  0
          frame.setSize(w, h);
 90   
       }
 91   
 
 92  0
       this.moveToFront(frame);
 93  0
       frame.setVisible(true);
 94  0
       try
 95   
       {
 96  0
          frame.setSelected(true);
 97   
       }
 98   
       catch (PropertyVetoException e)
 99   
       {
 100  0
          frame.toBack();
 101   
       }
 102   
 
 103  0
       return retval;
 104   
    }
 105   
 
 106   
 
 107   
    /** Cascade all internal frames */
 108  0
    public void cascadeFrames()
 109   
    {
 110  0
       DesktopArranger.setCurrent(DesktopArranger.CASCADE);
 111  0
       this.arrangeFrames();
 112   
    }
 113   
 
 114   
 
 115   
    /**
 116   
     * Description of the Method
 117   
     *
 118   
     * @param c  Description of Parameter
 119   
     */
 120  0
    public void remove(Component c)
 121   
    {
 122  0
       super.remove(c);
 123  0
       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  0
    public void setAllSize(Dimension d)
 134   
    {
 135  0
       this.setMinimumSize(d);
 136  0
       this.setMaximumSize(d);
 137  0
       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  0
    public void setAllSize(int width, int height)
 148   
    {
 149  0
       this.setAllSize(new Dimension(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  0
    public void setBounds(int x, int y, int w, int h)
 162   
    {
 163  0
       super.setBounds(x, y, w, h);
 164  0
       this.checkDesktopSize();
 165   
    }
 166   
 
 167   
 
 168   
    /** Tile all internal frames */
 169  0
    public void tileFrames()
 170   
    {
 171  0
       DesktopArranger.setCurrent(DesktopArranger.TILES);
 172  0
       this.arrangeFrames();
 173   
    }
 174   
 
 175   
 
 176   
    /** Description of the Method */
 177  0
    private void arrangeFrames()
 178   
    {
 179  0
       JInternalFrame frames[] = getAllFrames();
 180  0
       DesktopArranger.getCurrent().arrange(this.manager, frames, this.getBounds());
 181   
    }
 182   
 
 183   
 
 184   
    /** Description of the Method */
 185  0
    private void checkDesktopSize()
 186   
    {
 187  0
       if (this.getParent() != null && this.isVisible())
 188   
       {
 189  0
          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
 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  0
       public MDIDesktopManager(MDIDesktopPane desktop)
 212   
       {
 213  0
          this.desktop = desktop;
 214   
       }
 215   
 
 216   
 
 217   
       /**
 218   
        * Description of the Method
 219   
        *
 220   
        * @param f  Description of Parameter
 221   
        */
 222  0
       public void endDraggingFrame(JComponent f)
 223   
       {
 224  0
          super.endDraggingFrame(f);
 225  0
          this.resizeDesktop();
 226   
       }
 227   
 
 228   
 
 229   
       /**
 230   
        * Description of the Method
 231   
        *
 232   
        * @param f  Description of Parameter
 233   
        */
 234  0
       public void endResizingFrame(JComponent f)
 235   
       {
 236  0
          super.endResizingFrame(f);
 237  0
          this.resizeDesktop();
 238   
       }
 239   
 
 240   
 
 241   
       /** Sets the NormalSize attribute of the MDIDesktopManager object */
 242  0
       public void setNormalSize()
 243   
       {
 244  0
          JScrollPane scrollPane = this.getScrollPane();
 245  0
          int x = 0;
 246  0
          int y = 0;
 247  0
          Insets scrollInsets = this.getScrollPaneInsets();
 248   
 
 249  0
          if (scrollPane != null)
 250   
          {
 251  0
             Dimension d = scrollPane.getVisibleRect().getSize();
 252  0
             if (scrollPane.getBorder() != null)
 253   
             {
 254  0
                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
 255   
                   d.getHeight() - scrollInsets.top - scrollInsets.bottom);
 256   
             }
 257   
 
 258  0
             d.setSize(d.getWidth() - 20, d.getHeight() - 20);
 259  0
             this.desktop.setAllSize(x, y);
 260  0
             scrollPane.invalidate();
 261  0
             scrollPane.validate();
 262   
          }
 263   
       }
 264   
 
 265   
 
 266   
       /** Description of the Method */
 267  0
       protected void resizeDesktop()
 268   
       {
 269  0
          int x = 0;
 270  0
          int y = 0;
 271  0
          JScrollPane scrollPane = this.getScrollPane();
 272  0
          Insets scrollInsets = this.getScrollPaneInsets();
 273   
 
 274  0
          if (scrollPane != null)
 275   
          {
 276  0
             JInternalFrame frames[] = this.desktop.getAllFrames();
 277  0
             for (int i = 0; i < frames.length; i++)
 278   
             {
 279  0
                if (frames[i].getX() + frames[i].getWidth() > x)
 280   
                {
 281  0
                   x = frames[i].getX() + frames[i].getWidth();
 282   
                }
 283  0
                if (frames[i].getY() + frames[i].getHeight() > y)
 284   
                {
 285  0
                   y = frames[i].getY() + frames[i].getHeight();
 286   
                }
 287   
             }
 288  0
             Dimension d = scrollPane.getVisibleRect().getSize();
 289  0
             if (scrollPane.getBorder() != null)
 290   
             {
 291  0
                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
 292   
                   d.getHeight() - scrollInsets.top - scrollInsets.bottom);
 293   
             }
 294   
 
 295  0
             if (x <= d.getWidth())
 296   
             {
 297  0
                x = ((int) d.getWidth()) - 20;
 298   
             }
 299  0
             if (y <= d.getHeight())
 300   
             {
 301  0
                y = ((int) d.getHeight()) - 20;
 302   
             }
 303  0
             this.desktop.setAllSize(x, y);
 304  0
             scrollPane.invalidate();
 305  0
             scrollPane.validate();
 306   
          }
 307   
       }
 308   
 
 309   
 
 310   
       /**
 311   
        * Gets the ScrollPane attribute of the MDIDesktopManager object
 312   
        *
 313   
        * @return   The ScrollPane value
 314   
        */
 315  0
       private JScrollPane getScrollPane()
 316   
       {
 317  0
          if (this.desktop.getParent() instanceof JViewport)
 318   
          {
 319  0
             JViewport viewPort = (JViewport) this.desktop.getParent();
 320  0
             if (viewPort.getParent() instanceof JScrollPane)
 321   
             {
 322  0
                return (JScrollPane) viewPort.getParent();
 323   
             }
 324   
          }
 325  0
          return null;
 326   
       }
 327   
 
 328   
 
 329   
       /**
 330   
        * Gets the ScrollPaneInsets attribute of the MDIDesktopManager object
 331   
        *
 332   
        * @return   The ScrollPaneInsets value
 333   
        */
 334  0
       private Insets getScrollPaneInsets()
 335   
       {
 336  0
          JScrollPane scrollPane = this.getScrollPane();
 337  0
          if (scrollPane != null)
 338   
          {
 339  0
             Border border = this.getScrollPane().getBorder();
 340  0
             if (border != null)
 341   
             {
 342  0
                return border.getBorderInsets(scrollPane);
 343   
             }
 344   
          }
 345  0
          return new Insets(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  0
       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  0
       public void arrange(MDIDesktopManager manager, JInternalFrame[] frames, Rectangle bounds)
 372   
       {
 373  0
          manager.setNormalSize();
 374   
 
 375  0
          int x = 0;
 376  0
          int y = 0;
 377   
 
 378  0
          int frameWidth = bounds.width - frames.length * FRAME_OFFSET;
 379  0
          if (frameWidth < (bounds.width / 2))
 380   
          {
 381  0
             frameWidth = bounds.width / 2;
 382   
          }
 383   
 
 384  0
          int frameHeight = bounds.height - frames.length * FRAME_OFFSET;
 385  0
          if (frameHeight < (bounds.height / 2))
 386   
          {
 387  0
             frameHeight = bounds.height / 2;
 388   
          }
 389   
 
 390  0
          for (int i = frames.length - 1; i >= 0; i--)
 391   
          {
 392  0
             if (!frames[i].isIcon())
 393   
             {
 394  0
                frames[i].setSize(frameWidth, frameHeight);
 395  0
                frames[i].setLocation(x, y);
 396  0
                x = x + FRAME_OFFSET;
 397  0
                y = y + FRAME_OFFSET;
 398  0
                if ((x > (bounds.width - frameWidth)) || (y > (bounds.height - frameHeight)))
 399   
                {
 400  0
                   x = 0;
 401  0
                   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  0
       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[] frames, Rectangle bounds);
 439   
 
 440   
 
 441   
       /**
 442   
        * Gets the current attribute of the DesktopArranger class
 443   
        *
 444   
        * @return   The current value
 445   
        */
 446  0
       public static DesktopArranger getCurrent()
 447   
       {
 448  0
          if (DesktopArranger.current == null)
 449   
          {
 450  0
             return DesktopArranger.CASCADE;
 451   
          }
 452  0
          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  0
       public static void setCurrent(DesktopArranger arranger)
 462   
       {
 463  0
          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  0
       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  0
       public void arrange(MDIDesktopManager manager, JInternalFrame[] frames, Rectangle bounds)
 486   
       {
 487  0
          manager.setNormalSize();
 488  0
          try
 489   
          {
 490  0
             for (int i = 0; i < frames.length; i++)
 491   
             {
 492  0
                frames[i].setMaximum(true);
 493   
             }
 494   
          }
 495   
          catch (PropertyVetoException 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  0
       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  0
       public void arrange(MDIDesktopManager manager, JInternalFrame[] frames, Rectangle bounds)
 521   
       {
 522  0
          int totalNonIconFrames = 0;
 523   
 
 524  0
          for (int i = 0; i < frames.length; i++)
 525   
          {
 526   
             // don't include iconified frames...
 527  0
             if (!frames[i].isIcon())
 528   
             {
 529  0
                totalNonIconFrames++;
 530   
             }
 531   
          }
 532   
 
 533  0
          int curCol = 0;
 534  0
          int curRow = 0;
 535  0
          int i = 0;
 536   
 
 537  0
          if (totalNonIconFrames > 0)
 538   
          {
 539   
             // compute number of columns and rows then tile the frames
 540  0
             int numCols = (int) Math.sqrt(totalNonIconFrames);
 541   
 
 542  0
             int frameWidth = bounds.width / numCols;
 543  0
             for (curCol = 0; curCol < numCols; curCol++)
 544   
             {
 545  0
                int numRows = totalNonIconFrames / numCols;
 546  0
                int remainder = totalNonIconFrames % numCols;
 547   
 
 548  0
                if ((numCols - curCol) <= remainder)
 549   
                {
 550  0
                   numRows++;
 551   
                   // add an extra row for this guy
 552   
                }
 553   
 
 554  0
                int frameHeight = bounds.height / numRows;
 555  0
                for (curRow = 0; curRow < numRows; curRow++)
 556   
                {
 557  0
                   while (frames[i].isIcon())
 558   
                   {
 559   
                      // find the next visible frame
 560  0
                      i++;
 561   
                   }
 562   
 
 563  0
                   frames[i].setBounds(curCol * frameWidth, curRow * frameHeight, frameWidth, frameHeight);
 564  0
                   i++;
 565   
                }
 566   
             }
 567   
          }
 568   
       }
 569   
    }
 570   
 
 571   
 }
 572   
 
 573