1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class DialogEditCode extends DialogBox {
63
64
65
66
67 private static final String ATTRIBUTE_CODEPRESS_LOADED = "info.magnolia.cms.gui.dialog.codepress.loaded";
68
69
70
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));
75 if (this.getConfigValue("saveInfo").equals("false")) {
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"));
88 control.setCssStyles("width", this.getConfigValue("width", "100%"));
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
110 if (useCodePress && getRequest().getAttribute(ATTRIBUTE_CODEPRESS_LOADED) == null) {
111 out.write("<script type=\"text/javascript\" src=\""
112 + this.getRequest().getContextPath()
113 + "/.resources/js/codepress/codepress.js\"></script>");
114 getRequest().setAttribute(ATTRIBUTE_CODEPRESS_LOADED, "true");
115 }
116 out.write(control.getHtml());
117
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 }