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