View Javadoc

1   /**
2    * This file Copyright (c) 2008-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.Edit;
37  import info.magnolia.cms.gui.misc.CssConstants;
38  import info.magnolia.cms.util.BooleanUtil;
39  
40  import java.io.IOException;
41  import java.io.Writer;
42  
43  import javax.jcr.PropertyType;
44  
45  
46  /**
47   * Turns a textarea into a basic code editor with the <a href="http://codepress.sourceforge.net/">CodePress</a> js
48   * library.
49   * <p>
50   * Configuration options are:
51   * <ul>
52   * <li><strong>useCodeHighlighter</strong>: activate/deactivate the editor with code highlighting. Default value is
53   * <code>true</code>. If set as <code>false</code>, falls back to a plain textarea.
54   * <li><strong>language</strong>: one of the languages (e.g. <em>css</em>, <em>javascript</em>, <em>html</em>) supported
55   * by CodePress. Default value is <code>generic</code>.
56   * <li><strong>readOnly</strong>: make the editor read-only. Default value is <code>false</code>.
57   * <li><strong>lineNumbers</strong>: shows/hide line numbers. Default value is <code>true</code>.
58   * </ul>
59   * @author tmiyar
60   * @author fgrilli
61   */
62  public class DialogEditCode extends DialogBox {
63  
64      /**
65       * Used to make sure that the javascript files are loaded only once
66       */
67      private static final String ATTRIBUTE_CODEPRESS_LOADED = "info.magnolia.cms.gui.dialog.codepress.loaded";
68  
69      /**
70       * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
71       */
72      public void drawHtml(Writer out) throws IOException {
73          Edit control = new Edit(this.getName(), this.getValue());
74          control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); //$NON-NLS-1$
75          if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
76              control.setSaveInfo(false);
77          }
78  
79          boolean isBrowserSupported = false;
80          String userAgent = getRequest().getHeader("user-agent");
81          if (userAgent != null && !userAgent.matches(".*AppleWebKit.*|.*Opera.*")) {
82              isBrowserSupported = true;
83          }
84          boolean useCodePress = BooleanUtil.toBoolean(this.getConfigValue("useCodeHighlighter"), true)
85              && isBrowserSupported;
86          if (useCodePress) {
87              control.setRows(this.getConfigValue("rows", "25")); //$NON-NLS-1$ //$NON-NLS-2$
88              control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89              StringBuilder codePressClasses = new StringBuilder(" codepress ");
90              codePressClasses.append(this.getConfigValue("language", "generic "));
91              boolean readOnly = BooleanUtil.toBoolean(this.getConfigValue("readOnly"), false);
92              boolean lineNumbers = BooleanUtil.toBoolean(this.getConfigValue("lineNumbers"), true);
93              codePressClasses.append(readOnly ? " readonly-on " : " readonly-off ");
94              codePressClasses.append(lineNumbers ? " linenumbers-on " : " linenumbers-off ");
95              control.setCssClass(CssConstants.CSSCLASS_EDIT + codePressClasses.toString());
96          }
97          else {
98              control.setCssClass(CssConstants.CSSCLASS_EDIT);
99              control.setRows(this.getConfigValue("rows", "1"));
100             control.setCssStyles("width", this.getConfigValue("width", "100%"));
101             control.setCssStyles("font-family", "Courier New, monospace");
102             control.setCssStyles("font-size", "14px");
103             if (this.getConfigValue("onchange", null) != null) {
104                 control.setEvent("onchange", this.getConfigValue("onchange"));
105             }
106         }
107 
108         this.drawHtmlPre(out);
109         // load the script once if there are multiple instances
110         if (useCodePress && getRequest().getAttribute(ATTRIBUTE_CODEPRESS_LOADED) == null) {
111             out.write("<script type=\"text/javascript\" src=\"" //$NON-NLS-1$
112                 + this.getRequest().getContextPath()
113                 + "/.resources/js/codepress/codepress.js\"></script>"); //$NON-NLS-1$
114             getRequest().setAttribute(ATTRIBUTE_CODEPRESS_LOADED, "true"); //$NON-NLS-1$
115         }
116         out.write(control.getHtml());
117         // on submit put the code into a hidden field. OK, this is really ugly.
118         if (useCodePress) {
119             out.write("\n<script>\n");
120             out.write("MgnlDHTMLUtil.addOnLoad(function(){\n");
121             out.write("    var b = document.getElementById('mgnlSaveButton');\n");
122             out.write("    var existingOnClick = b.onclick;\n");
123             out.write("    b.onclick=function(){\n");
124             out.write("        document.getElementById('cp_hidden_"
125                 + this.getName()
126                 + "').value = eval('"
127                 + this.getName()
128                 + "').getCode();\n");
129             out.write("        existingOnClick.apply(this);\n");
130             out.write("    }\n});\n");
131             out.write("</script>\n");
132             out.write("<input type=\"hidden\" name=\"");
133             out.write(this.getName());
134             out.write("\" id=\"cp_hidden_" + this.getName() + "\" />\n");
135         }
136         this.drawHtmlPost(out);
137     }
138 }