Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/awt/menu/CheckboxMenuItem.java


1   /*
2    * CheckboxMenuItem.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:42 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.awt.menu;
12  
13  import java.awt.event.ItemEvent;
14  import java.util.Observable;
15  import java.util.Observer;
16  
17  import com.flexstor.common.awt.AWTActionEvent;
18  import com.flexstor.common.resources.Resources;
19  import com.flexstor.common.util.FlexObservable;
20  
21  /**
22   * This class adds an identifier to each menu item in order
23   * to uniquely determine an item independant of its label.
24   * The code is identical with MenuItem, because of hierarchy
25   * constraints, this class cannot inherit from java.awt.CheckboxMenuItem
26   * AND com.flexstor.common.awt.menu.MenuItem. It sends a ActionEvent with its
27   * action id to its container class when selected.
28   * In addition, this class implements an Observable behavior
29   * in order to create exclusive menu item groups.
30   * @see com.flexstor.common.awt.menu.MenuGroup
31   * @see com.flexstor.client.FlexObservable
32   * @author Raimund Dettmer
33   */
34  public class CheckboxMenuItem
35     extends java.awt.CheckboxMenuItem
36     implements com.flexstor.common.awt.ActionComponentI
37  {
38     FlexObservable observable = new FlexObservable();
39  
40     int nActionIdTrue  = 0;
41     int nActionIdFalse = 0;
42     
43     /**
44      * Create a new CheckboxMenuItem with the given label id(text resource number)
45      * and action id.
46      * @param nTextId   the id for text resources, defines the label
47      * @param nActionId the new action id, defines the action to be invoked
48      * @see com.flexstor.common.Constants
49      */
50     public CheckboxMenuItem(int nTextId, int nActionId)
51     {
52        super( Resources.get( nTextId ) );
53  //      setActionCommand(Integer.toString(nActionId));
54        this.nActionIdTrue  = nActionId;
55        this.nActionIdFalse = nActionId;
56        enableEvents(java.awt.AWTEvent.ITEM_EVENT_MASK);
57     }
58     
59     /**
60      * Create a new CheckboxMenuItem with the given label id(text resource number)
61      * and action ids. one for each state.
62      * @param nTextId   the id for text resources, defines the label
63      * @param nActionId the action id for true state
64      * @param nActionId the action id for false state
65      * @see com.flexstor.common.Constants
66      */
67     public CheckboxMenuItem(int nTextId, int nActionIdTrue, int nActionIdFalse)
68     {
69        super( Resources.get( nTextId ) );
70  //      setActionCommand(Integer.toString(nActionIdTrue));
71        this.nActionIdTrue  = nActionIdTrue;
72        this.nActionIdFalse = nActionIdFalse;
73        enableEvents(java.awt.AWTEvent.ITEM_EVENT_MASK);
74     }
75  
76     public void addNotify()
77     {
78        super.addNotify();
79        
80        setFont( getParent().getFont() );
81     }
82  
83     
84     /**
85      * Create a new CheckboxMenuItem with the given label id(text resource number)
86      * and action id(defined in Constants).
87      * @param nTextId   the id for text resources, defines the label
88      * @param nActionId the new action id, defines the action to be invoked
89      * @param nShortcutKey the raw key code (defined in KeyEvent) for this shortcut.
90      * @param bUseShift the shortcut will use the shift key if true.
91      * @see com.flexstor.common.Constants
92      */
93     //public CheckboxMenuItem ( int nTextId, int nActionId, int nShortcutKey, boolean bUseShift )
94     //{
95        //this ( nTextId, nActionId );
96        //setShortcut ( new MenuShortcut(nShortcutKey, bUseShift) );
97     //}
98  
99     /**
100     * Gets the menu item action id
101     * @return the action id
102     */
103    public int getActionId()
104    {
105       //return (new Integer(getActionCommand())).intValue();
106       return nActionIdTrue;
107    }
108 
109    /**
110     * Set the menu item state to a new value and notify observers.
111     * (FYI: The peer calls setState and then action which
112     * eventually calls postEvent).
113     */
114    public void setState(boolean bState)
115    {
116       //System.out.println("setState called");
117       if (getState() != bState)
118       {
119          super.setState(bState);
120          observable.setChanged();
121          observable.notifyObservers(this);
122       }
123    }
124 
125    /**
126     * This method is called from the menu item group to manipulate
127     * the state without notification
128     * @see com.flexstor.common.awt.menu.MenuGroup
129     */
130    void setStateUnNotified(boolean bState)
131    {
132       super.setState(bState);
133    }
134 
135    /**
136     * Gets the embedded observable object used for delegation
137     * of the observable pattern when class does NOT derive from
138     * java.util.Observable
139     * @see java.util.Observable
140     */
141    public Observable getObservable()
142    {
143       return observable;
144    }
145 
146    /**
147     * Add an observer to this menu item
148     * @param the observer
149     */
150    public synchronized void addObserver(Observer o)
151    {
152       observable.addObserver(o);
153    }
154 
155    /**
156     * Convert an item event into an action event, so an event
157     * can be handled with an action listener for checkboxmenuitems
158     * as well as regular menu items.
159     */
160    public void processItemEvent(ItemEvent e)
161    {
162        //dispatchEvent(new AWTActionEvent(this, getActionId()));
163        dispatchEvent(new AWTActionEvent(this, getState() ? nActionIdTrue : nActionIdFalse));
164    }
165 }  // end of class
166