001    /*
002     * Created on Jul 10, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2008-2010 the original author or authors.
015     */
016    package org.fest.swing.applet;
017    
018    import static java.util.Collections.enumeration;
019    import static org.fest.util.Collections.list;
020    
021    import java.applet.*;
022    import java.awt.Image;
023    import java.io.InputStream;
024    import java.net.URL;
025    import java.util.*;
026    
027    /**
028     * Understands a basic (and limited) implementation of <code>{@link AppletContext}</code>.
029     *
030     * @author Alex Ruiz
031     * @author Yvonne Wang
032     */
033    public class BasicAppletContext implements AppletContext {
034    
035      private static final Enumeration<Applet> NO_APPLETS = new EmptyAppletEnumeration();
036    
037      private static final class EmptyAppletEnumeration implements Enumeration<Applet> {
038        EmptyAppletEnumeration() {}
039    
040        public boolean hasMoreElements() {
041          return false;
042        }
043    
044        public Applet nextElement() {
045          return null;
046        }
047      }
048    
049      private final Map<String, InputStream> streamMap = new HashMap<String, InputStream>();
050      private final StatusDisplay statusDisplay;
051    
052      /**
053       * Creates a new </code>{@link BasicAppletContext}</code>.
054       * @param statusDisplay where the application can inform users of its current state.
055       * @throws NullPointerException if <code>statusDisplay</code> is <code>null</code>.
056       */
057      public BasicAppletContext(StatusDisplay statusDisplay) {
058        if (statusDisplay == null) throw new NullPointerException("Instance of StatusDisplay should not be null");
059        this.statusDisplay = statusDisplay;
060      }
061    
062      /**
063       * If the <code>{@link StatusDisplay}</code> passed to this context is an instance of
064       * <code>{@link AppletViewer}</code>, this method will return the {@code Applet} of such {@code AppletViewer}.
065       * Otherwise this method returns <code>null</code>.
066       * @return the {@code Applet} in this context's {@link StatusDisplay} (if any.)
067       * @see AppletContext#getApplet(String)
068       */
069      public Applet getApplet(String name) {
070        return appletFrom(statusDisplay);
071      }
072    
073      /**
074       * Returns an enumeration containing the {@code Applet} returned by <code>{@link #getApplet(String)}</code>. If
075       * <code>{@link #getApplet(String)}</code> returns <code>null</code>, this method will return an empty enumeration.
076       * @return an enumeration containing the {@code Applet} in this context's {@link StatusDisplay} (if any.)
077       * @see #getApplet(String)
078       * @see AppletContext#getApplets()
079       */
080      public Enumeration<Applet> getApplets() {
081        Applet applet = appletFrom(statusDisplay);
082        if (applet == null) return NO_APPLETS;
083        return enumeration(list(applet));
084      }
085    
086      private static Applet appletFrom(StatusDisplay statusDisplay) {
087        if (!(statusDisplay instanceof AppletViewer)) return null;
088        AppletViewer viewer = (AppletViewer)statusDisplay;
089        return viewer.applet();
090      }
091    
092      /**
093       * Not implemented. Returns <code>null</code>.
094       * @see AppletContext#getAudioClip(URL)
095       */
096      public AudioClip getAudioClip(URL url) { return null; }
097    
098      /**
099       * Not implemented. Returns <code>null</code>.
100       * @see AppletContext#getImage(URL)
101       */
102      public Image getImage(URL url) { return null; }
103    
104      /**
105       * Returns the stream to which specified key is associated within this applet context. Returns <code>null</code> if
106       * the applet context contains no stream for this key.
107       * @return the stream to which this applet context maps the key.
108       * @param key key whose associated stream is to be returned.
109       */
110      public InputStream getStream(String key) {
111        return streamMap.get(key);
112      }
113    
114      /**
115       * Finds all the keys of the streams in this applet context.
116       * @return an iterator of all the names of the streams in this applet context.
117       */
118      public Iterator<String> getStreamKeys() {
119        return streamMap.keySet().iterator();
120      }
121    
122      /**
123       * Associates the specified stream with the specified key in this applet context.
124       * @param key key with which the specified value is to be associated.
125       * @param stream stream to be associated with the specified key. If this parameter is <code>null</code>, the specified
126       * key is removed in this applet context.
127       */
128      public void setStream(String key, InputStream stream) {
129        if (stream == null) {
130          streamMap.remove(key);
131          return;
132        }
133        streamMap.put(key, stream);
134      }
135    
136      /**
137       * Not implemented.
138       * @see AppletContext#showDocument(URL)
139       */
140      public void showDocument(URL url) {}
141    
142      /**
143       * Not implemented.
144       * @see AppletContext#showDocument(URL, String)
145       */
146      public void showDocument(URL url, String target) {}
147    
148      /**
149       * Requests that the given status be displayed in this context's <code>{@link StatusDisplay}</code>.
150       * @param status a message to display.
151       */
152      public void showStatus(String status) {
153        statusDisplay.showStatus(status);
154      }
155    }