src/macosx/classes/com/apple/laf/ScreenMenuBar.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 



  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.lang.reflect.*;
  31 import java.security.*;
  32 import java.util.*;
  33 
  34 import javax.swing.*;
  35 
  36 @SuppressWarnings("serial") // JDK implementation class
  37 public class ScreenMenuBar extends MenuBar implements ContainerListener, ScreenMenuPropertyHandler, ComponentListener {
  38     static boolean sJMenuBarHasHelpMenus = false; //$ could check by calling getHelpMenu in a try block
  39 
  40     JMenuBar fSwingBar;
  41     Hashtable<JMenu, ScreenMenu> fSubmenus;
  42 
  43     ScreenMenuPropertyListener fPropertyListener;
  44     ScreenMenuPropertyListener fAccessibleListener;
  45 
  46     public ScreenMenuBar(final JMenuBar swingBar) {
  47         fSwingBar = swingBar;


 226 
 227         return sm;
 228     }
 229 
 230     /**
 231      * Remove the screen menu associated with the specifiec menu.  This
 232      * also removes any associated component listener on the screen menu
 233      * and removes the key/value (menu/screen menu) from the fSubmenus cache.
 234      *
 235      * @param menu The swing menu we want to remove the screen menu for.
 236      */
 237     private void removeSubmenu(final JMenu menu) {
 238         final ScreenMenu screenMenu = fSubmenus.get(menu);
 239         if (screenMenu == null) return;
 240 
 241             menu.removeComponentListener(this);
 242             remove(screenMenu);
 243             fSubmenus.remove(menu);
 244     }
 245 
 246     private static Field[] stolenFields = null;
 247 
 248     static {
 249         stolenFields = AccessController.doPrivileged(new PrivilegedAction<Field[]>() {
 250             public Field[] run() {
 251                 try {
 252                     final Field[] localFields = new Field[2];
 253                     localFields[0] = MenuBar.class.getDeclaredField("menus");
 254                     localFields[1] = MenuComponent.class.getDeclaredField("parent");
 255                     AccessibleObject.setAccessible(localFields, true);
 256                     return localFields;
 257                 } catch (final NoSuchFieldException nsf) {
 258                     // If this happens, Sun changed the definition of MenuBar and MenuComponent!
 259                     nsf.printStackTrace(System.err);
 260                     return null;
 261                 }
 262             }
 263         });
 264     };
 265 
 266     public Menu add(final Menu m, final int index) {
 267         synchronized (getTreeLock()) {
 268             if (m.getParent() != null) {
 269                 m.getParent().remove(m);
 270             }
 271 
 272             // Use nasty reflection to get at the menus array and parent fields.
 273             try {
 274                 if (stolenFields == null) return m;
 275 
 276                 @SuppressWarnings("unchecked")
 277                 final Vector<Menu> menus = (Vector<Menu>)stolenFields[0].get(this);
 278                     menus.insertElementAt(m, index);

 279 
 280                     stolenFields[1].set(m, this);
 281 
 282                     final sun.lwawt.macosx.CMenuBar peer = (sun.lwawt.macosx.CMenuBar)getPeer();
 283                 if (peer == null) return m;
 284 
 285                         peer.setNextInsertionIndex(index);
 286                         if (m.getPeer() == null) {
 287                             m.addNotify();
 288                         }
 289 
 290                         peer.setNextInsertionIndex(-1);
 291             } catch (final IllegalAccessException iae) {
 292                 iae.printStackTrace(System.err);
 293             }
 294 
 295             return m;
 296         }
 297     }
 298 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import sun.awt.AWTAccessor;
  29 import sun.lwawt.macosx.CMenuBar;
  30 
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import java.lang.reflect.*;
  34 import java.security.*;
  35 import java.util.*;
  36 
  37 import javax.swing.*;
  38 
  39 @SuppressWarnings("serial") // JDK implementation class
  40 public class ScreenMenuBar extends MenuBar implements ContainerListener, ScreenMenuPropertyHandler, ComponentListener {
  41     static boolean sJMenuBarHasHelpMenus = false; //$ could check by calling getHelpMenu in a try block
  42 
  43     JMenuBar fSwingBar;
  44     Hashtable<JMenu, ScreenMenu> fSubmenus;
  45 
  46     ScreenMenuPropertyListener fPropertyListener;
  47     ScreenMenuPropertyListener fAccessibleListener;
  48 
  49     public ScreenMenuBar(final JMenuBar swingBar) {
  50         fSwingBar = swingBar;


 229 
 230         return sm;
 231     }
 232 
 233     /**
 234      * Remove the screen menu associated with the specifiec menu.  This
 235      * also removes any associated component listener on the screen menu
 236      * and removes the key/value (menu/screen menu) from the fSubmenus cache.
 237      *
 238      * @param menu The swing menu we want to remove the screen menu for.
 239      */
 240     private void removeSubmenu(final JMenu menu) {
 241         final ScreenMenu screenMenu = fSubmenus.get(menu);
 242         if (screenMenu == null) return;
 243 
 244             menu.removeComponentListener(this);
 245             remove(screenMenu);
 246             fSubmenus.remove(menu);
 247     }
 248 




















 249     public Menu add(final Menu m, final int index) {
 250         synchronized (getTreeLock()) {
 251             if (m.getParent() != null) {
 252                 m.getParent().remove(m);
 253             }
 254 
 255             final Vector<Menu> menus = AWTAccessor.getMenuBarAccessor().getMenus(this);





 256             menus.insertElementAt(m, index);
 257             AWTAccessor.getMenuComponentAccessor().setParent(m, this);
 258 
 259             final CMenuBar peer = (CMenuBar)getPeer();


 260             if (peer == null) return m;
 261 
 262             peer.setNextInsertionIndex(index);
 263             if (m.getPeer() == null) {
 264                 m.addNotify();
 265             }
 266 
 267             peer.setNextInsertionIndex(-1);




 268             return m;
 269         }
 270     }
 271 }