Source for gnu.java.awt.peer.swing.SwingMenuPeer

   1: /* SwingMenuPeer.java -- A Swing based peer for AWT menus
   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.Font;
  41: import java.awt.Menu;
  42: import java.awt.MenuItem;
  43: import java.awt.Point;
  44: import java.awt.event.MouseEvent;
  45: import java.awt.peer.MenuPeer;
  46: 
  47: import javax.swing.JMenu;
  48: 
  49: /**
  50:  * A Swing based peer for the AWT menu.
  51:  *
  52:  * @author Roman Kennke (kennke@aicas.com)
  53:  */
  54: public class SwingMenuPeer
  55:   implements MenuPeer
  56: {
  57: 
  58:   /**
  59:    * The AWT menu.
  60:    */
  61:   Menu awtMenu;
  62: 
  63:   /**
  64:    * The Swing menu.
  65:    */
  66:   SwingMenu menu;
  67: 
  68:   /**
  69:    * A specialized JMenu that can be used as 'backend' for an AWT menu.
  70:    *
  71:    * @author Roman Kennke (kennke@aicas.com)
  72:    */
  73:   private class SwingMenu
  74:     extends JMenu
  75:   {
  76: 
  77:     /**
  78:      * Unconditionally returns <code>true</code>, since we assume that when the
  79:      * menu has a peer, it must be showing.
  80:      *
  81:      * @return <code>true</code>
  82:      */
  83:     public boolean isShowing()
  84:     {
  85:       // FIXME: This might be wrong. Maybe find a better way to do that.
  86:       return true;
  87:     }
  88: 
  89:     /**
  90:      * Overridden so that we can provide a location even without a real peer
  91:      * attached.
  92:      *
  93:      * @return the screen location of this menu
  94:      */
  95:     public Point getLocationOnScreen()
  96:     {
  97:       Point parentLoc = getParent().getLocationOnScreen();
  98:       parentLoc.x += getX();
  99:       parentLoc.y += getY();
 100:       return parentLoc;
 101:     }
 102: 
 103:     /**
 104:      * Handles mouse events by forwarding them to
 105:      * <code>processMouseEvent()</code>.
 106:      *
 107:      * @param ev the mouse event
 108:      */
 109:     public void handleMouseEvent(MouseEvent ev)
 110:     {
 111:       ev.setSource(this);
 112:       processMouseEvent(ev);
 113:     }
 114: 
 115:     /**
 116:      * Handles mouse events by forwarding them to
 117:      * <code>processMouseMotionEvent()</code>.
 118:      *
 119:      * @param ev the mouse event
 120:      */
 121:     public void handleMouseMotionEvent(MouseEvent ev)
 122:     {
 123:       ev.setSource(this);
 124:       processMouseMotionEvent(ev);
 125:     }
 126:   }
 127: 
 128:   /**
 129:    * Creates a new <code>SwingMenuPeer</code> instance.
 130:    *
 131:    * @param awtMenu the AWT menu
 132:    */
 133:   public SwingMenuPeer(Menu awtMenu)
 134:   {
 135:     this.awtMenu = awtMenu;
 136:     menu = new SwingMenu();
 137:     menu.setDoubleBuffered(false);
 138:     menu.setText(awtMenu.getLabel());
 139:     for (int i = 0; i < awtMenu.getItemCount(); i++)
 140:       {
 141:         MenuItem item = awtMenu.getItem(i);
 142:         item.addNotify();
 143:         SwingMenuItemPeer peer = (SwingMenuItemPeer) item.getPeer();
 144:         menu.add(peer.menuItem);
 145:       }
 146:   }
 147: 
 148:   /**
 149:    * Adds a menu item to this menu.
 150:    *
 151:    * @param item the menu item to add
 152:    */
 153:   public void addItem(MenuItem item)
 154:   {
 155:     SwingMenuItemPeer menuItemPeer = (SwingMenuItemPeer) item.getPeer();
 156:     menu.add(menuItemPeer.menuItem);
 157:   }
 158: 
 159:   /**
 160:    * Adds a separator to the menu.
 161:    */
 162:   public void addSeparator()
 163:   {
 164:     menu.addSeparator();
 165:   }
 166: 
 167:   /**
 168:    * Removes a menu item from the menu.
 169:    *
 170:    * @param index the index of the menu item to remove
 171:    */
 172:   public void delItem(int index)
 173:   {
 174:     menu.remove(index);
 175:   }
 176: 
 177:   /**
 178:    * Disables the menu.
 179:    */
 180:   public void disable()
 181:   {
 182:     menu.setEnabled(false);
 183:   }
 184: 
 185:   /**
 186:    * Enables the menu.
 187:    */
 188:   public void enable()
 189:   {
 190:     menu.setEnabled(true);
 191:   }
 192: 
 193:   /**
 194:    * Sets the enabled state of the menu to <code>enabled</code>.
 195:    *
 196:    * @param enabled if the menu should be enabled or not
 197:    */
 198:   public void setEnabled(boolean enabled)
 199:   {
 200:     menu.setEnabled(enabled);
 201:   }
 202: 
 203:   /**
 204:    * Sets the label of the menu.
 205:    *
 206:    * @param text the label to set
 207:    */
 208:   public void setLabel(String text)
 209:   {
 210:     menu.setText(text);
 211:   }
 212: 
 213:   /**
 214:    * Releases any reference to the AWT and Swing menu instances.
 215:    */
 216:   public void dispose()
 217:   {
 218:     menu = null;
 219:     awtMenu = null;
 220:   }
 221: 
 222:   /**
 223:    * Sets the font for the menu.
 224:    *
 225:    * @param font the font to set
 226:    */
 227:   public void setFont(Font font)
 228:   {
 229:     menu.setFont(font);
 230:   }
 231: 
 232:   /**
 233:    * Handles mouse events by forwarding them to the Swing menu.
 234:    *
 235:    * @param ev the mouse event
 236:    */
 237:   public void handleMouseEvent(MouseEvent ev)
 238:   {
 239:     menu.handleMouseEvent(ev);
 240:   }
 241: 
 242:   /**
 243:    * Handles mouse motion events by forwarding them to the Swing menu.
 244:    *
 245:    * @param ev the mouse event
 246:    */
 247:   public void handleMouseMotionEvent(MouseEvent ev)
 248:   {
 249:     menu.handleMouseMotionEvent(ev);
 250:   }
 251: 
 252:   /**
 253:    * Returns the X coordinate of the upper left corner of the menu. This is
 254:    * used internally by the SwingMenuBarPeer.
 255:    *
 256:    * @return the X coordinate of the upper left corner of the menu
 257:    */
 258:   int getX()
 259:   {
 260:     return menu.getX();
 261:   }
 262: 
 263:   /**
 264:    * Returns the width of the menu. This is used internally by the
 265:    * SwingMenuBarPeer.
 266:    *
 267:    * @return the X coordinate of the upper left corner of the menu
 268:    */
 269:   int getWidth()
 270:   {
 271:     return menu.getWidth();
 272:   }
 273: 
 274:   /**
 275:    * Returns the Y coordinate of the upper left corner of the menu. This is
 276:    * used internally by the SwingMenuBarPeer.
 277:    *
 278:    * @return the X coordinate of the upper left corner of the menu
 279:    */
 280:   public int getY()
 281:   {
 282:     return menu.getY();
 283:   }
 284: }