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 public void drawHtmlPreSubs(Writer out) throws IOException {
152
153 out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ");
154 out.write(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
155
156 out.write("<html>");
157 out.write("<head>");
158 this.drawHtmlPreSubsHead(out);
159
160 if (AlertUtil.isMessageSet()) {
161 out.write("<script type=\"text/javascript\">mgnl.util.DHTMLUtil.addOnLoad(function(){alert('"
162 + StringEscapeUtils.escapeJavaScript(AlertUtil.getMessage())
163 + "');})</script>");
164 }
165 out.write("<script type=\"text/javascript\">mgnl.util.DHTMLUtil.addOnLoad(mgnlDialogInit);</script>");
166
167 out.write("</head>\n");
168 out.write("<body class=\"mgnlDialogBody\">\n");
169 this.drawHtmlPreSubsForm(out);
170 this.drawHtmlPreSubsTabSet(out);
171 }
172
173 protected void drawHtmlPreSubsHead(Writer out) throws IOException {
174 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n");
175
176 out.write("<title>"
177 + this.getMessage(this.getConfigValue("label", MessagesManager.get("dialog.editTitle")))
178 + "</title>\n");
179 out.write(new Sources(this.getRequest().getContextPath()).getHtmlJs());
180 out.write(new Sources(this.getRequest().getContextPath()).getHtmlCss());
181 out.write("<script type=\"text/javascript\">\n");
182
183 out.write("window.onresize = eventHandlerOnResize;\n");
184 out.write("window.resizeTo("
185 + this.getConfigValue("width", DIALOGSIZE_NORMAL_WIDTH)
186 + ","
187 + this.getConfigValue("height", DIALOGSIZE_NORMAL_HEIGHT)
188 + ");\n");
189 out.write("</script>\n");
190
191 this.drawJavascriptSources(out);
192 this.drawCssSources(out);
193 }
194
195 protected void drawHtmlPreSubsForm(Writer out) throws IOException {
196 out.write("<form action=\""
197 + this.getAction()
198 + "\" id=\"mgnlFormMain\" method=\"post\" enctype=\"multipart/form-data\"><div>\n");
199 out.write(new Hidden("mgnlDialog", this.getConfigValue("dialog"), false).getHtml());
200 out.write(new Hidden("mgnlRepository", this.getConfigValue("repository"), false).getHtml());
201 out.write(new Hidden("mgnlPath", this.getConfigValue("path"), false).getHtml());
202 out.write(new Hidden("mgnlNodeCollection", this.getConfigValue("nodeCollection"), false).getHtml());
203 out.write(new Hidden("mgnlNode", this.getConfigValue("node"), false).getHtml());
204 out.write(new Hidden("mgnlLocale", this.getConfigValue("locale"), false).getHtml());
205 out.write(new Hidden("mgnlJsCallback", this.getCallbackJavascript(), false).getHtml());
206 out.write(new Hidden("mgnlRichE", this.getConfigValue("richE"), false).getHtml());
207 out.write(new Hidden("mgnlRichEPaste", this.getConfigValue("richEPaste"), false).getHtml());
208 if (this.getConfigValue("paragraph").indexOf(",") == -1) {
209 out.write(new Hidden("mgnlParagraph", this.getConfigValue("paragraph"), false).getHtml());
210 }
211 }
212
213 protected void drawHtmlPreSubsTabSet(Writer out) throws IOException {
214 String id = this.getId();
215 out.write("<script type=\"text/javascript\">");
216 out.write("mgnlControlSets['" + id + "']=new Object();");
217 out.write("mgnlControlSets['" + id + "'].items=new Array();");
218 out.write("mgnlControlSets['" + id + "'].resize=true;");
219 out.write("</script>\n");
220 }
221
222 public void drawHtmlPostSubs(Writer out) throws IOException {
223 this.drawHtmlPostSubsTabSet(out);
224 this.drawHtmlPostSubsButtons(out);
225
226 out.write("</div></form></body></html>");
227 }
228
229 protected void drawHtmlPostSubsTabSet(Writer out) throws IOException {
230
231 String id = this.getId();
232 out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETBUTTONBAR + "\">\n");
233 out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td class=\""
234 + CssConstants.CSSCLASS_TABSETBUTTONBAR
235 + "\">");
236 if (this.getOptions().size() != 0) {
237 ButtonSet control = new ButtonSet();
238 ((Button) this.getOptions().get(0)).setState(ControlImpl.BUTTONSTATE_PUSHED);
239 control.setButtons(this.getOptions());
240 control.setName(this.getId());
241 control.setSaveInfo(false);
242 control.setButtonType(ControlImpl.BUTTONTYPE_PUSHBUTTON);
243 out.write(control.getHtml());
244 }
245 out.write("</td></tr></table>\n</div>\n");
246 out.write("<script type=\"text/javascript\">");
247 out.write("mgnlDialogResizeTabs('" + id + "');");
248 out.write("mgnlDialogShiftTab('" + id + "',false,0)");
249 out.write("</script>\n");
250
251 }
252
253 protected void drawHtmlPostSubsButtons(Writer out) throws IOException {
254 Messages msgs = MessagesManager.getMessages();
255
256 out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETSAVEBAR + "\">\n");
257
258 Button save = new Button();
259 String saveOnclick = this.getConfigValue("saveOnclick", "mgnlDialogFormSubmit();");
260 String saveLabel = this.getConfigValue("saveLabel", msgs.get("buttons.save"));
261 if (StringUtils.isNotEmpty(saveOnclick) && StringUtils.isNotEmpty("saveLabel")) {
262 save.setId("mgnlSaveButton");
263 save.setOnclick(saveOnclick);
264 save.setLabel(saveLabel);
265 out.write(save.getHtml());
266 }
267 Button cancel = new Button();
268 cancel.setId("mgnlCancelButton");
269 cancel.setOnclick(this.getConfigValue("cancelOnclick", "window.close();"));
270 cancel.setLabel(this.getConfigValue("cancelLabel", msgs.get("buttons.cancel")));
271 out.write(cancel.getHtml());
272
273 out.write("</div>\n");
274 }
275 }