001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.AWTEventMulticaster;
024: import java.awt.ItemSelectable;
025: import java.awt.Rectangle;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.ItemEvent;
029: import java.awt.event.ItemListener;
030: import java.awt.event.KeyEvent;
031:
032: /**
033: * <code>WingMenuItem</code> implements an item in a menu.
034: * It can be used as a simple menu item, checkbox menu item,
035: * radio button menu item and menu separator.
036: * <br>
037: * <br>
038: * <b>This class is thread safe.</b>
039: **/
040: public class WingMenuItem implements ItemSelectable, Shortcut,
041: WingConst {
042: protected/*final*/int kind;
043: protected/*final*/WingMenu menu;
044:
045: protected ActionListener actionListener;
046: protected ItemListener itemListener;
047: protected Object label;
048: protected boolean enabled;
049: protected boolean selected;
050: protected Object tooltip;
051: protected Object userData;
052: protected MenuRadioGroup group;
053: protected WingMenu owner;
054: protected int shortcutCode;
055: protected int shortcutModifiers;
056: protected Rectangle bounds;
057:
058: public WingMenuItem(Object label) {
059: this (label, WingMenu.NORMAL);
060: }
061:
062: public WingMenuItem(Object label, int kind) {
063: this (label, kind, KeyEvent.VK_UNDEFINED, 0);
064: }
065:
066: public WingMenuItem(Object label, int kind, int shortcutCode,
067: int shortcutModifiers) {
068: this (label, kind, shortcutCode, shortcutModifiers, null);
069: }
070:
071: protected WingMenuItem(Object label, int kind, int shortcutCode,
072: int shortcutModifiers, WingMenu menu) {
073: this .label = label;
074: this .kind = kind;
075: this .shortcutCode = shortcutCode;
076: this .shortcutModifiers = shortcutModifiers;
077: this .menu = menu;
078: selected = false;
079: enabled = true;
080: bounds = new Rectangle();
081: }
082:
083: public void addActionListener(ActionListener l) {
084: actionListener = AWTEventMulticaster.add(actionListener, l);
085: }
086:
087: public void removeActionListener(ActionListener l) {
088: actionListener = AWTEventMulticaster.remove(actionListener, l);
089: }
090:
091: public void addItemListener(ItemListener l) {
092: itemListener = AWTEventMulticaster.add(itemListener, l);
093: }
094:
095: public void removeItemListener(ItemListener l) {
096: itemListener = AWTEventMulticaster.remove(itemListener, l);
097: }
098:
099: public Object[] getSelectedObjects() {
100: if (selected) {
101: Object[] items = { label };
102: return items;
103: }
104: return null;
105: }
106:
107: public boolean isSelected() {
108: return selected;
109: }
110:
111: public void setSelected(boolean selected) {
112: if (this .selected == selected)
113: return;
114: this .selected = selected;
115: MenuRadioGroup group = this .group;
116: if (group != null)
117: group.setSelected(this , selected);
118: repaint();
119: }
120:
121: public void setTooltip(Object tooltip) {
122: this .tooltip = tooltip;
123: }
124:
125: public void setEnabled(boolean enabled) {
126: this .enabled = enabled;
127: repaint();
128: }
129:
130: public void setLabel(Object label) {
131: this .label = label;
132: WingMenu owner = this .owner;
133: if (owner != null)
134: owner.revalidateAndRepaint();
135: }
136:
137: public Object getLabel() {
138: return label;
139: }
140:
141: public WingMenu getSubMenu() {
142: return menu;
143: }
144:
145: public int getKind() {
146: return kind;
147: }
148:
149: public WingMenu getOwner() {
150: return owner;
151: }
152:
153: public Object getUserData() {
154: return userData;
155: }
156:
157: public void setUserData(Object userData) {
158: this .userData = userData;
159: }
160:
161: private void repaint() {
162: WingMenu owner = this .owner;
163: if (owner != null) {
164: owner.repaintVisible(bounds.x, bounds.y, bounds.width,
165: bounds.height);
166: }
167: }
168:
169: protected void fireAction() {
170: if (kind == SEPARATOR || kind == MENU)
171: return;
172:
173: Object item = this .label;
174: WingMenu owner = this .owner;
175:
176: if (kind == CHECKBOX || kind == RADIO) {
177: boolean oldsel = selected;
178: setSelected((group == null) ? !selected : true);
179:
180: if (oldsel == selected)
181: return;
182:
183: ItemListener l2 = itemListener;
184: ItemEvent e2 = new ItemEvent(this ,
185: ItemEvent.ITEM_STATE_CHANGED, item,
186: (selected) ? ItemEvent.SELECTED
187: : ItemEvent.DESELECTED);
188: if (l2 != null)
189: l2.itemStateChanged(e2);
190: if (owner != null) {
191: owner.wingProcessItemEvent(e2);
192: }
193: }
194: ActionListener l = actionListener;
195: ActionEvent e = new ActionEvent(this ,
196: ActionEvent.ACTION_PERFORMED, (item != null) ? item
197: .toString() : null);
198: if (l != null)
199: l.actionPerformed(e);
200: if (owner != null)
201: owner.wingProcessActionEvent(e);
202: }
203:
204: public int getShortcutCode() {
205: return shortcutCode;
206: }
207:
208: public int getShortcutModifiers() {
209: return shortcutModifiers;
210: }
211:
212: public void processShortcut(KeyEvent e) {
213: WingMenu owner = this .owner;
214: if (enabled && owner != null) {
215: owner.itemShortcut(this , e);
216: }
217: }
218:
219: public String getShortcutText() {
220: if (shortcutCode == KeyEvent.VK_UNDEFINED)
221: return null;
222: StringBuffer s = new StringBuffer();
223: if ((shortcutModifiers & KeyEvent.CTRL_MASK) != 0)
224: s.append("Ctrl+");
225: if ((shortcutModifiers & KeyEvent.ALT_MASK) != 0)
226: s.append("Alt+");
227: if ((shortcutModifiers & KeyEvent.SHIFT_MASK) != 0)
228: s.append("Shift+");
229: s.append(KeyEvent.getKeyText(shortcutCode));
230: return s.toString();
231: }
232: }
|