OSDN Git Service

* java/awt/MenuItem.java (paramString): Now protected.
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / CheckboxMenuItem.java
1 /* Copyright (C) 2000  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.awt;
10 import java.awt.peer.CheckboxMenuItemPeer;
11 import java.awt.event.ItemListener;
12 import java.awt.event.ItemEvent;
13
14 /** This implements a menu item which keeps track of a boolean state.
15  * @author Tom Tromey <tromey@redhat.com>
16  * @date December 25, 2000
17  */
18 public class CheckboxMenuItem extends MenuItem implements ItemSelectable
19 {
20   /** Create a new CheckboxMenuItem.
21    * @param label The checkbox label.  A null value is the same as "";
22    *              null is the default.
23    * @param state The initial check state; defaults to false.
24    */
25   public CheckboxMenuItem ()
26   {
27     this (null, false);
28   }
29
30   public CheckboxMenuItem (String label)
31   {
32     this (label, false);
33   }
34
35   public CheckboxMenuItem (String label, boolean state)
36   {
37     this.label = label;
38     this.state = state;
39   }
40
41   /** Add a listener for item events.
42    * @param listener The listener to add.
43    */
44   public synchronized void addItemListener (ItemListener listener)
45   {
46     listeners = AWTEventMulticaster.add (listeners, listener);
47   }
48
49   /** This creates the component's peer.  */
50   public void addNotify ()
51   {
52     if (peer != null)
53       {
54         // This choice of toolkit seems unsatisfying, but I'm not sure
55         // what else to do.
56         peer = Toolkit.getDefaultToolkit ().createCheckboxMenuItem (this);
57       }
58   }
59
60   /** Returns this checkbox's label if this checkbox is selected.  */
61   public Object[] getSelectedObjects ()
62   {
63     Object[] r;
64     if (state)
65       {
66         r = new Object[1];
67         r[0] = label;
68       }
69     else
70       r = new Object[0];
71     return r;
72   }
73
74   /** Returns the current state of this checkbox.  */
75   public boolean getState ()
76   {
77     return state;
78   }
79
80   /** Generates a String representation of this Checkbox's state.  */
81   public String paramString ()
82   {
83     return ("[" + getClass ().getName ()
84             + "state=" + state + ","
85             + "label=" + label + "]");
86   }
87
88   /** Process an event for this Checkbox.
89    * @param event The event the process.
90    */
91   protected void processEvent (AWTEvent event) 
92   {
93     if (event instanceof ItemEvent)
94       processItemEvent ((ItemEvent) event);
95     else
96       super.processEvent (event);
97   }
98
99   /** Process an item event for this Checkbox.
100    * @param event The ItemEvent to process
101    */
102   protected void processItemEvent (ItemEvent event)
103   {
104     if (listeners != null)
105       listeners.itemStateChanged (event);
106   }
107
108   /** Remove an item listener.
109    * @param listener Item listener to remove.
110    */
111   public synchronized void removeItemListener (ItemListener listener)
112   {
113     listeners = AWTEventMulticaster.remove (listeners, listener);
114   }
115
116   /** Set the checkbox's state.
117    * @param state The new state.
118    */
119   public void setState (boolean state)
120   {
121     this.state = state;
122     if (peer != null)
123       {
124         CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
125         cp.setState (state);
126       }
127   }
128
129   // Private state.
130   String label;
131   boolean state;
132   ItemListener listeners;
133 }