001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit;
039:
040: import java.util.ArrayList;
041: import java.util.List;
042: import java.util.ListIterator;
043:
044: import com.gargoylesoftware.htmlunit.html.FrameWindow;
045:
046: /**
047: * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br/>
048: *
049: * Base class for common WebWindow functionality. While public, this class is not
050: * exposed in any other places of the API. Internally we can cast to this class
051: * when we need access to functionality that is not present in {@link WebWindow}
052: *
053: * @version $Revision: 2132 $
054: * @author Brad Clarke
055: * @author David K. Taylor
056: * @author Ahmed Ashour
057: */
058: public abstract class WebWindowImpl implements WebWindow {
059: private WebClient webClient_;
060: private Page enclosedPage_;
061: private Object scriptObject_;
062: private ThreadManager threadManager_ = new ThreadManager();
063: private List childWindows_ = new ArrayList();
064: private String name_ = "";
065:
066: /**
067: * Never call this, used for Serialization.
068: * @deprecated
069: */
070: protected WebWindowImpl() {
071: }
072:
073: /**
074: * Creates a window and associates it with the client
075: *
076: * @param webClient The web client that "owns" this window.
077: */
078: public WebWindowImpl(final WebClient webClient) {
079: Assert.notNull("webClient", webClient);
080: webClient_ = webClient;
081: performRegistration();
082: }
083:
084: /**
085: * Registers the window with the client.
086: */
087: protected void performRegistration() {
088: webClient_.registerWebWindow(this );
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: public WebClient getWebClient() {
095: return webClient_;
096: }
097:
098: /**
099: * {@inheritDoc}
100: */
101: public Page getEnclosedPage() {
102: return enclosedPage_;
103: }
104:
105: /**
106: * {@inheritDoc}
107: */
108: public void setEnclosedPage(final Page page) {
109: if (page == enclosedPage_) {
110: return;
111: }
112: destroyChildren();
113: enclosedPage_ = page;
114: if (isJavaScriptInitializationNeeded()) {
115: webClient_.initialize(this );
116: }
117: webClient_.initialize(page);
118: }
119:
120: /**
121: * Returns <tt>true</tt> if this window needs JavaScript initialization to occur when the enclosed page is set.
122: * @return <tt>true</tt> if this window needs JavaScript initialization to occur when the enclosed page is set
123: */
124: protected abstract boolean isJavaScriptInitializationNeeded();
125:
126: /**
127: * {@inheritDoc}
128: */
129: public void setScriptObject(final Object scriptObject) {
130: scriptObject_ = scriptObject;
131: }
132:
133: /**
134: * {@inheritDoc}
135: */
136: public Object getScriptObject() {
137: return scriptObject_;
138: }
139:
140: /**
141: * {@inheritDoc}
142: */
143: public ThreadManager getThreadManager() {
144: return threadManager_;
145: }
146:
147: /**
148: * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br/>
149: *
150: * Adds a child to this window, for shutdown purposes.
151: *
152: * @param child The child window to associate with this window.
153: */
154: public void addChildWindow(final FrameWindow child) {
155: childWindows_.add(child);
156: }
157:
158: void destroyChildren() {
159: getThreadManager().interruptAll();
160: final ListIterator iter = childWindows_.listIterator();
161: while (iter.hasNext()) {
162: final WebWindowImpl child = (WebWindowImpl) iter.next();
163: child.destroyChildren();
164: iter.remove();
165: }
166: }
167:
168: /**
169: * {@inheritDoc}
170: */
171: public String getName() {
172: return name_;
173: }
174:
175: /**
176: * {@inheritDoc}
177: */
178: public void setName(final String name) {
179: name_ = name;
180: }
181: }
|