001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision: 1.3 $
020: */package java.applet;
021:
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.awt.GraphicsEnvironment;
025: import java.awt.HeadlessException;
026: import java.awt.Image;
027: import java.awt.Panel;
028: import java.awt.Toolkit;
029: import java.awt.Window;
030: import java.io.IOException;
031: import java.io.ObjectInputStream;
032: import java.net.MalformedURLException;
033: import java.net.URL;
034: import java.util.Locale;
035:
036: import javax.accessibility.AccessibleContext;
037: import javax.accessibility.AccessibleRole;
038: import javax.accessibility.AccessibleState;
039: import javax.accessibility.AccessibleStateSet;
040:
041: import org.apache.harmony.applet.AudioClipImpl;
042: import org.apache.harmony.awt.AWTPermissionCollection;
043: import org.apache.harmony.awt.FieldsAccessor;
044:
045: public class Applet extends Panel {
046:
047: private static final long serialVersionUID = -5836846270535785031L;
048:
049: private transient AppletStub stub;
050:
051: private transient AccessibleContext accessibleContext;
052:
053: /** monitor for applet's private data */
054: private final transient Object appletLock = new Object();
055:
056: protected class AccessibleApplet extends Panel.AccessibleAWTPanel {
057:
058: private static final long serialVersionUID = 8127374778187708896L;
059:
060: protected AccessibleApplet() {
061: super ();
062: }
063:
064: @Override
065: public AccessibleRole getAccessibleRole() {
066: return AccessibleRole.FRAME;
067: }
068:
069: @Override
070: public AccessibleStateSet getAccessibleStateSet() {
071: AccessibleStateSet set = super .getAccessibleStateSet();
072: Window owner = null;
073: for (Component c = Applet.this ; c != null; c = c
074: .getParent()) {
075: if (c instanceof Window) {
076: owner = (Window) c;
077: break;
078: }
079: }
080:
081: if (owner != null) {
082: if (owner.isActive()) {
083: set.add(AccessibleState.ACTIVE);
084: }
085: }
086: return set;
087: }
088: }
089:
090: public Applet() throws HeadlessException {
091: if (GraphicsEnvironment.isHeadless()) {
092: throw new HeadlessException();
093: }
094: }
095:
096: public void init() {
097: // do nothing by specification
098: }
099:
100: public void start() {
101: // do nothing by specification
102: }
103:
104: public void destroy() {
105: // do nothing by specification
106: }
107:
108: public void stop() {
109: // do nothing by specification
110: }
111:
112: @SuppressWarnings("deprecation")
113: @Deprecated
114: @Override
115: public void resize(int width, int height) {
116: synchronized (appletLock) {
117: stub.appletResize(width, height);
118: }
119: }
120:
121: @SuppressWarnings("deprecation")
122: @Deprecated
123: @Override
124: public void resize(Dimension d) {
125: synchronized (appletLock) {
126: stub.appletResize(d.width, d.height);
127: }
128: }
129:
130: public URL getCodeBase() {
131: synchronized (appletLock) {
132: return stub.getCodeBase();
133: }
134: }
135:
136: @Override
137: public AccessibleContext getAccessibleContext() {
138: synchronized (appletLock) {
139: if (accessibleContext == null) {
140: accessibleContext = new AccessibleApplet();
141: }
142: return accessibleContext;
143: }
144: }
145:
146: public AppletContext getAppletContext() {
147: synchronized (appletLock) {
148: return stub.getAppletContext();
149: }
150: }
151:
152: public String getAppletInfo() {
153: // return null by specification
154: return null;
155: }
156:
157: public AudioClip getAudioClip(URL url, String name) {
158: return getAppletContext().getAudioClip(appendURL(url, name));
159: }
160:
161: public AudioClip getAudioClip(URL url) {
162: return getAppletContext().getAudioClip(url);
163: }
164:
165: public URL getDocumentBase() {
166: synchronized (appletLock) {
167: return stub.getDocumentBase();
168: }
169: }
170:
171: public Image getImage(URL url) {
172: return getAppletContext().getImage(url);
173: }
174:
175: public Image getImage(URL url, String name) {
176: return getAppletContext().getImage(appendURL(url, name));
177: }
178:
179: @Override
180: public Locale getLocale() {
181: return super .getLocale();
182: }
183:
184: public String getParameter(String name) {
185: synchronized (appletLock) {
186: return stub.getParameter(name);
187: }
188: }
189:
190: public String[][] getParameterInfo() {
191: // return null by specification
192: return null;
193: }
194:
195: public boolean isActive() {
196: synchronized (appletLock) {
197: return stub.isActive();
198: }
199: }
200:
201: public static final AudioClip newAudioClip(URL url) {
202: return new AudioClipImpl(url);
203: }
204:
205: public void play(URL url, String name) {
206: AudioClip clip = getAudioClip(url, name);
207: clip.play();
208: }
209:
210: public void play(URL url) {
211: AudioClip clip = getAudioClip(url);
212: clip.play();
213: }
214:
215: public final void setStub(AppletStub stub) {
216: SecurityManager sm = System.getSecurityManager();
217: if (sm != null) {
218: sm
219: .checkPermission(AWTPermissionCollection.SET_APPLET_STUB_PERMISSION);
220: }
221: synchronized (appletLock) {
222: this .stub = stub;
223: }
224: }
225:
226: public void showStatus(String msg) {
227: synchronized (appletLock) {
228: stub.getAppletContext().showStatus(msg);
229: }
230: }
231:
232: private static URL appendURL(URL url, String name) {
233: try {
234: return new URL(url, name);
235: } catch (MalformedURLException e) {
236: return null;
237: }
238: }
239:
240: private void readObject(ObjectInputStream stream)
241: throws IOException, ClassNotFoundException {
242:
243: stream.defaultReadObject();
244: FieldsAccessor accessor = new FieldsAccessor(Applet.class, this );
245: accessor.set("appletLock", new Object());
246: }
247:
248: }
|