View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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     @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         // alert if a message was set
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"); // kupu
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         //using jQuery.ready() function to call dialog resizing functions only when DOM is ready. See MAGNOLIA-3846.
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("mgnlNodeCollection", this.getConfigValue("nodeCollection"), false).getHtml());
213         out.write(new Hidden("mgnlNode", this.getConfigValue("node"), false).getHtml());
214         out.write(new Hidden("mgnlLocale", this.getConfigValue("locale"), false).getHtml());
215         out.write(new Hidden("mgnlJsCallback", this.getCallbackJavascript(), false).getHtml());
216         out.write(new Hidden("mgnlRichE", this.getConfigValue("richE"), false).getHtml());
217         out.write(new Hidden("mgnlRichEPaste", this.getConfigValue("richEPaste"), false).getHtml());
218         if (this.getConfigValue("paragraph").indexOf(",") == -1) {
219             out.write(new Hidden("mgnlParagraph", this.getConfigValue("paragraph"), false).getHtml());
220         } // else multiple paragraph selection -> radios for selection
221         if (StringUtils.isNotEmpty(this.getConfigValue("collectionNodeCreationItemType"))) {
222             out.write(new Hidden("mgnlCollectionNodeCreationItemType", this.getConfigValue("collectionNodeCreationItemType"), false).getHtml());
223         }
224         if (StringUtils.isNotEmpty(this.getConfigValue("creationItemType"))) {
225             out.write(new Hidden("mgnlCreationItemType", this.getConfigValue("creationItemType"), false).getHtml());
226         }
227     }
228 
229     protected void drawHtmlPreSubsTabSet(Writer out) throws IOException {
230         String id = this.getId();
231         out.write("<script type=\"text/javascript\">");
232         out.write("mgnlControlSets['" + id + "']=new Object();");
233         out.write("mgnlControlSets['" + id + "'].items=new Array();");
234         out.write("mgnlControlSets['" + id + "'].resize=true;");
235         out.write("</script>\n");
236     }
237 
238     @Override
239     public void drawHtmlPostSubs(Writer out) throws IOException {
240         this.drawHtmlPostSubsTabSet(out);
241         this.drawHtmlPostSubsButtons(out);
242         out.write("</div></form></body></html>");
243         //out.write("\n<script type=\"text/javascript\">\n");
244         //calling mgnlDialogResizeTabs() as late as possible. See MAGNOLIA-3846.
245         //out.write("mgnlDialogResizeTabs();");
246         //out.write("</script>\n");
247         //out.write("</body></html>");
248     }
249 
250     protected void drawHtmlPostSubsTabSet(Writer out) throws IOException {
251         // TabSet stuff
252         String id = this.getId();
253         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETBUTTONBAR + "\">\n");
254         out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td  class=\""
255             + CssConstants.CSSCLASS_TABSETBUTTONBAR
256             + "\">");
257         if (this.getOptions().size() != 0) {
258             ButtonSet control = new ButtonSet();
259             ((Button) this.getOptions().get(0)).setState(ControlImpl.BUTTONSTATE_PUSHED);
260             control.setButtons(this.getOptions());
261             control.setName(this.getId());
262             control.setSaveInfo(false);
263             control.setButtonType(ControlImpl.BUTTONTYPE_PUSHBUTTON);
264             out.write(control.getHtml());
265         }
266         out.write("</td></tr></table>\n</div>\n");
267         //out.write("<script type=\"text/javascript\">");
268         //out.write("mgnlDialogShiftTab('" + id + "',false,0)");
269         //out.write("</script>\n");
270         // end TabSet stuff
271     }
272 
273     protected void drawHtmlPostSubsButtons(Writer out) throws IOException {
274         Messages msgs = MessagesManager.getMessages();
275 
276         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETSAVEBAR + "\">\n");
277 
278         Button save = new Button();
279         String saveOnclick = this.getConfigValue("saveOnclick", "mgnlDialogFormSubmit();");
280         String saveLabel = this.getConfigValue("saveLabel", msgs.get("buttons.save"));
281         if (StringUtils.isNotEmpty(saveOnclick) && StringUtils.isNotEmpty(saveLabel)) {
282             save.setId("mgnlSaveButton");
283             save.setOnclick(saveOnclick);
284             save.setLabel(saveLabel);
285             out.write(save.getHtml());
286         }
287         Button cancel = new Button();
288         cancel.setId("mgnlCancelButton");
289         cancel.setOnclick(this.getConfigValue("cancelOnclick", "window.close();"));
290         cancel.setLabel(this.getConfigValue("cancelLabel", msgs.get("buttons.cancel")));
291         out.write(cancel.getHtml());
292 
293         out.write("</div>\n");
294     }
295 }