KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXRootPane


1 /*
2  * $Id: JXRootPane.java,v 1.2 2004/07/28 21:21:14 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Container JavaDoc;
13
14 import javax.swing.BoxLayout JavaDoc;
15 import javax.swing.JMenuBar JavaDoc;
16 import javax.swing.JPanel JavaDoc;
17 import javax.swing.JRootPane JavaDoc;
18 import javax.swing.JToolBar JavaDoc;
19
20 import org.jdesktop.swing.event.MessageSource;
21 import org.jdesktop.swing.event.ProgressSource;
22
23 /**
24  * Extends the JRootPane by supporting specific placements for a toolbar and
25  * a status bar. If a status bar exists, then toolbars, menus and
26  * any MessageSource components will be registered with the status bar.
27  * <p>
28  * Components should be added using the <code>addComponent</code> method.
29  * This method will walk the containment hierarchy of the added component
30  * and will register all <code>MessageSource</code> or
31  * <code>ProgressSource</code> components.
32  *
33  * @see JXStatusBar
34  * @see org.jdesktop.swing.event.MessageEvent
35  * @see org.jdesktop.swing.event.MessageSource
36  * @see org.jdesktop.swing.event.ProgressSource
37  * @author Mark Davidson
38  */

39 public class JXRootPane extends JRootPane JavaDoc {
40
41     private JXStatusBar statusBar;
42     private JToolBar JavaDoc toolBar;
43
44     private JPanel JavaDoc contentPanel;
45
46     public JXRootPane() {
47     contentPanel = new JPanel JavaDoc();
48     contentPanel.setLayout(new BoxLayout JavaDoc(contentPanel, BoxLayout.Y_AXIS));
49
50     getContentPane().add(contentPanel, BorderLayout.CENTER);
51     }
52
53     /**
54      * Adds a component to the root pane.
55      * If this component and/or it's children is a <code>MessageSource</code>
56      * then it will be registered with the status bar.
57      */

58     public void addComponent(Component JavaDoc comp) {
59     contentPanel.add(comp);
60     registerStatusBar(comp);
61     }
62
63     /**
64      * Removes a component from the center panel.
65      */

66     public void removeComponent(Component JavaDoc comp) {
67     contentPanel.remove(comp);
68     unregisterStatusBar(statusBar, comp);
69     }
70
71     /**
72      * Return an array of components that were added to the content panel
73      * with addComponent.
74      */

75     public Component JavaDoc[] getContentComponents() {
76     return contentPanel.getComponents();
77     }
78
79     private void registerStatusBar(Component JavaDoc comp) {
80     if (statusBar == null || comp == null) {
81         return;
82     }
83     if (comp instanceof MessageSource) {
84         MessageSource source = (MessageSource)comp;
85         source.addMessageListener(statusBar);
86     }
87     if (comp instanceof ProgressSource) {
88         ProgressSource source = (ProgressSource)comp;
89         source.addProgressListener(statusBar);
90     }
91     if (comp instanceof Container JavaDoc) {
92         Component JavaDoc[] comps = ((Container JavaDoc)comp).getComponents();
93         for (int i = 0; i < comps.length; i++) {
94         registerStatusBar(comps[i]);
95         }
96     }
97     }
98
99     private void unregisterStatusBar(JXStatusBar statusBar, Component JavaDoc 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 JavaDoc) {
112         Component JavaDoc[] comps = ((Container JavaDoc)comp).getComponents();
113         for (int i = 0; i < comps.length; i++) {
114         unregisterStatusBar(statusBar, comps[i]);
115         }
116     }
117     }
118
119     /**
120      * Set the status bar for this root pane. Any components held by this
121      * root pane will be registered. If this is replacing an existing
122      * status bar then the existing component will be unregistered from
123      * the old status bar.
124      *
125      * @param statusBar the status bar to use
126      */

127     public void setStatusBar(JXStatusBar statusBar) {
128     JXStatusBar oldStatusBar = this.statusBar;
129     this.statusBar = statusBar;
130
131     if (statusBar != null) {
132         if (handler == null) {
133         // Create the new mouse handler and register the toolbar
134
// and menu components.
135
handler = new MouseMessagingHandler(this, statusBar);
136         if (toolBar != null) {
137             handler.registerListeners(toolBar.getComponents());
138         }
139         if (menuBar != null) {
140             handler.registerListeners(menuBar.getSubElements());
141         }
142         } else {
143         handler.setMessageListener(statusBar);
144         }
145     }
146
147     Component JavaDoc[] comps = contentPanel.getComponents();
148     for (int i = 0; i < comps.length; i++) {
149         // Unregister the old status bar.
150
unregisterStatusBar(oldStatusBar, comps[i]);
151
152         // register the new status bar.
153
registerStatusBar(comps[i]);
154     }
155     getContentPane().add(BorderLayout.SOUTH, statusBar);
156     }
157
158     public JXStatusBar getStatusBar() {
159     return statusBar;
160     }
161
162     private MouseMessagingHandler handler;
163
164
165     /**
166      * Set the toolbar bar for this root pane.
167      * If the status bar exists, then all components will be registered
168      * with a <code>MouseMessagingHandler</code> so that mouse over
169      * messages will be sent to the status bar.
170      *
171      * @param toolBar the toolbar to register
172      * @see MouseMessagingHandler
173      */

174     public void setToolBar(JToolBar JavaDoc toolBar) {
175     JToolBar JavaDoc oldToolBar = this.toolBar;
176     this.toolBar = toolBar;
177
178     if (handler != null && oldToolBar != null) {
179         handler.unregisterListeners(oldToolBar.getComponents());
180     }
181
182     if (handler != null && toolBar != null) {
183         handler.registerListeners(toolBar.getComponents());
184     }
185
186     getContentPane().add(BorderLayout.NORTH, toolBar);
187     }
188
189     public JToolBar JavaDoc getToolBar() {
190     return toolBar;
191     }
192
193     /**
194      * Set the menu bar for this root pane.
195      * If the status bar exists, then all components will be registered
196      * with a <code>MouseMessagingHandler</code> so that mouse over
197      * messages will be sent to the status bar.
198      *
199      * @param menuBar the menu bar to register
200      * @see MouseMessagingHandler
201      */

202     public void setJMenuBar(JMenuBar JavaDoc menuBar) {
203     JMenuBar JavaDoc oldMenuBar = this.menuBar;
204
205     super.setJMenuBar(menuBar);
206
207     if (handler != null && oldMenuBar != null) {
208         handler.unregisterListeners(oldMenuBar.getSubElements());
209     }
210
211     if (handler != null && menuBar != null) {
212         handler.registerListeners(menuBar.getSubElements());
213     }
214     }
215
216 }
217
Popular Tags