1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.contrib.link; |
16 |
| |
17 |
| import org.apache.hivemind.HiveMind; |
18 |
| import org.apache.hivemind.service.BodyBuilder; |
19 |
| import org.apache.tapestry.IRequestCycle; |
20 |
| import org.apache.tapestry.PageRenderSupport; |
21 |
| import org.apache.tapestry.TapestryUtils; |
22 |
| import org.apache.tapestry.engine.ILink; |
23 |
| import org.apache.tapestry.link.DefaultLinkRenderer; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class PopupLinkRenderer extends DefaultLinkRenderer |
32 |
| { |
33 |
| |
34 |
| private String _windowName; |
35 |
| |
36 |
| private String _features; |
37 |
| |
38 |
0
| public PopupLinkRenderer()
|
39 |
| { |
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
0
| public PopupLinkRenderer(String windowName, String features)
|
51 |
| { |
52 |
0
| _windowName = windowName;
|
53 |
0
| _features = features;
|
54 |
| } |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
0
| protected String constructURL(ILink link, String anchor, IRequestCycle cycle)
|
61 |
| { |
62 |
0
| String url = link.getURL(anchor, true);
|
63 |
| |
64 |
0
| PageRenderSupport support = (PageRenderSupport) cycle
|
65 |
| .getAttribute(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE); |
66 |
| |
67 |
| |
68 |
| |
69 |
0
| String functionName = support.getUniqueString("popup_window");
|
70 |
| |
71 |
0
| BodyBuilder builder = new BodyBuilder();
|
72 |
| |
73 |
0
| builder.addln("function {0}()", functionName);
|
74 |
0
| builder.begin();
|
75 |
0
| builder.addln(
|
76 |
| "var newWindow = window.open({0}, {1}, {2});", |
77 |
| normalizeString(url), |
78 |
| normalizeString(getWindowName()), |
79 |
| normalizeString(getFeatures())); |
80 |
0
| builder.addln("newWindow.focus();");
|
81 |
0
| builder.end();
|
82 |
| |
83 |
0
| support.addBodyScript(builder.toString());
|
84 |
| |
85 |
0
| return "javascript:" + functionName + "();";
|
86 |
| } |
87 |
| |
88 |
| private static final String QUOTE = "\""; |
89 |
| |
90 |
0
| static String normalizeString(String str)
|
91 |
| { |
92 |
0
| if (HiveMind.isBlank(str))
|
93 |
0
| return QUOTE + QUOTE;
|
94 |
| |
95 |
0
| int length = str.length();
|
96 |
| |
97 |
| |
98 |
| |
99 |
0
| StringBuffer buffer = new StringBuffer(length + 2);
|
100 |
| |
101 |
0
| buffer.append(QUOTE);
|
102 |
| |
103 |
0
| for (int i = 0; i < length; i++)
|
104 |
| { |
105 |
0
| char ch = str.charAt(i);
|
106 |
| |
107 |
| |
108 |
| |
109 |
0
| if (ch == '"' || ch == '\\')
|
110 |
0
| buffer.append('\\');
|
111 |
| |
112 |
0
| buffer.append(ch);
|
113 |
| } |
114 |
| |
115 |
0
| buffer.append(QUOTE);
|
116 |
| |
117 |
0
| return buffer.toString();
|
118 |
| } |
119 |
| |
120 |
0
| public String getWindowName()
|
121 |
| { |
122 |
0
| return _windowName;
|
123 |
| } |
124 |
| |
125 |
0
| public void setWindowName(String windowName)
|
126 |
| { |
127 |
0
| _windowName = windowName;
|
128 |
| } |
129 |
| |
130 |
0
| public String getFeatures()
|
131 |
| { |
132 |
0
| return _features;
|
133 |
| } |
134 |
| |
135 |
0
| public void setFeatures(String features)
|
136 |
| { |
137 |
0
| _features = features;
|
138 |
| } |
139 |
| } |