1 /* 2 * Copyright (c) 2002, 2013, 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 ************************************************/ XMenuPeer(Menu target)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 @Override setContainer(XBaseMenuWindow container)63 void setContainer(XBaseMenuWindow container) { 64 super.setContainer(container); 65 menuWindow = new XMenuWindow(this); 66 } 67 68 69 /************************************************ 70 * 71 * Implementaion of interface methods 72 * 73 ************************************************/ 74 75 /* 76 * From MenuComponentPeer 77 */ 78 79 /** 80 * Disposes menu window if needed 81 */ 82 @Override dispose()83 public void dispose() { 84 if (menuWindow != null) { 85 menuWindow.dispose(); 86 } 87 super.dispose(); 88 } 89 90 /** 91 * Resets text metrics for this item, for its menu window 92 * and for all descendant menu windows 93 */ 94 @Override setFont(Font font)95 public void setFont(Font font) { 96 //TODO:We can decrease count of repaints here 97 //and get rid of recursion 98 resetTextMetrics(); 99 100 XMenuWindow menuWindow = getMenuWindow(); 101 if (menuWindow != null) { 102 menuWindow.setItemsFont(font); 103 } 104 105 repaintIfShowing(); 106 } 107 108 /* 109 * From MenuPeer 110 */ 111 @Override addItem(MenuItem item)112 public void addItem(MenuItem item) { 113 XMenuWindow menuWindow = getMenuWindow(); 114 if (menuWindow != null) { 115 menuWindow.addItem(item); 116 } else { 117 if (log.isLoggable(PlatformLogger.Level.FINE)) { 118 log.fine("Attempt to use XMenuWindowPeer without window"); 119 } 120 } 121 } 122 123 @Override delItem(int index)124 public void delItem(int index) { 125 XMenuWindow menuWindow = getMenuWindow(); 126 if (menuWindow != null) { 127 menuWindow.delItem(index); 128 } else { 129 if (log.isLoggable(PlatformLogger.Level.FINE)) { 130 log.fine("Attempt to use XMenuWindowPeer without window"); 131 } 132 } 133 } 134 135 /************************************************ 136 * 137 * Access to target's fields 138 * 139 ************************************************/ getTargetItems()140 Vector<MenuItem> getTargetItems() { 141 return AWTAccessor.getMenuAccessor().getItems((Menu)getTarget()); 142 } 143 144 /************************************************ 145 * 146 * Overriden behaviour 147 * 148 ************************************************/ 149 @Override isSeparator()150 boolean isSeparator() { 151 return false; 152 } 153 154 //Fix for 6180416: Shortcut keys are displayed against Menus on XToolkit 155 //Menu should always return null as shortcutText 156 @Override getShortcutText()157 String getShortcutText() { 158 return null; 159 } 160 161 /************************************************ 162 * 163 * Utility functions 164 * 165 ************************************************/ 166 167 /** 168 * Returns menu window of this menu or null 169 * it this menu has no container and so its 170 * window can't be created. 171 */ getMenuWindow()172 XMenuWindow getMenuWindow() { 173 return menuWindow; 174 } 175 176 } 177