View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.javascript.host.html;
16  
17  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
20  
21  import java.applet.Applet;
22  import java.lang.reflect.Method;
23  
24  import com.gargoylesoftware.htmlunit.html.DomNode;
25  import com.gargoylesoftware.htmlunit.html.HtmlApplet;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
30  
31  import net.sourceforge.htmlunit.corejs.javascript.BaseFunction;
32  import net.sourceforge.htmlunit.corejs.javascript.Context;
33  import net.sourceforge.htmlunit.corejs.javascript.Function;
34  import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
35  import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
36  
37  /**
38   * The JavaScript object {@code HTMLAppletElement}.
39   *
40   * @author Ahmed Ashour
41   * @author Marc Guillemot
42   * @author Daniel Gredler
43   */
44  @JsxClass(domClass = HtmlApplet.class, value = {FF, IE, EDGE})
45  public class HTMLAppletElement extends HTMLElement {
46  
47      /**
48       * The constructor.
49       */
50      @JsxConstructor({FF, EDGE})
51      public HTMLAppletElement() {
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public void setDomNode(final DomNode domNode) {
59          super.setDomNode(domNode);
60  
61          if (domNode.getPage().getWebClient().getOptions().isAppletEnabled()) {
62              try {
63                  createAppletMethodAndProperties();
64              }
65              catch (final Exception e) {
66                  throw new RuntimeException(e);
67              }
68          }
69      }
70  
71      private void createAppletMethodAndProperties() throws Exception {
72          final HtmlApplet appletNode = (HtmlApplet) getDomNodeOrDie();
73          final Applet applet = appletNode.getApplet();
74          if (applet == null) {
75              return;
76          }
77  
78          // Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
79          for (final Method method : applet.getClass().getMethods()) {
80              final Function f = new BaseFunction() {
81                  @Override
82                  public Object call(final Context cx, final Scriptable scope,
83                          final Scriptable thisObj, final Object[] args) {
84  
85                      final Object[] realArgs = new Object[method.getParameterTypes().length];
86                      for (int i = 0; i < realArgs.length; i++) {
87                          final Object arg;
88                          if (i > args.length) {
89                              arg = null;
90                          }
91                          else {
92                              arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
93                          }
94                          realArgs[i] = arg;
95                      }
96                      try {
97                          return method.invoke(applet, realArgs);
98                      }
99                      catch (final Exception e) {
100                         throw Context.throwAsScriptRuntimeEx(e);
101                     }
102                 }
103             };
104             ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
105         }
106     }
107 
108     /**
109      * Returns the value of the {@code alt} property.
110      * @return the value of the {@code alt} property
111      */
112     @JsxGetter
113     public String getAlt() {
114         final String alt = getDomNodeOrDie().getAttribute("alt");
115         return alt;
116     }
117 
118     /**
119      * Returns the value of the {@code alt} property.
120      * @param alt the value
121      */
122     @JsxSetter
123     public void setAlt(final String alt) {
124         getDomNodeOrDie().setAttribute("alt", alt);
125     }
126 
127     /**
128      * Gets the {@code border} attribute.
129      * @return the {@code border} attribute
130      */
131     @JsxGetter(IE)
132     public String getBorder() {
133         final String border = getDomNodeOrDie().getAttribute("border");
134         return border;
135     }
136 
137     /**
138      * Sets the {@code border} attribute.
139      * @param border the {@code border} attribute
140      */
141     @JsxSetter(IE)
142     public void setBorder(final String border) {
143         getDomNodeOrDie().setAttribute("border", border);
144     }
145 
146     /**
147      * Returns the value of the {@code align} property.
148      * @return the value of the {@code align} property
149      */
150     @JsxGetter
151     public String getAlign() {
152         return getAlign(true);
153     }
154 
155     /**
156      * Sets the value of the {@code align} property.
157      * @param align the value of the {@code align} property
158      */
159     @JsxSetter
160     public void setAlign(final String align) {
161         setAlign(align, false);
162     }
163 
164     /**
165      * Gets the {@code classid} attribute.
166      * @return the {@code classid} attribute
167      */
168     @JsxGetter(IE)
169     public String getClassid() {
170         return getDomNodeOrDie().getAttribute("classid");
171     }
172 
173     /**
174      * Sets the {@code classid} attribute.
175      * @param classid the {@code classid} attribute
176      */
177     @JsxSetter(IE)
178     public void setClassid(final String classid) {
179         getDomNodeOrDie().setAttribute("classid", classid);
180         // see HTMLObjectElement.setClassid
181     }
182 
183     /**
184      * Returns the value of the {@code width} property.
185      * @return the value of the {@code width} property
186      */
187     @JsxGetter(propertyName = "width")
188     public String getWidth_js() {
189         return getWidthOrHeight("width", Boolean.TRUE);
190     }
191 
192     /**
193      * Sets the value of the {@code width} property.
194      * @param width the value of the {@code width} property
195      */
196     @JsxSetter
197     public void setWidth(final String width) {
198         setWidthOrHeight("width", width, true);
199     }
200 
201     /**
202      * Returns the value of the {@code height} property.
203      * @return the value of the {@code height} property
204      */
205     @JsxGetter(propertyName = "height")
206     public String getHeight_js() {
207         return getWidthOrHeight("height", Boolean.TRUE);
208     }
209 
210     /**
211      * Sets the value of the {@code height} property.
212      * @param height the value of the {@code height} property
213      */
214     @JsxSetter
215     public void setHeight(final String height) {
216         setWidthOrHeight("height", height, true);
217     }
218 
219 }