001: /*
002: * @(#)MenuComponent.java 1.43 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package java.awt;
028:
029: import sun.awt.peer.MenuComponentPeer;
030: import java.awt.event.ActionEvent;
031: import sun.awt.AppContext;
032: import sun.awt.SunToolkit;
033:
034: /**
035: * The abstract class <code>MenuComponent</code> is the superclass
036: * of all menu-related components. In this respect, the class
037: * <code>MenuComponent</code> is analogous to the abstract superclass
038: * <code>Component</code> for AWT components.
039: * <p>
040: * Menu components receive and process AWT events, just as components do,
041: * through the method <code>processEvent</code>.
042: *
043: * @version 1.35, 06/12/02
044: * @author Arthur van Hoff
045: * @since JDK1.0
046: */
047: public abstract class MenuComponent implements java.io.Serializable {
048: transient MenuComponentPeer peer;
049: transient MenuContainer parent;
050: Font font;
051: private String name;
052: private boolean nameExplicitlySet = false;
053: boolean newEventsOnly = false;
054: /**
055: * The AppContext of the MenuComponent. This is set in the constructor
056: * and never changes.
057: */
058: transient AppContext appContext;
059: /*
060: * Internal constants for serialization
061: */
062: final static String actionListenerK = Component.actionListenerK;
063: final static String itemListenerK = Component.itemListenerK;
064: /*
065: * JDK 1.1 serialVersionUID
066: */
067: private static final long serialVersionUID = -4536902356223894379L;
068:
069: /**
070: * Constructor for MenuComponent.
071: */
072: public MenuComponent() {
073: appContext = AppContext.getAppContext();
074: SunToolkit.insertTargetMapping(this , appContext);
075: }
076:
077: /**
078: * Construct a name for this MenuComponent. Called by getName() when
079: * the name is null.
080: */
081: String constructComponentName() {
082: return null; // For strict compliance with prior JDKs, a MenuComponent
083: // that doesn't set its name should return null from
084: // getName()
085: }
086:
087: /**
088: * Gets the name of the menu component.
089: * @return the name of the menu component.
090: * @see java.awt.MenuComponent#setName(java.lang.String)
091: * @since JDK1.1
092: */
093: public String getName() {
094: if (name == null && !nameExplicitlySet) {
095: synchronized (this ) {
096: if (name == null && !nameExplicitlySet) {
097: name = constructComponentName();
098: }
099: }
100: }
101: return name;
102: }
103:
104: /**
105: * Sets the name of the component to the specified string.
106: * @param name the name of the menu component.
107: * @see java.awt.MenuComponent#getName
108: * @since JDK1.1
109: */
110: public void setName(String name) {
111: synchronized (this ) {
112: this .name = name;
113: nameExplicitlySet = true;
114: }
115: }
116:
117: /**
118: * Returns the parent container for this menu component.
119: * @return the menu component containing this menu component,
120: * or <code>null</code> if this menu component
121: * is the outermost component, the menu bar itself.
122: * @since JDK1.0
123: */
124: public MenuContainer getParent() {
125: return parent;
126: }
127:
128: /**
129: * Gets the font used for this menu component.
130: * @return the font used in this menu component, if there is one;
131: * <code>null</code> otherwise.
132: * @see java.awt.MenuComponent#setFont
133: * @since JDK1.0
134: */
135: public Font getFont() {
136: Font font = this .font;
137: if (font != null) {
138: return font;
139: }
140: MenuContainer parent = this .parent;
141: if (parent != null) {
142: return parent.getFont();
143: }
144: return null;
145: }
146:
147: /**
148: * Sets the font to be used for this menu component to the specified
149: * font. This font is also used by all subcomponents of this menu
150: * component, unless those subcomponents specify a different font.
151: * @param f the font to be set.
152: * @see java.awt.MenuComponent#getFont
153: * @since JDK1.0
154: */
155: public void setFont(Font f) {
156: font = f;
157: }
158:
159: /**
160: * Removes the menu component's peer. The peer allows us to modify the
161: * appearance of the menu component without changing the functionality of
162: * the menu component.
163: */
164: public void removeNotify() {
165: synchronized (getTreeLock()) {
166: MenuComponentPeer p = (MenuComponentPeer) this .peer;
167: if (p != null) {
168: Toolkit.getEventQueue().removeSourceEvents(this );
169: this .peer = null;
170: p.dispose();
171: }
172: }
173: }
174:
175: /**
176: * Posts the specified event to the menu.
177: * This method is part of the Java 1.0 event system
178: * and it is maintained only for backwards compatibility.
179: * Its use is discouraged, and it may not be supported
180: * in the future.
181: * @param evt the event which is to take place
182: * @deprecated As of JDK version 1.1,
183: * replaced by <code>dispatchEvent(AWTEvent)</code>.
184: * @since JDK1.0
185: */
186: public boolean postEvent(Event evt) {
187: MenuContainer parent = this .parent;
188: if (parent != null) {
189: parent.postEvent(evt);
190: }
191: return false;
192: }
193:
194: /*
195: * Delivers an event to this component or one of its sub components.
196: * @param e the event
197: */
198: public final void dispatchEvent(AWTEvent e) {
199: dispatchEventImpl(e);
200: }
201:
202: void dispatchEventImpl(AWTEvent e) {
203: if (newEventsOnly
204: || (parent != null && parent instanceof MenuComponent && ((MenuComponent) parent).newEventsOnly)) {
205: if (eventEnabled(e)) {
206: processEvent(e);
207: } else if (e instanceof ActionEvent && parent != null) {
208: ((MenuComponent) parent).dispatchEvent(new ActionEvent(
209: parent, e.getID(), ((ActionEvent) e)
210: .getActionCommand()));
211: }
212: } else { // backward compatibility
213: Event olde = e.convertToOld();
214: if (olde != null) {
215: postEvent(olde);
216: }
217: }
218: }
219:
220: // NOTE: remove when filtering is done at lower level
221: boolean eventEnabled(AWTEvent e) {
222: return false;
223: }
224:
225: /**
226: * Processes events occurring on this menu component.
227: * @param e the event
228: * @since JDK1.1
229: */
230: protected void processEvent(AWTEvent e) {
231: }
232:
233: /**
234: * Returns the parameter string representing the state of this
235: * menu component. This string is useful for debugging.
236: * @return the parameter string of this menu component.
237: * @since JDK1.0
238: */
239: protected String paramString() {
240: String this Name = getName();
241: return (this Name != null ? this Name : "");
242: }
243:
244: /**
245: * Returns a representation of this menu component as a string.
246: * @return a string representation of this menu component.
247: * @since JDK1.0
248: */
249: public String toString() {
250: return getClass().getName() + "[" + paramString() + "]";
251: }
252:
253: /**
254: * Gets this component's locking object (the object that owns the thread
255: * sychronization monitor) for AWT component-tree and layout
256: * operations.
257: * @return This component's locking object.
258: */
259: protected final Object getTreeLock() {
260: return Component.LOCK;
261: }
262: }
|