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 java.applet.AppletContext;
019    import java.applet.AppletStub;
020    import java.awt.Window;
021    import java.net.URL;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    /**
026     * Understands a basic (and limited) implementation of <code>{@link AppletStub}</code>.
027     *
028     * @author Alex Ruiz
029     * @author Yvonne Wang
030     */
031    public class BasicAppletStub implements AppletStub {
032    
033      private final Window viewer;
034      private final AppletContext context;
035      private final Map<String, String> parameters = new HashMap<String, String>();
036    
037      /**
038       * Creates a new </code>{@link BasicAppletStub}</code>.
039       * @param viewer the window to host the applet.
040       * @param context the applet context.
041       * @param parameters the parameters included in an applet HTML tag.
042       * @throws NullPointerException if <code>viewer</code> is <code>null</code>.
043       * @throws NullPointerException if <code>context</code> is <code>null</code>.
044       * @throws NullPointerException if <code>parameters</code> is <code>null</code>.
045       */
046      public BasicAppletStub(Window viewer, AppletContext context, Map<String, String> parameters) {
047        this(viewer, context);
048        if (parameters == null) throw new NullPointerException("The map of parameters should not be null");
049        this.parameters.putAll(parameters);
050      }
051    
052      /**
053       * Creates a new </code>{@link BasicAppletStub}</code>.
054       * @param viewer the window to host the applet.
055       * @param context the applet context.
056       * @throws NullPointerException if <code>viewer</code> is <code>null</code>.
057       * @throws NullPointerException if <code>context</code> is <code>null</code>.
058       */
059      public BasicAppletStub(Window viewer, AppletContext context) {
060        if (viewer == null) throw new NullPointerException("The apple viewer should not be null");
061        if (context == null) throw new NullPointerException("The AppletContext should not be null");
062        this.viewer = viewer;
063        this.context = context;
064      }
065    
066      /**
067       * Not implemented. Returns <code>true</code>.
068       * @see AppletStub#isActive()
069       */
070      public boolean isActive() {
071        return true;
072      }
073      
074      /**
075       * Resizes this stub's viewer when the applet wants to be resized.
076       * @param width the new requested width for the applet.
077       * @param height the new requested height for the applet.
078       */
079      public void appletResize(int width, int height) {
080        viewer.setSize(width, height);
081      }
082    
083      /**
084       * Returns the applet's context.
085       * @return the applet's context.
086       */
087      public AppletContext getAppletContext() {
088        return context;
089      }
090    
091      /**
092       * Not implemented. Returns <code>getClass().getResource(".")</code>.
093       * @see AppletStub#getCodeBase()
094       */
095      public URL getCodeBase() {
096        return getDocumentBase();
097      }
098    
099      /**
100       * Not implemented. Returns <code>getClass().getResource(".")</code>.
101       * @see AppletStub#getDocumentBase()
102       */
103      public URL getDocumentBase() {
104        return Thread.currentThread().getContextClassLoader().getResource(".");
105      }
106    
107      /**
108       * Returns the value of the named parameter in the HTML tag.
109       * @param name a parameter name.
110       * @return the value of the named parameter, or <code>null</code> if not set.
111       */
112      public String getParameter(String name) {
113        return parameters.get(name);
114      }
115    }