001: /*
002: * @(#)CheckboxMenuItem.java 1.51 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.CheckboxMenuItemPeer;
030: import sun.awt.PeerBasedToolkit;
031: import java.awt.event.*;
032: import java.io.ObjectOutputStream;
033: import java.io.ObjectInputStream;
034: import java.io.IOException;
035: import java.util.EventListener;
036: import java.awt.AWTEventMulticaster;
037:
038: /**
039: * This class represents a check box that can be included in a menu.
040: * Clicking on the check box in the menu changes its state from
041: * "on" to "off" or from "off" to "on."
042: * <p>
043: * The following picture depicts a menu which contains an instance
044: * of <code>CheckBoxMenuItem</code>:
045: * <p>
046: * <img src="doc-files/MenuBar-1.gif"
047: * ALIGN=center HSPACE=10 VSPACE=7>
048: * <p>
049: * The item labeled <code>Check</code> shows a check box menu item
050: * in its "off" state.
051: * <p>
052: * When a check box menu item is selected, AWT sends an item event to
053: * the item. Since the event is an instance of <code>ItemEvent</code>,
054: * the <code>processEvent</code> method examines the event and passes
055: * it along to <code>processItemEvent</code>. The latter method redirects
056: * the event to any <code>ItemListener</code> objects that have
057: * registered an interest in item events generated by this menu item.
058: *
059: * @version 1.44, 08/19/02
060: * @author Sami Shaio
061: * @see java.awt.event.ItemEvent
062: * @see java.awt.event.ItemListener
063: * @see java.awt.Frame
064: * @see java.awt.Menu
065: * @see java.awt.MenuBar
066: * @see java.awt.PopupMenu
067: * @see java.awt.MenuShortcut
068: * @since JDK1.0
069: */
070: public class CheckboxMenuItem extends MenuItem implements
071: ItemSelectable {
072: /*
073: * The state of a checkbox menu item
074: * @serial
075: * @see getState()
076: * @see setState()
077: */
078: boolean state = false;
079: transient ItemListener itemListener;
080: /*
081: * JDK 1.1 serialVersionUID
082: */
083: private static final long serialVersionUID = 6190621106981774043L;
084:
085: /**
086: * Create a check box menu item with an empty label.
087: * The item's state is initially set to "off."
088: * @since JDK1.1
089: */
090: public CheckboxMenuItem() {
091: this ("", false);
092: }
093:
094: /**
095: * Create a check box menu item with the specified label.
096: * The item's state is initially set to "off."
097:
098: * @param label a string label for the check box menu item,
099: * or <code>null</code> for an unlabeled menu item.
100: */
101: public CheckboxMenuItem(String label) {
102: this (label, false);
103: }
104:
105: /**
106: * Create a check box menu item with the specified label and state.
107: * @param label a string label for the check box menu item,
108: * or <code>null</code> for an unlabeled menu item.
109: * @param state the initial state of the menu item, where
110: * <code>true</code> indicates "on" and
111: * <code>false</code> indicates "off."
112: * @since JDK1.1
113: */
114: public CheckboxMenuItem(String label, boolean state) {
115: super (label);
116: this .state = state;
117: }
118:
119: /**
120: * Creates the peer of the checkbox item. This peer allows us to
121: * change the look of the checkbox item without changing its
122: * functionality.
123: * Most applications do not call this method directly.
124: * @see java.awt.Toolkit#createCheckboxMenuItem(java.awt.CheckboxMenuItem)
125: * @see java.awt.Component#getToolkit()
126: * @since JDK1.0
127: */
128: public void addNotify() {
129: synchronized (getTreeLock()) {
130: if (peer == null)
131: peer = ((PeerBasedToolkit) Toolkit.getDefaultToolkit())
132: .createCheckboxMenuItem(this );
133: super .addNotify();
134: }
135: }
136:
137: /**
138: * Determines whether the state of this check box menu item
139: * is "on" or "off."
140: * @return the state of this check box menu item, where
141: * <code>true</code> indicates "on" and
142: * <code>false</code> indicates "off."
143: * @see java.awt.CheckboxMenuItem#setState
144: */
145: public boolean getState() {
146: return state;
147: }
148:
149: /**
150: * Sets this check box menu item to the specifed state.
151: * The boolean value <code>true</code> indicates "on" while
152: * <code>false</code> indicates "off."
153: * @param b the boolean state of this
154: * check box menu item.
155: * @see java.awt.CheckboxMenuItem#getState
156: */
157: public synchronized void setState(boolean b) {
158: state = b;
159: CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer) this .peer;
160: if (peer != null) {
161: peer.setState(b);
162: }
163: }
164:
165: /**
166: * Returns the an array (length 1) containing the checkbox menu item
167: * label or null if the checkbox is not selected.
168: * @see ItemSelectable
169: */
170: public synchronized Object[] getSelectedObjects() {
171: if (state) {
172: Object[] items = new Object[1];
173: items[0] = label;
174: return items;
175: }
176: return null;
177: }
178:
179: /**
180: * Adds the specified item listener to receive item events from
181: * this check box menu item.
182: * If l is null, no exception is thrown and no action is performed.
183: *
184: * @param l the item listener
185: * @see java.awt.event.ItemEvent
186: * @see java.awt.event.ItemListener
187: * @see java.awt.Choice#removeItemListener
188: * @since JDK1.1
189: */
190: public synchronized void addItemListener(ItemListener l) {
191: itemListener = AWTEventMulticaster.add(itemListener, l);
192: newEventsOnly = true;
193: }
194:
195: /**
196: * Removes the specified item listener so that it no longer receives
197: * item events from this check box menu item.
198: * If l is null, no exception is thrown and no action is performed.
199: *
200: * @param l the item listener
201: * @see java.awt.event.ItemEvent
202: * @see java.awt.event.ItemListener
203: * @see java.awt.Choice#addItemListener
204: * @since JDK1.1
205: */
206: public synchronized void removeItemListener(ItemListener l) {
207: itemListener = AWTEventMulticaster.remove(itemListener, l);
208: }
209:
210: /**
211: * Returns an array of all the item listeners
212: * registered on this checkbox menuitem.
213: *
214: * @return all of this checkbox menuitem's <code>ItemListener</code>s
215: * or an empty array if no item
216: * listeners are currently registered
217: *
218: * @see #addItemListener
219: * @see #removeItemListener
220: * @see java.awt.event.ItemEvent
221: * @see java.awt.event.ItemListener
222: * @since 1.4
223: */
224: public synchronized ItemListener[] getItemListeners() {
225: return (ItemListener[]) AWTEventMulticaster.getListeners(
226: (EventListener) itemListener, ItemListener.class);
227: }
228:
229: // NOTE: remove when filtering is done at lower level
230: boolean eventEnabled(AWTEvent e) {
231: if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
232: if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0
233: || itemListener != null) {
234: return true;
235: }
236: return false;
237: }
238: return super .eventEnabled(e);
239: }
240:
241: /**
242: * Processes events on this check box menu item.
243: * If the event is an instance of <code>ItemEvent</code>,
244: * this method invokes the <code>processItemEvent</code> method.
245: * If the event is not an item event,
246: * it invokes <code>processEvent</code> on the superclass.
247: * <p>
248: * Check box menu items currently support only item events.
249: * @param e the event
250: * @see java.awt.event.ItemEvent
251: * @see java.awt.CheckboxMenuItem#processItemEvent
252: * @since JDK1.1
253: */
254: protected void processEvent(AWTEvent e) {
255: if (e instanceof ItemEvent) {
256: processItemEvent((ItemEvent) e);
257: return;
258: }
259: super .processEvent(e);
260: }
261:
262: /**
263: * Processes item events occurring on this check box menu item by
264: * dispatching them to any registered <code>ItemListener</code> objects.
265: * <p>
266: * This method is not called unless item events are
267: * enabled for this menu item. Item events are enabled
268: * when one of the following occurs:
269: * <p><ul>
270: * <li>An <code>ItemListener</code> object is registered
271: * via <code>addItemListener</code>.
272: * <li>Item events are enabled via <code>enableEvents</code>.
273: * </ul>
274: * @param e the item event.
275: * @see java.awt.event.ItemEvent
276: * @see java.awt.event.ItemListener
277: * @see java.awt.CheckboxMenuItem#addItemListener
278: * @see java.awt.MenuItem#enableEvents
279: * @since JDK1.1
280: */
281: protected void processItemEvent(ItemEvent e) {
282: if (itemListener != null) {
283: itemListener.itemStateChanged(e);
284: }
285: }
286:
287: /*
288: * Post an ItemEvent and toggle state.
289: */
290: void doMenuEvent() {
291: setState(!state);
292: Toolkit.getEventQueue().postEvent(
293: new ItemEvent(this , ItemEvent.ITEM_STATE_CHANGED,
294: getLabel(), state ? ItemEvent.SELECTED
295: : ItemEvent.DESELECTED));
296: }
297:
298: /**
299: * Returns the parameter string representing the state of this check
300: * box menu item. This string is useful for debugging.
301: * @return the parameter string of this check box menu item.
302: */
303: public String paramString() {
304: return super .paramString() + ",state=" + state;
305: }
306:
307: /*
308: * Serial Data Version
309: * @serial
310: */
311: private int checkboxMenuItemSerializedDataVersion = 1;
312:
313: /**
314: * Writes default serializable fields to stream. Writes
315: * a list of serializable ItemListener(s) as optional data.
316: * The non-serializable ItemListner(s) are detected and
317: * no attempt is made to serialize them.
318: *
319: * @serialData Null terminated sequence of 0 or more pairs.
320: * The pair consists of a String and Object.
321: * The String indicates the type of object and
322: * is one of the following :
323: * itemListenerK indicating and ItemListener object.
324: *
325: * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
326: * @see java.awt.Component.itemListenerK
327: */
328: private void writeObject(ObjectOutputStream s)
329: throws java.io.IOException {
330: s.defaultWriteObject();
331: AWTEventMulticaster.save(s, itemListenerK, itemListener);
332: s.writeObject(null);
333: }
334:
335: /*
336: * Read the ObjectInputStream and if it isnt null
337: * add a listener to receive item events fired
338: * by the Checkbox menu item.
339: * Unrecognised keys or values will be Ignored.
340: * @serial
341: * @see removeActionListener()
342: * @see addActionListener()
343: */
344: private void readObject(ObjectInputStream s)
345: throws ClassNotFoundException, IOException {
346: s.defaultReadObject();
347: Object keyOrNull;
348: while (null != (keyOrNull = s.readObject())) {
349: String key = ((String) keyOrNull).intern();
350: if (itemListenerK == key)
351: addItemListener((ItemListener) (s.readObject()));
352: else
353: // skip value for unrecognized key
354: s.readObject();
355: }
356: }
357: }
|