-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAbstractMenuItem.java
More file actions
164 lines (144 loc) · 3.58 KB
/
AbstractMenuItem.java
File metadata and controls
164 lines (144 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.lcdproc.lcdjava;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.EventListenerList;
/**
* Abstract parent class for menu items.
* @author Stefan Krupop
*/
public abstract class AbstractMenuItem implements MenuItem
{
/**
* The item id.
*/
protected final String _id;
/**
* The item text.
*/
private String _text;
/**
* The menu this item belongs to.
*/
protected Submenu _menu;
/**
* The event listeners connected to this item
*/
protected EventListenerList _listeners;
/**
* Constructor.
* @param id the of the item.
* @param menu the menu to add this item to
*/
protected AbstractMenuItem(String id, Submenu menu)
{
_id = id;
_menu = menu;
_listeners = new EventListenerList();
}
/**
* Get the item id.
* @return the item id.
*/
public String getID()
{
return _id;
}
/**
* Set the menu item text.
* @param text the menu item text.
*/
public void setText(String text)
{
_text = text;
update();
}
/**
* Get the menu item text.
* @return the menu item text.
*/
public String getText()
{
return _text;
}
/**
* Add this MenuItem to the Submenu that constructed us.
* @return whether or not the menu item was added successfully.
*/
public boolean activate()
{
return _menu.addItem(this);
}
/**
* Remove this MenuItem from the Screen that constructed us.
*/
public void remove()
{
_menu.removeItem(this);
}
/**
* Strip any quotes from the provided text.
* @param text the text to strip quotes from.
* @return the text with any quotes removed.
*/
protected String stripQuotes(String text)
{
StringBuffer ret = new StringBuffer();
if (text != null)
{
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++)
{
if (chars[i] != '\"')
{
ret.append(chars[i]);
}
}
}
return (text == null) ? null : ret.toString();
}
/**
* Update this MenuItems's state.
*/
protected void update()
{
_menu.updateItem(this);
}
/**
* Return the data this menu item needs to update itself.
* @return the data to update this menu item.
*/
public String getData()
{
return "-text \"" + stripQuotes(_text) + "\"";
}
/**
* Adds an ActionListener to the menu item.
* @param listener the ActionListener to be added
*/
public void addActionListener(ActionListener listener) {
_listeners.add(ActionListener.class, listener);
}
/**
* Removes an ActionListener from the menu item.
* @param listener the ActionListener to be removed
*/
public void removeActionListener(ActionListener listener) {
_listeners.remove(ActionListener.class, listener);
}
public void notifyActionPerformed(ActionEvent e) {
for (ActionListener l : _listeners.getListeners(ActionListener.class)) {
l.actionPerformed(e);
}
}
/**
* Return a String representing this MenuItem.
* @return a String representing this MenuItem.
*/
public String toString()
{
return "Type = " + getType() +
"; menu id = " + _menu.getID() +
"; id = " + getID() +
"; data = " + getData();
}
}