KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > taglib > Module


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.taglib;
24
25 import java.io.*;
26 import java.text.*;
27 import java.util.*;
28 import javax.servlet.*;
29 import javax.servlet.jsp.*;
30 import org.meshcms.core.*;
31 import org.meshcms.util.*;
32 import org.meshcms.webui.*;
33
34 /**
35  * Writes a module or the needed fields to edit it.
36  */

37 public final class Module extends AbstractTag {
38   public static final String JavaDoc DATE_NONE = "none";
39   public static final String JavaDoc DATE_NORMAL = "normal";
40   public static final String JavaDoc DATE_FULL = "full";
41
42   private String JavaDoc name;
43   private String JavaDoc date;
44   private String JavaDoc style;
45   private String JavaDoc location = "";
46   private String JavaDoc alt = " ";
47   private String JavaDoc parameters;
48
49   public void setName(String JavaDoc name) {
50     this.name = name;
51   }
52
53   public void setDate(String JavaDoc date) {
54     this.date = date;
55   }
56
57   public void setStyle(String JavaDoc style) {
58     this.style = style;
59   }
60
61   public void setLocation(String JavaDoc location) {
62     this.location = location;
63   }
64
65   public void setAlt(String JavaDoc alt) {
66     this.alt = alt;
67   }
68
69   public void writeTag() throws IOException, JspException {
70     if (date == null) {
71       date = DATE_NONE;
72     }
73
74     ModuleDescriptor md = getModuleDescriptor(location, name);
75
76     if (md != null) {
77       if (parameters != null) {
78         md.parseParameters(parameters);
79       }
80
81       Path modulePath = webSite.getModulePath(md.getTemplate());
82
83       if (modulePath != null) {
84         md.setPagePath(pagePath);
85         md.setModulePath(modulePath);
86         md.setDateFormat(date);
87         md.setStyle(style);
88
89         String JavaDoc moduleCode = "meshcmsmodule_" + location;
90         request.setAttribute(moduleCode, md);
91
92         try {
93           pageContext.include("/" + webSite.getServedPath(modulePath) + "/" +
94               SiteMap.MODULE_INCLUDE_FILE + "?modulecode=" + moduleCode);
95         } catch (ServletException ex) {
96           WebUtils.setBlockCache(request);
97           webSite.log("Exception while including module " + modulePath, ex);
98
99           if (!webSite.getConfiguration().isHideExceptions()) {
100             Writer w = getOut();
101             PrintWriter pw = new PrintWriter(w);
102             w.write("<pre class='meshcmserror'>\n");
103             ex.printStackTrace(pw);
104
105             if (ex instanceof ServletException) {
106               Throwable JavaDoc t = ((ServletException) ex).getRootCause();
107
108               if (t != null) {
109                 t.printStackTrace(pw);
110               }
111             }
112
113             w.write("</pre>");
114           }
115         }
116       }
117     } else {
118       getOut().write(alt);
119     }
120   }
121
122   public void writeEditTag() throws IOException, JspException {
123     String JavaDoc uniqueHash = Integer.toString(new Object JavaDoc().hashCode());
124     String JavaDoc tagIdPrefix = "meshcmsmodule_"+ location +"_"+ uniqueHash +"_";
125     String JavaDoc idCont = tagIdPrefix +"cont";
126     String JavaDoc idElem = tagIdPrefix +"elem";
127     String JavaDoc idIcon = tagIdPrefix +"icon";
128     boolean isEditorModulesCollapsed = webSite.getConfiguration().isEditorModulesCollapsed();
129
130     String JavaDoc template = null;
131     String JavaDoc argPath = null;
132     String JavaDoc advParms = null;
133
134     ModuleDescriptor md = getModuleDescriptor(location, name);
135
136     if (md != null) {
137       template = md.getTemplate();
138       argPath = md.getArgument();
139       advParms = Utils.listProperties(md.getAdvancedParams(), ", ");
140     }
141
142     Locale locale = WebUtils.getPageLocale(pageContext);
143     ResourceBundle bundle = ResourceBundle.getBundle("org/meshcms/webui/Locales", locale);
144     MessageFormat formatter = new MessageFormat("", locale);
145
146     Writer w = getOut();
147
148     Object JavaDoc[] args = {
149       location,
150       template != null ? Utils.beautify(template,true) : bundle.getString("editorNoTemplate"),
151       Utils.noNull(argPath),
152       Utils.noNull(advParms)
153     };
154
155     if (isEditorModulesCollapsed) {
156       w.write("<div id=\"" + idCont + "\" class='meshcmsfieldlabel' " +
157           " style=\"cursor:pointer;position:relative;\" onclick=\"javascript:editor_moduleShow('" +
158           idCont + "','" + idElem + "','" + idIcon + "');\">" +
159           "<img alt=\"\" SRC=\"" + afp + "/images/tree_plus.gif\" id=\"" + idIcon + "\" />\n");
160       formatter.applyPattern(bundle.getString("editorModuleLocExt"));
161       w.write("<label for=\"" + idElem + "\">" + formatter.format(args) + "</label>");
162       w.write("</div>");
163     }
164
165     w.write("<fieldset "+ (isEditorModulesCollapsed ? "style=\"display:none;\"" : "") +
166         " id=\""+ idElem +"\" class='meshcmseditor' >\n");
167     formatter.applyPattern(bundle.getString("editorModuleLoc"));
168     w.write(" <legend>" + formatter.format(args) + "</legend>\n");
169
170     if (name != null) {
171       w.write(bundle.getString("editorFixedModule"));
172
173       if (argPath != null) {
174         w.write("<img alt=\"\" SRC='" + afp + "/images/small_browse.gif' title='" +
175           bundle.getString("editorBrowseModule") +
176           "' onclick=\"javascript:window.open('" +
177           afp + "/filemanager/index.jsp?folder=" +
178           Utils.escapeSingleQuotes(argPath) +
179           "', '_blank').focus();\" style='vertical-align:middle;' />\n");
180       }
181     } else {
182       w.write(" <div class='meshcmsfieldlabel'><label for='" +
183         ModuleDescriptor.TEMPLATE_ID + location + "'>" +
184         bundle.getString("editorModuleTemplate") + "</label></div>\n");
185       w.write(" <div class='meshcmsfield'>\n <select name='" +
186         ModuleDescriptor.TEMPLATE_ID + location + "' id='" +
187         ModuleDescriptor.TEMPLATE_ID + location + "'>\n");
188       w.write(" <option value='" + PageAssembler.EMPTY + "'>" +
189         bundle.getString("editorNoTemplate") + "</option>\n");
190
191       String JavaDoc[] mtNames = webSite.getSiteMap().getModuleNames();
192
193       for (int i = 0; i < mtNames.length; i++) {
194         w.write(" <option value='" + mtNames[i] + "'");
195
196         if (md != null && mtNames[i].equals(template)) {
197           w.write(" selected='selected'");
198         }
199
200         w.write(">" + Utils.beautify(Utils.removeExtension(mtNames[i]), true) + "</option>\n");
201       }
202
203       w.write(" </select>&nbsp;");
204       w.write(Help.icon(webSite, cp, Help.MODULES, userInfo, "module_'+document.getElementById('" +
205           ModuleDescriptor.TEMPLATE_ID + location + "').value+'", true));
206       w.write("\n </div>\n");
207       w.write(" <div class='meshcmsfieldlabel'><label for='" +
208         ModuleDescriptor.ARGUMENT_ID + location + "'>" +
209         bundle.getString("editorModuleArgument") + "</label></div>\n");
210       w.write(" <div class='meshcmsfield'><img alt=\"\" SRC='" + afp +
211         "/images/clear_field.gif' onclick=\"javascript:editor_clr('" +
212         ModuleDescriptor.ARGUMENT_ID + location + "');\" style='vertical-align:middle;' /><input type='text' id='" +
213         ModuleDescriptor.ARGUMENT_ID + location + "' name='" +
214         ModuleDescriptor.ARGUMENT_ID + location + "' value=\"" +
215         (md == null || argPath == null ? "" : argPath) +
216         "\" style='width: 80%;' /><img alt=\"\" SRC='" + afp +
217         "/images/small_browse.gif' title='" + bundle.getString("genericBrowse") +
218         "' onclick=\"javascript:editor_openFileManager('" +
219         ModuleDescriptor.ARGUMENT_ID + location + "');\" style='vertical-align:middle;' /></div>\n");
220
221       w.write(" <div class='meshcmsfieldlabel'><label for='" +
222         ModuleDescriptor.PARAMETERS_ID + location + "'>" +
223         bundle.getString("editorModuleParameters") + "</label></div>\n");
224       w.write(" <div class='meshcmsfield'><img alt=\"\" SRC='" + afp +
225         "/images/clear_field.gif' onclick=\"javascript:editor_clr('" +
226         ModuleDescriptor.PARAMETERS_ID + location + "');\" style='vertical-align:middle;' /><input type='text' id='" +
227         ModuleDescriptor.PARAMETERS_ID + location + "' name='" +
228         ModuleDescriptor.PARAMETERS_ID + location + "' value=\"" +
229         (md == null || md.getAdvancedParams() == null ? "" :
230         Utils.listProperties(md.getAdvancedParams(), ", ")) +
231         "\" style='width: 80%;' /></div>\n");
232     }
233
234     w.write("</fieldset>");
235   }
236
237   public String JavaDoc getName() {
238     return name;
239   }
240
241   public String JavaDoc getDate() {
242     return date;
243   }
244
245   public String JavaDoc getStyle() {
246     return style;
247   }
248
249   public String JavaDoc getLocation() {
250     return location;
251   }
252
253   public String JavaDoc getAlt() {
254     return alt;
255   }
256
257   public String JavaDoc getParameters() {
258     return parameters;
259   }
260
261   public void setParameters(String JavaDoc parameters) {
262     this.parameters = parameters;
263   }
264 }
265
Popular Tags