KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > editor > BasicEditor


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.editor;
11
12 import java.io.*;
13 import java.util.*;
14
15 import org.mmbase.bridge.*;
16 import org.mmbase.bridge.jsp.taglib.LocaleTag;
17 import javax.servlet.jsp.PageContext JavaDoc;
18 import org.mmbase.util.functions.*;
19 import org.mmbase.util.logging.Logger;
20 import org.mmbase.util.logging.Logging;
21
22 /**
23  * This is an example implementation of the EditTag. It extends Editor.
24  * To create your own editor with the edittag you could or rather should also
25  * extend Editor.<br />
26  * BasicEditor accepts two parameters url and icon and returns a link with a nodenr
27  * of the very first field the edittag encounters, with an icon to click on.
28  *
29  * @author Andr&eacute; van Toly
30  * @version $Id: BasicEditor.java,v 1.9 2006/07/06 11:36:12 michiel Exp $
31  * @see EditTag
32  * @see YAMMEditor
33  * @since MMBase-1.8
34  */

35
36 public class BasicEditor extends Editor {
37
38     private static final Logger log = Logging.getLoggerInstance(BasicEditor.class);
39
40     private static final FunctionProvider patterns = PatternNodeFunctionProvider.getInstance();
41
42     private static final Parameter[] PARAMS = new Parameter[] {
43         new Parameter("url", String JavaDoc.class, "/mmbase/edit/basic/"),
44         new Parameter("urlparams", Map.class, null),
45         new Parameter("icon", String JavaDoc.class, ""),
46         new Parameter("iconparams", Map.class, null),
47         new Parameter("when", String JavaDoc.class, "always"),
48         new Parameter("target", String JavaDoc.class, "new"),
49         Parameter.CLOUD
50     };
51
52
53     protected Parameter[] getParameterDefinition() {
54         return PARAMS;
55     }
56
57
58     /**
59      * Makes html with a link to a node in an editor using the parameters url and icon.
60      *
61      * @param context The PageContext
62      */

63     public void getEditorHTML(PageContext JavaDoc context) throws IOException {
64
65         String JavaDoc nodenr = "";
66         if (!nodenrList.isEmpty()) { // get the first node from this list to edit
67
nodenr = (String JavaDoc) nodenrList.get(0);
68         }
69
70         makeHTML(nodenr, context);
71
72     }
73     /**
74      * Fills parameters of the parameters to be interpreted as PatternNodeFunctions
75      */

76     protected String JavaDoc getValue(String JavaDoc param, Cloud cloud, String JavaDoc nodenr, PageContext JavaDoc context) {
77            Function urlFunction = patterns.getFunction(parameters.getString(param));
78            Parameters urlParameters = urlFunction.createParameters();
79            if (cloud != null) {
80                Node node = cloud.getNode(nodenr);
81                urlParameters.set(Parameter.NODE, node);
82            }
83            urlParameters.setAll((Map) parameters.get(param + "params"));
84            urlParameters.setIfDefined(Parameter.REQUEST, context.getRequest());
85            urlParameters.setIfDefined(Parameter.RESPONSE, context.getResponse());
86            return (String JavaDoc) urlFunction.getFunctionValue(urlParameters);
87     }
88     /**
89     * Creates a string with the link (and icon) to the editor
90     * @param nodenr Node to make HTML link to.
91     * @param context PageContex object, which must be used to write the HTML to.
92     *
93     */

94     protected void makeHTML(String JavaDoc nodenr, PageContext JavaDoc context) throws IOException {
95         String JavaDoc when = parameters.getString("when");
96
97         if ("always".equals(when) || "true".equals(context.getRequest().getParameter("edit"))) {
98             Cloud cloud = (Cloud) parameters.get(Parameter.CLOUD);
99
100             String JavaDoc url = getValue("url", cloud, nodenr, context);
101             String JavaDoc icon = getValue("icon", cloud, nodenr, context);
102             url = makeRelative(url, context);
103             Writer html = context.getOut();
104             Locale locale = (Locale) context.getAttribute(LocaleTag.KEY, PageContext.PAGE_SCOPE);
105             if (locale == null) {
106                 locale = org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultLocale();
107             }
108             String JavaDoc title = ResourceBundle.getBundle("org.mmbase.bridge.jsp.taglib.resources.messages", locale).getString("edit");
109             html.write("<a class=\"mm_edit\" title=\"" + title + "\" HREF=\"");
110             html.write(url);
111             html.write("\" ");
112             if ("new".equals(parameters.getString("target"))) {
113                 html.write("onclick=\"window.open(this.href); return false;\" ");
114             }
115             html.write(">");
116             if (! "".equals(icon)) {
117                 icon = makeRelative(icon, context);
118                 html.write("<img SRC=\"");
119                 html.write(icon);
120                 html.write("\" alt=\"" + title + "\">");
121             } else {
122                 html.write("[" + title + "]");
123             }
124             html.write("</a>");
125         }
126     }
127
128     /**
129      * Copied this method from the UrlTag.
130      * If it would be nice that an URL starting with '/' would be generated relatively to the current request URL, then this method can do it.
131      * If the URL is not used to write to (this) page, then you probably don't want that.
132      *
133      * The behaviour can be overruled by starting the URL with two '/'s.
134      *
135      */

136     protected String JavaDoc makeRelative(String JavaDoc url, PageContext JavaDoc pageContext) {
137         StringBuffer JavaDoc show = new StringBuffer JavaDoc(url);
138         javax.servlet.http.HttpServletRequest JavaDoc req = (javax.servlet.http.HttpServletRequest JavaDoc)pageContext.getRequest();
139         if (show.charAt(0) == '/') { // absolute on servletcontex
140
if (show.length() > 1 && show.charAt(1) == '/') {
141                 log.debug("'absolute' url, not making relative");
142                 show.deleteCharAt(0);
143                 show.insert(0, req.getContextPath());
144
145             } else {
146                 log.debug("'absolute' url");
147                 String JavaDoc thisDir = new java.io.File JavaDoc(req.getServletPath()).getParent();
148                 show.insert(0, org.mmbase.util.UriParser.makeRelative(thisDir, "/")); // makes a relative path to root.
149
}
150         }
151         return show.toString();
152     }
153
154 }
155
Popular Tags