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.impl.model.portal;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.core.model.content.Content;
027: import org.jboss.portal.core.model.content.ContentType;
028: import org.jboss.portal.core.model.content.spi.handler.ContentHandler;
029: import org.jboss.portal.core.model.content.spi.handler.ContentState;
030: import org.jboss.portal.core.model.portal.Page;
031: import org.jboss.portal.core.model.portal.PortalObject;
032: import org.jboss.portal.core.model.portal.Window;
033: import org.jboss.portal.jems.hibernate.ContextObject;
034:
035: import java.util.HashMap;
036: import java.util.Iterator;
037:
038: /**
039: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
040: * @version $Revision: 9134 $
041: */
042: public class WindowImpl extends PortalObjectImpl implements Window,
043: ContextObject {
044:
045: public static final String PORTAL_PROP_WINDOW_CONTENT_TYPE = "portal.windowContentType";
046:
047: public static final String PORTAL_INITIAL_WINDOW_STATE = "portal.windowInitialState";
048:
049: public static final String PORTAL_INITIAL_MODE = "portal.windowInitialMode";
050:
051: // Persistent state
052: protected String uri;
053:
054: // Runtime fields
055: protected AbstractPortalObjectContainer.ContainerContext containerContext;
056: protected ContentType contentType;
057: protected ContentStateImpl contentState;
058:
059: public WindowImpl() {
060: super (true);
061:
062: //
063: this .contentType = null;
064: this .uri = null;
065: }
066:
067: /**
068: * Creates a new window.
069: *
070: * @param contentType the window content type
071: * @throws IllegalArgumentException if the content type is null or the content URI is null
072: */
073: public WindowImpl(ContentType contentType, String contentURI)
074: throws IllegalArgumentException {
075: super (false);
076:
077: //
078: if (contentType == null) {
079: throw new IllegalArgumentException(
080: "No null content type accepted");
081: }
082: if (contentURI == null) {
083: throw new IllegalArgumentException(
084: "No null content URI accepted");
085: }
086:
087: //
088: this .contentType = contentType;
089: this .uri = contentURI;
090: }
091:
092: private ContentStateImpl getContentState() {
093: if (contentState == null) {
094: contentState = new ContentStateImpl();
095: }
096: return contentState;
097: }
098:
099: public void setContext(Object context) {
100: this .containerContext = (AbstractPortalObjectContainer.ContainerContext) context;
101: }
102:
103: public String getURI() {
104: return uri;
105: }
106:
107: public void setURI(String uri) {
108: this .uri = uri;
109: }
110:
111: public Page getPage() {
112: return (Page) getParent();
113: }
114:
115: public Content getContent() {
116: return getContentState().getContent();
117: }
118:
119: public ContentType getContentType() {
120: if (contentType == null) {
121: String value = getDeclaredProperty(PORTAL_PROP_WINDOW_CONTENT_TYPE);
122: if (value == null) {
123: // If nothing is provided then we use the default content type
124: contentType = containerContext.getDefaultContentType();
125: } else {
126: contentType = ContentType.create(value);
127: }
128: }
129: return contentType;
130: }
131:
132: public int getType() {
133: return PortalObject.TYPE_WINDOW;
134: }
135:
136: protected PortalObjectImpl cloneObject() {
137: WindowImpl clone = new WindowImpl();
138: clone.setURI(uri);
139: clone.setDeclaredPropertyMap(new HashMap(
140: getDeclaredPropertyMap()));
141: clone.setListener(getListener());
142: clone.setDisplayName(getDisplayName());
143: return clone;
144: }
145:
146: protected void destroy() {
147: // Destroy the associated content if it is necessary/possible
148: getContentState().destroy();
149: }
150:
151: /** Encapsulate content behavior for a window. */
152: private class ContentStateImpl implements ContentState {
153:
154: /** . */
155: private final String contextId = getId().toString();
156:
157: /** . */
158: private Content content;
159:
160: public String getURI() {
161: return WindowImpl.this .uri;
162: }
163:
164: public void setURI(String uri) {
165: WindowImpl.this .uri = uri;
166: }
167:
168: private void destroy() {
169: ContentHandler handler = getContentHandler();
170:
171: //
172: if (handler != null) {
173: handler.contentDestroyed(contextId, this );
174: }
175: }
176:
177: private Content getContent() {
178: if (content == null) {
179: ContentHandler handler = getContentHandler();
180:
181: //
182: if (handler != null) {
183: String contextId = getId().toString();
184: content = handler.newContent(contextId, this );
185: }
186: }
187: return content;
188: }
189:
190: private ContentHandler getContentHandler() {
191: ContentType contentType = getContentType();
192:
193: //
194: return containerContext.getContentHandler(contentType);
195: }
196:
197: public Iterator getParameterNames() {
198: return new Iterator() {
199: Iterator i = getDeclaredProperties().keySet()
200: .iterator();
201: String next;
202:
203: {
204: findNext();
205: }
206:
207: public boolean hasNext() {
208: return next != null;
209: }
210:
211: public Object next() {
212: String tmp = next;
213: findNext();
214: return tmp;
215: }
216:
217: public void remove() {
218: throw new UnsupportedOperationException();
219: }
220:
221: void findNext() {
222: next = null;
223: while (i.hasNext() && next == null) {
224: String propertyName = (String) i.next();
225: if (propertyName.startsWith("content.")) {
226: next = propertyName.substring(8);
227: }
228: }
229: }
230: };
231: }
232:
233: public void setParameter(String name, String value)
234: throws IllegalArgumentException {
235: setDeclaredProperty("content." + name, value);
236: }
237:
238: public String getParameter(String name)
239: throws IllegalArgumentException {
240: return getDeclaredProperty("content." + name);
241: }
242:
243: public void clearParameters() {
244: for (Iterator i = getDeclaredProperties().keySet()
245: .iterator(); i.hasNext();) {
246: String propertyName = (String) i.next();
247: if (propertyName.startsWith("content.")) {
248: i.remove();
249: }
250: }
251: }
252: }
253:
254: public WindowState getInitialWindowState() {
255: String value = getDeclaredProperty(PORTAL_INITIAL_WINDOW_STATE);
256: if (value != null) {
257: return WindowState.create(value);
258: } else {
259: return WindowState.NORMAL;
260: }
261: }
262:
263: public Mode getInitialMode() {
264: String value = getDeclaredProperty(PORTAL_INITIAL_MODE);
265: if (value != null) {
266: return Mode.create(value);
267: } else {
268: return Mode.VIEW;
269: }
270: }
271: }
|