Frames | No Frames |
1: /* SwingMenuBarPeer.java -- A Swing based peer for AWT menu bars 2: Copyright (C) 2006 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: package gnu.java.awt.peer.swing; 39: 40: import java.awt.Container; 41: import java.awt.Font; 42: import java.awt.Graphics; 43: import java.awt.Menu; 44: import java.awt.MenuBar; 45: import java.awt.Point; 46: import java.awt.event.MouseEvent; 47: import java.awt.peer.MenuBarPeer; 48: 49: import javax.swing.JMenuBar; 50: 51: /** 52: * A Swing based peer for the AWT menu bar. This is a little bit different from 53: * the other peers, since the AWT MenuBar is not derived from the AWT 54: * component. 55: * 56: * @author Roman Kennke (kennke@aicas.com) 57: */ 58: public class SwingMenuBarPeer 59: implements MenuBarPeer 60: { 61: 62: /** 63: * The AWT menu bar. 64: */ 65: MenuBar awtMenuBar; 66: 67: /** 68: * The Swing menu bar. 69: */ 70: SwingMenuBar menuBar; 71: 72: /** 73: * The peer of the frame that contains this menu bar. 74: */ 75: SwingFramePeer framePeer; 76: 77: /** 78: * A specialized JMenuBar that can be used as 'backend' for AWT MenuBars. 79: * 80: * @author Roman Kennke (kennke@aicas.com) 81: */ 82: private class SwingMenuBar 83: extends JMenuBar 84: { 85: /** 86: * Overridden in order to provide a parent frame for this menu bar. The 87: * menu bar still is not inside the component hierarchy, we are faking 88: * here. 89: */ 90: public Container getParent() 91: { 92: Container result = null; 93: if (framePeer != null) 94: result = (Container) framePeer.awtComponent; 95: return result; 96: } 97: 98: /** 99: * Unconditionally returns <code>true</code>, since we assume that when the 100: * menubar has a peer, it must be showing. 101: * 102: * @return <code>true</code> 103: */ 104: public boolean isShowing() 105: { 106: // FIXME: This might be wrong. Maybe find a better way to do that. 107: return true; 108: } 109: 110: /** 111: * Handles mouse events by forwarding it to 112: * <code>processMouseEvent()</code>. 113: * 114: * @param ev the mouse event 115: */ 116: public void handleMouseEvent(MouseEvent ev) 117: { 118: ev.setSource(this); 119: processMouseEvent(ev); 120: } 121: 122: /** 123: * Determines the menubar's screen location by asking the SwingFramePeer 124: * for it. 125: * 126: * @return the screen location of the menu bar 127: */ 128: public Point getLocationOnScreen() 129: { 130: return framePeer.getMenuLocationOnScreen(); 131: } 132: } 133: 134: /** 135: * Creates a new <code>SwingMenuBarPeer</code> instance. 136: * 137: * @param awtMenuBar the AWT menu bar 138: */ 139: public SwingMenuBarPeer(MenuBar awtMenuBar) 140: { 141: this.awtMenuBar = awtMenuBar; 142: menuBar = new SwingMenuBar(); 143: menuBar.setDoubleBuffered(false); 144: // Add all the menus that are already in the MenuBar. 145: for (int i = 0; i < awtMenuBar.getMenuCount(); i++) 146: { 147: Menu menu = awtMenuBar.getMenu(i); 148: menu.addNotify(); 149: addMenu(awtMenuBar.getMenu(i)); 150: } 151: } 152: 153: /** 154: * Sets the <code>SwingFramePeer</code> of the frame that holds this menu. 155: * 156: * @param peer the <code>SwingFramePeer</code> to set 157: */ 158: public void setFramePeer(SwingFramePeer peer) 159: { 160: framePeer = peer; 161: } 162: 163: /** 164: * Adds a menu to the menu bar. 165: * 166: * @param m the menu to add 167: */ 168: public void addMenu(Menu m) 169: { 170: SwingMenuPeer menuPeer = (SwingMenuPeer) m.getPeer(); 171: menuBar.add(menuPeer.menu); 172: } 173: 174: /** 175: * Adds a help menu to the menu bar. 176: * 177: * @param menu the menu to add 178: */ 179: public void addHelpMenu(Menu menu) 180: { 181: // FIXME: We should manage the help menu differently, so that it always 182: // appears at the rightmost position. 183: SwingMenuPeer menuPeer = (SwingMenuPeer) menu.getPeer(); 184: menuBar.add(menuPeer.menu); 185: } 186: 187: /** 188: * Removes the menu with the specified index. 189: * 190: * @param index the index of the menu to remove 191: */ 192: public void delMenu(int index) 193: { 194: menuBar.remove(index); 195: } 196: 197: /** 198: * Disposes this peer. This releases any reference to the AWT and Swing 199: * components. 200: */ 201: public void dispose() 202: { 203: menuBar = null; 204: awtMenuBar = null; 205: } 206: 207: /** 208: * Sets a font for the menu bar. 209: * 210: * @param font the font to set 211: */ 212: public void setFont(Font font) 213: { 214: menuBar.setFont(font); 215: } 216: 217: /** 218: * Sets the width of the menu bar. This is called from the top level 219: * component peers to adjust the width of the menubar when their sizes 220: * change. 221: * 222: * @param w the width to set 223: */ 224: public void setWidth(int w) 225: { 226: menuBar.setSize(w, menuBar.getPreferredSize().height); 227: menuBar.doLayout(); 228: } 229: 230: /** 231: * Paints the menu bar. 232: * 233: * @param g the graphics context to use for painting 234: */ 235: public void peerPaint(Graphics g) 236: { 237: menuBar.paint(g); 238: } 239: 240: /** 241: * Determines the height of the menubar. 242: * 243: * @return the height of the menu bar 244: */ 245: public int getHeight() 246: { 247: return menuBar.getPreferredSize().height; 248: } 249: 250: /** 251: * Handles mouse events. 252: * 253: * @param ev the mouse event 254: */ 255: public void handleMouseEvent(MouseEvent ev) 256: { 257: Point point = ev.getPoint(); 258: for (int i = 0; i < awtMenuBar.getMenuCount(); i++) 259: { 260: Menu menu = awtMenuBar.getMenu(i); 261: SwingMenuPeer peer = (SwingMenuPeer) menu.getPeer(); 262: int x1 = peer.getX(); 263: int x2 = x1 + peer.getWidth(); 264: if (point.x >= x1 && point.x <= x2) 265: { 266: ev.translatePoint(peer.getX(), peer.getY()); 267: peer.handleMouseEvent(ev); 268: break; 269: } 270: } 271: } 272: 273: /** 274: * Handles mouse motion events. 275: * 276: * @param ev the mouse motion event 277: */ 278: public void handleMouseMotionEvent(MouseEvent ev) 279: { 280: Point point = ev.getPoint(); 281: for (int i = 0; i < awtMenuBar.getMenuCount(); i++) 282: { 283: Menu menu = awtMenuBar.getMenu(i); 284: SwingMenuPeer peer = (SwingMenuPeer) menu.getPeer(); 285: int x1 = peer.getX(); 286: int x2 = x1 + peer.getWidth(); 287: if (point.x >= x1 && point.x <= x2) 288: { 289: ev.translatePoint(peer.getX(), peer.getY()); 290: peer.handleMouseMotionEvent(ev); 291: break; 292: } 293: } 294: } 295: }