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.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import javax.portlet.PortletMode;
24  import javax.portlet.WindowState;
25  
26  import org.apache.jetspeed.JetspeedActions;
27  import org.apache.jetspeed.om.common.portlet.PortletApplication;
28  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
29  import org.apache.jetspeed.om.page.ContentPage;
30  import org.apache.jetspeed.om.page.ContentFragment;
31  import org.apache.jetspeed.request.RequestContext;
32  import org.apache.jetspeed.security.SecurityAccessController;
33  import org.apache.pluto.om.window.PortletWindow;
34  
35  public class CustomDecoratorActionsFactory extends AbstractDecoratorActionsFactory
36  {
37      private static final DecoratorActionTemplate ABOUT_MODE_TEMPLATE = new DecoratorActionTemplate(JetspeedActions.ABOUT_MODE);
38      private static final DecoratorActionTemplate CONFIG_MODE_TEMPLATE = new DecoratorActionTemplate(JetspeedActions.CONFIG_MODE);
39      private static final DecoratorActionTemplate EDIT_DEFAULTS_MODE_TEMPLATE = new DecoratorActionTemplate(JetspeedActions.EDIT_DEFAULTS_MODE);
40      //private static final DecoratorActionTemplate PREVIEW_MODE_TEMPLATE = new DecoratorActionTemplate(JetspeedActions.PREVIEW_MODE);
41      private static final DecoratorActionTemplate PRINT_MODE_TEMPLATE = new DecoratorActionTemplate(JetspeedActions.PRINT_MODE);
42      private static final DecoratorActionTemplate SOLO_ACTION_TEMPLATE = new DecoratorActionTemplate(JetspeedActions.SOLO_STATE);
43      
44      private final List supportedActions;
45      private final List supportedSoloActions;
46      
47      public CustomDecoratorActionsFactory()
48      {
49          ArrayList list = new ArrayList(JetspeedActions.getStandardPortletModes());
50          list.add(JetspeedActions.ABOUT_MODE);
51          list.add(JetspeedActions.CONFIG_MODE);
52          list.add(JetspeedActions.EDIT_DEFAULTS_MODE);
53          //list.add(JetspeedActions.PREVIEW_MODE);
54          list.add(JetspeedActions.PRINT_MODE);
55          list.addAll(JetspeedActions.getStandardWindowStates());
56          list.add(JetspeedActions.SOLO_STATE);
57          supportedActions = Collections.unmodifiableList(list);
58          
59          list = new ArrayList(JetspeedActions.getStandardPortletModes());
60          list.add(JetspeedActions.PRINT_MODE);
61          supportedSoloActions = Collections.unmodifiableList(list);
62      }
63  
64      public List getSupportedActions(RequestContext rc, PortletApplication pa, PortletWindow pw, PortletMode cm,
65                      WindowState ws, Decoration decoration)
66      {
67          // don't support any window state actions when in "solo" state
68          return JetspeedActions.SOLO_STATE.equals(ws) ? supportedSoloActions : supportedActions;
69      }
70      
71      public List getDecoratorActions(RequestContext rc, PortletApplication pa, PortletWindow pw, PortletMode pm,
72                      WindowState ws, Decoration decoration, List actionTemplates, 
73                      PortletDefinitionComposite portlet, ContentFragment fragment, SecurityAccessController accessController)
74      {
75          int printModeIndex = actionTemplates.indexOf(PRINT_MODE_TEMPLATE);
76          int soloStateIndex = actionTemplates.indexOf(SOLO_ACTION_TEMPLATE);
77          
78          if ( printModeIndex != -1 && soloStateIndex != -1 )
79          {
80              // merge "solo" state with "print" mode
81              DecoratorActionTemplate soloStateTemplate = (DecoratorActionTemplate)actionTemplates.remove(soloStateIndex);
82              DecoratorActionTemplate printActionTemplate = (DecoratorActionTemplate)actionTemplates.get(printModeIndex);
83              printActionTemplate.setState(soloStateTemplate.getState());
84              printActionTemplate.setCustomState((soloStateTemplate.getCustomState()));
85          }
86          else if ( soloStateIndex != -1 )
87          {
88              // don't provide "solo" action separately without "print" mode
89              actionTemplates.remove(soloStateIndex);
90          }
91          // else if (printModeIndex != -1)
92          //   support switching to different modes once in "solo" state, even back to "print"
93          
94          int configModeIndex = actionTemplates.indexOf(CONFIG_MODE_TEMPLATE);
95          if (configModeIndex != -1)
96          {
97              try
98              {
99                  ContentPage page = rc.getPage();
100                 page.checkAccess(JetspeedActions.CONFIG);
101             }
102             catch (SecurityException e)
103             {
104                 actionTemplates.remove(configModeIndex);
105             }
106         }
107         
108         int editDefaultsModeIndex = actionTemplates.indexOf(EDIT_DEFAULTS_MODE_TEMPLATE);
109         if (editDefaultsModeIndex != -1)
110         {
111             try
112             {
113                 ContentPage page = rc.getPage();
114                 page.checkAccess(JetspeedActions.EDIT_DEFAULTS);
115             }
116             catch (SecurityException e)
117             {
118                 actionTemplates.remove(editDefaultsModeIndex);
119             }
120         }
121         
122         return super.getDecoratorActions(rc,pa,pw,pm,ws,decoration,actionTemplates, portlet,  fragment, accessController);
123     }
124     
125     protected DecoratorAction createAction(RequestContext rc, PortletWindow pw, Decoration decoration,
126                     DecoratorActionTemplate template)
127     {
128         DecoratorAction action = super.createAction(rc,pw,decoration,template);
129         if ( template.getState() != null && JetspeedActions.SOLO_STATE.equals(template.getState()))
130         {
131             // "solo" opens in a new popup winodw
132             action.setTarget("_blank");
133         }
134         return action;
135     }
136 }