Source for javax.swing.JRootPane

   1: /* JRootPane.java --
   2:    Copyright (C) 2002, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.awt.BorderLayout;
  42: import java.awt.Component;
  43: import java.awt.Container;
  44: import java.awt.Dimension;
  45: import java.awt.LayoutManager;
  46: import java.awt.LayoutManager2;
  47: import java.io.Serializable;
  48: 
  49: import javax.accessibility.AccessibleRole;
  50: import javax.swing.plaf.RootPaneUI;
  51: 
  52: /**
  53:  * This class is where JComponents are added to. Unlike awt where you could
  54:  * just say frame.add(), with swing you need to say frame.getRootPane()
  55:  * (which delivers an instance of this class) and add your components to
  56:  * that. It is implemented by several 'layers' (pane() should be read as
  57:  * plane()) each on top of the others where you can add components to.
  58:  * (getContentPane(), getGlassPane(), getLayeredPane())
  59:  *
  60:  * @author Ronald Veldema (rveldema@cs.vu.nl)
  61:  */
  62: public class JRootPane extends JComponent
  63: {
  64:   //  The class used to obtain the accessible role for this object.
  65:   protected static class AccessibleJRootPane
  66:   {
  67:     /**
  68:      * For compatability with Sun's JDK
  69:      */
  70:     private static final long serialVersionUID = 1082432482784468088L;
  71: 
  72:     /**
  73:      * Creates a new <code>AccessibleJRootPane</code> object.
  74:      */
  75:     protected AccessibleJRootPane()
  76:     {
  77:     }
  78: 
  79:     /**
  80:      * DOCUMENT ME!
  81:      *
  82:      * @return DOCUMENT ME!
  83:      */
  84:     public AccessibleRole getAccessibleRole()
  85:     {
  86:       return AccessibleRole.ROOT_PANE;
  87:     }
  88:   }
  89: 
  90:   // Custom Layout Manager for JRootPane. It positions contentPane and 
  91:   // menuBar withing its layeredPane.
  92:   protected class RootLayout implements LayoutManager2, Serializable
  93:   {
  94:     /** DOCUMENT ME! */
  95:     private static final long serialVersionUID = -4100116998559815027L;
  96: 
  97:     /**
  98:      * Creates a new <code>RootLayout</code> object.
  99:      */
 100:     protected RootLayout()
 101:     {
 102:     }
 103: 
 104:     /**
 105:      * DOCUMENT ME!
 106:      *
 107:      * @param comp DOCUMENT ME!
 108:      * @param constraints DOCUMENT ME!
 109:      */
 110:     public void addLayoutComponent(Component comp, Object constraints)
 111:     {
 112:     }
 113: 
 114:     /**
 115:      * DOCUMENT ME!
 116:      *
 117:      * @param name DOCUMENT ME!
 118:      * @param comp DOCUMENT ME!
 119:      */
 120:     public void addLayoutComponent(String name, Component comp)
 121:     {
 122:     }
 123: 
 124:     /**
 125:      * DOCUMENT ME!
 126:      *
 127:      * @param target DOCUMENT ME!
 128:      *
 129:      * @return DOCUMENT ME!
 130:      */
 131:     public float getLayoutAlignmentX(Container target)
 132:     {
 133:       return target.getAlignmentX();
 134:     }
 135: 
 136:     /**
 137:      * DOCUMENT ME!
 138:      *
 139:      * @param target DOCUMENT ME!
 140:      *
 141:      * @return DOCUMENT ME!
 142:      */
 143:     public float getLayoutAlignmentY(Container target)
 144:     {
 145:       return target.getAlignmentY();
 146:     }
 147: 
 148:     /**
 149:      * DOCUMENT ME!
 150:      *
 151:      * @param target DOCUMENT ME!
 152:      */
 153:     public void invalidateLayout(Container target)
 154:     {
 155:     }
 156: 
 157:     /**
 158:      * DOCUMENT ME!
 159:      *
 160:      * @param c DOCUMENT ME!
 161:      */
 162:     public void layoutContainer(Container c)
 163:     {
 164:       Dimension menuBarSize;
 165:       Dimension containerSize = c.getSize(null);
 166:       Dimension contentPaneSize = contentPane.getPreferredSize();
 167: 
 168:       /*
 169:        if size of top-level window wasn't set then just set
 170:        contentPane and menuBar to its preferred sizes.
 171:        Otherwise, if the size of top-level window was specified then
 172:        set menuBar to its preferred size and make content pane
 173:        to fit into the remaining space
 174: 
 175: 
 176:        +-------------------------------+
 177:        |  JLayeredPane                 |
 178:        |  +--------------------------+ |
 179:        |  | menuBar                  | |
 180:        |  +--------------------------+ |
 181:        |  +--------------------------+ |
 182:        |  |contentPane               | |
 183:        |  |                          | |
 184:        |  |                          | |
 185:        |  |                          | |
 186:        |  +--------------------------+ |
 187:        +-------------------------------+
 188: 
 189:       */
 190:       if (containerSize.width == 0 && containerSize.height == 0)
 191:         {
 192:           if (menuBar != null)
 193:             {
 194:               int maxWidth;
 195:               menuBarSize = menuBar.getPreferredSize();
 196:               maxWidth = Math.max(menuBarSize.width, contentPaneSize.width);
 197:               menuBar.setBounds(0, 0, maxWidth, menuBarSize.height);
 198:               glassPane.setBounds(0, menuBarSize.height, maxWidth,
 199:                                   contentPaneSize.height);
 200:               contentPane.setBounds(0, menuBarSize.height, maxWidth,
 201:                                     contentPaneSize.height);
 202:               layeredPane.setSize(maxWidth,
 203:                                   menuBarSize.height + contentPaneSize.height);
 204:             }
 205:           else
 206:             {
 207:               glassPane.setBounds(0, 0, contentPaneSize.width,
 208:                                   contentPaneSize.height);
 209:               contentPane.setBounds(0, 0, contentPaneSize.width,
 210:                                     contentPaneSize.height);
 211:               layeredPane.setSize(contentPaneSize.width, contentPaneSize.height);
 212:             }
 213:         }
 214:       else
 215:         {
 216:           if (menuBar != null)
 217:             {
 218:               menuBarSize = menuBar.getPreferredSize();
 219:               if (menuBarSize.height > containerSize.height)
 220:                 menuBarSize.height = containerSize.height;
 221:               menuBar.setBounds(0, 0, containerSize.width, menuBarSize.height);
 222:               int remainingHeight = containerSize.height - menuBarSize.height;
 223:               glassPane.setBounds(0, menuBarSize.height, containerSize.width,
 224:                                   containerSize.height - menuBarSize.height);
 225:               contentPane.setBounds(0, menuBarSize.height,
 226:                                     containerSize.width,
 227:                                     (containerSize.height - menuBarSize.height));
 228:             }
 229:           else
 230:             {
 231:               glassPane.setBounds(0, 0, containerSize.width,
 232:                                   containerSize.height);
 233:               contentPane.setBounds(0, 0, containerSize.width,
 234:                                     containerSize.height);
 235:             }
 236: 
 237:           layeredPane.setSize(containerSize.width, containerSize.height);
 238:         }
 239:     }
 240: 
 241:     /**
 242:      * DOCUMENT ME!
 243:      *
 244:      * @param target DOCUMENT ME!
 245:      *
 246:      * @return DOCUMENT ME!
 247:      */
 248:     public Dimension maximumLayoutSize(Container target)
 249:     {
 250:       return preferredLayoutSize(target);
 251:     }
 252: 
 253:     /**
 254:      * DOCUMENT ME!
 255:      *
 256:      * @param target DOCUMENT ME!
 257:      *
 258:      * @return DOCUMENT ME!
 259:      */
 260:     public Dimension minimumLayoutSize(Container target)
 261:     {
 262:       return preferredLayoutSize(target);
 263:     }
 264: 
 265:     /**
 266:      * DOCUMENT ME!
 267:      *
 268:      * @param c DOCUMENT ME!
 269:      *
 270:      * @return DOCUMENT ME!
 271:      */
 272:     public Dimension preferredLayoutSize(Container c)
 273:     {
 274:       Dimension menuBarSize;
 275:       Dimension prefSize;
 276: 
 277:       Dimension containerSize = c.getSize();
 278:       Dimension contentPaneSize = contentPane.getPreferredSize();
 279: 
 280:       if (containerSize.width == 0 && containerSize.height == 0)
 281:         {
 282:           if (menuBar != null)
 283:             {
 284:               int maxWidth;
 285:               menuBarSize = menuBar.getPreferredSize();
 286:               maxWidth = Math.max(menuBarSize.width, contentPaneSize.width);
 287:               prefSize = new Dimension(maxWidth,
 288:                                        contentPaneSize.height
 289:                                        + menuBarSize.height);
 290:             }
 291:           else
 292:             prefSize = contentPaneSize;
 293:         }
 294:       else
 295:         prefSize = c.getSize();
 296: 
 297:       return prefSize;
 298:     }
 299: 
 300:     /**
 301:      * DOCUMENT ME!
 302:      *
 303:      * @param comp DOCUMENT ME!
 304:      */
 305:     public void removeLayoutComponent(Component comp)
 306:     {
 307:     }
 308:   }
 309: 
 310:   /** DOCUMENT ME! */
 311:   private static final long serialVersionUID = 8690748000348575668L;
 312: 
 313:   public static final int NONE = 0;
 314:   public static final int FRAME = 1;
 315:   public static final int PLAIN_DIALOG = 2;
 316:   public static final int INFORMATION_DIALOG = 3;
 317:   public static final int ERROR_DIALOG = 4;
 318:   public static final int COLOR_CHOOSER_DIALOG = 5;
 319:   public static final int FILE_CHOOSER_DIALOG = 6;
 320:   public static final int QUESTION_DIALOG = 7;
 321:   public static final int WARNING_DIALOG = 8;
 322:           
 323:   /** DOCUMENT ME! */
 324:   protected Component glassPane;
 325: 
 326:   /** DOCUMENT ME! */
 327:   protected JLayeredPane layeredPane;
 328: 
 329:   /** DOCUMENT ME! */
 330:   protected JMenuBar menuBar;
 331: 
 332:   /** DOCUMENT ME! */
 333:   protected Container contentPane;
 334: 
 335:   protected JButton defaultButton;
 336: 
 337:   /**
 338:    * @since 1.4
 339:    */
 340:   private int windowDecorationStyle = NONE;
 341:   
 342:   /**
 343:    * DOCUMENT ME!
 344:    *
 345:    * @param m DOCUMENT ME!
 346:    */
 347:   public void setJMenuBar(JMenuBar m)
 348:   {
 349:     JLayeredPane jlPane = getLayeredPane();
 350:     if (menuBar != null)
 351:       jlPane.remove(menuBar);
 352:     menuBar = m;
 353:     if (menuBar != null)
 354:       jlPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
 355:   }
 356: 
 357:   /**
 358:    * @deprecated Replaced by <code>setJMenuBar()</code>
 359:    */
 360:   public void setMenuBar(JMenuBar m)
 361:   {
 362:     setJMenuBar(m);
 363:   }
 364: 
 365:   /**
 366:    * DOCUMENT ME!
 367:    *
 368:    * @return DOCUMENT ME!
 369:    */
 370:   public JMenuBar getJMenuBar()
 371:   {
 372:     return menuBar;
 373:   }
 374: 
 375:   /**
 376:    * @deprecated Replaced by <code>getJMenuBar()</code>
 377:    */
 378:   public JMenuBar getMenuBar()
 379:   {
 380:     return getJMenuBar();
 381:   }
 382: 
 383:   /**
 384:    * DOCUMENT ME!
 385:    *
 386:    * @return DOCUMENT ME!
 387:    */
 388:   public boolean isValidateRoot()
 389:   {
 390:     return true;
 391:   }
 392: 
 393:   /**
 394:    * DOCUMENT ME!
 395:    *
 396:    * @return DOCUMENT ME!
 397:    */
 398:   public Container getContentPane()
 399:   {
 400:     if (contentPane == null)
 401:       setContentPane(createContentPane());
 402:     return contentPane;
 403:   }
 404: 
 405:   /**
 406:    * DOCUMENT ME!
 407:    *
 408:    * @param p DOCUMENT ME!
 409:    */
 410:   public void setContentPane(Container p)
 411:   {
 412:     contentPane = p;
 413:     getLayeredPane().add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
 414:   }
 415: 
 416:   /**
 417:    * DOCUMENT ME!
 418:    *
 419:    * @param comp DOCUMENT ME!
 420:    * @param constraints DOCUMENT ME!
 421:    * @param index DOCUMENT ME!
 422:    */
 423:   protected void addImpl(Component comp, Object constraints, int index)
 424:   {
 425:     super.addImpl(comp, constraints, index);
 426:   }
 427: 
 428:   /**
 429:    * DOCUMENT ME!
 430:    *
 431:    * @return DOCUMENT ME!
 432:    */
 433:   public Component getGlassPane()
 434:   {
 435:     if (glassPane == null)
 436:       setGlassPane(createGlassPane());
 437:     return glassPane;
 438:   }
 439: 
 440:   /**
 441:    * DOCUMENT ME!
 442:    *
 443:    * @param f DOCUMENT ME!
 444:    */
 445:   public void setGlassPane(Component f)
 446:   {
 447:     if (glassPane != null)
 448:       remove(glassPane);
 449: 
 450:     glassPane = f;
 451: 
 452:     glassPane.setVisible(false);
 453:     add(glassPane, 0);
 454:   }
 455: 
 456:   /**
 457:    * DOCUMENT ME!
 458:    *
 459:    * @return DOCUMENT ME!
 460:    */
 461:   public JLayeredPane getLayeredPane()
 462:   {
 463:     if (layeredPane == null)
 464:       setLayeredPane(createLayeredPane());
 465:     return layeredPane;
 466:   }
 467: 
 468:   /**
 469:    * DOCUMENT ME!
 470:    *
 471:    * @param f DOCUMENT ME!
 472:    */
 473:   public void setLayeredPane(JLayeredPane f)
 474:   {
 475:     if (layeredPane != null)
 476:       remove(layeredPane);
 477: 
 478:     layeredPane = f;
 479:     add(f, -1);
 480:   }
 481: 
 482:   /**
 483:    * Creates a new <code>JRootPane</code> object.
 484:    */
 485:   public JRootPane()
 486:   {
 487:     setLayout(createRootLayout());
 488:     getGlassPane();
 489:     getLayeredPane();
 490:     getContentPane();
 491:     setDoubleBuffered(true);
 492:     updateUI();
 493:   }
 494: 
 495:   /**
 496:    * DOCUMENT ME!
 497:    *
 498:    * @return DOCUMENT ME!
 499:    */
 500:   protected LayoutManager createRootLayout()
 501:   {
 502:     return new RootLayout();
 503:   }
 504: 
 505:   /**
 506:    * DOCUMENT ME!
 507:    *
 508:    * @return DOCUMENT ME!
 509:    */
 510:   protected Container createContentPane()
 511:   {
 512:     JPanel p = new JPanel();
 513:     p.setName(this.getName() + ".contentPane");
 514:     p.setLayout(new BorderLayout());
 515:     return p;
 516:   }
 517: 
 518:   /**
 519:    * DOCUMENT ME!
 520:    *
 521:    * @return DOCUMENT ME!
 522:    */
 523:   protected Component createGlassPane()
 524:   {
 525:     JPanel p = new JPanel();
 526:     p.setName(this.getName() + ".glassPane");
 527:     p.setLayout(new BorderLayout());
 528:     p.setVisible(false);
 529:     p.setOpaque(false);
 530:     return p;
 531:   }
 532: 
 533:   /**
 534:    * DOCUMENT ME!
 535:    *
 536:    * @return DOCUMENT ME!
 537:    */
 538:   protected JLayeredPane createLayeredPane()
 539:   {
 540:     JLayeredPane l = new JLayeredPane();
 541:     l.setLayout(null);
 542:     return l;
 543:   }
 544: 
 545:   /**
 546:    * DOCUMENT ME!
 547:    *
 548:    * @return DOCUMENT ME!
 549:    */
 550:   public RootPaneUI getUI()
 551:   {
 552:     return (RootPaneUI) ui;
 553:   }
 554: 
 555:   /**
 556:    * DOCUMENT ME!
 557:    *
 558:    * @param ui DOCUMENT ME!
 559:    */
 560:   public void setUI(RootPaneUI ui)
 561:   {
 562:     super.setUI(ui);
 563:   }
 564: 
 565:   /**
 566:    * DOCUMENT ME!
 567:    */
 568:   public void updateUI()
 569:   {
 570:     setUI((RootPaneUI) UIManager.getUI(this));
 571:   }
 572: 
 573:   /**
 574:    * DOCUMENT ME!
 575:    *
 576:    * @return DOCUMENT ME!
 577:    */
 578:   public String getUIClassID()
 579:   {
 580:     return "RootPaneUI";
 581:   }
 582: 
 583:   public JButton getDefaultButton()
 584:   {
 585:     return defaultButton;
 586:   }
 587:   
 588:   public void setDefaultButton(JButton newButton)
 589:   {
 590:     if (defaultButton == newButton)
 591:       return;
 592:     
 593:     JButton oldButton = defaultButton;
 594:     defaultButton = newButton;
 595:     firePropertyChange("defaultButton", oldButton, newButton);
 596:   }
 597: 
 598:   /**
 599:    * @since 1.4
 600:    */
 601:   public int getWindowDecorationStyle()
 602:   {
 603:     return windowDecorationStyle;
 604:   }
 605: 
 606:   /**
 607:    * @since 1.4
 608:    */
 609:   public void setWindowDecorationStyle(int style)
 610:   {
 611:     if (style != NONE
 612:         && style != FRAME
 613:         && style != INFORMATION_DIALOG
 614:         && style != ERROR_DIALOG
 615:         && style != COLOR_CHOOSER_DIALOG
 616:         && style != FILE_CHOOSER_DIALOG
 617:         && style != QUESTION_DIALOG
 618:         && style != WARNING_DIALOG)
 619:       throw new IllegalArgumentException("invalid style");
 620:     
 621:     int oldStyle = windowDecorationStyle;
 622:     windowDecorationStyle = style;
 623:     firePropertyChange("windowDecorationStyle", oldStyle, style);
 624:   }
 625: }