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      */
setContainer(XBaseMenuWindow container)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      */
dispose()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      */
setFont(Font font)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      */
addSeparator()113     public void addSeparator() {
114         if (log.isLoggable(PlatformLogger.Level.FINER)) {
115             log.finer("addSeparator is not implemented");
116         }
117     }
118 
addItem(MenuItem item)119     public void addItem(MenuItem item) {
120         XMenuWindow menuWindow = getMenuWindow();
121         if (menuWindow != null) {
122             menuWindow.addItem(item);
123         } else {
124             if (log.isLoggable(PlatformLogger.Level.FINE)) {
125                 log.fine("Attempt to use XMenuWindowPeer without window");
126             }
127         }
128     }
129 
delItem(int index)130     public void delItem(int index) {
131         XMenuWindow menuWindow = getMenuWindow();
132         if (menuWindow != null) {
133             menuWindow.delItem(index);
134         } else {
135             if (log.isLoggable(PlatformLogger.Level.FINE)) {
136                 log.fine("Attempt to use XMenuWindowPeer without window");
137             }
138         }
139     }
140 
141     /************************************************
142      *
143      * Access to target's fields
144      *
145      ************************************************/
getTargetItems()146     Vector getTargetItems() {
147         return AWTAccessor.getMenuAccessor().getItems((Menu)getTarget());
148     }
149 
150     /************************************************
151      *
152      * Overriden behaviour
153      *
154      ************************************************/
isSeparator()155     boolean isSeparator() {
156         return false;
157     }
158 
159     //Fix for 6180416: Shortcut keys are displayed against Menus on XToolkit
160     //Menu should always return null as shortcutText
getShortcutText()161     String getShortcutText() {
162         return null;
163     }
164 
165     /************************************************
166      *
167      * Utility functions
168      *
169      ************************************************/
170 
171     /**
172      * Returns menu window of this menu or null
173      * it this menu has no container and so its
174      * window can't be created.
175      */
getMenuWindow()176     XMenuWindow getMenuWindow() {
177         return menuWindow;
178     }
179 
180 }
181