1 16 package org.apache.cocoon.portal.pluto.test; 17 18 import java.io.IOException ; 19 import java.io.Writer ; 20 import java.util.Map ; 21 import java.util.Iterator ; 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 42 public class TestPortlet implements Portlet { 43 44 47 public void destroy() { 48 } 50 51 54 public void init(PortletConfig config) throws PortletException { 55 } 57 58 61 public void processAction(ActionRequest req, ActionResponse res) 62 throws PortletException, IOException { 63 64 PortletPreferences prefs = req.getPreferences(); 65 String key = req.getParameter("name"); 66 String value = req.getParameter("value"); 67 if (key != null && value != null) { 68 prefs.setValue(key, value); 69 try { 70 prefs.store(); 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 } 75 } 77 78 81 public void render(RenderRequest req, RenderResponse res) 82 throws PortletException, IOException { 83 res.setContentType("text/html"); 84 Writer 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 map = prefs.getMap(); 93 for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) { 94 Map.Entry me = (Map.Entry )iter.next(); 95 String key = (String )me.getKey(); 96 String [] values = (String [])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 writer, PortletURL url, String text) 154 throws IOException { 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 |