JAL-1807 Bob
[jalviewjs.git] / site / j2s / swingjs / plaf / JSComponentUI.js
1 Clazz.declarePackage ("swingjs.plaf");
2 Clazz.load (["javax.swing.plaf.ComponentUI", "swingjs.plaf.JSEventHandler"], "swingjs.plaf.JSComponentUI", ["java.lang.IllegalArgumentException", "$.NullPointerException", "java.awt.Color", "$.Component", "$.Dimension", "$.Point", "$.Toolkit", "java.awt.event.FocusEvent", "swingjs.JSToolkit", "swingjs.api.DOMNode"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.id = null;
5 this.c = null;
6 this.outerNode = null;
7 this.domNode = null;
8 this.enableNode = null;
9 this.textNode = null;
10 this.valueNode = null;
11 this.scrollNode = null;
12 this.focusNode = null;
13 this.components = null;
14 this.num = 0;
15 this.isTainted = true;
16 this.x = 0;
17 this.y = 0;
18 this.preferredSize = null;
19 this.isContainer = false;
20 this.parent = null;
21 this.currentText = null;
22 this.scrollerNode = null;
23 this.classID = null;
24 this.document = null;
25 this.body = null;
26 this.needPreferred = false;
27 Clazz.instantialize (this, arguments);
28 }, swingjs.plaf, "JSComponentUI", javax.swing.plaf.ComponentUI, swingjs.plaf.JSEventHandler);
29 Clazz.makeConstructor (c$, 
30 function () {
31 Clazz.superConstructor (this, swingjs.plaf.JSComponentUI, []);
32 this.setDoc ();
33 });
34 Clazz.defineMethod (c$, "setDoc", 
35 function () {
36 {
37 this.document = document;
38 this.body = document.body;
39 }});
40 Clazz.overrideMethod (c$, "installUI", 
41 function (c) {
42 }, "javax.swing.JComponent");
43 Clazz.overrideMethod (c$, "uninstallUI", 
44 function (c) {
45 this.uninstallJSUI ();
46 }, "javax.swing.JComponent");
47 Clazz.defineMethod (c$, "$", 
48 function (node) {
49 return swingjs.JSToolkit.getJQuery ().$ (node);
50 }, "swingjs.api.DOMNode");
51 Clazz.defineMethod (c$, "setTainted", 
52 function () {
53 this.isTainted = true;
54 });
55 Clazz.defineMethod (c$, "set", 
56 function (target) {
57 this.c = target;
58 this.newID ();
59 if (this.needPreferred) this.getPreferredSize (this.c);
60 this.installJSUI ();
61 return this;
62 }, "javax.swing.JComponent");
63 Clazz.defineMethod (c$, "newID", 
64 function () {
65 this.classID = this.c.getUIClassID ();
66 if (this.id == null) {
67 this.num = ++swingjs.plaf.JSComponentUI.incr;
68 this.id = this.c.getHTMLName (this.classID) + "_" + this.num;
69 }});
70 Clazz.defineMethod (c$, "setCssFont", 
71 function (obj, font) {
72 if (font != null) {
73 var istyle = font.getStyle ();
74 var name = font.getFamily ();
75 if (name === "Dialog") name = "Arial";
76 swingjs.api.DOMNode.setStyles (obj, ["font-family", name, "font-size", font.getSize () + "px", "font-style", ((istyle & 2) == 0 ? "normal" : "italic"), "font-weight", ((istyle & 1) == 0 ? "normal" : "bold")]);
77 }if (this.c.isBackgroundSet ()) this.setBackground (this.c.getBackground ());
78 this.setForeground (this.c.getForeground ());
79 return obj;
80 }, "swingjs.api.DOMNode,java.awt.Font");
81 Clazz.defineMethod (c$, "createDOMObject", 
82 function (key, id, attr) {
83 var obj = swingjs.api.DOMNode.createElement (key, id);
84 for (var i = 0; i < attr.length; ) swingjs.api.DOMNode.setAttr (obj, attr[i++], attr[i++]);
85
86 if (!this.c.isEnabled ()) this.setEnabled (false);
87 return obj;
88 }, "~S,~S,~A");
89 Clazz.defineMethod (c$, "bindMouse", 
90 function (node) {
91 swingjs.api.DOMNode.setAttr (node, "data-UI", this);
92 }, "swingjs.api.DOMNode");
93 Clazz.overrideMethod (c$, "handleJSEvent", 
94 function (target, eventType, jQueryEvent) {
95 return false;
96 }, "~O,~N,~O");
97 Clazz.defineMethod (c$, "wrap", 
98 function (type, id, elements) {
99 return this.append (this.createDOMObject (type, id + type, []), elements);
100 }, "~S,~S,~A");
101 Clazz.defineMethod (c$, "append", 
102 function (obj, elements) {
103 for (var i = 0; i < elements.length; i++) {
104 obj.appendChild (elements[i]);
105 }
106 return obj;
107 }, "swingjs.api.DOMNode,~A");
108 Clazz.defineMethod (c$, "debugDump", 
109 function (d) {
110 System.out.println (swingjs.api.DOMNode.getAttr (d, "outerHTML"));
111 }, "swingjs.api.DOMNode");
112 c$.vCenter = Clazz.defineMethod (c$, "vCenter", 
113 function (obj, offset) {
114 swingjs.api.DOMNode.setStyles (obj, ["top", "50%", "transform", "translateY(" + offset + "%)"]);
115 }, "swingjs.api.DOMNode,~N");
116 Clazz.defineMethod (c$, "setHTMLSize", 
117 function (obj, addCSS) {
118 return this.setHTMLSize1 (obj, addCSS, true);
119 }, "swingjs.api.DOMNode,~B");
120 Clazz.defineMethod (c$, "setHTMLSize1", 
121 function (node, addCSS, usePreferred) {
122 if (node == null) return null;
123 var h;
124 var w;
125 var w0 = null;
126 var h0 = null;
127 var parentNode = null;
128 if (this.scrollerNode != null) {
129 w = this.scrollerNode.c.getWidth ();
130 h = this.scrollerNode.c.getHeight ();
131 } else if (usePreferred && this.preferredSize != null) {
132 w = this.preferredSize.width;
133 h = this.preferredSize.height;
134 } else {
135 parentNode = swingjs.api.DOMNode.remove (node);
136 {
137 w0 = node.style.width;
138 h0 = node.style.height;
139 }swingjs.api.DOMNode.setStyles (node, ["position", null, "width", null, "height", null]);
140 var div;
141 if (swingjs.api.DOMNode.getAttr (node, "tagName") === "DIV") div = node;
142  else div = this.wrap ("div", this.id + "_temp", [node]);
143 swingjs.api.DOMNode.setStyles (div, ["position", "absolute"]);
144 this.body.appendChild (div);
145 w = Clazz.doubleToInt (Math.ceil (this.$ (div).width () + 0.5));
146 h = Clazz.doubleToInt (Math.ceil (this.$ (div).height () + 0.5));
147 this.body.removeChild (div);
148 }var size = this.getCSSDimension (w, h);
149 if (addCSS) {
150 swingjs.api.DOMNode.setStyles (node, ["position", "absolute"]);
151 swingjs.api.DOMNode.setSize (node, size.width, size.height);
152 } else {
153 swingjs.api.DOMNode.setStyles (node, ["position", null]);
154 if (w0 != null) swingjs.api.DOMNode.setStyles (node, ["width", w0, "height", h0]);
155 }if (parentNode != null) parentNode.appendChild (node);
156 return size;
157 }, "swingjs.api.DOMNode,~B,~B");
158 Clazz.defineMethod (c$, "getCSSDimension", 
159 function (w, h) {
160 return  new java.awt.Dimension (w, h);
161 }, "~N,~N");
162 Clazz.defineMethod (c$, "setHTMLElement", 
163 function () {
164 if (!this.isTainted) return this.outerNode;
165 var root = (this.isContainer ? this.c.getRootPane () : null);
166 if (this.c === root) {
167 this.isTainted = false;
168 return this.outerNode;
169 }this.domNode = this.getDOMObject ();
170 if (this.outerNode == null) {
171 this.outerNode = this.wrap ("div", this.id, [this.domNode]);
172 if (root != null && root.getContentPane () === this.c) swingjs.JSToolkit.getHTML5Applet (this.c)._getContentLayer ().appendChild (this.outerNode);
173 }swingjs.api.DOMNode.setStyles (this.outerNode, ["position", "absolute", "left", (this.x = this.c.getX ()) + "px", "top", (this.y = this.c.getY ()) + "px"]);
174 if (this.isContainer) {
175 System.out.println ("JSComponentUI container " + this.id + " " + this.c.getBounds ());
176 swingjs.api.DOMNode.setSize (this.outerNode, this.c.getWidth (), this.c.getHeight ());
177 var children = (this.components == null ? this.c.getComponents () : this.components);
178 for (var i = children.length; --i >= 0; ) {
179 var ui = swingjs.JSToolkit.getUI (children[i], false);
180 if (ui == null) {
181 continue;
182 }if (ui.outerNode == null) ui.setHTMLElement ();
183 if (ui.outerNode == null) {
184 System.out.println ("JSCUI could not add " + ui.c.getName () + " to " + this.c.getName ());
185 } else {
186 this.outerNode.appendChild (ui.outerNode);
187 }ui.parent = this;
188 }
189 }this.isTainted = false;
190 return this.outerNode;
191 });
192 Clazz.defineMethod (c$, "getPreferredSize", 
193 function (c) {
194 var d = this.setHTMLSize (this.getDOMObject (), false);
195 return d;
196 }, "javax.swing.JComponent");
197 Clazz.defineMethod (c$, "paint", 
198 function (g, c) {
199 if (c.isOpaque ()) {
200 g.setColor (c.getBackground ());
201 g.fillRect (0, 0, c.getWidth (), c.getHeight ());
202 }}, "java.awt.Graphics,javax.swing.JComponent");
203 Clazz.overrideMethod (c$, "update", 
204 function (g, c) {
205 var testing = false;
206 if (testing) {
207 g.setColor (java.awt.Color.red);
208 g.drawRect (0, 0, c.getWidth (), c.getHeight ());
209 System.out.println ("drawing " + c.getWidth () + " " + c.getHeight ());
210 }this.setHTMLElement ();
211 this.paint (g, c);
212 }, "java.awt.Graphics,javax.swing.JComponent");
213 Clazz.defineMethod (c$, "getMinimumSize", 
214 function (c) {
215 return this.getPreferredSize (c);
216 }, "javax.swing.JComponent");
217 Clazz.overrideMethod (c$, "getMaximumSize", 
218 function (c) {
219 return null;
220 }, "javax.swing.JComponent");
221 Clazz.overrideMethod (c$, "contains", 
222 function (c, x, y) {
223 return c.inside (x, y);
224 }, "javax.swing.JComponent,~N,~N");
225 c$.createUI = Clazz.overrideMethod (c$, "createUI", 
226 function (c) {
227 return null;
228 }, "javax.swing.JComponent");
229 Clazz.overrideMethod (c$, "getBaseline", 
230 function (c, width, height) {
231 if (c == null) {
232 throw  new NullPointerException ("Component must be non-null");
233 }if (width < 0 || height < 0) {
234 throw  new IllegalArgumentException ("Width and height must be >= 0");
235 }return -1;
236 }, "javax.swing.JComponent,~N,~N");
237 Clazz.overrideMethod (c$, "getBaselineResizeBehavior", 
238 function (c) {
239 if (c == null) {
240 throw  new NullPointerException ("Component must be non-null");
241 }return java.awt.Component.BaselineResizeBehavior.OTHER;
242 }, "javax.swing.JComponent");
243 Clazz.defineMethod (c$, "getJSTextValue", 
244 function () {
245 return swingjs.api.DOMNode.getAttr (this.domNode, this.valueNode == null ? "innerHTML" : "value");
246 });
247 Clazz.defineMethod (c$, "notifyPropertyChanged", 
248 function (prop) {
249 var obj = null;
250 var val = null;
251 if (prop === "text") {
252 val = (this.c).getText ();
253 if (val.equals (this.currentText)) return;
254 this.currentText = val;
255 if (this.textNode != null) {
256 prop = "innerHTML";
257 obj = this.textNode;
258 } else if (this.valueNode != null) {
259 prop = "value";
260 obj = this.valueNode;
261 }} else if (prop === "preferredSize") {
262 this.preferredSize = this.c.getPreferredSize ();
263 this.getPreferredSize ();
264 return;
265 }if (obj == null) {
266 System.out.println ("JSComponentUI: unrecognized prop: " + prop);
267 } else {
268 System.out.println ("JSComponentUI: setting " + this.id + " " + prop);
269 this.setProp (obj, prop, val);
270 }}, "~S");
271 Clazz.defineMethod (c$, "setProp", 
272 function (obj, prop, val) {
273 return swingjs.api.DOMNode.setAttr (obj, prop, val);
274 }, "swingjs.api.DOMNode,~S,~S");
275 Clazz.overrideMethod (c$, "isObscured", 
276 function () {
277 swingjs.JSToolkit.notImplemented ("");
278 return false;
279 });
280 Clazz.overrideMethod (c$, "canDetermineObscurity", 
281 function () {
282 swingjs.JSToolkit.notImplemented ("");
283 return false;
284 });
285 Clazz.overrideMethod (c$, "setVisible", 
286 function (b) {
287 swingjs.api.DOMNode.setStyles (this.outerNode, ["display", b ? "block" : "none"]);
288 }, "~B");
289 Clazz.overrideMethod (c$, "setEnabled", 
290 function (b) {
291 if (this.enableNode != null) swingjs.api.DOMNode.setAttr (this.enableNode, "disabled", (b ? null : "TRUE"));
292 }, "~B");
293 Clazz.defineMethod (c$, "paint", 
294 function (g) {
295 }, "java.awt.Graphics");
296 Clazz.overrideMethod (c$, "repaint", 
297 function (tm, x, y, width, height) {
298 }, "~N,~N,~N,~N,~N");
299 Clazz.overrideMethod (c$, "print", 
300 function (g) {
301 swingjs.JSToolkit.notImplemented ("");
302 }, "java.awt.Graphics");
303 Clazz.overrideMethod (c$, "setBounds", 
304 function (x, y, width, height, op) {
305 switch (op) {
306 case 2:
307 case 3:
308 case 4:
309 if (this.scrollerNode != null) {
310 width = Math.min (width, this.scrollerNode.c.getWidth ());
311 height = Math.min (height, this.scrollerNode.c.getHeight ());
312 }System.out.println (this.id + " setBounds " + x + " " + y + " " + width + " " + height + " op=" + op);
313 if (this.domNode != null) swingjs.api.DOMNode.setSize (this.domNode, width, height);
314 break;
315 }
316 }, "~N,~N,~N,~N,~N");
317 Clazz.overrideMethod (c$, "handleEvent", 
318 function (e) {
319 swingjs.JSToolkit.notImplemented ("");
320 }, "java.awt.AWTEvent");
321 Clazz.overrideMethod (c$, "coalescePaintEvent", 
322 function (e) {
323 swingjs.JSToolkit.notImplemented ("");
324 }, "java.awt.event.PaintEvent");
325 Clazz.overrideMethod (c$, "getLocationOnScreen", 
326 function () {
327 var offset = this.$ (this.outerNode).offset ();
328 return  new java.awt.Point (offset.left, offset.top);
329 });
330 Clazz.defineMethod (c$, "getPreferredSize", 
331 function () {
332 return this.getPreferredSize (this.c);
333 });
334 Clazz.defineMethod (c$, "getMinimumSize", 
335 function () {
336 swingjs.JSToolkit.notImplemented ("");
337 return this.getPreferredSize (this.c);
338 });
339 Clazz.overrideMethod (c$, "getColorModel", 
340 function () {
341 return java.awt.Toolkit.getDefaultToolkit ().getColorModel ();
342 });
343 Clazz.overrideMethod (c$, "getToolkit", 
344 function () {
345 return java.awt.Toolkit.getDefaultToolkit ();
346 });
347 Clazz.overrideMethod (c$, "getGraphics", 
348 function () {
349 return null;
350 });
351 Clazz.overrideMethod (c$, "getFontMetrics", 
352 function (font) {
353 return this.c.getFontMetrics (font);
354 }, "java.awt.Font");
355 Clazz.overrideMethod (c$, "dispose", 
356 function () {
357 swingjs.JSToolkit.notImplemented ("");
358 });
359 Clazz.overrideMethod (c$, "setForeground", 
360 function (color) {
361 if (this.domNode != null) swingjs.api.DOMNode.setStyles (this.domNode, ["color", swingjs.JSToolkit.getCSSColor (color == null ? java.awt.Color.black : color)]);
362 }, "java.awt.Color");
363 Clazz.overrideMethod (c$, "setBackground", 
364 function (color) {
365 if (this.domNode != null) swingjs.api.DOMNode.setStyles (this.domNode, ["background-color", swingjs.JSToolkit.getCSSColor (color == null ? java.awt.Color.white : color)]);
366 }, "java.awt.Color");
367 Clazz.overrideMethod (c$, "setFont", 
368 function (f) {
369 if (this.domNode != null) this.setCssFont (this.domNode, f);
370 }, "java.awt.Font");
371 Clazz.overrideMethod (c$, "updateCursorImmediately", 
372 function () {
373 swingjs.JSToolkit.notImplemented ("");
374 });
375 Clazz.overrideMethod (c$, "requestFocus", 
376 function (lightweightChild, temporary, focusedWindowChangeAllowed, time, cause) {
377 if (this.focusNode == null) return false;
378 this.$ (this.focusNode).focus ();
379 if (this.textNode != null) this.$ (this.textNode).select ();
380 return true;
381 }, "java.awt.Component,~B,~B,~N,jssun.awt.CausedFocusEvent.Cause");
382 Clazz.overrideMethod (c$, "isFocusable", 
383 function () {
384 return (this.focusNode != null);
385 });
386 Clazz.defineMethod (c$, "createImage", 
387 function (producer) {
388 swingjs.JSToolkit.notImplemented ("");
389 return null;
390 }, "java.awt.image.ImageProducer");
391 Clazz.defineMethod (c$, "createImage", 
392 function (width, height) {
393 swingjs.JSToolkit.notImplemented ("");
394 return null;
395 }, "~N,~N");
396 Clazz.overrideMethod (c$, "createVolatileImage", 
397 function (width, height) {
398 swingjs.JSToolkit.notImplemented ("");
399 return null;
400 }, "~N,~N");
401 Clazz.overrideMethod (c$, "prepareImage", 
402 function (img, w, h, o) {
403 swingjs.JSToolkit.notImplemented ("");
404 return false;
405 }, "java.awt.Image,~N,~N,java.awt.image.ImageObserver");
406 Clazz.overrideMethod (c$, "checkImage", 
407 function (img, w, h, o) {
408 swingjs.JSToolkit.notImplemented ("");
409 return 0;
410 }, "java.awt.Image,~N,~N,java.awt.image.ImageObserver");
411 Clazz.overrideMethod (c$, "getGraphicsConfiguration", 
412 function () {
413 swingjs.JSToolkit.notImplemented ("");
414 return null;
415 });
416 Clazz.overrideMethod (c$, "handlesWheelScrolling", 
417 function () {
418 swingjs.JSToolkit.notImplemented ("");
419 return false;
420 });
421 Clazz.overrideMethod (c$, "getBackBuffer", 
422 function () {
423 swingjs.JSToolkit.notImplemented ("");
424 return null;
425 });
426 Clazz.overrideMethod (c$, "destroyBuffers", 
427 function () {
428 swingjs.JSToolkit.notImplemented ("");
429 });
430 Clazz.overrideMethod (c$, "reparent", 
431 function (newContainer) {
432 swingjs.JSToolkit.notImplemented ("");
433 }, "java.awt.peer.ContainerPeer");
434 Clazz.overrideMethod (c$, "isReparentSupported", 
435 function () {
436 swingjs.JSToolkit.notImplemented ("");
437 return false;
438 });
439 Clazz.overrideMethod (c$, "layout", 
440 function () {
441 swingjs.JSToolkit.notImplemented ("");
442 });
443 Clazz.overrideMethod (c$, "getBounds", 
444 function () {
445 swingjs.JSToolkit.notImplemented ("");
446 return null;
447 });
448 Clazz.defineMethod (c$, "hasFocus", 
449 function () {
450 return this.focusNode != null && this.focusNode === swingjs.api.DOMNode.getAttr (this.document, "activeElement");
451 });
452 Clazz.defineMethod (c$, "notifyFocus", 
453 function (focusGained) {
454 java.awt.Toolkit.getEventQueue ().postEvent ( new java.awt.event.FocusEvent (this.c, focusGained ? 1004 : 1005));
455 }, "~B");
456 Clazz.defineStatics (c$,
457 "incr", 0);
458 });