KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > test > TestPortlet


1 /*
2  * Copyright 2004,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.pluto.test;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.portlet.ActionRequest;
24 import javax.portlet.ActionResponse;
25 import javax.portlet.Portlet;
26 import javax.portlet.PortletConfig;
27 import javax.portlet.PortletException;
28 import javax.portlet.PortletMode;
29 import javax.portlet.PortletURL;
30 import javax.portlet.RenderRequest;
31 import javax.portlet.RenderResponse;
32 import javax.portlet.WindowState;
33 import javax.portlet.PortletPreferences;
34
35 /**
36  * This is a very simple test portlet
37  *
38  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
39  *
40  * @version CVS $Id: TestPortlet.java 331830 2005-11-08 15:20:56Z cziegeler $
41  */

42 public class TestPortlet implements Portlet {
43     
44     /* (non-Javadoc)
45      * @see javax.portlet.Portlet#destroy()
46      */

47     public void destroy() {
48         // nothing to do
49
}
50
51     /* (non-Javadoc)
52      * @see javax.portlet.Portlet#init(javax.portlet.PortletConfig)
53      */

54     public void init(PortletConfig config) throws PortletException {
55         // nothing to do
56
}
57
58     /* (non-Javadoc)
59      * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
60      */

61     public void processAction(ActionRequest req, ActionResponse res)
62     throws PortletException, IOException JavaDoc {
63
64         PortletPreferences prefs = req.getPreferences();
65         String JavaDoc key = req.getParameter("name");
66         String JavaDoc value = req.getParameter("value");
67         if (key != null && value != null) {
68             prefs.setValue(key, value);
69             try {
70                 prefs.store();
71             } catch (Exception JavaDoc e) {
72                 e.printStackTrace();
73             }
74         }
75          // nothing to do
76
}
77
78     /* (non-Javadoc)
79      * @see javax.portlet.Portlet#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
80      */

81     public void render(RenderRequest req, RenderResponse res)
82     throws PortletException, IOException JavaDoc {
83         res.setContentType("text/html");
84         Writer JavaDoc writer = res.getWriter();
85         writer.write("<h1>Simple TestPortlet</h1>\n");
86         writer.write("<p>This is a Cocoon test portlet.</p>\n");
87         writer.write("<p>I'm running in: " + req.getPortalContext().getPortalInfo() + "</p>");
88         writer.write("<p>Current portlet mode: " + req.getPortletMode() + "</p>");
89         writer.write("<p>Current window state: " + req.getWindowState() + "</p>");
90         writer.write("<p>Portlet Preferences:</p>");
91         PortletPreferences prefs = req.getPreferences();
92         Map JavaDoc map = prefs.getMap();
93         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext(); ) {
94             Map.Entry JavaDoc me = (Map.Entry JavaDoc)iter.next();
95             String JavaDoc key = (String JavaDoc)me.getKey();
96             String JavaDoc[] values = (String JavaDoc [])me.getValue();
97             if (values.length == 1) {
98                 writer.write(" Key: " + key + " Value: " + values[0] + "<br />");
99             } else if (values.length > 1) {
100                 writer.write(" Key: " + key + " Value: " + values[0] + "<br />");
101                 for (int i=1; i < values.length; ++i) {
102                     writer.write(" ");
103                     for (int j=0; j < key.length(); ++j) {
104                         writer.write(" ");
105                     }
106                     writer.write(" Value: " + values[i] + "<br />");
107                 }
108             } else {
109                 writer.write(" Key: " + key + " Value: <br />");
110             }
111         }
112
113         PortletURL url;
114         url = res.createActionURL();
115         url.setPortletMode(PortletMode.EDIT);
116
117         writer.write("<form method=\"POST\" action=\"" + url.toString() + "\"><br />");
118         writer.write("Update Preferences: <br />");
119         writer.write(" Key: <input type=\"text\" name=\"name\" size=\"16\" maxlength=\"16\">");
120         writer.write(" Value: <input type=\"text\" name=\"value\" size=\"32\" maxlength=\"32\">");
121         writer.write("<br /><input type=\"submit\" value=\"Update Preferences\"/>");
122         writer.write("</form>");
123         writer.write("<table><tr><td>Change Portlet Mode:</td>");
124
125         url = res.createRenderURL();
126         url.setPortletMode(PortletMode.EDIT);
127         this.writeLink(writer, url, "Edit");
128
129
130         url = res.createRenderURL();
131         url.setPortletMode(PortletMode.HELP);
132         this.writeLink(writer, url, "Help");
133
134         url = res.createRenderURL();
135         url.setPortletMode(PortletMode.VIEW);
136         this.writeLink(writer, url, "View");
137         
138         writer.write("</tr><tr><td>Change Window Mode:</td>");
139         url = res.createRenderURL();
140         url.setWindowState(WindowState.MINIMIZED);
141         this.writeLink(writer, url, "Minimized");
142
143         url = res.createRenderURL();
144         url.setWindowState(WindowState.NORMAL);
145         this.writeLink(writer, url, "Normal");
146
147         url = res.createRenderURL();
148         url.setWindowState(WindowState.MAXIMIZED);
149         this.writeLink(writer, url, "Maximized");
150         writer.write("</tr></table>");
151     }
152
153     protected void writeLink(Writer JavaDoc writer, PortletURL url, String JavaDoc text)
154     throws IOException JavaDoc {
155         writer.write("<td><a HREF=\"");
156         writer.write(url.toString());
157         writer.write("\">");
158         writer.write(text);
159         writer.write("</a></td>");
160     }
161 }
162
Popular Tags