1 /*
   2  * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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 package sun.awt.X11;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 
  30 import java.util.Vector;
  31 import sun.util.logging.PlatformLogger;
  32 import sun.awt.AWTAccessor;
  33 
  34 public class XMenuPeer extends XMenuItemPeer implements MenuPeer {
  35 
  36     /************************************************
  37      *
  38      * Data members
  39      *
  40      ************************************************/
  41     private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XMenuPeer");
  42 
  43     /**
  44      * Window that correspond to this menu
  45      */
  46     XMenuWindow menuWindow;
  47 
  48     /************************************************
  49      *
  50      * Construction
  51      *
  52      ************************************************/
  53     XMenuPeer(Menu target) {
  54         super(target);
  55     }
  56 
  57     /**
  58      * This function is called when menu is bound
  59      * to its container window. Creates submenu window
  60      * that fills its items vector while construction
  61      */
  62     void setContainer(XBaseMenuWindow container) {
  63         super.setContainer(container);
  64         menuWindow = new XMenuWindow(this);
  65     }
  66 
  67 
  68     /************************************************
  69      *
  70      * Implementaion of interface methods
  71      *
  72      ************************************************/
  73 
  74     /*
  75      * From MenuComponentPeer
  76      */
  77 
  78     /**
  79      * Disposes menu window if needed
  80      */
  81     public void dispose() {
  82         if (menuWindow != null) {
  83             menuWindow.dispose();
  84         }
  85         super.dispose();
  86     }
  87 
  88     /**
  89      * Resets text metrics for this item, for its menu window
  90      * and for all descendant menu windows
  91      */
  92     public void setFont(Font font) {
  93         //TODO:We can decrease count of repaints here
  94         //and get rid of recursion
  95         resetTextMetrics();
  96 
  97         XMenuWindow menuWindow = getMenuWindow();
  98         if (menuWindow != null) {
  99             menuWindow.setItemsFont(font);
 100         }
 101 
 102         repaintIfShowing();
 103     }
 104 
 105     /*
 106      * From MenuPeer
 107      */
 108     /**
 109      * addSeparator routines are not used
 110      * in peers. Shared code invokes addItem("-")
 111      * for adding separators
 112      */
 113     public void addSeparator() {
 114         if (log.isLoggable(PlatformLogger.FINER)) log.finer("addSeparator is not implemented");
 115     }
 116 
 117     public void addItem(MenuItem item) {
 118         XMenuWindow menuWindow = getMenuWindow();
 119         if (menuWindow != null) {
 120             menuWindow.addItem(item);
 121         } else {
 122             if (log.isLoggable(PlatformLogger.FINE)) {
 123                 log.fine("Attempt to use XMenuWindowPeer without window");
 124             }
 125         }
 126     }
 127 
 128     public void delItem(int index) {
 129         XMenuWindow menuWindow = getMenuWindow();
 130         if (menuWindow != null) {
 131             menuWindow.delItem(index);
 132         } else {
 133             if (log.isLoggable(PlatformLogger.FINE)) {
 134                 log.fine("Attempt to use XMenuWindowPeer without window");
 135             }
 136         }
 137     }
 138 
 139     /************************************************
 140      *
 141      * Access to target's fields
 142      *
 143      ************************************************/
 144     Vector getTargetItems() {
 145         return AWTAccessor.getMenuAccessor().getItems((Menu)getTarget());
 146     }
 147 
 148     /************************************************
 149      *
 150      * Overriden behaviour
 151      *
 152      ************************************************/
 153     boolean isSeparator() {
 154         return false;
 155     }
 156 
 157     //Fix for 6180416: Shortcut keys are displayed against Menus on XToolkit
 158     //Menu should always return null as shortcutText
 159     String getShortcutText() {
 160         return null;
 161     }
 162 
 163     /************************************************
 164      *
 165      * Utility functions
 166      *
 167      ************************************************/
 168 
 169     /**
 170      * Returns menu window of this menu or null
 171      * it this menu has no container and so its
 172      * window can't be created.
 173      */
 174     XMenuWindow getMenuWindow() {
 175         return menuWindow;
 176     }
 177 
 178 }