001: package example;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import java.text.MessageFormat;
007:
008: import java.util.Locale;
009: import java.util.ResourceBundle;
010: import java.util.logging.Level;
011: import java.util.logging.Logger;
012:
013: import javax.portlet.*;
014:
015: public class HelloWorldPortlet implements Portlet {
016: static protected final Logger log = Logger
017: .getLogger(HelloWorldPortlet.class.getName());
018:
019: final private PortletMode MODE_VIEW = PortletMode.VIEW;
020: final private PortletMode MODE_EDIT = PortletMode.EDIT;
021: final private PortletMode MODE_GOODBYE = new PortletMode("goodbye");
022:
023: private String[][] _locales = new String[][] {
024: { "default", "Default for Browser" }, { "en", "English" },
025: { "es", "Español" }, { "fr", "Français" }, };
026:
027: private PortletConfig _config;
028:
029: public HelloWorldPortlet() {
030: }
031:
032: public void init(PortletConfig config) throws PortletException {
033: _config = config;
034: }
035:
036: /**
037: * This portlet allows a preference named 'locale' to override the locale
038: * specified by the browser.
039: */
040: protected Locale getLocale(PortletRequest request) {
041: PortletPreferences pref = request.getPreferences();
042: String locale = pref.getValue("locale", null);
043:
044: return (locale != null) ? new Locale(locale) : request
045: .getLocale();
046: }
047:
048: protected ResourceBundle getBundle(PortletRequest request) {
049: return _config.getResourceBundle(getLocale(request));
050: }
051:
052: public void processAction(ActionRequest request,
053: ActionResponse response) throws PortletException,
054: IOException {
055: PortletMode mode = request.getPortletMode();
056:
057: if (mode.equals(MODE_VIEW))
058: actionView(request, response);
059: else if (mode.equals(MODE_EDIT))
060: actionEdit(request, response);
061: }
062:
063: public void render(RenderRequest request, RenderResponse response)
064: throws PortletException, IOException {
065: PortletMode mode = request.getPortletMode();
066: WindowState winstate = request.getWindowState();
067:
068: if (winstate.equals(WindowState.MINIMIZED))
069: return;
070:
071: response.setContentType("text/html");
072: response.setTitle(getBundle(request).getString(
073: "javax.portlet.title"));
074:
075: if (mode.equals(MODE_VIEW))
076: doView(request, response);
077: else if (mode.equals(MODE_EDIT))
078: doEdit(request, response);
079: else if (mode.equals(MODE_GOODBYE))
080: doGoodbye(request, response);
081: else
082: throw new PortletException("unknown portlet mode: " + mode);
083: }
084:
085: protected void doView(RenderRequest request, RenderResponse response)
086: throws PortletException, IOException {
087: ResourceBundle bundle = getBundle(request);
088:
089: String salutation = request.getParameter("salutation");
090: if (salutation == null)
091: salutation = bundle.getString("world");
092:
093: PrintWriter out = new PrintWriter(response.getWriter());
094:
095: String hello = bundle.getString("hello");
096: out.println("<p>" + hello + " " + salutation + "</p>");
097:
098: PortletURL goodbyeUrl = response.createRenderURL();
099: goodbyeUrl.setPortletMode(MODE_GOODBYE);
100:
101: String youcansaygoodbye = MessageFormat.format(bundle
102: .getString("youcansaygoodbye"), new Object[] {
103: "<a href='" + goodbyeUrl + "'>", "</a>" });
104:
105: out.println("<p>" + youcansaygoodbye + "</p>");
106:
107: PortletURL actionUrl = response.createActionURL();
108: String locale = request.getPreferences().getValue("locale",
109: "default");
110: out.println("<form method='POST' action='" + actionUrl + "'>");
111: out.println("<select name='locale'>");
112: for (int i = 0; i < _locales.length; i++) {
113: String value = _locales[i][0];
114: String text = _locales[i][1];
115: out.print("<option value='" + value + "'");
116: if (value.equals(locale))
117: out.print(" selected='SELECTED'");
118: out.println(">" + text + "</option>");
119: }
120: out.println("</select>");
121:
122: String ok = bundle.getString("ok");
123: out.println("<input type='submit' value='" + ok + "'/><br>");
124: out.println("</form>");
125: }
126:
127: public void actionView(ActionRequest request,
128: ActionResponse response) throws PortletException,
129: IOException {
130: String locale = request.getParameter("locale");
131: if (locale != null) {
132: if (locale.equals("default"))
133: locale = null;
134:
135: PortletPreferences pref = request.getPreferences();
136:
137: pref.setValue("locale", locale);
138: pref.store();
139: }
140: }
141:
142: protected void doEdit(RenderRequest request, RenderResponse response)
143: throws PortletException, IOException {
144: ResourceBundle bundle = getBundle(request);
145: String salutation = request.getParameter("salutation");
146: if (salutation == null)
147: salutation = bundle.getString("world");
148:
149: PrintWriter out = new PrintWriter(response.getWriter());
150:
151: PortletURL submitUrl = response.createActionURL();
152: String ok = bundle.getString("ok");
153: String cancel = bundle.getString("cancel");
154: out.println("<form method='POST' action='" + submitUrl + "'>");
155: out.println("<p>");
156: out.println("<input name='salutation' value='" + salutation
157: + "'/><br>");
158: out.println("</p>");
159: out.println("<input type='submit' value='" + ok + "'/>");
160: out.println("<input type='submit' name='CANCEL' value='"
161: + cancel + "'/>");
162: out.println("</form>");
163: }
164:
165: public void actionEdit(ActionRequest request,
166: ActionResponse response) throws PortletException,
167: IOException {
168: if (request.getParameter("CANCEL") != null) {
169: response.setPortletMode(MODE_VIEW);
170: return;
171: }
172:
173: ResourceBundle bundle = getBundle(request);
174:
175: String salutation = request.getParameter("salutation");
176: if (salutation == null)
177: salutation = bundle.getString("world");
178: if (salutation != null)
179: response.setRenderParameter("salutation", salutation);
180:
181: // after a sucessful EDIT, switch to VIEW
182: response.setPortletMode(MODE_VIEW);
183: }
184:
185: protected void doGoodbye(RenderRequest request,
186: RenderResponse response) throws PortletException,
187: IOException {
188: ResourceBundle bundle = getBundle(request);
189:
190: String salutation = request.getParameter("salutation");
191: if (salutation == null)
192: salutation = bundle.getString("world");
193:
194: PrintWriter out = new PrintWriter(response.getWriter());
195: String goodbye = bundle.getString("goodbye");
196: out.println("<p>" + goodbye + " " + salutation + "</p>");
197: }
198:
199: public void destroy() {
200: }
201: }
|