swingjs/api, javajs/async
[jalview.git] / src / swingjs / api / js / DOMNode.java
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         public static DOMNode createElement(String key, String id) {
54                 DOMNode node = null;
55                 /**
56                  * @j2sNative
57                  *                                      node = document.createElement(key);
58                  *                                      id && (node.id = id);
59                  */
60                 return node;
61         }
62
63         public static DOMNode getElement(String id) {
64                 return (/**  @j2sNative  document.getElementById(id) ||*/ null);
65         }
66
67         public static DOMNode createTextNode(String text) {
68                 return (/** @j2sNative document.createTextNode(text) || */ null); 
69         }
70
71         public static DOMNode getParent(DOMNode node) {
72                 return (/**  @j2sNative  node.parentNode ||*/ null);
73         }
74         
75         public static DOMNode getPreviousSibling(DOMNode node) {
76                 return (/**  @j2sNative  node.previousSibling ||*/ null);
77         }
78         
79         public static DOMNode firstChild(DOMNode node) {
80                 return  (/**  @j2sNative node.firstChild ||*/ null);
81         }
82
83         public static DOMNode lastChild(DOMNode node) {
84                 return  (/**  @j2sNative node.lastChild ||*/ null);
85         }
86
87         public static DOMNode setZ(DOMNode node, int z) {
88                 return setStyles(node, "z-index", "" + z);
89         }
90
91         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                 return null;
102                 }
103         }
104
105         public static int getAttrInt(DOMNode node, String attr) {
106                 return  (/**  @j2sNative node && node[attr] ||*/ 0);
107         }
108
109         public static String getStyle(DOMNode node, String style) {
110                 return  (/**  @j2sNative node && node.style[style] ||*/ null);
111         }
112
113         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         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                 return node;
133         }
134
135
136         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         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                 return node;
162         }
163
164         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                 return node;
174         }
175
176         public static DOMNode setSize(DOMNode node, int width, int height) {
177                 return setStyles(node, "width", width + "px", "height", height + "px");
178         }
179
180         public static DOMNode setPositionAbsolute(DOMNode node) {
181                 return DOMNode.setStyles(node, "position", "absolute");
182         }
183
184         public static void setVisible(DOMNode node, boolean visible) {
185                 setStyles(node, "display", visible ? "block" : "none");
186         }
187
188         public static DOMNode setTopLeftAbsolute(DOMNode node, int top, int left) {
189                 DOMNode.setStyles(node, "top", top + "px");
190                 DOMNode.setStyles(node, "left", left + "px");
191                 return DOMNode.setStyles(node, "position", "absolute");
192         }
193
194         public static void addHorizontalGap(DOMNode domNode, int gap) {
195                 DOMNode label = DOMNode.setStyles(DOMNode.createElement("label", null), 
196                                 "letter-spacing", gap + "px", "font-size", "0pt");
197                 label.appendChild(DOMNode.createTextNode("."));
198                 domNode.appendChild(label);
199         }
200
201         public static void appendChildSafely(DOMNode parent, DOMNode node) {
202                 /**
203                  * @j2sNative
204                  * if (!parent || node.parentElement == parent)
205                  *   return;
206                  */
207                 parent.appendChild(node);
208         }
209         
210         // static jQuery calls
211         
212         /**
213          * jQuery height()
214          * 
215          * @param node
216          * @return height
217          */
218         public static int getHeight(DOMNode node) {
219                 return jQuery.$(node).height();
220         }
221
222         /**
223          * jQuery width()
224          * 
225          * @param node
226          * @return width
227          */
228         public static int getWidth(DOMNode node) {
229                 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         public static void dispose(DOMNode node) {
242                 if (node != null)               
243                         jQuery.$(node).remove();
244         }
245
246         /**
247          * Just remove the node, keeping its events and data 
248          * @param node
249          */
250         public static void remove(DOMNode node) {
251                 
252                 // NOTE: IE does not have node.remove()
253                 
254                 DOMNode p = getParent(node);
255                 if (p != null)
256                         p.removeChild(node);
257         }
258
259         /**
260          * just detaches all the nodes; doesn't remove their listeners
261          * @param node
262          */
263         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         public static DOMNode transferTo(DOMNode node, DOMNode container) {
280                 if (node == null)
281                         return null;
282                 DOMNode p = getParent(node);
283                 try {
284                         if (p != null)
285                                 jQuery.$(node).detach();
286                 } catch (Throwable e) {
287                         // ignore
288                 }
289                  if (container == null)
290                         return p; 
291                  jQuery.$(container).append(node);
292                 return container;
293         }
294
295         public static Object getEmbedded(String name, String type) {
296                 DOMNode node = DOMNode.getElement(name + "-div");
297                 if (node == null)
298                         return null;
299                 switch (type) {
300                 case "node":
301                         return node;
302                 case "dim":
303                         return new Dimension(DOMNode.getWidth(node), DOMNode.getHeight(node));
304                 default:
305                         return DOMNode.getAttr(node, type);
306                 }
307         }
308
309 }