Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package swingjs.api.js

File DOMNode.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

14
52
29
1
309
138
39
0.75
1.79
29
1.34

Classes

Class Line # Actions
DOMNode 14 52 39
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package swingjs.api.js;
2   
3    import java.awt.Dimension;
4    import java.awt.Rectangle;
5   
6    /**
7    * A mix of direct DOM calls on DOM nodes and convenience methods to do that.
8    *
9    * NOTE: DO NOT OVERLOAD THESE METHODS, as this package will not be qualified.
10    *
11    * @author hansonr
12    *
13    */
 
14    public interface DOMNode {
15   
16    public static JQuery jQuery = /** @j2sNative jQuery.$ || (jQuery.$ = jQuery) || */null;
17   
18    // "abstract" in the sense that these are the exact calls to JavaScript
19   
20   
21    public void addEventListener(String event, Object listener);
22    public void removeEventListener(String event);
23    public void removeEventListener(String event, Object listener);
24   
25   
26   
27    public String[] getAttributeNames();
28   
29    public String getAttribute(String name);
30   
31    public void setAttribute(String attr, String val);
32   
33    public void appendChild(DOMNode node);
34   
35    public void prepend(DOMNode node);
36   
37    public void insertBefore(DOMNode node, DOMNode refNode);
38   
39    public DOMNode removeChild(DOMNode node);
40   
41    public void focus();
42    public boolean hasFocus();
43    public void blur();
44   
45    public DOMNode removeAttribute(String attr);
46   
47    public void setSelectionRange(int start, int end, String direction);
48   
49    public Rectangle getBoundingClientRect();
50   
51    // static convenience methods
52   
 
53  0 toggle public static DOMNode createElement(String key, String id) {
54  0 DOMNode node = null;
55    /**
56    * @j2sNative
57    * node = document.createElement(key);
58    * id && (node.id = id);
59    */
60  0 return node;
61    }
62   
 
63  0 toggle public static DOMNode getElement(String id) {
64  0 return (/** @j2sNative document.getElementById(id) ||*/ null);
65    }
66   
 
67  0 toggle public static DOMNode createTextNode(String text) {
68  0 return (/** @j2sNative document.createTextNode(text) || */ null);
69    }
70   
 
71  0 toggle public static DOMNode getParent(DOMNode node) {
72  0 return (/** @j2sNative node.parentNode ||*/ null);
73    }
74   
 
75  0 toggle public static DOMNode getPreviousSibling(DOMNode node) {
76  0 return (/** @j2sNative node.previousSibling ||*/ null);
77    }
78   
 
79  0 toggle public static DOMNode firstChild(DOMNode node) {
80  0 return (/** @j2sNative node.firstChild ||*/ null);
81    }
82   
 
83  0 toggle public static DOMNode lastChild(DOMNode node) {
84  0 return (/** @j2sNative node.lastChild ||*/ null);
85    }
86   
 
87  0 toggle public static DOMNode setZ(DOMNode node, int z) {
88  0 return setStyles(node, "z-index", "" + z);
89    }
90   
 
91  0 toggle public static Object getAttr(Object node, String attr) {
92    /**
93    * @j2sNative
94    *
95    * if (!node)
96    * return null;
97    * var a = node[attr];
98    * return (typeof a == "undefined" ? null : a);
99    */
100    {
101  0 return null;
102    }
103    }
104   
 
105  0 toggle public static int getAttrInt(DOMNode node, String attr) {
106  0 return (/** @j2sNative node && node[attr] ||*/ 0);
107    }
108   
 
109  0 toggle public static String getStyle(DOMNode node, String style) {
110  0 return (/** @j2sNative node && node.style[style] ||*/ null);
111    }
112   
 
113  0 toggle public static void getCSSRectangle(DOMNode node, Rectangle r) {
114    /**
115    * @j2sNative
116    *
117    * r.x = parseInt(node.style.left.split("p")[0]);
118    * r.y = parseInt(node.style.top.split("p")[0]);
119    * r.width = parseInt(node.style.width.split("p")[0]);
120    * r.height = parseInt(node.style.height.split("p")[0]);
121    *
122    */
123    }
124   
 
125  0 toggle public static DOMNode setAttr(DOMNode node, String attr, Object val) {
126    /**
127    * @j2sNative
128    *
129    * attr && (node[attr] = (val == "秘TRUE" ? true : val == "秘FALSE" ? false : val));
130    *
131    */
132  0 return node;
133    }
134   
135   
 
136  0 toggle public static void setAttrInt(DOMNode node, String attr, int val) {
137    /**
138    * @j2sNative
139    *
140    * node[attr] = val;
141    *
142    */
143    }
144   
145   
146    /**
147    * allows for null key to be skipped (used in audio)
148    *
149    * @param node
150    * @param attr
151    * @return
152    */
 
153  0 toggle public static DOMNode setAttrs(DOMNode node, Object... attr) {
154    /**
155    * @j2sNative
156    *
157    * for (var i = 0; i < attr.length;) {
158    * C$.setAttr(node, attr[i++],attr[i++]);
159    * }
160    */
161  0 return node;
162    }
163   
 
164  0 toggle public static DOMNode setStyles(DOMNode node, String... attr) {
165    /**
166    * @j2sNative
167    *
168    * if (node) for (var i = 0; i < attr.length;) {
169    * node.style[attr[i++]] = attr[i++];
170    * }
171    *
172    */
173  0 return node;
174    }
175   
 
176  0 toggle public static DOMNode setSize(DOMNode node, int width, int height) {
177  0 return setStyles(node, "width", width + "px", "height", height + "px");
178    }
179   
 
180  0 toggle public static DOMNode setPositionAbsolute(DOMNode node) {
181  0 return DOMNode.setStyles(node, "position", "absolute");
182    }
183   
 
184  0 toggle public static void setVisible(DOMNode node, boolean visible) {
185  0 setStyles(node, "display", visible ? "block" : "none");
186    }
187   
 
188  0 toggle public static DOMNode setTopLeftAbsolute(DOMNode node, int top, int left) {
189  0 DOMNode.setStyles(node, "top", top + "px");
190  0 DOMNode.setStyles(node, "left", left + "px");
191  0 return DOMNode.setStyles(node, "position", "absolute");
192    }
193   
 
194  0 toggle public static void addHorizontalGap(DOMNode domNode, int gap) {
195  0 DOMNode label = DOMNode.setStyles(DOMNode.createElement("label", null),
196    "letter-spacing", gap + "px", "font-size", "0pt");
197  0 label.appendChild(DOMNode.createTextNode("."));
198  0 domNode.appendChild(label);
199    }
200   
 
201  0 toggle public static void appendChildSafely(DOMNode parent, DOMNode node) {
202    /**
203    * @j2sNative
204    * if (!parent || node.parentElement == parent)
205    * return;
206    */
207  0 parent.appendChild(node);
208    }
209   
210    // static jQuery calls
211   
212    /**
213    * jQuery height()
214    *
215    * @param node
216    * @return height
217    */
 
218  0 toggle public static int getHeight(DOMNode node) {
219  0 return jQuery.$(node).height();
220    }
221   
222    /**
223    * jQuery width()
224    *
225    * @param node
226    * @return width
227    */
 
228  0 toggle public static int getWidth(DOMNode node) {
229  0 return jQuery.$(node).width();
230    }
231   
232    /**
233    * jQuery remove()
234    *
235    * Remove this node and return its parent. Automatically removing all events
236    * attached to it.
237    *
238    * @param node
239    * @return parent or null
240    */
 
241  0 toggle public static void dispose(DOMNode node) {
242  0 if (node != null)
243  0 jQuery.$(node).remove();
244    }
245   
246    /**
247    * Just remove the node, keeping its events and data
248    * @param node
249    */
 
250  0 toggle public static void remove(DOMNode node) {
251   
252    // NOTE: IE does not have node.remove()
253   
254  0 DOMNode p = getParent(node);
255  0 if (p != null)
256  0 p.removeChild(node);
257    }
258   
259    /**
260    * just detaches all the nodes; doesn't remove their listeners
261    * @param node
262    */
 
263  0 toggle public static void detachAll(DOMNode node) {
264    /**
265    * @j2sNative
266    * if(node)
267    * while(node.lastChild)
268    * node.removeChild(node.lastChild);
269    */
270    }
271   
272    /**
273    * jQuery detach() + append()
274    *
275    * @param node
276    * @param container
277    * @return parent if container is null, or container if it is not null
278    */
 
279  0 toggle public static DOMNode transferTo(DOMNode node, DOMNode container) {
280  0 if (node == null)
281  0 return null;
282  0 DOMNode p = getParent(node);
283  0 try {
284  0 if (p != null)
285  0 jQuery.$(node).detach();
286    } catch (Throwable e) {
287    // ignore
288    }
289  0 if (container == null)
290  0 return p;
291  0 jQuery.$(container).append(node);
292  0 return container;
293    }
294   
 
295  0 toggle public static Object getEmbedded(String name, String type) {
296  0 DOMNode node = DOMNode.getElement(name + "-div");
297  0 if (node == null)
298  0 return null;
299  0 switch (type) {
300  0 case "node":
301  0 return node;
302  0 case "dim":
303  0 return new Dimension(DOMNode.getWidth(node), DOMNode.getHeight(node));
304  0 default:
305  0 return DOMNode.getAttr(node, type);
306    }
307    }
308   
309    }