KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > Dialog


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.gui.control.Button;
16 import info.magnolia.cms.gui.control.ButtonSet;
17 import info.magnolia.cms.gui.control.ControlImpl;
18 import info.magnolia.cms.gui.control.Hidden;
19 import info.magnolia.cms.gui.misc.CssConstants;
20 import info.magnolia.cms.gui.misc.Sources;
21 import info.magnolia.cms.i18n.Messages;
22 import info.magnolia.cms.i18n.MessagesManager;
23 import info.magnolia.cms.util.AlertUtil;
24
25 import java.io.IOException JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.commons.lang.StringUtils;
32
33
34 /**
35  * @author Vinzenz Wyser
36  * @version 2.0
37  */

38 public class Dialog extends DialogControlImpl {
39
40     public static final String JavaDoc DIALOGSIZE_NORMAL_WIDTH = "800"; //$NON-NLS-1$
41

42     public static final String JavaDoc DIALOGSIZE_NORMAL_HEIGHT = "650"; //$NON-NLS-1$
43

44     public static final String JavaDoc DIALOGSIZE_SLIM_WIDTH = "500"; //$NON-NLS-1$
45

46     public static final String JavaDoc DIALOGSIZE_SLIM_HEIGHT = "600"; //$NON-NLS-1$
47

48     private String JavaDoc callbackJavascript = "opener.document.location.reload();window.close();"; //$NON-NLS-1$
49

50     private List JavaDoc javascriptSources = new ArrayList JavaDoc();
51
52     private List JavaDoc cssSources = new ArrayList JavaDoc();
53
54     private String JavaDoc action;
55
56     /**
57      * Empty constructor should only be used by DialogFactory.
58      */

59     protected Dialog() {
60     }
61
62     public void setCallbackJavascript(String JavaDoc s) {
63         this.callbackJavascript = s;
64     }
65
66     public String JavaDoc getCallbackJavascript() {
67         return this.callbackJavascript;
68     }
69
70     public void setAction(String JavaDoc s) {
71         this.action = s;
72     }
73
74     public String JavaDoc getAction() {
75         if (this.action == null) {
76             return this.getRequest().getRequestURI();
77         }
78
79         return this.action;
80     }
81
82     public void setJavascriptSources(String JavaDoc s) {
83         this.getJavascriptSources().add(s);
84     }
85
86     public List JavaDoc getJavascriptSources() {
87         return this.javascriptSources;
88     }
89
90     public void drawJavascriptSources(Writer JavaDoc out) throws IOException JavaDoc {
91         Iterator JavaDoc it = this.getJavascriptSources().iterator();
92         while (it.hasNext()) {
93             out.write("<script type=\"text/javascript\" SRC=\"" + it.next() + "\"></script>"); //$NON-NLS-1$ //$NON-NLS-2$
94
}
95     }
96
97     public void setCssSources(String JavaDoc s) {
98         this.getCssSources().add(s);
99     }
100
101     public List JavaDoc getCssSources() {
102         return this.cssSources;
103     }
104
105     public void drawCssSources(Writer JavaDoc out) throws IOException JavaDoc {
106         Iterator JavaDoc it = this.getCssSources().iterator();
107         while (it.hasNext()) {
108             out.write("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"" + it.next() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
109
}
110     }
111
112     public DialogTab addTab() {
113         return this.addTab(StringUtils.EMPTY);
114     }
115
116     public DialogTab addTab(String JavaDoc label) {
117         DialogTab tab = new DialogTab();
118         tab.setLabel(label);
119         this.getSubs().add(tab);
120         return tab;
121     }
122
123     public DialogTab getTab(int i) {
124         return (DialogTab) this.getSubs().get(i);
125     }
126
127     public void drawHtmlPreSubs(Writer JavaDoc out) throws IOException JavaDoc {
128
129         // @todo fix html and add a good doctype. At the moment dialogs don't work in standard compliant mode
130
// out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ");
131
// out.write(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
132

133         out.write("<html>"); //$NON-NLS-1$
134
out.write("<head>"); //$NON-NLS-1$
135
this.drawHtmlPreSubsHead(out);
136         // alert if a message was set
137
if (AlertUtil.isMessageSet()) {
138             out.write("<script>mgnl.util.DHTMLUtil.addOnLoad(function(){alert('"
139                 + AlertUtil.getMessage()
140                 + "');})</script>");
141         }
142         out.write("<script>mgnl.util.DHTMLUtil.addOnLoad(mgnlDialogInit);</script>");
143
144         out.write("</head>\n"); //$NON-NLS-1$
145
out.write("<body class=\"mgnlDialogBody\">\n"); //$NON-NLS-1$
146
this.drawHtmlPreSubsForm(out);
147         this.drawHtmlPreSubsTabSet(out);
148     }
149
150     protected void drawHtmlPreSubsHead(Writer JavaDoc out) throws IOException JavaDoc {
151         out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n"); // kupu
152
// //$NON-NLS-1$
153
out.write("<title>" //$NON-NLS-1$
154
+ this.getMessage(this.getConfigValue("label", MessagesManager.get("dialog.editTitle"))) //$NON-NLS-1$ //$NON-NLS-2$
155
+ "</title>\n"); //$NON-NLS-1$
156
out.write(new Sources(this.getRequest().getContextPath()).getHtmlJs());
157         out.write(new Sources(this.getRequest().getContextPath()).getHtmlCss());
158         out.write(getHtmlKupuEditor(this.getRequest().getContextPath()));
159         out.write("<script type=\"text/javascript\">\n"); //$NON-NLS-1$
160
out.write("var mgnlRichEditors=new Array();\n"); // will be extended at each richEdit control //$NON-NLS-1$
161
out.write("var kupu = null;\n"); //$NON-NLS-1$
162
out.write("var kupuui = null;\n"); //$NON-NLS-1$
163

164         out.write("window.onresize = eventHandlerOnResize;\n"); //$NON-NLS-1$
165
out.write("window.resizeTo(" //$NON-NLS-1$
166
+ this.getConfigValue("width", DIALOGSIZE_NORMAL_WIDTH) //$NON-NLS-1$
167
+ "," //$NON-NLS-1$
168
+ this.getConfigValue("height", DIALOGSIZE_NORMAL_HEIGHT) //$NON-NLS-1$
169
+ ");\n"); //$NON-NLS-1$
170
out.write("</script>\n"); //$NON-NLS-1$
171

172         this.drawJavascriptSources(out);
173         this.drawCssSources(out);
174     }
175
176     public String JavaDoc getHtmlKupuEditor(String JavaDoc contextPath) {
177         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
178         html.append("<link rel=\"stylesheet\" type=\"text/css\" HREF=\""); //$NON-NLS-1$
179
html.append(contextPath);
180         html.append("/.resources/kupu/kupustyles.css\" />\n"); //$NON-NLS-1$
181
html.append("<link rel=\"stylesheet\" type=\"text/css\" HREF=\""); //$NON-NLS-1$
182
html.append(contextPath);
183         html.append("/.resources/kupu/kupucustom.css\" />\n"); //$NON-NLS-1$
184
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
185
html.append(contextPath);
186         html.append("/.resources/kupu/sarissa.js\"> </script>\n"); //$NON-NLS-1$
187
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
188
html.append(contextPath);
189         html.append("/.resources/kupu/kupuhelpers.js\"> </script>\n"); //$NON-NLS-1$
190
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
191
html.append(contextPath);
192         html.append("/.resources/kupu/kupueditor.js\"> </script>\n"); //$NON-NLS-1$
193
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
194
html.append(contextPath);
195         html.append("/.resources/kupu/kupubasetools.js\"> </script>\n"); //$NON-NLS-1$
196
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
197
html.append(contextPath);
198         html.append("/.resources/kupu/kupuloggers.js\"> </script>\n"); //$NON-NLS-1$
199
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
200
html.append(contextPath);
201         html.append("/.resources/kupu/kupucontentfilters.js\"> </script>\n"); //$NON-NLS-1$
202
html.append("<script type=\"text/javascript\" SRC=\""); //$NON-NLS-1$
203
html.append(contextPath);
204         html.append("/.resources/kupu/kupuinit.js\"> </script>\n"); //$NON-NLS-1$
205
return html.toString();
206     }
207
208     protected void drawHtmlPreSubsForm(Writer JavaDoc out) throws IOException JavaDoc {
209         out.write("<form action=\"" //$NON-NLS-1$
210
+ this.getAction()
211             + "\" name=\"mgnlFormMain\" method=\"post\" enctype=\"multipart/form-data\">\n"); //$NON-NLS-1$
212
out.write(new Hidden("mgnlDialog", this.getConfigValue("dialog"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
213
out.write(new Hidden("mgnlRepository", this.getConfigValue("repository"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
214
out.write(new Hidden("mgnlPath", this.getConfigValue("path"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
215
out.write(new Hidden("mgnlNodeCollection", this.getConfigValue("nodeCollection"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
216
out.write(new Hidden("mgnlNode", this.getConfigValue("node"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
217
out.write(new Hidden("mgnlJsCallback", this.getCallbackJavascript(), false).getHtml()); //$NON-NLS-1$
218
out.write(new Hidden("mgnlRichE", this.getConfigValue("richE"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
219
out.write(new Hidden("mgnlRichEPaste", this.getConfigValue("richEPaste"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
220
if (this.getConfigValue("paragraph").indexOf(",") == -1) { //$NON-NLS-1$ //$NON-NLS-2$
221
out.write(new Hidden("mgnlParagraph", this.getConfigValue("paragraph"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
222
} // else multiple paragraph selection -> radios for selection
223
}
224
225     protected void drawHtmlPreSubsTabSet(Writer JavaDoc out) throws IOException JavaDoc {
226         String JavaDoc id = this.getId();
227         out.write("<script type=\"text/javascript\">"); //$NON-NLS-1$
228
out.write("mgnlControlSets['" + id + "']=new Object();"); //$NON-NLS-1$ //$NON-NLS-2$
229
out.write("mgnlControlSets['" + id + "'].items=new Array();"); //$NON-NLS-1$ //$NON-NLS-2$
230
out.write("mgnlControlSets['" + id + "'].resize=true;"); //$NON-NLS-1$ //$NON-NLS-2$
231
out.write("</script>\n"); //$NON-NLS-1$
232
}
233
234     public void drawHtmlPostSubs(Writer JavaDoc out) throws IOException JavaDoc {
235         this.drawHtmlPostSubsTabSet(out);
236         this.drawHtmlPostSubsButtons(out);
237
238         out.write("</form></body></html>"); //$NON-NLS-1$
239
}
240
241     protected void drawHtmlPostSubsTabSet(Writer JavaDoc out) throws IOException JavaDoc {
242         // TabSet stuff
243
String JavaDoc id = this.getId();
244         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETBUTTONBAR + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
245
out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td class=\"" //$NON-NLS-1$
246
+ CssConstants.CSSCLASS_TABSETBUTTONBAR
247             + "\">"); //$NON-NLS-1$
248
if (this.getOptions().size() != 0) {
249             ButtonSet control = new ButtonSet();
250             ((Button) this.getOptions().get(0)).setState(ControlImpl.BUTTONSTATE_PUSHED);
251             control.setButtons(this.getOptions());
252             control.setName(this.getId());
253             control.setSaveInfo(false);
254             control.setButtonType(ControlImpl.BUTTONTYPE_PUSHBUTTON);
255             out.write(control.getHtml());
256         }
257         out.write("</td></tr></table>\n</div>\n"); //$NON-NLS-1$
258
out.write("<script type=\"text/javascript\">"); //$NON-NLS-1$
259
out.write("mgnlDialogResizeTabs('" + id + "');"); //$NON-NLS-1$ //$NON-NLS-2$
260
out.write("mgnlDialogShiftTab('" + id + "',false,0)"); //$NON-NLS-1$ //$NON-NLS-2$
261
out.write("</script>\n"); //$NON-NLS-1$
262
// end TabSet stuff
263
}
264
265     protected void drawHtmlPostSubsButtons(Writer JavaDoc out) throws IOException JavaDoc {
266         Messages msgs = MessagesManager.getMessages();
267
268         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETSAVEBAR + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
269

270         Button save = new Button();
271         String JavaDoc saveOnclick = this.getConfigValue("saveOnclick", "mgnlDialogFormSubmit();");
272         String JavaDoc saveLabel = this.getConfigValue("saveLabel", msgs.get("buttons.save"));
273         if (StringUtils.isNotEmpty(saveOnclick) && StringUtils.isNotEmpty("saveLabel")) {
274             save.setOnclick(saveOnclick);
275             save.setLabel(saveLabel);
276             out.write(save.getHtml());
277         }
278         Button cancel = new Button();
279         cancel.setOnclick(this.getConfigValue("cancelOnclick", "window.close();")); //$NON-NLS-1$ //$NON-NLS-2$
280
cancel.setLabel(this.getConfigValue("cancelLabel", msgs.get("buttons.cancel"))); //$NON-NLS-1$ //$NON-NLS-2$
281
out.write(cancel.getHtml());
282
283         out.write("</div>\n"); //$NON-NLS-1$
284
}
285 }
286
Popular Tags