JXRootPane.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXRootPane.java,v 1.4 2005/10/10 18:01:40 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.awt.BorderLayout;
  23. import java.awt.Component;
  24. import java.awt.Container;
  25. import javax.swing.BoxLayout;
  26. import javax.swing.JMenuBar;
  27. import javax.swing.JPanel;
  28. import javax.swing.JRootPane;
  29. import javax.swing.JToolBar;
  30. import org.jdesktop.swingx.event.MessageSource;
  31. import org.jdesktop.swingx.event.ProgressSource;
  32. /**
  33.  * Extends the JRootPane by supporting specific placements for a toolbar and a
  34.  * status bar. If a status bar exists, then toolbars, menus and any
  35.  * MessageSource components will be registered with the status bar.
  36.  * <p>
  37.  * Components should be added using the <code>addComponent</code> method. This
  38.  * method will walk the containment hierarchy of the added component and will
  39.  * register all <code>MessageSource</code> or <code>ProgressSource</code>
  40.  * components.
  41.  * 
  42.  * @see JXStatusBar
  43.  * @see org.jdesktop.swingx.event.MessageEvent
  44.  * @see org.jdesktop.swingx.event.MessageSource
  45.  * @see org.jdesktop.swingx.event.ProgressSource
  46.  * @author Mark Davidson
  47.  */
  48. public class JXRootPane extends JRootPane {
  49.     private JXStatusBar statusBar;
  50.     private JToolBar toolBar;
  51.     private JPanel contentPanel;
  52.     public JXRootPane() {
  53.         contentPanel = new JPanel();
  54.         contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
  55.         getContentPane().add(contentPanel, BorderLayout.CENTER);
  56.     }
  57.     /**
  58.      * Adds a component to the root pane. If this component and/or it's children
  59.      * is a <code>MessageSource</code> then it will be registered with the
  60.      * status bar.
  61.      */
  62.     public void addComponent(Component comp) {
  63.         contentPanel.add(comp);
  64.         registerStatusBar(comp);
  65.     }
  66.     /**
  67.      * Removes a component from the center panel.
  68.      */
  69.     public void removeComponent(Component comp) {
  70.         contentPanel.remove(comp);
  71.         unregisterStatusBar(statusBar, comp);
  72.     }
  73.     /**
  74.      * Return an array of components that were added to the content panel with
  75.      * addComponent.
  76.      */
  77.     public Component[] getContentComponents() {
  78.         return contentPanel.getComponents();
  79.     }
  80.     private void registerStatusBar(Component comp) {
  81.         if (statusBar == null || comp == null) {
  82.             return;
  83.         }
  84.         if (comp instanceof MessageSource) {
  85.             MessageSource source = (MessageSource) comp;
  86.             source.addMessageListener(statusBar);
  87.         }
  88.         if (comp instanceof ProgressSource) {
  89.             ProgressSource source = (ProgressSource) comp;
  90.             source.addProgressListener(statusBar);
  91.         }
  92.         if (comp instanceof Container) {
  93.             Component[] comps = ((Container) comp).getComponents();
  94.             for (int i = 0; i < comps.length; i++) {
  95.                 registerStatusBar(comps[i]);
  96.             }
  97.         }
  98.     }
  99.     private void unregisterStatusBar(JXStatusBar statusBar, Component comp) {
  100.         if (statusBar == null || comp == null) {
  101.             return;
  102.         }
  103.         if (comp instanceof MessageSource) {
  104.             MessageSource source = (MessageSource) comp;
  105.             source.removeMessageListener(statusBar);
  106.         }
  107.         if (comp instanceof ProgressSource) {
  108.             ProgressSource source = (ProgressSource) comp;
  109.             source.removeProgressListener(statusBar);
  110.         }
  111.         if (comp instanceof Container) {
  112.             Component[] comps = ((Container) comp).getComponents();
  113.             for (int i = 0; i < comps.length; i++) {
  114.                 unregisterStatusBar(statusBar, comps[i]);
  115.             }
  116.         }
  117.     }
  118.     /**
  119.      * Set the status bar for this root pane. Any components held by this root
  120.      * pane will be registered. If this is replacing an existing status bar then
  121.      * the existing component will be unregistered from the old status bar.
  122.      * 
  123.      * @param statusBar
  124.      *            the status bar to use
  125.      */
  126.     public void setStatusBar(JXStatusBar statusBar) {
  127.         JXStatusBar oldStatusBar = this.statusBar;
  128.         this.statusBar = statusBar;
  129.         if (statusBar != null) {
  130.             if (handler == null) {
  131.                 // Create the new mouse handler and register the toolbar
  132.                 // and menu components.
  133.                 handler = new MouseMessagingHandler(this, statusBar);
  134.                 if (toolBar != null) {
  135.                     handler.registerListeners(toolBar.getComponents());
  136.                 }
  137.                 if (menuBar != null) {
  138.                     handler.registerListeners(menuBar.getSubElements());
  139.                 }
  140.             } else {
  141.                 handler.setMessageListener(statusBar);
  142.             }
  143.         }
  144.         Component[] comps = contentPanel.getComponents();
  145.         for (int i = 0; i < comps.length; i++) {
  146.             // Unregister the old status bar.
  147.             unregisterStatusBar(oldStatusBar, comps[i]);
  148.             // register the new status bar.
  149.             registerStatusBar(comps[i]);
  150.         }
  151.         if (oldStatusBar != null) {
  152.             getContentPane().remove(oldStatusBar);
  153.         }
  154.         if (statusBar != null) {
  155.             getContentPane().add(BorderLayout.SOUTH, statusBar);
  156.         }
  157.     }
  158.     public JXStatusBar getStatusBar() {
  159.         return statusBar;
  160.     }
  161.     private MouseMessagingHandler handler;
  162.     /**
  163.      * Set the toolbar bar for this root pane. If the status bar exists, then
  164.      * all components will be registered with a
  165.      * <code>MouseMessagingHandler</code> so that mouse over messages will be
  166.      * sent to the status bar.
  167.      * 
  168.      * @param toolBar
  169.      *            the toolbar to register
  170.      * @see MouseMessagingHandler
  171.      */
  172.     public void setToolBar(JToolBar toolBar) {
  173.         JToolBar oldToolBar = this.toolBar;
  174.         this.toolBar = toolBar;
  175.         if (handler != null && oldToolBar != null) {
  176.             handler.unregisterListeners(oldToolBar.getComponents());
  177.         }
  178.         if (handler != null && toolBar != null) {
  179.             handler.registerListeners(toolBar.getComponents());
  180.         }
  181.         getContentPane().add(BorderLayout.NORTH, toolBar);
  182.     }
  183.     public JToolBar getToolBar() {
  184.         return toolBar;
  185.     }
  186.     /**
  187.      * Set the menu bar for this root pane. If the status bar exists, then all
  188.      * components will be registered with a <code>MouseMessagingHandler</code>
  189.      * so that mouse over messages will be sent to the status bar.
  190.      * 
  191.      * @param menuBar
  192.      *            the menu bar to register
  193.      * @see MouseMessagingHandler
  194.      */
  195.     public void setJMenuBar(JMenuBar menuBar) {
  196.         JMenuBar oldMenuBar = this.menuBar;
  197.         super.setJMenuBar(menuBar);
  198.         if (handler != null && oldMenuBar != null) {
  199.             handler.unregisterListeners(oldMenuBar.getSubElements());
  200.         }
  201.         if (handler != null && menuBar != null) {
  202.             handler.registerListeners(menuBar.getSubElements());
  203.         }
  204.     }
  205. }