001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.decoration;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.LinkedHashSet;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import org.apache.jetspeed.om.page.ContentPage;
031: import org.apache.jetspeed.om.page.Fragment;
032: import org.apache.jetspeed.om.page.Page;
033: import org.apache.jetspeed.request.RequestContext;
034:
035: /**
036: * Default implementation of <code>org.apache.jetspeed.decoration.Theme</code>
037: *
038: * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
039: *
040: * @see org.apache.jetspeed.decoration.Theme
041: */
042: public class PageTheme implements Theme, Serializable {
043: private transient Page page;
044: private transient DecorationFactory decorationFactory;
045: private transient RequestContext requestContext;
046: private final Set styleSheets;
047: private final LayoutDecoration layoutDecoration;
048: private final Map fragmentDecorations;
049: private final Collection portletDecorationNames;
050: private boolean invalidated = false;
051:
052: public PageTheme(Page page, DecorationFactory decorationFactory,
053: RequestContext requestContext) {
054: this .page = page;
055: this .decorationFactory = decorationFactory;
056: this .requestContext = requestContext;
057: this .styleSheets = new LinkedHashSet();
058: this .fragmentDecorations = new HashMap();
059:
060: boolean isDesktopEnabled = decorationFactory
061: .isDesktopEnabled(requestContext);
062: HashMap portletDecorationNames = new HashMap();
063: this .layoutDecoration = (LayoutDecoration) setupFragmentDecorations(
064: page.getRootFragment(), true, portletDecorationNames,
065: isDesktopEnabled);
066:
067: if (isDesktopEnabled) {
068: String defaultDesktopPortletDecoration = decorationFactory
069: .getDefaultDesktopPortletDecoration();
070: if (defaultDesktopPortletDecoration != null
071: && defaultDesktopPortletDecoration.length() > 0) {
072: if (portletDecorationNames
073: .get(defaultDesktopPortletDecoration) == null) {
074: portletDecorationNames.put(
075: defaultDesktopPortletDecoration,
076: defaultDesktopPortletDecoration);
077: }
078: }
079: }
080: this .portletDecorationNames = Collections
081: .unmodifiableCollection(new ArrayList(
082: portletDecorationNames.keySet()));
083: }
084:
085: /**
086: * setupFragmentDecorations
087: *
088: * Setup styleSheets and fragmentDecorations from all fragments
089: * in page, including nested fragments.
090: *
091: * @param fragment page fragment
092: * @return fragment decoration
093: */
094: private Decoration setupFragmentDecorations(Fragment fragment,
095: boolean isRootLayout, HashMap portletDecorationNames,
096: boolean isDesktopEnabled) {
097: // setup fragment decorations
098: Decoration decoration = decorationFactory.getDecoration(page,
099: fragment, requestContext);
100:
101: fragmentDecorations.put(fragment.getId(), decoration);
102: boolean isPortlet = (!isRootLayout && fragment.getType()
103: .equals(Fragment.PORTLET));
104:
105: if (isPortlet || isRootLayout) {
106: String commonStyleSheet = decoration.getStyleSheet();
107: if (commonStyleSheet != null) {
108: styleSheets.add(commonStyleSheet);
109: }
110: if (isDesktopEnabled) {
111: String desktopStyleSheet = decoration
112: .getStyleSheetDesktop();
113: if (desktopStyleSheet != null) {
114: styleSheets.add(desktopStyleSheet);
115: }
116: } else {
117: String portalStyleSheet = decoration
118: .getStyleSheetPortal();
119: if (portalStyleSheet != null) {
120: styleSheets.add(portalStyleSheet);
121: }
122: }
123:
124: if (isPortlet) {
125: portletDecorationNames.put(decoration.getName(),
126: decoration.getName());
127: }
128: }
129:
130: // setup nested fragment decorations
131: List fragments = fragment.getFragments();
132: if ((fragments != null) && !fragments.isEmpty()) {
133: Iterator fragmentsIter = fragments.iterator();
134: while (fragmentsIter.hasNext()) {
135: setupFragmentDecorations((Fragment) fragmentsIter
136: .next(), false, portletDecorationNames,
137: isDesktopEnabled);
138: }
139: }
140:
141: // return decoration; used to save page layout decoration
142: return decoration;
143: }
144:
145: public Set getStyleSheets() {
146: return styleSheets;
147: }
148:
149: public Decoration getDecoration(Fragment fragment) {
150: return (Decoration) fragmentDecorations.get(fragment.getId());
151: }
152:
153: public Collection getPortletDecorationNames() {
154: return portletDecorationNames; // is unmodifiable
155: }
156:
157: public LayoutDecoration getPageLayoutDecoration() {
158: return layoutDecoration;
159: }
160:
161: public void init(Page page, DecorationFactory decoration,
162: RequestContext context) {
163: this .page = page;
164: this .decorationFactory = decoration;
165: this .requestContext = context;
166: }
167:
168: public Page getPage() {
169: return page;
170: }
171:
172: public ContentPage getContentPage() {
173: return (ContentPage) page;
174: }
175:
176: public boolean isInvalidated() {
177: return this .invalidated;
178: }
179:
180: public void setInvalidated(boolean flag) {
181: this.invalidated = flag;
182: }
183: }
|