1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.wsrp4j.util;
18  
19  import java.util.HashMap;
20  
21  import javax.portlet.WindowState;
22  
23  public class WindowStates implements java.io.Serializable {
24      private String _value_;
25  
26      private static HashMap _table_ = new HashMap();
27  
28      // Constructor
29      protected WindowStates(String value) {
30          _value_ = value;
31          _table_.put(_value_, this);
32      }
33  
34      // define the window states we can currently handle
35      public static final String _normal = "wsrp:normal";
36  
37      public static final String _minimized = "wsrp:minimized";
38  
39      public static final String _maximized = "wsrp:maximized";
40  
41      public static final String _solo = "wsrp:solo";
42  
43      public static final WindowStates normal = new WindowStates(_normal);
44  
45      public static final WindowStates minimized = new WindowStates(_minimized);
46  
47      public static final WindowStates maximized = new WindowStates(_maximized);
48  
49      public static final WindowStates solo = new WindowStates(_solo);
50  
51      public String getValue() {
52          return _value_;
53      }
54  
55      /**
56       * Returns the WSRP window state build from a string representation
57       * If a not supported window state is requested, null is returned
58       * @param value <code>String</string> representation of the WSRP window state
59       * @return The WSRP <code>WindowStates</code> represented by the passed string
60       */
61      public static WindowStates fromValue(String value) {
62          return (WindowStates) _table_.get(value);
63      }
64  
65      /**
66       * Returns the WSRP window state build from a string representation
67       * If a not supported window state is requested, null is returned
68       * @param value <code>String</string> representation of the WSRP window state
69       * @return The WSRP <code>WindowStates</code> represented by the passed string
70       */
71      public static WindowStates fromString(String value) {
72          return fromValue(value);
73      }
74  
75      public boolean equals(java.lang.Object obj) {
76          return (obj == this);
77      }
78  
79      public int hashCode() {
80          return toString().hashCode();
81      }
82  
83      public String toString() {
84          return _value_;
85      }
86  
87      public java.lang.Object readResolve() throws java.io.ObjectStreamException {
88          return fromValue(_value_);
89      }
90  
91      /**
92       * This helper method maps portlet window states defined in wsrp to portlet 
93       * window states defined in the java portlet standard (JSR-168). 
94       * If the passed wsrp window state is null or can not be mapped 
95       * directly the normal state is returned. 
96       *
97       * @return The <code>javax.portlet.WindowState</code> which corresponds to the given wsrp state.
98       **/
99      public static WindowState getJsrPortletStateFromWsrpState(
100             WindowStates wsrpState) {
101         if (wsrpState == null) {
102             return WindowState.NORMAL;
103         }
104         else if (wsrpState.equals(WindowStates.maximized)) {
105             return WindowState.MAXIMIZED;
106         }
107         else if (wsrpState.equals(WindowStates.minimized)) {
108             return WindowState.MINIMIZED;
109         }
110         else if (wsrpState.equals(WindowStates.normal)) {
111             return WindowState.NORMAL;
112         }
113 
114         return WindowState.NORMAL;
115     }
116 
117     /**
118      * This helper method maps portlet window states defined in tha java portlet standard (JSR-168)
119      * to window states defined in wsrp. If the passed state can not be resolved wsrp:normal state
120      * is returned.
121      *
122      * @param portletState The <code>javax.portlet.WindowState</code> which should be resolved as
123      *                     as portlet window state defined in wsrp.
124      * @return     
125      **/
126     public static WindowStates getWsrpStateFromJsrPortletState(
127             WindowState portletState) {
128         if (portletState.equals(WindowState.MAXIMIZED)) {
129             return WindowStates.maximized;
130         }
131         else if (portletState.equals(WindowState.MINIMIZED)) {
132             return WindowStates.minimized;
133         }
134         else if (portletState.equals(WindowState.NORMAL)) {
135             return WindowStates.normal;
136         }
137 
138         return WindowStates.normal;
139     }
140 
141     public static String[] getWindowStatesAsStringArray() {
142         return (String[]) _table_.keySet().toArray();
143     }
144 }