001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.theme;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.core.controller.ControllerContext;
027: import org.jboss.portal.core.model.portal.PortalObjectPath;
028: import org.jboss.portal.core.model.portal.Window;
029: import org.jboss.portal.core.model.portal.command.action.InvokePortletWindowRenderCommand;
030: import org.jboss.portal.core.model.portal.command.response.MarkupResponse;
031: import org.jboss.portal.core.model.portal.content.WindowRendition;
032: import org.jboss.portal.server.ServerInvocationContext;
033: import org.jboss.portal.server.request.URLContext;
034: import org.jboss.portal.server.request.URLFormat;
035: import org.jboss.portal.theme.ThemeConstants;
036: import org.jboss.portal.theme.page.WindowContext;
037: import org.jboss.portal.theme.page.WindowResult;
038: import org.jboss.portal.theme.render.renderer.ActionRendererContext;
039:
040: import java.util.ArrayList;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.List;
044: import java.util.Map;
045:
046: /**
047: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
048: * @version $Revision: 8786 $
049: */
050: public class WindowContextFactory {
051:
052: /** . */
053: private final ControllerContext context;
054:
055: public WindowContextFactory(ControllerContext context) {
056: this .context = context;
057: }
058:
059: public WindowContext createWindowContext(Window window,
060: WindowRendition context) {
061: Map actionMap = new HashMap();
062: addModeActions(window, actionMap, context.getMode(), context
063: .getSupportedModes());
064: addWindowStateActions(window, actionMap, context
065: .getWindowState(), context.getSupportedWindowStates());
066:
067: //
068: MarkupResponse markup = (MarkupResponse) context
069: .getControllerResponse();
070:
071: //
072: String region = window
073: .getDeclaredProperty(ThemeConstants.PORTAL_PROP_REGION);
074: String order = window
075: .getDeclaredProperty(ThemeConstants.PORTAL_PROP_ORDER);
076:
077: //
078: WindowResult windowResult = new WindowResult(markup.getTitle(),
079: markup.getContent(), actionMap,
080: context.getProperties(), markup.getHeaderContent(),
081: context.getWindowState(), context.getMode());
082:
083: //
084: return new WindowContext(window.getId().toString(
085: PortalObjectPath.LEGACY_BASE64_FORMAT), region, order,
086: windowResult);
087: }
088:
089: /**
090: * Create the action URLs for the allowed window states of the rendered portlet window and add them to the provided
091: * actionMap.
092: */
093: protected final void addWindowStateActions(Window window,
094: Map actionMap, WindowState currentWindowState,
095: List supportedWindowStates) {
096: List windowStates = new ArrayList(supportedWindowStates.size());
097: for (Iterator j = supportedWindowStates.iterator(); j.hasNext();) {
098: WindowState windowState = (WindowState) j.next();
099: String url = createUpdateNavigationalStateURL(window, null,
100: windowState);
101: boolean disabled = windowState.equals(currentWindowState);
102: WindowResult.Action action = new WindowResult.Action(
103: windowState.toString(), "window_state", url,
104: !disabled);
105: windowStates.add(action);
106: }
107: actionMap.put(ActionRendererContext.WINDOWSTATES_KEY,
108: windowStates);
109: }
110:
111: /**
112: * Create the action URLs for the allowed portlet modes of the rendered portlet window and add them to the provided
113: * actionMap.
114: */
115: protected final void addModeActions(Window window, Map actionMap,
116: Mode currentMode, List supportedModes) {
117: List modes = new ArrayList(supportedModes.size());
118: for (Iterator j = supportedModes.iterator(); j.hasNext();) {
119: Mode mode = (Mode) j.next();
120: String url = createUpdateNavigationalStateURL(window, mode,
121: null);
122: boolean disabled = mode.equals(currentMode);
123: WindowResult.Action action = new WindowResult.Action(mode
124: .toString(), "mode", url, !disabled);
125: modes.add(action);
126: }
127: actionMap.put(ActionRendererContext.MODES_KEY, modes);
128: }
129:
130: protected final String createUpdateNavigationalStateURL(
131: Window window, Mode mode, WindowState windowState) {
132: InvokePortletWindowRenderCommand cmd = new InvokePortletWindowRenderCommand(
133: window.getId(), mode, windowState);
134: ServerInvocationContext serverContext = context
135: .getServerInvocation().getServerContext();
136: boolean secure = serverContext.getURLContext().isSecure();
137: boolean authenticated = serverContext.getURLContext()
138: .isAuthenticated();
139: URLContext urlContext = URLContext.newInstance(secure,
140: authenticated);
141: return context.renderURL(cmd, urlContext, URLFormat
142: .newInstance(true, true));
143: }
144: }
|