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 package info.magnolia.cms.gui.dialog;
35
36 import info.magnolia.cms.gui.control.Button;
37 import info.magnolia.cms.gui.control.ButtonSet;
38 import info.magnolia.cms.gui.control.ControlImpl;
39 import info.magnolia.cms.gui.control.Hidden;
40 import info.magnolia.cms.gui.misc.CssConstants;
41 import info.magnolia.cms.gui.misc.Sources;
42 import info.magnolia.cms.i18n.Messages;
43 import info.magnolia.cms.i18n.MessagesManager;
44 import info.magnolia.cms.util.AlertUtil;
45
46 import java.io.IOException;
47 import java.io.Writer;
48 import java.util.ArrayList;
49 import java.util.Iterator;
50 import java.util.List;
51
52 import javax.jcr.RepositoryException;
53
54 import org.apache.commons.lang.StringEscapeUtils;
55 import org.apache.commons.lang.StringUtils;
56
57
58
59
60
61
62 public class Dialog extends DialogControlImpl {
63
64 public static final String DIALOGSIZE_NORMAL_WIDTH = "800";
65
66 public static final String DIALOGSIZE_NORMAL_HEIGHT = "650";
67
68 public static final String DIALOGSIZE_SLIM_WIDTH = "500";
69
70 public static final String DIALOGSIZE_SLIM_HEIGHT = "600";
71
72 private String callbackJavascript = "opener.document.location.reload();window.close();";
73
74 private List javascriptSources = new ArrayList();
75
76 private List cssSources = new ArrayList();
77
78 private String action;
79
80 public void setCallbackJavascript(String s) {
81 this.callbackJavascript = s;
82 }
83
84 public String getCallbackJavascript() {
85 return this.callbackJavascript;
86 }
87
88 public void setAction(String s) {
89 this.action = s;
90 }
91
92 public String getAction() {
93 if (this.action == null) {
94 return this.getRequest().getRequestURI();
95 }
96
97 return this.action;
98 }
99
100 public void setJavascriptSources(String s) {
101 this.getJavascriptSources().add(s);
102 }
103
104 public List getJavascriptSources() {
105 return this.javascriptSources;
106 }
107
108 public void drawJavascriptSources(Writer out) throws IOException {
109 Iterator it = this.getJavascriptSources().iterator();
110 while (it.hasNext()) {
111 out.write("<script type=\"text/javascript\" src=\"" + it.next() + "\"></script>");
112 }
113 }
114
115 public void setCssSources(String s) {
116 this.getCssSources().add(s);
117 }
118
119 public List getCssSources() {
120 return this.cssSources;
121 }
122
123 public void drawCssSources(Writer out) throws IOException {
124 Iterator it = this.getCssSources().iterator();
125 while (it.hasNext()) {
126 out.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + it.next() + "\"/>");
127 }
128 }
129
130 public DialogTab addTab() {
131 return this.addTab(StringUtils.EMPTY);
132 }
133
134 public DialogTab addTab(String label) {
135 DialogTab tab = new DialogTab();
136 try {
137 tab.init(getRequest(), getResponse(), null, null);
138 }
139 catch (RepositoryException e) {
140
141 }
142 tab.setLabel(label);
143 this.getSubs().add(tab);
144 return tab;
145 }
146
147 public DialogTab getTab(int i) {
148 return (DialogTab) this.getSubs().get(i);
149 }
150
151 @Override
152 public void drawHtmlPreSubs(Writer out) throws IOException {
153
154 out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ");
155 out.write(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
156
157 out.write("<html>");
158 out.write("<head>");
159 this.drawHtmlPreSubsHead(out);
160
161 if (AlertUtil.isMessageSet()) {
162 out.write("<script type=\"text/javascript\">mgnl.util.DHTMLUtil.addOnLoad(function(){alert('"
163 + StringEscapeUtils.escapeJavaScript(AlertUtil.getMessage())
164 + "');})</script>");
165 }
166 out.write("<script type=\"text/javascript\">mgnl.util.DHTMLUtil.addOnLoad(mgnlDialogInit);</script>");
167
168 out.write("</head>\n");
169 out.write("<body class=\"mgnlDialogBody\">\n");
170 this.drawHtmlPreSubsForm(out);
171 this.drawHtmlPreSubsTabSet(out);
172 }
173
174 protected void drawHtmlPreSubsHead(Writer out) throws IOException {
175 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n");
176
177 out.write("<title>"
178 + this.getMessage(this.getConfigValue("label", MessagesManager.get("dialog.editTitle")))
179 + "</title>\n");
180 out.write(new Sources(this.getRequest().getContextPath()).getHtmlJs());
181 out.write(new Sources(this.getRequest().getContextPath()).getHtmlCss());
182
183
184 out.write("<script type=\"text/javascript\" src=\"");
185 out.write(this.getRequest().getContextPath());
186 out.write("/.resources/js/jquery/jquery-latest.min.js\"></script>\n");
187 out.write("<script type=\"text/javascript\">\n");
188 out.write("jQuery.noConflict();\n");
189 out.write("jQuery(document).ready(function($) {\n");
190 out.write(" window.onresize = eventHandlerOnResize;\n");
191 out.write(" window.resizeTo("
192 + this.getConfigValue("width", DIALOGSIZE_NORMAL_WIDTH)
193 + ","
194 + this.getConfigValue("height", DIALOGSIZE_NORMAL_HEIGHT)
195 + ");\n");
196 out.write(" mgnlDialogResizeTabs();\n");
197 out.write(" mgnlDialogShiftTab('" + this.getId() + "',false,0)\n");
198 out.write("});\n");
199 out.write("</script>\n");
200
201 this.drawJavascriptSources(out);
202 this.drawCssSources(out);
203 }
204
205 protected void drawHtmlPreSubsForm(Writer out) throws IOException {
206 out.write("<form action=\""
207 + this.getAction()
208 + "\" id=\"mgnlFormMain\" method=\"post\" enctype=\"multipart/form-data\"><div>\n");
209 out.write(new Hidden("mgnlDialog", this.getConfigValue("dialog"), false).getHtml());
210 out.write(new Hidden("mgnlRepository", this.getConfigValue("repository"), false).getHtml());
211 out.write(new Hidden("mgnlPath", this.getConfigValue("path"), false).getHtml());
212 out.write(new Hidden("mgnlTreeBasePath", this.getConfigValue("mgnlTreeBasePath"), false).getHtml());
213 out.write(new Hidden("mgnlNodeCollection", this.getConfigValue("nodeCollection"), false).getHtml());
214 out.write(new Hidden("mgnlNode", this.getConfigValue("node"), false).getHtml());
215 out.write(new Hidden("mgnlLocale", this.getConfigValue("locale"), false).getHtml());
216 out.write(new Hidden("mgnlJsCallback", this.getCallbackJavascript(), false).getHtml());
217 out.write(new Hidden("mgnlRichE", this.getConfigValue("richE"), false).getHtml());
218 out.write(new Hidden("mgnlRichEPaste", this.getConfigValue("richEPaste"), false).getHtml());
219 if (this.getConfigValue("paragraph").indexOf(",") == -1) {
220 out.write(new Hidden("mgnlParagraph", this.getConfigValue("paragraph"), false).getHtml());
221 }
222 if (StringUtils.isNotEmpty(this.getConfigValue("collectionNodeCreationItemType"))) {
223 out.write(new Hidden("mgnlCollectionNodeCreationItemType", this.getConfigValue("collectionNodeCreationItemType"), false).getHtml());
224 }
225 if (StringUtils.isNotEmpty(this.getConfigValue("creationItemType"))) {
226 out.write(new Hidden("mgnlCreationItemType", this.getConfigValue("creationItemType"), false).getHtml());
227 }
228 }
229
230 protected void drawHtmlPreSubsTabSet(Writer out) throws IOException {
231 String id = this.getId();
232 out.write("<script type=\"text/javascript\">");
233 out.write("mgnlControlSets['" + id + "']=new Object();");
234 out.write("mgnlControlSets['" + id + "'].items=new Array();");
235 out.write("mgnlControlSets['" + id + "'].resize=true;");
236 out.write("</script>\n");
237 }
238
239 @Override
240 public void drawHtmlPostSubs(Writer out) throws IOException {
241 this.drawHtmlPostSubsTabSet(out);
242 this.drawHtmlPostSubsButtons(out);
243 out.write("</div></form></body></html>");
244
245
246
247
248
249 }
250
251 protected void drawHtmlPostSubsTabSet(Writer out) throws IOException {
252
253 String id = this.getId();
254 out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETBUTTONBAR + "\">\n");
255 out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td class=\""
256 + CssConstants.CSSCLASS_TABSETBUTTONBAR
257 + "\">");
258 if (this.getOptions().size() != 0) {
259 ButtonSet control = new ButtonSet();
260 ((Button) this.getOptions().get(0)).setState(ControlImpl.BUTTONSTATE_PUSHED);
261 control.setButtons(this.getOptions());
262 control.setName(this.getId());
263 control.setSaveInfo(false);
264 control.setButtonType(ControlImpl.BUTTONTYPE_PUSHBUTTON);
265 out.write(control.getHtml());
266 }
267 out.write("</td></tr></table>\n</div>\n");
268
269
270
271
272 }
273
274 protected void drawHtmlPostSubsButtons(Writer out) throws IOException {
275 Messages msgs = MessagesManager.getMessages();
276
277 out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETSAVEBAR + "\">\n");
278
279 Button save = new Button();
280 String saveOnclick = this.getConfigValue("saveOnclick", "mgnlDialogFormSubmit();");
281 String saveLabel = this.getConfigValue("saveLabel", msgs.get("buttons.save"));
282 if (StringUtils.isNotEmpty(saveOnclick) && StringUtils.isNotEmpty(saveLabel)) {
283 save.setId("mgnlSaveButton");
284 save.setOnclick(saveOnclick);
285 save.setLabel(saveLabel);
286 out.write(save.getHtml());
287 }
288 Button cancel = new Button();
289 cancel.setId("mgnlCancelButton");
290 cancel.setOnclick(this.getConfigValue("cancelOnclick", "window.close();"));
291 cancel.setLabel(this.getConfigValue("cancelLabel", msgs.get("buttons.cancel")));
292 out.write(cancel.getHtml());
293
294 out.write("</div>\n");
295 }
296 }