1
|
|
package com.tirsen.angkor.widget;
|
2
|
|
|
3
|
|
import com.tirsen.angkor.Controller;
|
4
|
|
import com.tirsen.angkor.Debug;
|
5
|
|
import com.tirsen.angkor.RenderContext;
|
6
|
|
import com.tirsen.angkor.ViewLink;
|
7
|
|
import com.tirsen.angkor.Component;
|
8
|
|
import com.tirsen.angkor.event.AbstractAction;
|
9
|
|
import com.tirsen.angkor.event.Action;
|
10
|
|
import com.tirsen.angkor.event.ActionEvent;
|
11
|
|
import com.tirsen.angkor.event.ActionListener;
|
12
|
|
import com.tirsen.angkor.event.ActionSource;
|
13
|
|
import com.tirsen.angkor.event.ActionSourceHelper;
|
14
|
|
import org.apache.log4j.Category;
|
15
|
|
|
16
|
|
import java.io.IOException;
|
17
|
|
|
18
|
|
/**
|
19
|
|
* This has essentially been refactored into something almost exactly similar to {@link Link}.
|
20
|
|
* TODO refactor this into Link and remove this class and all its uses.
|
21
|
|
*
|
22
|
|
* <!-- $Id: MenuItem.java,v 1.6 2002/10/13 19:59:23 tirsen Exp $ -->
|
23
|
|
* <!-- $Author: tirsen $ -->
|
24
|
|
*
|
25
|
|
* @author Jon Tirsén (tirsen@users.sourceforge.net)
|
26
|
|
* @version $Revision: 1.6 $
|
27
|
|
*/
|
28
|
|
public class MenuItem extends Component implements ActionSource
|
29
|
|
{
|
30
|
|
private static final Category logger = Category.getInstance(Debug.LOGGER_NAME);
|
31
|
|
|
32
|
|
private String text;
|
33
|
|
private ActionSourceHelper actionSourceHelper = new ActionSourceHelper(this);
|
34
|
|
|
35
|
2
|
public MenuItem(Action action)
|
36
|
|
{
|
37
|
2
|
setAction(action);
|
38
|
|
}
|
39
|
|
|
40
|
|
/**
|
41
|
|
* The specified text or if specified text is <code>null</code> the name of the action.
|
42
|
|
*/
|
43
|
4
|
public String getText()
|
44
|
|
{
|
45
|
4
|
String ret = text;
|
46
|
4
|
if (ret == null && getAction() != null) ret = (String) getAction().getValue(Action.NAME);
|
47
|
4
|
return ret;
|
48
|
|
}
|
49
|
|
|
50
|
0
|
public void setText(String text)
|
51
|
|
{
|
52
|
0
|
this.text = text;
|
53
|
|
}
|
54
|
|
|
55
|
0
|
public boolean isParsing()
|
56
|
|
{
|
57
|
0
|
return true;
|
58
|
|
}
|
59
|
|
|
60
|
2
|
public void parse(RenderContext context)
|
61
|
|
{
|
62
|
2
|
super.parse(context);
|
63
|
2
|
if ("clicked".equals(context.getRequestParameter(getId())))
|
64
|
|
{
|
65
|
1
|
click(context);
|
66
|
|
}
|
67
|
|
}
|
68
|
|
|
69
|
|
public static class GotoAction extends AbstractAction
|
70
|
|
{
|
71
|
|
private ViewLink link;
|
72
|
|
private String linkName;
|
73
|
|
private Controller target;
|
74
|
|
|
75
|
0
|
public GotoAction(String linkName, Controller target)
|
76
|
|
{
|
77
|
0
|
super(target.getDisplayName());
|
78
|
0
|
this.linkName = linkName;
|
79
|
0
|
this.target = target;
|
80
|
|
}
|
81
|
|
|
82
|
2
|
public GotoAction(ViewLink link, Controller target)
|
83
|
|
{
|
84
|
2
|
super(target.getDisplayName());
|
85
|
2
|
this.link = link;
|
86
|
2
|
this.target = target;
|
87
|
|
}
|
88
|
|
|
89
|
1
|
public void actionPerformed(ActionEvent evt)
|
90
|
|
{
|
91
|
1
|
logger.debug("going to " + target);
|
92
|
0
|
if (linkName != null) target.getApplication().relink(linkName, target);
|
93
|
1
|
if (link != null) link.relink(target);
|
94
|
|
}
|
95
|
|
|
96
|
6
|
public boolean isViewShown()
|
97
|
|
{
|
98
|
6
|
boolean result = false;
|
99
|
6
|
if (target == null)
|
100
|
0
|
result = false;
|
101
|
6
|
else if (linkName != null)
|
102
|
0
|
result = (target == target.getApplication().getComponent(linkName));
|
103
|
6
|
else if (link != null) result = target == link.getTarget();
|
104
|
6
|
return result;
|
105
|
|
}
|
106
|
|
|
107
|
6
|
public boolean isEnabled()
|
108
|
|
{
|
109
|
6
|
return super.isEnabled() && !isViewShown();
|
110
|
|
}
|
111
|
|
}
|
112
|
|
|
113
|
1
|
public void click(RenderContext context)
|
114
|
|
{
|
115
|
1
|
logger.info("" + this + " was clicked.");
|
116
|
1
|
actionSourceHelper.signalActionEvent(context);
|
117
|
|
}
|
118
|
|
|
119
|
4
|
public void render(RenderContext context) throws IOException
|
120
|
|
{
|
121
|
4
|
if (isVisible())
|
122
|
|
{
|
123
|
4
|
if (!isEnabled())
|
124
|
|
{
|
125
|
|
// render as disabled
|
126
|
2
|
context.startTag("SPAN id=\"" + uniqueId(context) + "\"");
|
127
|
2
|
context.println(getText());
|
128
|
2
|
context.endTag("SPAN");
|
129
|
|
}
|
130
|
|
else
|
131
|
|
{
|
132
|
2
|
String url = context.requestURL(uniqueId(context) + "=clicked");
|
133
|
2
|
context.startTag("A id=\"" + uniqueId(context) + "\" href=\"" + url + "\"");
|
134
|
2
|
context.println(getText());
|
135
|
2
|
context.endTag("A");
|
136
|
2
|
context.registerParsingComponent(this);
|
137
|
|
}
|
138
|
4
|
context.println(" ");
|
139
|
|
}
|
140
|
|
}
|
141
|
|
|
142
|
6
|
public boolean isEnabled()
|
143
|
|
{
|
144
|
6
|
return getAction().isEnabled();
|
145
|
|
}
|
146
|
|
|
147
|
14
|
public Action getAction()
|
148
|
|
{
|
149
|
14
|
return actionSourceHelper.getAction();
|
150
|
|
}
|
151
|
|
|
152
|
2
|
public void setAction(Action action)
|
153
|
|
{
|
154
|
2
|
actionSourceHelper.setAction(action);
|
155
|
|
}
|
156
|
|
|
157
|
0
|
public void addActionListener(ActionListener listener)
|
158
|
|
{
|
159
|
0
|
actionSourceHelper.addActionListener(listener);
|
160
|
|
}
|
161
|
|
|
162
|
0
|
public void removeActionListener(ActionListener listener)
|
163
|
|
{
|
164
|
0
|
actionSourceHelper.removeActionListener(listener);
|
165
|
|
}
|
166
|
|
|
167
|
0
|
public void signalActionEvent()
|
168
|
|
{
|
169
|
0
|
actionSourceHelper.signalActionEvent();
|
170
|
|
}
|
171
|
|
}
|
172
|
|
|