01: package org.osbl.client.wings;
02:
03: import org.wings.*;
04:
05: import java.util.regex.Pattern;
06: import java.util.regex.Matcher;
07: import java.net.URLEncoder;
08: import java.io.UnsupportedEncodingException;
09:
10: public class XLabel extends SLabel implements LowLevelEventListener {
11: private static final Pattern PATTERN = Pattern
12: .compile("\\[[^\\[\\]]+\\]");
13:
14: String rawText;
15: boolean ajaxLinking;
16: private String event;
17:
18: public boolean isAjaxLinking() {
19: return ajaxLinking;
20: }
21:
22: public void setAjaxLinking(boolean ajaxLinking) {
23: this .ajaxLinking = ajaxLinking;
24: setText(parse(rawText));
25: }
26:
27: public void setXText(String rawText) {
28: this .rawText = rawText;
29: setText(parse(rawText));
30: }
31:
32: protected String parse(String xText) {
33: if (xText == null)
34: return null;
35:
36: StringBuffer buffer = new StringBuffer("<html>");
37:
38: Matcher matcher = PATTERN.matcher(xText);
39: while (matcher.find()) {
40: String link = xText.substring(matcher.start() + 1, matcher
41: .end() - 1);
42: int pos = link.indexOf(' ');
43: if (pos != -1)
44: matcher.appendReplacement(buffer, "<a href=\""
45: + encode(link.substring(0, pos)) + "\">"
46: + link.substring(pos + 1) + "</a>");
47: else
48: matcher.appendReplacement(buffer, "<a href=\""
49: + encode(link) + "\">" + link + "</a>");
50: }
51: matcher.appendTail(buffer);
52:
53: return buffer.toString();
54: }
55:
56: private String encode(String link) {
57: if (ajaxLinking) {
58: int pos = link.indexOf('?');
59: if (pos != -1)
60: link = link.substring(pos + 1);
61: try {
62: link = URLEncoder.encode(link, "UTF-8");
63: } catch (UnsupportedEncodingException e) {
64: e.printStackTrace();
65: }
66:
67: return "javascript:wingS.request.sendEvent(null,true,true,'"
68: + getName() + "', '1&" + link + "');";
69: } else
70: return link;
71: }
72:
73: public void processLowLevelEvent(String name, String[] values) {
74: event = values[0];
75: SForm.addArmedComponent(this );
76: }
77:
78: public void fireIntermediateEvents() {
79: }
80:
81: public void fireFinalEvents() {
82: super .fireFinalEvents();
83: if (event != null) {
84: handleEvent();
85: event = null;
86: }
87: }
88:
89: protected void handleEvent() {
90: }
91:
92: public boolean isEpochCheckEnabled() {
93: return false;
94: }
95: }
|