001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005: package org.jasig.portal.container.services.information;
006:
007: import java.io.IOException;
008: import java.io.ObjectInputStream;
009: import java.io.ObjectOutputStream;
010: import java.io.Serializable;
011:
012: import javax.portlet.PortletMode;
013: import javax.portlet.WindowState;
014:
015: /**
016: * Class for tracking the current and previous WindowState and PortletMode for
017: * a PortletWindow. The previous mode and state fields are automaticly updated
018: * when the current mode and state are updated. The getters/setters are
019: * synchronized since setting the previous and current is not an atomic
020: * operation.
021: *
022: * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
023: * @version $Id: PortletWindowStateInfo.java 35884 2005-06-07 00:01:07Z edalquist $
024: */
025: public class PortletWindowStateInfo implements Serializable {
026: private final Lock stateLock = new Lock();
027: private final Lock modeLock = new Lock();
028:
029: private transient WindowState currentState = WindowState.NORMAL;
030: private transient WindowState previousState = WindowState.NORMAL;
031: private transient PortletMode currentMode = PortletMode.VIEW;
032: private transient PortletMode previousMode = PortletMode.VIEW;
033:
034: /**
035: * @return Returns the currentMode.
036: */
037: public PortletMode getCurrentMode() {
038: synchronized (this .modeLock) {
039: return this .currentMode;
040: }
041: }
042:
043: /**
044: * @return Returns the currentState.
045: */
046: public WindowState getCurrentState() {
047: synchronized (this .stateLock) {
048: return this .currentState;
049: }
050: }
051:
052: /**
053: * @return Returns the previousMode.
054: */
055: public PortletMode getPreviousMode() {
056: synchronized (this .modeLock) {
057: return this .previousMode;
058: }
059: }
060:
061: /**
062: * @return Returns the previousState.
063: */
064: public WindowState getPreviousState() {
065: synchronized (this .stateLock) {
066: return this .previousState;
067: }
068: }
069:
070: /**
071: * @param currentMode The currentMode to set.
072: */
073: public void setCurrentMode(PortletMode currentMode) {
074: if (currentMode == null)
075: throw new IllegalArgumentException(
076: "currentMode cannot be null");
077:
078: synchronized (this .modeLock) {
079: this .previousMode = this .currentMode;
080: this .currentMode = currentMode;
081: }
082: }
083:
084: /**
085: * @param currentState The currentState to set.
086: */
087: public void setCurrentState(WindowState currentState) {
088: if (currentState == null)
089: throw new IllegalArgumentException(
090: "currentState cannot be null");
091:
092: synchronized (this .stateLock) {
093: this .previousState = this .currentState;
094: this .currentState = currentState;
095: }
096: }
097:
098: /**
099: * @see java.lang.Object#toString()
100: */
101: public String toString() {
102: final StringBuffer buff = new StringBuffer();
103:
104: buff.append("[");
105: buff.append("currentMode='").append(this .currentMode).append(
106: "', ");
107: buff.append("previousMode='").append(this .previousMode).append(
108: "', ");
109: buff.append("currentState='").append(this .currentState).append(
110: "', ");
111: buff.append("previousState='").append(this .previousState)
112: .append("'");
113: buff.append("]");
114:
115: return buff.toString();
116: }
117:
118: /**
119: * Used by the Java serialization system to write this object out.
120: *
121: * @see Serializable
122: */
123: private void writeObject(ObjectOutputStream out) throws IOException {
124: //Write out non-transient fields
125: out.defaultWriteObject();
126:
127: //Write modes and states as strings since they aren't serializable
128: out.writeObject(this .currentMode.toString());
129: out.writeObject(this .previousMode.toString());
130: out.writeObject(this .currentState.toString());
131: out.writeObject(this .previousState.toString());
132: }
133:
134: /**
135: * Used by the Java serialization system to re-create this object
136: *
137: * @see Serializable
138: */
139: private void readObject(ObjectInputStream in) throws IOException,
140: ClassNotFoundException {
141: //Read in non-transient fields
142: in.defaultReadObject();
143:
144: //Read in mode and state strings
145: final String currentModeStr = (String) in.readObject();
146: final String previousModeStr = (String) in.readObject();
147: final String currentStateStr = (String) in.readObject();
148: final String previousStateStr = (String) in.readObject();
149:
150: //Convert mode and state strings to real objects
151: this .currentMode = this .stringToMode(currentModeStr);
152: this .previousMode = this .stringToMode(previousModeStr);
153: this .currentState = this .stringToState(currentStateStr);
154: this .previousState = this .stringToState(previousStateStr);
155: }
156:
157: /**
158: * Utility to converting PortletMode string names to objects. Tries
159: * to re-use standard objects before resorting to creating a new object.
160: *
161: * @param modeName The name of the mode.
162: * @return The PorltetMode representing the name
163: */
164: private PortletMode stringToMode(String modeName) {
165: if (PortletMode.VIEW.toString().equalsIgnoreCase(modeName))
166: return PortletMode.VIEW;
167: else if (PortletMode.EDIT.toString().equalsIgnoreCase(modeName))
168: return PortletMode.EDIT;
169: else if (PortletMode.HELP.toString().equalsIgnoreCase(modeName))
170: return PortletMode.HELP;
171: else
172: return new PortletMode(modeName);
173: }
174:
175: /**
176: * Utility to converting WindowState string names to objects. Tries
177: * to re-use standard objects before resorting to creating a new object.
178: *
179: * @param stateName The name of the state.
180: * @return The WindowState representing the name
181: */
182: private WindowState stringToState(String stateName) {
183: if (WindowState.NORMAL.toString().equalsIgnoreCase(stateName))
184: return WindowState.NORMAL;
185: else if (WindowState.MINIMIZED.toString().equalsIgnoreCase(
186: stateName))
187: return WindowState.MINIMIZED;
188: else if (WindowState.MAXIMIZED.toString().equalsIgnoreCase(
189: stateName))
190: return WindowState.MAXIMIZED;
191: else
192: return new WindowState(stateName);
193: }
194:
195: /**
196: * Utility class that is used for synchornization. Keeping the two
197: * lock fields final is important to ensure the synchronization works
198: * correctly. Had to create a custom class instead of using Object since
199: * Object is not serializable.
200: */
201: private class Lock implements Serializable {
202: }
203: }
|