001: /*
002: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.awt.*;
029: import java.awt.peer.*;
030: import java.awt.event.*;
031:
032: import java.util.logging.*;
033:
034: import java.lang.reflect.Field;
035: import sun.awt.SunToolkit;
036:
037: class XCheckboxMenuItemPeer extends XMenuItemPeer implements
038: CheckboxMenuItemPeer {
039:
040: /************************************************
041: *
042: * Data members
043: *
044: ************************************************/
045:
046: private static Logger log = Logger
047: .getLogger("sun.awt.X11.XCheckboxMenuItemPeer");
048:
049: /*
050: * CheckboxMenuItem's fields
051: */
052: private final static Field f_state;
053: static {
054: f_state = SunToolkit.getField(CheckboxMenuItem.class, "state");
055: }
056:
057: /************************************************
058: *
059: * Construction
060: *
061: ************************************************/
062: XCheckboxMenuItemPeer(CheckboxMenuItem target) {
063: super (target);
064: }
065:
066: /************************************************
067: *
068: * Implementaion of interface methods
069: *
070: ************************************************/
071:
072: //Prom CheckboxMenuItemtPeer
073: public void setState(boolean t) {
074: repaintIfShowing();
075: }
076:
077: /************************************************
078: *
079: * Access to target's fields
080: *
081: ************************************************/
082: boolean getTargetState() {
083: MenuItem target = getTarget();
084: if (target == null) {
085: return false;
086: }
087: try {
088: return f_state.getBoolean(target);
089: } catch (IllegalAccessException e) {
090: e.printStackTrace();
091: }
092: return false;
093: }
094:
095: /************************************************
096: *
097: * Utility functions
098: *
099: ************************************************/
100:
101: /**
102: * Toggles state and generates ItemEvent
103: */
104: void action(final long when) {
105: XToolkit.executeOnEventHandlerThread(
106: (CheckboxMenuItem) getTarget(), new Runnable() {
107: public void run() {
108: doToggleState(when);
109: }
110: });
111: }
112:
113: /************************************************
114: *
115: * Private
116: *
117: ************************************************/
118: private void doToggleState(long when) {
119: CheckboxMenuItem cb = (CheckboxMenuItem) getTarget();
120: boolean newState = !getTargetState();
121: cb.setState(newState);
122: ItemEvent e = new ItemEvent(cb, ItemEvent.ITEM_STATE_CHANGED,
123: getTargetLabel(), getTargetState() ? ItemEvent.SELECTED
124: : ItemEvent.DESELECTED);
125: XWindow.postEventStatic(e);
126: //WToolkit does not post ActionEvent when clicking on menu item
127: //MToolkit _does_ post.
128: //Fix for 5005195 MAWT: CheckboxMenuItem fires action events
129: //Events should not be fired
130: //XWindow.postEventStatic(new ActionEvent(cb, ActionEvent.ACTION_PERFORMED,
131: // getTargetActionCommand(), when,
132: // 0));
133: }
134:
135: } // class XCheckboxMenuItemPeer
|