Frames | No Frames |
1: /* BasicMenuBarUI.java -- 2: Copyright (C) 2002, 2004, 2005 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.plaf.basic; 40: 41: import gnu.classpath.NotImplementedException; 42: 43: import java.awt.Dimension; 44: import java.awt.event.ContainerEvent; 45: import java.awt.event.ContainerListener; 46: import java.awt.event.MouseEvent; 47: import java.beans.PropertyChangeEvent; 48: import java.beans.PropertyChangeListener; 49: 50: import javax.swing.BoxLayout; 51: import javax.swing.JComponent; 52: import javax.swing.JMenu; 53: import javax.swing.JMenuBar; 54: import javax.swing.LookAndFeel; 55: import javax.swing.MenuElement; 56: import javax.swing.event.ChangeEvent; 57: import javax.swing.event.ChangeListener; 58: import javax.swing.event.MouseInputListener; 59: import javax.swing.plaf.ComponentUI; 60: import javax.swing.plaf.MenuBarUI; 61: 62: /** 63: * UI Delegate for JMenuBar. 64: */ 65: public class BasicMenuBarUI extends MenuBarUI 66: { 67: protected ChangeListener changeListener; 68: 69: /*ContainerListener that listens to the ContainerEvents fired from menu bar*/ 70: protected ContainerListener containerListener; 71: 72: /*Property change listeners that listener to PropertyChangeEvent from menu bar*/ 73: private PropertyChangeListener propertyChangeListener; 74: 75: /* menu bar for which this UI delegate is for*/ 76: protected JMenuBar menuBar; 77: 78: /* MouseListener that listens to the mouseEvents fired from menu bar*/ 79: private MouseInputListener mouseListener; 80: 81: /** 82: * Creates a new BasicMenuBarUI object. 83: */ 84: public BasicMenuBarUI() 85: { 86: changeListener = createChangeListener(); 87: containerListener = createContainerListener(); 88: propertyChangeListener = new PropertyChangeHandler(); 89: mouseListener = new MouseInputHandler(); 90: } 91: 92: /** 93: * Creates ChangeListener 94: * 95: * @return The ChangeListener 96: */ 97: protected ChangeListener createChangeListener() 98: { 99: return new ChangeHandler(); 100: } 101: 102: /** 103: * Creates ContainerListener() to listen for ContainerEvents 104: * fired by JMenuBar. 105: * 106: * @return The ContainerListener 107: */ 108: protected ContainerListener createContainerListener() 109: { 110: return new ContainerHandler(); 111: } 112: 113: /** 114: * Factory method to create a BasicMenuBarUI for the given {@link 115: * JComponent}, which should be a {@link JMenuBar}. 116: * 117: * @param x The {@link JComponent} a UI is being created for. 118: * 119: * @return A BasicMenuBarUI for the {@link JComponent}. 120: */ 121: public static ComponentUI createUI(JComponent x) 122: { 123: return new BasicMenuBarUI(); 124: } 125: 126: /** 127: * Returns maximum size for the specified menu bar 128: * 129: * @param c component for which to get maximum size 130: * 131: * @return Maximum size for the specified menu bar 132: */ 133: public Dimension getMaximumSize(JComponent c) 134: { 135: // let layout manager calculate its size 136: return null; 137: } 138: 139: /** 140: * Returns maximum allowed size of JMenuBar. 141: * 142: * @param c menuBar for which to return maximum size 143: * 144: * @return Maximum size of the give menu bar. 145: */ 146: public Dimension getMinimumSize(JComponent c) 147: { 148: // let layout manager calculate its size 149: return null; 150: } 151: 152: /** 153: * Returns preferred size of JMenuBar. 154: * 155: * @param c menuBar for which to return preferred size 156: * 157: * @return Preferred size of the give menu bar. 158: */ 159: public Dimension getPreferredSize(JComponent c) 160: { 161: // let layout manager calculate its size 162: return null; 163: } 164: 165: /** 166: * Initializes any default properties that this UI has from the defaults for 167: * the Basic look and feel. 168: */ 169: protected void installDefaults() 170: { 171: LookAndFeel.installBorder(menuBar, "MenuBar.border"); 172: LookAndFeel.installColorsAndFont(menuBar, "MenuBar.background", 173: "MenuBar.foreground", "MenuBar.font"); 174: menuBar.setOpaque(true); 175: } 176: 177: /** 178: * This method installs the keyboard actions for the JMenuBar. 179: */ 180: protected void installKeyboardActions() 181: throws NotImplementedException 182: { 183: // FIXME: implement 184: } 185: 186: /** 187: * This method installs the listeners needed for this UI to function. 188: */ 189: protected void installListeners() 190: { 191: menuBar.addContainerListener(containerListener); 192: menuBar.addPropertyChangeListener(propertyChangeListener); 193: menuBar.addMouseListener(mouseListener); 194: } 195: 196: /** 197: * Installs and initializes all fields for this UI delegate. Any properties 198: * of the UI that need to be initialized and/or set to defaults will be 199: * done now. It will also install any listeners necessary. 200: * 201: * @param c The {@link JComponent} that is having this UI installed. 202: */ 203: public void installUI(JComponent c) 204: { 205: super.installUI(c); 206: menuBar = (JMenuBar) c; 207: menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.X_AXIS)); 208: installDefaults(); 209: installListeners(); 210: installKeyboardActions(); 211: } 212: 213: /** 214: * This method uninstalls the defaults and nulls any objects created during 215: * install. 216: */ 217: protected void uninstallDefaults() 218: { 219: menuBar.setBackground(null); 220: menuBar.setBorder(null); 221: menuBar.setFont(null); 222: menuBar.setForeground(null); 223: } 224: 225: /** 226: * This method reverses the work done in installKeyboardActions. 227: */ 228: protected void uninstallKeyboardActions() 229: throws NotImplementedException 230: { 231: // FIXME: implement. 232: } 233: 234: /** 235: * Unregisters all the listeners that this UI delegate was using. 236: */ 237: protected void uninstallListeners() 238: { 239: menuBar.removeContainerListener(containerListener); 240: menuBar.removePropertyChangeListener(propertyChangeListener); 241: menuBar.removeMouseListener(mouseListener); 242: } 243: 244: /** 245: * Performs the opposite of installUI. Any properties or resources that need 246: * to be cleaned up will be done now. It will also uninstall any listeners 247: * it has. In addition, any properties of this UI will be nulled. 248: * 249: * @param c The {@link JComponent} that is having this UI uninstalled. 250: */ 251: public void uninstallUI(JComponent c) 252: { 253: uninstallDefaults(); 254: uninstallListeners(); 255: uninstallKeyboardActions(); 256: menuBar = null; 257: } 258: 259: private class ChangeHandler implements ChangeListener 260: { 261: public void stateChanged(ChangeEvent event) 262: { 263: // TODO: What should be done here, if anything? 264: } 265: } 266: 267: /** 268: * This class handles ContainerEvents fired by JMenuBar. It revalidates 269: * and repaints menu bar whenever menu is added or removed from it. 270: */ 271: private class ContainerHandler implements ContainerListener 272: { 273: /** 274: * This method is called whenever menu is added to the menu bar 275: * 276: * @param e The ContainerEvent. 277: */ 278: public void componentAdded(ContainerEvent e) 279: { 280: menuBar.revalidate(); 281: menuBar.repaint(); 282: } 283: 284: /** 285: * This method is called whenever menu is removed from the menu bar. 286: * 287: * @param e The ContainerEvent. 288: */ 289: public void componentRemoved(ContainerEvent e) 290: { 291: menuBar.revalidate(); 292: menuBar.repaint(); 293: } 294: } 295: 296: /** 297: * This class handles PropertyChangeEvents fired from the JMenuBar 298: */ 299: private class PropertyChangeHandler implements PropertyChangeListener 300: { 301: /** 302: * This method is called whenever one of the properties of the MenuBar 303: * changes. 304: * 305: * @param e The PropertyChangeEvent. 306: */ 307: public void propertyChange(PropertyChangeEvent e) 308: { 309: if (e.getPropertyName().equals("borderPainted")) 310: menuBar.repaint(); 311: if (e.getPropertyName().equals("margin")) 312: menuBar.repaint(); 313: } 314: } 315: 316: private class MouseInputHandler implements MouseInputListener 317: { 318: /** 319: * Handles mouse clicked event 320: * 321: * @param e Mouse event 322: */ 323: public void mouseClicked(MouseEvent e) 324: { 325: MenuElement[] me = menuBar.getSubElements(); 326: 327: for (int i = 0; i < me.length; i++) 328: { 329: JMenu menu = menuBar.getMenu(i); 330: if (menu != null) 331: menu.setSelected(false); 332: } 333: } 334: 335: /** 336: * Handles mouse pressed event 337: * 338: * @param e Mouse event 339: */ 340: public void mousePressed(MouseEvent e) 341: { 342: // TODO: What should be done here, if anything? 343: } 344: 345: /** 346: * Handles mouse released event 347: * 348: * @param e Mouse event 349: */ 350: public void mouseReleased(MouseEvent e) 351: { 352: // TODO: What should be done here, if anything? 353: } 354: 355: /** 356: * Handles mouse exited event 357: * 358: * @param e Mouse event 359: */ 360: public void mouseExited(MouseEvent e) 361: { 362: // TODO: What should be done here, if anything? 363: } 364: 365: /** 366: * Handles mouse dragged event 367: * 368: * @param e Mouse event 369: */ 370: public void mouseDragged(MouseEvent e) 371: { 372: // TODO: What should be done here, if anything? 373: } 374: 375: /** 376: * Handles mouse moved event 377: * 378: * @param e Mouse event 379: */ 380: public void mouseMoved(MouseEvent e) 381: { 382: // TODO: What should be done here, if anything? 383: } 384: 385: /** 386: * Handles mouse entered event 387: * 388: * @param e Mouse event 389: */ 390: public void mouseEntered(MouseEvent e) 391: { 392: // TODO: What should be done here, if anything? 393: } 394: } 395: }