001: package org.wings;
002:
003: import java.util.HashMap;
004: import java.util.Iterator;
005: import java.util.Map;
006: import java.util.Set;
007:
008: import javax.portlet.PortletMode;
009: import javax.portlet.PortletModeException;
010: import javax.portlet.PortletURL;
011: import javax.portlet.RenderResponse;
012: import javax.portlet.WindowState;
013: import javax.portlet.WindowStateException;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.SAnchor;
018: import org.wings.event.SRenderEvent;
019: import org.wings.event.SRenderListener;
020: import org.wings.portlet.Const;
021: import org.wings.portlet.PortletParameterCodec;
022: import org.wings.session.Session;
023: import org.wings.session.SessionManager;
024:
025: /**
026: *
027: * This implementation of a SAnchor exists for the wingSPortletBridge. With the
028: * SPortletAnchor a link equivalent to a javax.portlet.PortletURL can be
029: * created. This link behaves like a SAnchor. Additionally a PortletMode and a
030: * WindowState can be associated with the generated PortletURL. The String
031: * "kind" in the constructors defines if an action or a render PortletURL should
032: * be created. <br>
033: * <br>
034: * Also parametes can be asscociated with the PortletURL. This parameters are
035: * automatically encoded with the {@link PortletParameterCodec}
036: *
037: * @see javax.portlet.PortletURL
038: *
039: * @author <a href="mailto:marc.musch@mercatis.com">Marc Musch</a>
040: *
041: */
042: public class SPortletAnchor extends SAnchor {
043:
044: private final transient static Log log = LogFactory
045: .getLog(SPortletAnchor.class);
046:
047: /**
048: * String used for the constructor parameter "kind" to generate an action
049: * PortletURL.
050: */
051: public static final String ACTION_URL = "actionURL";
052:
053: /**
054: * String used for the constructor parameter "kind" to generate a render
055: * PortletURL.
056: */
057: public static final String RENDER_URL = "renderURL";
058:
059: /**
060: * String used for the parameter "portletMode" to set the
061: * PortletMode view in the PortletURL
062: */
063: public static final String VIEW_MODE = "view";
064:
065: /**
066: * String used for the parameter "portletMode" to set the
067: * PortletMode edit in the PortletURL
068: */
069: public static final String EDIT_MODE = "edit";
070:
071: /**
072: * String used for the parameter "portletMode" to set the
073: * PortletMode help in the PortletURL
074: */
075: public static final String HELP_MODE = "help";
076:
077: /**
078: * String used for the parameter "windowState" to set the
079: * WindowState maximized in the PortletURL
080: */
081: public static final String MAXIMIZED_WS = "maximized";
082:
083: /**
084: * String used for the parameter "windowState" to set the
085: * WindowState minimized in the PortletURL
086: */
087: public static final String MINIMIZED_WS = "minimized";
088:
089: /**
090: * String used for the parameter "windowState" to set the
091: * WindowState normal in the PortletURL
092: */
093: public static final String NORMAL_WS = "normal";
094:
095: private PortletURL portletURL;
096:
097: private String kind;
098:
099: private String portletMode;
100:
101: private String windowState;
102:
103: private Map<String, String[]> parameters;
104:
105: /**
106: * Creates a SPortletAnchor
107: *
108: * @param kind -
109: * action or render PortletURL, use the static Strings ACTION_URL
110: * or RENDER_URL for this parameter.
111: */
112: public SPortletAnchor(String kind) {
113:
114: this (kind, null);
115: }
116:
117: /**
118: *
119: * Creates a SPortletAnchor
120: *
121: * @param kind -
122: * action or render PortletURL, use the static Strings ACTION_URL
123: * or RENDER_URL for this parameter.
124: * @param portletMode -
125: * sets the Portlet Mode in the PortletURL, use the static
126: * Strings VIEW_MODE, EDIT_MODE or HELP_MODE for this parameter.
127: * Also a custom Portlet Mode can be set. If the Portlet Mode is
128: * not supported, no Portlet Mode is set.
129: */
130: public SPortletAnchor(String kind, String portletMode) {
131:
132: this (kind, portletMode, null);
133:
134: }
135:
136: /**
137: *
138: * Creates a SPortletAnchor
139: *
140: * @param kind -
141: * action or render PortletURL, use the static Strings ACTION_URL
142: * or RENDER_URL for this parameter.
143: * @param portletMode -
144: * sets the Portlet Mode in the PortletURL, use the static
145: * Strings VIEW_MODE, EDIT_MODE or HELP_MODE for this parameter.
146: * Also a custom Portlet Mode can be set. If the Portlet Mode is
147: * not supported, no Portlet Mode is set.
148: * @param windowState -
149: * sets the Window State in the PortletURL, use the static
150: * Strings MAXIMIZED_WS, MINIMIZED_WS or NORMAL_WS for this
151: * parameter. Also custom Window States can be set. If the Window
152: * State is not supported, no Window State is set.
153: */
154: public SPortletAnchor(String kind, String portletMode,
155: String windowState) {
156:
157: this (kind, portletMode, windowState, null);
158:
159: }
160:
161: /**
162: *
163: * Creates a SPortletAnchor
164: *
165: * @param kind -
166: * action or render PortletURL, use the static Strings ACTION_URL
167: * or RENDER_URL for this parameter.
168: * @param portletMode -
169: * sets the Portlet Mode in the PortletURL, use the static
170: * Strings VIEW_MODE, EDIT_MODE or HELP_MODE for this parameter.
171: * Also a custom Portlet Mode can be set. If the Portlet Mode is
172: * not supported, no Portlet Mode is set.
173: * @param windowState -
174: * sets the Window State in the PortletURL, use the static
175: * Strings MAXIMIZED_WS, MINIMIZED_WS or NORMAL_WS for this
176: * parameter. Also custom Window States can be set. If the Window
177: * State is not supported, no Window State is set.
178: * @param parameters -
179: * set a map with parameters in the PortletURL. This parameters
180: * will be automatically encoded with the
181: * {@link PortletParameterCodec}
182: */
183: public SPortletAnchor(String kind, String portletMode,
184: String windowState, Map<String, String[]> parameters) {
185:
186: this .kind = kind;
187: this .portletMode = portletMode;
188: this .windowState = windowState;
189: if (parameters != null) {
190: this .parameters = new HashMap<String, String[]>(parameters);
191: } else {
192: this .parameters = new HashMap<String, String[]>();
193: }
194: this .setStyle("SAnchor");
195:
196: this .addRenderListener(new SRenderListener() {
197:
198: public void startRendering(SRenderEvent renderEvent) {
199:
200: updatePortletAnchor();
201: }
202:
203: public void doneRendering(SRenderEvent renderEvent) {
204: }
205:
206: });
207:
208: }
209:
210: /**
211: *
212: * Sets the Portlet Mode in the PortletURL
213: *
214: * @param portletMode -
215: * sets the Portlet Mode in the PortletURL, use the static
216: * Strings VIEW_MODE, EDIT_MODE or HELP_MODE for this parameter.
217: * Also a custom Portlet Mode can be set. If the Portlet Mode is
218: * not supported, no Portlet Mode is set.
219: */
220: public void setPortletMode(String portletMode) {
221: this .portletMode = portletMode;
222: updatePortletAnchor();
223: }
224:
225: /**
226: * Sets the Window State in the PortletURL
227: *
228: * @param windowState -
229: * sets the Window State in the PortletURL, use the static
230: * Strings MAXIMIZED_WS, MINIMIZED_WS or NORMAL_WS for this
231: * parameter. Also custom Window States can be set. If the Window
232: * State is not supported, no Window State is set.
233: */
234: public void setWindowState(String windowState) {
235: this .windowState = windowState;
236: updatePortletAnchor();
237: }
238:
239: /**
240: *
241: * Adds a single parameter with a single value.
242: *
243: * @param name -
244: * the name of the parameter, the name will be automatically
245: * encoded with the {@link PortletParameterCodec}
246: * @param value -
247: * the value of the parameter
248: */
249: public void setParameter(String name, String value) {
250: String[] valueArray = new String[] { value };
251: parameters.put(name, valueArray);
252: updatePortletAnchor();
253: }
254:
255: /**
256: *
257: * Adds a single parameter with an array of values.
258: *
259: * @param name -
260: * the name of the parameter, the name will be automatically
261: * encoded with the {@link PortletParameterCodec}
262: * @param values -
263: * the values of the parameter
264: */
265: public void setParameters(String name, String[] values) {
266: parameters.put(name, values);
267: updatePortletAnchor();
268: }
269:
270: /**
271: *
272: * Sets the parameters, this method will delete all previous parameters.
273: *
274: * @param parameters -
275: * the parameter map, the names will be automatically encoded
276: * with the {@link PortletParameterCodec}
277: */
278: public void setParameterMap(Map<String, String[]> parameters) {
279: this .parameters = new HashMap<String, String[]>(parameters);
280: updatePortletAnchor();
281: }
282:
283: private void updatePortletAnchor() {
284:
285: Session session = SessionManager.getSession();
286: RenderResponse renderResponse = (RenderResponse) session
287: .getProperty(Const.WINGS_SESSION_PROPERTY_RENDER_RESPONSE);
288: if (kind != null && kind.equals(ACTION_URL)) {
289: portletURL = renderResponse.createActionURL();
290: } else {
291: portletURL = renderResponse.createRenderURL();
292: }
293: if (portletMode != null && !portletMode.equals("")) {
294: try {
295: portletURL.setPortletMode(new PortletMode(portletMode));
296: } catch (PortletModeException e) {
297: // create a portletURL without a mode set, so the application
298: // doesn't crash, but log a warning.
299: log.warn("WingS-Portlet-Bridge: portlet mode \""
300: + portletMode + "\" not supported");
301: }
302: }
303:
304: if (windowState != null && !windowState.equals("")) {
305: try {
306: portletURL.setWindowState(new WindowState(windowState));
307: } catch (WindowStateException e) {
308: // create a portletURL without a window state set, so the
309: // application doesn't crash, but log a warning.
310: log
311: .warn("WingS-Portlet-Bridge: portlet window state \""
312: + windowState + "\" not supported");
313: }
314: }
315:
316: if (parameters != null && !parameters.isEmpty()) {
317: Map<String, String[]> encodedParams = new HashMap<String, String[]>();
318: Set params = parameters.keySet();
319: Iterator iter = params.iterator();
320: while (iter.hasNext()) {
321: String name = (String) iter.next();
322: String[] value = (String[]) parameters.get(name);
323: encodedParams.put(PortletParameterCodec.encode(name),
324: value);
325: }
326: portletURL.setParameters(encodedParams);
327: }
328:
329: setURL(portletURL.toString());
330: setTarget(null);
331:
332: }
333:
334: }
|