View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Creates a dialog.
60   * @version 2.0
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             // ignore
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         // alert if a message was set
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"); // kupu
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 
182         //using jQuery.ready() function to call dialog resizing functions only when DOM is ready. See MAGNOLIA-3846.
183         out.write("<script type=\"text/javascript\" src=\"");
184         out.write(this.getRequest().getContextPath());
185         out.write("/.resources/js/jquery/jquery-latest.min.js\"></script>\n");
186         out.write("<script type=\"text/javascript\">\n");
187         out.write("jQuery.noConflict();\n");
188         out.write("jQuery(document).ready(function($) {\n");
189         out.write("  window.onresize = eventHandlerOnResize;\n");
190         out.write("  window.resizeTo("
191                 + this.getConfigValue("width", DIALOGSIZE_NORMAL_WIDTH)
192                 + ","
193                 + this.getConfigValue("height", DIALOGSIZE_NORMAL_HEIGHT)
194                 + ");\n");
195         out.write("  mgnlDialogResizeTabs();\n");
196         out.write("  mgnlDialogShiftTab('" + this.getId() + "',false,0)\n");
197         out.write("});\n");
198         out.write("</script>\n");
199 
200         this.drawJavascriptSources(out);
201         this.drawCssSources(out);
202     }
203 
204     protected void drawHtmlPreSubsForm(Writer out) throws IOException {
205         out.write("<form action=\""
206             + this.getAction()
207             + "\" id=\"mgnlFormMain\" method=\"post\" enctype=\"multipart/form-data\"><div>\n");
208         out.write(new Hidden("mgnlDialog", this.getConfigValue("dialog"), false).getHtml());
209         out.write(new Hidden("mgnlRepository", this.getConfigValue("repository"), false).getHtml());
210         out.write(new Hidden("mgnlPath", this.getConfigValue("path"), false).getHtml());
211         out.write(new Hidden("mgnlNodeCollection", this.getConfigValue("nodeCollection"), false).getHtml());
212         out.write(new Hidden("mgnlNode", this.getConfigValue("node"), false).getHtml());
213         out.write(new Hidden("mgnlLocale", this.getConfigValue("locale"), false).getHtml());
214         out.write(new Hidden("mgnlJsCallback", this.getCallbackJavascript(), false).getHtml());
215         out.write(new Hidden("mgnlRichE", this.getConfigValue("richE"), false).getHtml());
216         out.write(new Hidden("mgnlRichEPaste", this.getConfigValue("richEPaste"), false).getHtml());
217         if (this.getConfigValue("paragraph").indexOf(",") == -1) {
218             out.write(new Hidden("mgnlParagraph", this.getConfigValue("paragraph"), false).getHtml());
219         } // else multiple paragraph selection -> radios for selection
220     }
221 
222     protected void drawHtmlPreSubsTabSet(Writer out) throws IOException {
223         String id = this.getId();
224         out.write("<script type=\"text/javascript\">");
225         out.write("mgnlControlSets['" + id + "']=new Object();");
226         out.write("mgnlControlSets['" + id + "'].items=new Array();");
227         out.write("mgnlControlSets['" + id + "'].resize=true;");
228         out.write("</script>\n");
229     }
230 
231     public void drawHtmlPostSubs(Writer out) throws IOException {
232         this.drawHtmlPostSubsTabSet(out);
233         this.drawHtmlPostSubsButtons(out);
234         out.write("</div></form></body></html>");
235         //out.write("\n<script type=\"text/javascript\">\n");
236         //calling mgnlDialogResizeTabs() as late as possible. See MAGNOLIA-3846.
237         //out.write("mgnlDialogResizeTabs();");
238         //out.write("</script>\n");
239         //out.write("</body></html>");
240     }
241 
242     protected void drawHtmlPostSubsTabSet(Writer out) throws IOException {
243         // TabSet stuff
244         String id = this.getId();
245         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETBUTTONBAR + "\">\n");
246         out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td  class=\""
247             + CssConstants.CSSCLASS_TABSETBUTTONBAR
248             + "\">");
249         if (this.getOptions().size() != 0) {
250             ButtonSet control = new ButtonSet();
251             ((Button) this.getOptions().get(0)).setState(ControlImpl.BUTTONSTATE_PUSHED);
252             control.setButtons(this.getOptions());
253             control.setName(this.getId());
254             control.setSaveInfo(false);
255             control.setButtonType(ControlImpl.BUTTONTYPE_PUSHBUTTON);
256             out.write(control.getHtml());
257         }
258         out.write("</td></tr></table>\n</div>\n");
259         //out.write("<script type=\"text/javascript\">");
260         //out.write("mgnlDialogShiftTab('" + id + "',false,0)");
261         //out.write("</script>\n");
262         // end TabSet stuff
263     }
264 
265     protected void drawHtmlPostSubsButtons(Writer out) throws IOException {
266         Messages msgs = MessagesManager.getMessages();
267 
268         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETSAVEBAR + "\">\n");
269 
270         Button save = new Button();
271         String saveOnclick = this.getConfigValue("saveOnclick", "mgnlDialogFormSubmit();");
272         String saveLabel = this.getConfigValue("saveLabel", msgs.get("buttons.save"));
273         if (StringUtils.isNotEmpty(saveOnclick) && StringUtils.isNotEmpty(saveLabel)) {
274             save.setId("mgnlSaveButton");
275             save.setOnclick(saveOnclick);
276             save.setLabel(saveLabel);
277             out.write(save.getHtml());
278         }
279         Button cancel = new Button();
280         cancel.setId("mgnlCancelButton");
281         cancel.setOnclick(this.getConfigValue("cancelOnclick", "window.close();"));
282         cancel.setLabel(this.getConfigValue("cancelLabel", msgs.get("buttons.cancel")));
283         out.write(cancel.getHtml());
284 
285         out.write("</div>\n");
286     }
287 }