1 7 8 package org.jdesktop.swing; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Component ; 12 import java.awt.Container ; 13 14 import javax.swing.BoxLayout ; 15 import javax.swing.JMenuBar ; 16 import javax.swing.JPanel ; 17 import javax.swing.JRootPane ; 18 import javax.swing.JToolBar ; 19 20 import org.jdesktop.swing.event.MessageSource; 21 import org.jdesktop.swing.event.ProgressSource; 22 23 39 public class JXRootPane extends JRootPane { 40 41 private JXStatusBar statusBar; 42 private JToolBar toolBar; 43 44 private JPanel contentPanel; 45 46 public JXRootPane() { 47 contentPanel = new JPanel (); 48 contentPanel.setLayout(new BoxLayout (contentPanel, BoxLayout.Y_AXIS)); 49 50 getContentPane().add(contentPanel, BorderLayout.CENTER); 51 } 52 53 58 public void addComponent(Component comp) { 59 contentPanel.add(comp); 60 registerStatusBar(comp); 61 } 62 63 66 public void removeComponent(Component comp) { 67 contentPanel.remove(comp); 68 unregisterStatusBar(statusBar, comp); 69 } 70 71 75 public Component [] getContentComponents() { 76 return contentPanel.getComponents(); 77 } 78 79 private void registerStatusBar(Component 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 ) { 92 Component [] comps = ((Container )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 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 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 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 [] comps = contentPanel.getComponents(); 148 for (int i = 0; i < comps.length; i++) { 149 unregisterStatusBar(oldStatusBar, comps[i]); 151 152 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 174 public void setToolBar(JToolBar toolBar) { 175 JToolBar 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 getToolBar() { 190 return toolBar; 191 } 192 193 202 public void setJMenuBar(JMenuBar menuBar) { 203 JMenuBar 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 |