001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.model.portal.navstate;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.common.invocation.AttributeResolver;
027: import org.jboss.portal.core.model.portal.Window;
028: import org.jboss.portal.core.navstate.NavigationalStateKey;
029: import org.jboss.portal.portlet.StateString;
030:
031: import java.io.Serializable;
032:
033: /**
034: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
035: * @version $Revision: 8786 $
036: */
037: public final class WindowNavigationalState implements Serializable {
038:
039: /** . */
040: private static final WindowNavigationalState DEFAULT = new WindowNavigationalState();
041:
042: /** . */
043: private final WindowState windowState;
044:
045: /** . */
046: private final Mode mode;
047:
048: /** . */
049: private final StateString contentState;
050:
051: public static WindowNavigationalState create() {
052: return DEFAULT;
053: }
054:
055: public WindowNavigationalState(WindowState windowState, Mode mode,
056: StateString contentState) {
057: if (windowState == null) {
058: throw new IllegalArgumentException(
059: "No null window state accepted");
060: }
061: if (mode == null) {
062: throw new IllegalArgumentException("No null mode accepted");
063: }
064: this .windowState = windowState;
065: this .mode = mode;
066: this .contentState = contentState;
067: }
068:
069: private WindowNavigationalState() {
070: this (WindowState.NORMAL, Mode.VIEW, null);
071: }
072:
073: public WindowState getWindowState() {
074: return windowState;
075: }
076:
077: public Mode getMode() {
078: return mode;
079: }
080:
081: public StateString getContentState() {
082: return contentState;
083: }
084:
085: public static WindowState getWindowState(
086: AttributeResolver resolver, Object id) {
087: NavigationalStateKey key = new NavigationalStateKey(
088: WindowNavigationalState.class, id);
089:
090: //
091: WindowNavigationalState wns = (WindowNavigationalState) resolver
092: .getAttribute(key);
093:
094: //
095: return wns != null ? wns.getWindowState() : null;
096: }
097:
098: public static void setWindowState(AttributeResolver resolver,
099: Object id, WindowState windowState) {
100: setWindowState(resolver, new NavigationalStateKey(
101: WindowNavigationalState.class, id), windowState);
102: }
103:
104: public static void setWindowState(AttributeResolver resolver,
105: NavigationalStateKey key, WindowState windowState) {
106: if (resolver == null) {
107: throw new IllegalArgumentException("No null resolver");
108: }
109: if (key == null) {
110: throw new IllegalArgumentException("No null key");
111: }
112: if (windowState == null) {
113: throw new IllegalArgumentException("No null window state");
114: }
115:
116: //
117: WindowNavigationalState wns = (WindowNavigationalState) resolver
118: .getAttribute(key);
119:
120: //
121: if (wns == null) {
122: wns = new WindowNavigationalState(windowState, Mode.VIEW,
123: null);
124: } else {
125: wns = new WindowNavigationalState(windowState, wns
126: .getMode(), wns.getContentState());
127: }
128:
129: //
130: resolver.setAttribute(key, wns);
131: }
132:
133: public static Mode getMode(AttributeResolver resolver, Object id) {
134: NavigationalStateKey key = new NavigationalStateKey(
135: WindowNavigationalState.class, id);
136:
137: //
138: WindowNavigationalState wns = (WindowNavigationalState) resolver
139: .getAttribute(key);
140:
141: //
142: return wns != null ? wns.getMode() : null;
143: }
144:
145: public static void setMode(AttributeResolver context, Object id,
146: Mode mode) {
147: setMode(context, new NavigationalStateKey(
148: WindowNavigationalState.class, id), mode);
149: }
150:
151: public static void setMode(AttributeResolver resolver,
152: NavigationalStateKey key, Mode mode) {
153: if (resolver == null) {
154: throw new IllegalArgumentException("No null resolver");
155: }
156: if (key == null) {
157: throw new IllegalArgumentException("No null key");
158: }
159: if (mode == null) {
160: throw new IllegalArgumentException("No null mode");
161: }
162:
163: //
164: WindowNavigationalState wns = (WindowNavigationalState) resolver
165: .getAttribute(key);
166:
167: //
168: if (wns == null) {
169: wns = new WindowNavigationalState(WindowState.NORMAL, mode,
170: null);
171: } else {
172: wns = new WindowNavigationalState(wns.getWindowState(),
173: mode, wns.getContentState());
174: }
175:
176: //
177: resolver.setAttribute(key, wns);
178: }
179:
180: public static StateString getState(AttributeResolver resolver,
181: Object id) {
182: NavigationalStateKey key = new NavigationalStateKey(
183: WindowNavigationalState.class, id);
184:
185: //
186: WindowNavigationalState wns = (WindowNavigationalState) resolver
187: .getAttribute(key);
188:
189: //
190: return wns != null ? wns.getContentState() : null;
191: }
192:
193: public static void setState(AttributeResolver resolver, Object id,
194: StateString state) {
195: setState(resolver, new NavigationalStateKey(
196: WindowNavigationalState.class, id), state);
197: }
198:
199: public static void setState(AttributeResolver resolver,
200: NavigationalStateKey key, StateString state) {
201: if (resolver == null) {
202: throw new IllegalArgumentException("No null resolver");
203: }
204: if (key == null) {
205: throw new IllegalArgumentException("No null key");
206: }
207: if (state == null) {
208: throw new IllegalArgumentException("No null state");
209: }
210:
211: //
212: WindowNavigationalState wns = (WindowNavigationalState) resolver
213: .getAttribute(key);
214:
215: //
216: if (wns == null) {
217: wns = new WindowNavigationalState(WindowState.NORMAL,
218: Mode.VIEW, state);
219: } else {
220: wns = new WindowNavigationalState(wns.getWindowState(), wns
221: .getMode(), state);
222: }
223:
224: //
225: resolver.setAttribute(key, wns);
226: }
227:
228: public static void setState(AttributeResolver resolver,
229: NavigationalStateKey key, StateString state, Window window) {
230: if (resolver == null) {
231: throw new IllegalArgumentException("No null resolver");
232: }
233: if (key == null) {
234: throw new IllegalArgumentException("No null key");
235: }
236: if (state == null) {
237: throw new IllegalArgumentException("No null state");
238: }
239: if (window == null) {
240: throw new IllegalArgumentException("No null window");
241: }
242:
243: //
244: WindowNavigationalState wns = (WindowNavigationalState) resolver
245: .getAttribute(key);
246:
247: //
248: if (wns == null) {
249: wns = new WindowNavigationalState(window
250: .getInitialWindowState(), window.getInitialMode(),
251: state);
252: } else {
253: wns = new WindowNavigationalState(wns.getWindowState(), wns
254: .getMode(), state);
255: }
256:
257: //
258: resolver.setAttribute(key, wns);
259: }
260:
261: public static WindowNavigationalState bilto(
262: WindowNavigationalState oldNS, WindowState windowState,
263: Mode mode, StateString contentState) {
264: StateString newState = oldNS != null ? oldNS.getContentState()
265: : null;
266: WindowState newWindowState = oldNS != null ? oldNS
267: .getWindowState() : WindowState.NORMAL;
268: Mode newMode = oldNS != null ? oldNS.getMode() : Mode.VIEW;
269:
270: //
271: if (contentState != null) {
272: newState = contentState;
273: }
274:
275: //
276: if (mode != null) {
277: newMode = mode;
278: }
279:
280: //
281: if (windowState != null) {
282: newWindowState = windowState;
283: }
284:
285: // Create new NS
286: return new WindowNavigationalState(newWindowState, newMode,
287: newState);
288: }
289: }
|