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;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.StringTokenizer;
25  
26  import javax.portlet.PortletMode;
27  import javax.portlet.WindowState;
28  
29  
30  /***
31   * Jestpeed Action Declarations
32   *
33   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
35   * @version $Id: JetspeedActions.java 593513 2007-11-09 12:48:34Z woonsan $
36   */
37  public class JetspeedActions
38  {
39      public static final PortletMode ABOUT_MODE = new PortletMode("about");
40      public static final PortletMode CONFIG_MODE = new PortletMode("config");
41      public static final PortletMode EDIT_DEFAULTS_MODE = new PortletMode("edit_defaults");
42      //public static final PortletMode PREVIEW_MODE = new PortletMode("preview");
43      public static final PortletMode PRINT_MODE = new PortletMode("print");
44      public static final WindowState SOLO_STATE = new WindowState("solo");
45    
46      public static final int MASK_MINIMIZE = 0x01;    
47      public static final int MASK_MAXIMIZE = 0x02;
48      public static final int MASK_NORMAL = 0x04;
49      public static final int MASK_VIEW = 0x08;
50      public static final int MASK_EDIT = 0x10;
51      public static final int MASK_HELP = 0x20;
52      
53      public static final String VIEW = PortletMode.VIEW.toString();
54      public static final String EDIT = PortletMode.EDIT.toString();
55      public static final String HELP = PortletMode.HELP.toString();
56      public static final String ABOUT = ABOUT_MODE.toString();
57      public static final String CONFIG = CONFIG_MODE.toString();
58      public static final String EDIT_DEFAULTS = EDIT_DEFAULTS_MODE.toString();
59      //public static final String PREVIEW = PREVIEW_MODE.toString();
60      public static final String PRINT = PRINT_MODE.toString();
61      public static final String NORMAL = WindowState.NORMAL.toString();
62      public static final String MINIMIZE = WindowState.MINIMIZED.toString();
63      public static final String MAXIMIZE = WindowState.MAXIMIZED.toString();
64      public static final String SOLO = SOLO_STATE.toString();
65      
66      private static final List standardPortletModes;
67      private static final List standardWindowStates;
68     
69      static
70      {
71          ArrayList list = new ArrayList(3);
72          list.add(PortletMode.VIEW);
73          list.add(PortletMode.EDIT);
74          list.add(PortletMode.HELP);
75          standardPortletModes = Collections.unmodifiableList(list);
76          list = new ArrayList(3);
77          list.add(WindowState.NORMAL);
78          list.add(WindowState.MINIMIZED);
79          list.add(WindowState.MAXIMIZED);
80          standardWindowStates = Collections.unmodifiableList(list);
81      }
82  
83      private static JetspeedActions instance = new JetspeedActions(new String[]{}, new String[]{});
84          
85      private final List extendedPortletModes;
86      private final List extendedWindowStates;
87      private final Map actionsMap;
88      private final Object[] actions;
89      
90      public static List getStandardPortletModes()
91      {
92          return standardPortletModes;
93      }
94      
95      public static List getStandardWindowStates()
96      {
97          return standardWindowStates;
98      }
99      
100     public JetspeedActions(String[] supportedPortletModes, String[] supportedWindowStates)
101     {
102         int index = 0;
103         
104         ArrayList actionsList = new ArrayList();
105         
106         actionsMap = new HashMap();
107         
108         actionsMap.put(WindowState.MINIMIZED.toString(),new Integer(index++));
109         actionsList.add(WindowState.MINIMIZED);
110         actionsMap.put(WindowState.MAXIMIZED.toString(),new Integer(index++));
111         actionsList.add(WindowState.MAXIMIZED);
112         actionsMap.put(WindowState.NORMAL.toString(),new Integer(index++));
113         actionsList.add(WindowState.NORMAL);
114         actionsMap.put(PortletMode.VIEW.toString(), new Integer(index++));
115         actionsList.add(PortletMode.VIEW);
116         actionsMap.put(PortletMode.EDIT.toString(),new Integer(index++));
117         actionsList.add(PortletMode.EDIT);
118         actionsMap.put(PortletMode.HELP.toString(),new Integer(index++));
119         actionsList.add(PortletMode.HELP);
120         
121         ArrayList list = new ArrayList();
122         
123         for (int i=0; index < 32 && i<supportedWindowStates.length; i++) 
124         {
125             WindowState state = new WindowState(supportedWindowStates[i]);
126             if ( !actionsMap.containsKey(state.toString()) )
127             {
128                 actionsMap.put(state.toString(), new Integer(index++));
129                 actionsList.add(state);
130                 list.add(state);
131             }
132             else if (!standardWindowStates.contains(state))
133             {
134                 throw new IllegalArgumentException("WindowState "+state+" already defined as extended PortletMode or WindowState");
135             }
136         }
137         extendedWindowStates = Collections.unmodifiableList(list);
138         
139         list = new ArrayList();
140         
141         for (int i=0; index < 32 && i<supportedPortletModes.length; i++) 
142         {
143             PortletMode mode = new PortletMode(supportedPortletModes[i]);
144             if ( !actionsMap.containsKey(mode.toString()) )
145             {
146                 actionsMap.put(mode.toString(), new Integer(index++));
147                 actionsList.add(mode);
148                 list.add(mode);
149             }
150             else if (!standardPortletModes.contains(mode))
151             {
152                 throw new IllegalArgumentException("PortletMode "+mode+" already defined as extended PortletMode or WindowState");
153             }
154         }
155         extendedPortletModes = Collections.unmodifiableList(list);
156         
157         actions = actionsList.toArray();
158         
159         instance = this;
160     }
161 
162     public static List getExtendedPortletModes()
163     {
164         return instance.extendedPortletModes;
165     }
166     
167     public static List getExtendedWindowStates()
168     {
169         return instance.extendedWindowStates;
170     }
171     
172     public static int getContainerActionMask(String action)
173     {
174         Integer index = (Integer)instance.actionsMap.get(action);
175         if ( index == null )
176         {
177             throw new IllegalArgumentException("Unknown action: "+action);
178         }
179         return 1<<index.intValue();
180     }
181     
182     public static String getContainerAction(int index)
183     {
184         JetspeedActions ja = JetspeedActions.instance;
185         return index > -1 && index < ja.actions.length ? ja.actions[index].toString() : null;
186     }
187     
188     public static String getContainerActions(int mask)
189     {
190         JetspeedActions ja = JetspeedActions.instance;
191         StringBuffer buffer = new StringBuffer();
192         boolean append = false;
193         
194         for ( int i = 0, j=1<<i; i < ja.actions.length; i++, j=1<<i )
195         {
196             if ( (mask & j) == j )
197             {
198                 if ( append )
199                     buffer.append(", ");
200                 else
201                     append = true;
202                 buffer.append(ja.actions[i].toString());
203             }
204         }
205         return buffer.toString();
206     }
207     
208     public static int getContainerActionsMask(String actions)
209     {
210         int mask = 0;
211         
212         if ( actions != null )
213         {
214             JetspeedActions ja = JetspeedActions.instance;
215             
216             StringTokenizer tokenizer = new StringTokenizer(actions, ",\t ");
217 
218             Integer index;
219             while (tokenizer.hasMoreTokens())
220             {
221                 String action = tokenizer.nextToken();
222                 index = (Integer)ja.actionsMap.get(action);
223                 if ( index == null )
224                     throw new IllegalArgumentException("Unknown action: " + action);
225                 mask |= (1 << index.intValue());
226             }
227         }        
228         return mask;
229     }
230 }