View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.decoration;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.jetspeed.om.page.ContentPage;
31  import org.apache.jetspeed.om.page.Fragment;
32  import org.apache.jetspeed.om.page.Page;
33  import org.apache.jetspeed.request.RequestContext;
34  
35  /***
36   * Default implementation of <code>org.apache.jetspeed.decoration.Theme</code>
37   * 
38   * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
39   *
40   * @see org.apache.jetspeed.decoration.Theme
41   */
42  public class PageTheme implements Theme, Serializable
43  {
44      private transient Page page;
45      private transient DecorationFactory decorationFactory;
46      private transient RequestContext requestContext;
47      private final Set styleSheets;
48      private final LayoutDecoration layoutDecoration;
49      private final Map fragmentDecorations;
50      private final Collection portletDecorationNames;
51      private boolean invalidated = false;
52          
53      public PageTheme(Page page, DecorationFactory decorationFactory, RequestContext requestContext)
54      {
55          this.page = page;
56          this.decorationFactory = decorationFactory;
57          this.requestContext = requestContext;
58          this.styleSheets = new LinkedHashSet();
59          this.fragmentDecorations = new HashMap();
60          
61          boolean isDesktopEnabled = decorationFactory.isDesktopEnabled( requestContext );
62          HashMap portletDecorationNames = new HashMap();
63          this.layoutDecoration = (LayoutDecoration)setupFragmentDecorations( page.getRootFragment(), portletDecorationNames, isDesktopEnabled );
64          
65          if ( isDesktopEnabled )
66          {
67              String defaultDesktopPortletDecoration = decorationFactory.getDefaultDesktopPortletDecoration();
68              if ( defaultDesktopPortletDecoration != null && defaultDesktopPortletDecoration.length() > 0 )
69              {
70                  if ( portletDecorationNames.get( defaultDesktopPortletDecoration ) == null )
71                  {
72                      portletDecorationNames.put( defaultDesktopPortletDecoration, defaultDesktopPortletDecoration );
73                  }
74              }
75          }
76          this.portletDecorationNames = Collections.unmodifiableCollection( new ArrayList( portletDecorationNames.keySet() ) );
77      }
78  
79      /***
80       * setupFragmentDecorations
81       *
82       * Setup styleSheets and fragmentDecorations from all fragments
83       * in page, including nested fragments.
84       *
85       * @param fragment page fragment
86       * @return fragment decoration
87       */
88      private Decoration setupFragmentDecorations( Fragment fragment, HashMap portletDecorationNames, boolean isDesktopEnabled )
89      {
90          // setup fragment decorations
91          Decoration decoration = decorationFactory.getDecoration( page, fragment, requestContext );
92          
93          String commonStyleSheet = decoration.getStyleSheet();
94          if ( commonStyleSheet != null )
95          {
96              styleSheets.add( commonStyleSheet );
97          }
98          if ( isDesktopEnabled )
99          {
100             String desktopStyleSheet = decoration.getStyleSheetDesktop();
101             if ( desktopStyleSheet != null )
102             {
103                 styleSheets.add( desktopStyleSheet );
104             }
105         }
106         else
107         {
108             String portalStyleSheet = decoration.getStyleSheetPortal();
109             if ( portalStyleSheet != null )
110             {
111                 styleSheets.add( portalStyleSheet );
112             }
113         }
114         
115         fragmentDecorations.put( fragment.getId(), decoration );
116         if ( fragment.getType().equals( Fragment.PORTLET ) )
117         {
118             portletDecorationNames.put( decoration.getName(), decoration.getName() );
119         }
120         
121         // setup nested fragment decorations
122         List fragments = fragment.getFragments();
123         if ( ( fragments != null ) && ! fragments.isEmpty() )
124         {
125             Iterator fragmentsIter = fragments.iterator();
126             while ( fragmentsIter.hasNext() )
127             {
128                 setupFragmentDecorations( (Fragment)fragmentsIter.next(), portletDecorationNames, isDesktopEnabled );
129             }
130         }
131 
132         // return decoration; used to save page layout decoration
133         return decoration;
134     }
135 
136     public Set getStyleSheets()
137     {
138         return styleSheets;
139     }
140 
141     public Decoration getDecoration( Fragment fragment )
142     {
143         return (Decoration) fragmentDecorations.get( fragment.getId() );
144     }
145     
146     public Collection getPortletDecorationNames()
147     {
148         return portletDecorationNames;    // is unmodifiable
149     }
150     
151     public LayoutDecoration getPageLayoutDecoration()
152     {
153         return layoutDecoration;
154     }
155 
156     public void init(Page page, DecorationFactory decoration, RequestContext context)
157     {
158         this.page = page;
159         this.decorationFactory = decoration;
160         this.requestContext = context;        
161     }
162     
163     public Page getPage()
164     {
165         return page;
166     }    
167 
168     public ContentPage getContentPage()
169     {
170         return (ContentPage)page;
171     }
172 
173     public boolean isInvalidated()
174     {
175         return this.invalidated;
176     }
177     
178     public void setInvalidated(boolean flag)
179     {
180         this.invalidated = flag;
181     }
182 }