View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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   * @author Vinzenz Wyser
60   * @version 2.0
61   */
62  public class Dialog extends DialogControlImpl {
63  
64      public static final String DIALOGSIZE_NORMAL_WIDTH = "800"; //$NON-NLS-1$
65  
66      public static final String DIALOGSIZE_NORMAL_HEIGHT = "650"; //$NON-NLS-1$
67  
68      public static final String DIALOGSIZE_SLIM_WIDTH = "500"; //$NON-NLS-1$
69  
70      public static final String DIALOGSIZE_SLIM_HEIGHT = "600"; //$NON-NLS-1$
71  
72      private String callbackJavascript = "opener.document.location.reload();window.close();"; //$NON-NLS-1$
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>"); //$NON-NLS-1$ //$NON-NLS-2$
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() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$
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>"); //$NON-NLS-1$
157         out.write("<head>"); //$NON-NLS-1$
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"); //$NON-NLS-1$
168         out.write("<body class=\"mgnlDialogBody\">\n"); //$NON-NLS-1$
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         // //$NON-NLS-1$
176         out.write("<title>" //$NON-NLS-1$
177             + this.getMessage(this.getConfigValue("label", MessagesManager.get("dialog.editTitle"))) //$NON-NLS-1$ //$NON-NLS-2$
178             + "</title>\n"); //$NON-NLS-1$
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"); //$NON-NLS-1$
182 
183         out.write("window.onresize = eventHandlerOnResize;\n"); //$NON-NLS-1$
184         out.write("window.resizeTo(" //$NON-NLS-1$
185             + this.getConfigValue("width", DIALOGSIZE_NORMAL_WIDTH) //$NON-NLS-1$
186             + "," //$NON-NLS-1$
187             + this.getConfigValue("height", DIALOGSIZE_NORMAL_HEIGHT) //$NON-NLS-1$
188             + ");\n"); //$NON-NLS-1$
189         out.write("</script>\n"); //$NON-NLS-1$
190 
191         this.drawJavascriptSources(out);
192         this.drawCssSources(out);
193     }
194 
195     protected void drawHtmlPreSubsForm(Writer out) throws IOException {
196         out.write("<form action=\"" //$NON-NLS-1$
197             + this.getAction()
198             + "\" id=\"mgnlFormMain\" method=\"post\" enctype=\"multipart/form-data\"><div>\n"); //$NON-NLS-1$
199         out.write(new Hidden("mgnlDialog", this.getConfigValue("dialog"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
200         out.write(new Hidden("mgnlRepository", this.getConfigValue("repository"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
201         out.write(new Hidden("mgnlPath", this.getConfigValue("path"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
202         out.write(new Hidden("mgnlNodeCollection", this.getConfigValue("nodeCollection"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
203         out.write(new Hidden("mgnlNode", this.getConfigValue("node"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
204         out.write(new Hidden("mgnlLocale", this.getConfigValue("locale"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
205         out.write(new Hidden("mgnlJsCallback", this.getCallbackJavascript(), false).getHtml()); //$NON-NLS-1$
206         out.write(new Hidden("mgnlRichE", this.getConfigValue("richE"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
207         out.write(new Hidden("mgnlRichEPaste", this.getConfigValue("richEPaste"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
208         if (this.getConfigValue("paragraph").indexOf(",") == -1) { //$NON-NLS-1$ //$NON-NLS-2$
209             out.write(new Hidden("mgnlParagraph", this.getConfigValue("paragraph"), false).getHtml()); //$NON-NLS-1$ //$NON-NLS-2$
210         } // else multiple paragraph selection -> radios for selection
211     }
212 
213     protected void drawHtmlPreSubsTabSet(Writer out) throws IOException {
214         String id = this.getId();
215         out.write("<script type=\"text/javascript\">"); //$NON-NLS-1$
216         out.write("mgnlControlSets['" + id + "']=new Object();"); //$NON-NLS-1$ //$NON-NLS-2$
217         out.write("mgnlControlSets['" + id + "'].items=new Array();"); //$NON-NLS-1$ //$NON-NLS-2$
218         out.write("mgnlControlSets['" + id + "'].resize=true;"); //$NON-NLS-1$ //$NON-NLS-2$
219         out.write("</script>\n"); //$NON-NLS-1$
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>"); //$NON-NLS-1$
227     }
228 
229     protected void drawHtmlPostSubsTabSet(Writer out) throws IOException {
230         // TabSet stuff
231         String id = this.getId();
232         out.write("<div class=\"" + CssConstants.CSSCLASS_TABSETBUTTONBAR + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
233         out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td  class=\"" //$NON-NLS-1$
234             + CssConstants.CSSCLASS_TABSETBUTTONBAR
235             + "\">"); //$NON-NLS-1$
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"); //$NON-NLS-1$
246         out.write("<script type=\"text/javascript\">"); //$NON-NLS-1$
247         out.write("mgnlDialogResizeTabs('" + id + "');"); //$NON-NLS-1$ //$NON-NLS-2$
248         out.write("mgnlDialogShiftTab('" + id + "',false,0)"); //$NON-NLS-1$ //$NON-NLS-2$
249         out.write("</script>\n"); //$NON-NLS-1$
250         // end TabSet stuff
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"); //$NON-NLS-1$ //$NON-NLS-2$
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();")); //$NON-NLS-1$ //$NON-NLS-2$
270         cancel.setLabel(this.getConfigValue("cancelLabel", msgs.get("buttons.cancel"))); //$NON-NLS-1$ //$NON-NLS-2$
271         out.write(cancel.getHtml());
272 
273         out.write("</div>\n"); //$NON-NLS-1$
274     }
275 }