JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / javax / swing / border / TitledBorder.js
1 Clazz.declarePackage ("javax.swing.border");\r
2 Clazz.load (["javax.swing.border.AbstractBorder", "java.awt.Point"], "javax.swing.border.TitledBorder", ["java.lang.IllegalArgumentException", "$.NullPointerException", "java.awt.Component", "$.Dimension", "$.Font", "$.Insets", "$.Rectangle", "javax.swing.JComponent", "$.UIManager", "jssun.swing.SwingUtilities2"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.title = null;\r
5 this.border = null;\r
6 this.titlePosition = 0;\r
7 this.titleJustification = 0;\r
8 this.titleFont = null;\r
9 this.titleColor = null;\r
10 this.textLoc = null;\r
11 Clazz.instantialize (this, arguments);\r
12 }, javax.swing.border, "TitledBorder", javax.swing.border.AbstractBorder);\r
13 Clazz.prepareFields (c$, function () {\r
14 this.textLoc =  new java.awt.Point ();\r
15 });\r
16 Clazz.makeConstructor (c$, \r
17 function (title) {\r
18 this.construct (null, title, 4, 0, null, null);\r
19 }, "~S");\r
20 Clazz.makeConstructor (c$, \r
21 function (border) {\r
22 this.construct (border, "", 4, 0, null, null);\r
23 }, "javax.swing.border.Border");\r
24 Clazz.makeConstructor (c$, \r
25 function (border, title) {\r
26 this.construct (border, title, 4, 0, null, null);\r
27 }, "javax.swing.border.Border,~S");\r
28 Clazz.makeConstructor (c$, \r
29 function (border, title, titleJustification, titlePosition) {\r
30 this.construct (border, title, titleJustification, titlePosition, null, null);\r
31 }, "javax.swing.border.Border,~S,~N,~N");\r
32 Clazz.makeConstructor (c$, \r
33 function (border, title, titleJustification, titlePosition, titleFont) {\r
34 this.construct (border, title, titleJustification, titlePosition, titleFont, null);\r
35 }, "javax.swing.border.Border,~S,~N,~N,java.awt.Font");\r
36 Clazz.makeConstructor (c$, \r
37 function (border, title, titleJustification, titlePosition, titleFont, titleColor) {\r
38 Clazz.superConstructor (this, javax.swing.border.TitledBorder, []);\r
39 this.title = title;\r
40 this.border = border;\r
41 this.titleFont = titleFont;\r
42 this.titleColor = titleColor;\r
43 this.setTitleJustification (titleJustification);\r
44 this.setTitlePosition (titlePosition);\r
45 }, "javax.swing.border.Border,~S,~N,~N,java.awt.Font,java.awt.Color");\r
46 Clazz.overrideMethod (c$, "paintBorder", \r
47 function (c, g, x, y, width, height) {\r
48 var border = this.getBorder ();\r
49 if (this.getTitle () == null || this.getTitle ().equals ("")) {\r
50 if (border != null) {\r
51 border.paintBorder (c, g, x, y, width, height);\r
52 }return;\r
53 }var grooveRect =  new java.awt.Rectangle (x + 2, y + 2, width - (4), height - (4));\r
54 var font = g.getFont ();\r
55 var color = g.getColor ();\r
56 g.setFont (this.getFont (c));\r
57 var jc = (Clazz.instanceOf (c, javax.swing.JComponent)) ? c : null;\r
58 var fm = jssun.swing.SwingUtilities2.getFontMetrics (jc, g);\r
59 var fontHeight = fm.getHeight ();\r
60 var descent = fm.getDescent ();\r
61 var ascent = fm.getAscent ();\r
62 var diff;\r
63 var stringWidth = jssun.swing.SwingUtilities2.stringWidth (jc, fm, this.getTitle ());\r
64 var insets;\r
65 if (border != null) {\r
66 insets = border.getBorderInsets (c);\r
67 } else {\r
68 insets =  new java.awt.Insets (0, 0, 0, 0);\r
69 }var titlePos = this.getTitlePosition ();\r
70 switch (titlePos) {\r
71 case 1:\r
72 diff = ascent + descent + (Math.max (2, 4) - 2);\r
73 grooveRect.y += diff;\r
74 grooveRect.height -= diff;\r
75 this.textLoc.y = grooveRect.y - (descent + 2);\r
76 break;\r
77 case 2:\r
78 case 0:\r
79 diff = Math.max (0, ((Clazz.doubleToInt (ascent / 2)) + 2) - 2);\r
80 grooveRect.y += diff;\r
81 grooveRect.height -= diff;\r
82 this.textLoc.y = (grooveRect.y - descent) + Clazz.doubleToInt ((insets.top + ascent + descent) / 2);\r
83 break;\r
84 case 3:\r
85 this.textLoc.y = grooveRect.y + insets.top + ascent + 2;\r
86 break;\r
87 case 4:\r
88 this.textLoc.y = (grooveRect.y + grooveRect.height) - (insets.bottom + descent + 2);\r
89 break;\r
90 case 5:\r
91 grooveRect.height -= Clazz.doubleToInt (fontHeight / 2);\r
92 this.textLoc.y = ((grooveRect.y + grooveRect.height) - descent) + Clazz.doubleToInt (((ascent + descent) - insets.bottom) / 2);\r
93 break;\r
94 case 6:\r
95 grooveRect.height -= fontHeight;\r
96 this.textLoc.y = grooveRect.y + grooveRect.height + ascent + 2;\r
97 break;\r
98 }\r
99 var justification = this.getTitleJustification ();\r
100 if (javax.swing.border.AbstractBorder.isLeftToRight (c)) {\r
101 if (justification == 4 || justification == 0) {\r
102 justification = 1;\r
103 } else if (justification == 5) {\r
104 justification = 3;\r
105 }} else {\r
106 if (justification == 4 || justification == 0) {\r
107 justification = 3;\r
108 } else if (justification == 5) {\r
109 justification = 1;\r
110 }}switch (justification) {\r
111 case 1:\r
112 this.textLoc.x = grooveRect.x + 5 + insets.left;\r
113 break;\r
114 case 3:\r
115 this.textLoc.x = (grooveRect.x + grooveRect.width) - (stringWidth + 5 + insets.right);\r
116 break;\r
117 case 2:\r
118 this.textLoc.x = grooveRect.x + (Clazz.doubleToInt ((grooveRect.width - stringWidth) / 2));\r
119 break;\r
120 }\r
121 if (border != null) {\r
122 if (((titlePos == 2 || titlePos == 0) && (grooveRect.y > this.textLoc.y - ascent)) || (titlePos == 5 && (grooveRect.y + grooveRect.height < this.textLoc.y + descent))) {\r
123 var clipRect =  new java.awt.Rectangle ();\r
124 var saveClip = g.getClipBounds ();\r
125 clipRect.setBounds (saveClip);\r
126 if (javax.swing.border.TitledBorder.computeIntersection (clipRect, x, y, this.textLoc.x - 1 - x, height)) {\r
127 g.setClip (clipRect);\r
128 border.paintBorder (c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);\r
129 }clipRect.setBounds (saveClip);\r
130 if (javax.swing.border.TitledBorder.computeIntersection (clipRect, this.textLoc.x + stringWidth + 1, y, x + width - (this.textLoc.x + stringWidth + 1), height)) {\r
131 g.setClip (clipRect);\r
132 border.paintBorder (c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);\r
133 }if (titlePos == 2 || titlePos == 0) {\r
134 clipRect.setBounds (saveClip);\r
135 if (javax.swing.border.TitledBorder.computeIntersection (clipRect, this.textLoc.x - 1, this.textLoc.y + descent, stringWidth + 2, y + height - this.textLoc.y - descent)) {\r
136 g.setClip (clipRect);\r
137 border.paintBorder (c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);\r
138 }} else {\r
139 clipRect.setBounds (saveClip);\r
140 if (javax.swing.border.TitledBorder.computeIntersection (clipRect, this.textLoc.x - 1, y, stringWidth + 2, this.textLoc.y - ascent - y)) {\r
141 g.setClip (clipRect);\r
142 border.paintBorder (c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);\r
143 }}g.setClip (saveClip);\r
144 } else {\r
145 border.paintBorder (c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);\r
146 }}g.setColor (this.getTitleColor ());\r
147 jssun.swing.SwingUtilities2.drawString (jc, g, this.getTitle (), this.textLoc.x, this.textLoc.y);\r
148 g.setFont (font);\r
149 g.setColor (color);\r
150 }, "java.awt.Component,java.awt.Graphics,~N,~N,~N,~N");\r
151 Clazz.defineMethod (c$, "getBorderInsets", \r
152 function (c) {\r
153 return this.getBorderInsets (c,  new java.awt.Insets (0, 0, 0, 0));\r
154 }, "java.awt.Component");\r
155 Clazz.defineMethod (c$, "getBorderInsets", \r
156 function (c, insets) {\r
157 var fm;\r
158 var descent = 0;\r
159 var ascent = 16;\r
160 var height = 16;\r
161 var border = this.getBorder ();\r
162 if (border != null) {\r
163 if (Clazz.instanceOf (border, javax.swing.border.AbstractBorder)) {\r
164 (border).getBorderInsets (c, insets);\r
165 } else {\r
166 var i = border.getBorderInsets (c);\r
167 insets.top = i.top;\r
168 insets.right = i.right;\r
169 insets.bottom = i.bottom;\r
170 insets.left = i.left;\r
171 }} else {\r
172 insets.left = insets.top = insets.right = insets.bottom = 0;\r
173 }insets.left += 4;\r
174 insets.right += 4;\r
175 insets.top += 4;\r
176 insets.bottom += 4;\r
177 if (c == null || this.getTitle () == null || this.getTitle ().equals ("")) {\r
178 return insets;\r
179 }var font = this.getFont (c);\r
180 fm = c.getFontMetrics (font);\r
181 if (fm != null) {\r
182 descent = fm.getDescent ();\r
183 ascent = fm.getAscent ();\r
184 height = fm.getHeight ();\r
185 }switch (this.getTitlePosition ()) {\r
186 case 1:\r
187 insets.top += ascent + descent + (Math.max (2, 4) - 2);\r
188 break;\r
189 case 2:\r
190 case 0:\r
191 insets.top += ascent + descent;\r
192 break;\r
193 case 3:\r
194 insets.top += ascent + descent + 2;\r
195 break;\r
196 case 4:\r
197 insets.bottom += ascent + descent + 2;\r
198 break;\r
199 case 5:\r
200 insets.bottom += ascent + descent;\r
201 break;\r
202 case 6:\r
203 insets.bottom += height;\r
204 break;\r
205 }\r
206 return insets;\r
207 }, "java.awt.Component,java.awt.Insets");\r
208 Clazz.overrideMethod (c$, "isBorderOpaque", \r
209 function () {\r
210 return false;\r
211 });\r
212 Clazz.defineMethod (c$, "getTitle", \r
213 function () {\r
214 return this.title;\r
215 });\r
216 Clazz.defineMethod (c$, "getBorder", \r
217 function () {\r
218 var b = this.border;\r
219 if (b == null) b = javax.swing.UIManager.getBorder ("TitledBorder.border");\r
220 return b;\r
221 });\r
222 Clazz.defineMethod (c$, "getTitlePosition", \r
223 function () {\r
224 if (this.titlePosition == 0) {\r
225 var value = javax.swing.UIManager.get ("TitledBorder.position");\r
226 if (Clazz.instanceOf (value, String)) {\r
227 var s = value;\r
228 if ("ABOVE_TOP".equalsIgnoreCase (s)) {\r
229 return 1;\r
230 } else if ("TOP".equalsIgnoreCase (s)) {\r
231 return 2;\r
232 } else if ("BELOW_TOP".equalsIgnoreCase (s)) {\r
233 return 3;\r
234 } else if ("ABOVE_BOTTOM".equalsIgnoreCase (s)) {\r
235 return 4;\r
236 } else if ("BOTTOM".equalsIgnoreCase (s)) {\r
237 return 5;\r
238 } else if ("BELOW_BOTTOM".equalsIgnoreCase (s)) {\r
239 return 6;\r
240 }} else if (Clazz.instanceOf (value, Integer)) {\r
241 var i = (value).intValue ();\r
242 if (i >= 0 && i <= 6) {\r
243 return i;\r
244 }}}return this.titlePosition;\r
245 });\r
246 Clazz.defineMethod (c$, "getTitleJustification", \r
247 function () {\r
248 return this.titleJustification;\r
249 });\r
250 Clazz.defineMethod (c$, "getTitleFont", \r
251 function () {\r
252 var f = this.titleFont;\r
253 if (f == null) f = javax.swing.UIManager.getFont ("TitledBorder.font");\r
254 return f;\r
255 });\r
256 Clazz.defineMethod (c$, "getTitleColor", \r
257 function () {\r
258 var c = this.titleColor;\r
259 if (c == null) c = javax.swing.UIManager.getColor ("TitledBorder.titleColor");\r
260 return c;\r
261 });\r
262 Clazz.defineMethod (c$, "setTitle", \r
263 function (title) {\r
264 this.title = title;\r
265 }, "~S");\r
266 Clazz.defineMethod (c$, "setBorder", \r
267 function (border) {\r
268 this.border = border;\r
269 }, "javax.swing.border.Border");\r
270 Clazz.defineMethod (c$, "setTitlePosition", \r
271 function (titlePosition) {\r
272 switch (titlePosition) {\r
273 case 1:\r
274 case 2:\r
275 case 3:\r
276 case 4:\r
277 case 5:\r
278 case 6:\r
279 case 0:\r
280 this.titlePosition = titlePosition;\r
281 break;\r
282 default:\r
283 throw  new IllegalArgumentException (titlePosition + " is not a valid title position.");\r
284 }\r
285 }, "~N");\r
286 Clazz.defineMethod (c$, "setTitleJustification", \r
287 function (titleJustification) {\r
288 switch (titleJustification) {\r
289 case 0:\r
290 case 1:\r
291 case 2:\r
292 case 3:\r
293 case 4:\r
294 case 5:\r
295 this.titleJustification = titleJustification;\r
296 break;\r
297 default:\r
298 throw  new IllegalArgumentException (titleJustification + " is not a valid title justification.");\r
299 }\r
300 }, "~N");\r
301 Clazz.defineMethod (c$, "setTitleFont", \r
302 function (titleFont) {\r
303 this.titleFont = titleFont;\r
304 }, "java.awt.Font");\r
305 Clazz.defineMethod (c$, "setTitleColor", \r
306 function (titleColor) {\r
307 this.titleColor = titleColor;\r
308 }, "java.awt.Color");\r
309 Clazz.defineMethod (c$, "getMinimumSize", \r
310 function (c) {\r
311 var insets = this.getBorderInsets (c);\r
312 var minSize =  new java.awt.Dimension (insets.right + insets.left, insets.top + insets.bottom);\r
313 var font = this.getFont (c);\r
314 var fm = c.getFontMetrics (font);\r
315 var jc = (Clazz.instanceOf (c, javax.swing.JComponent)) ? c : null;\r
316 switch (this.getTitlePosition ()) {\r
317 case 1:\r
318 case 6:\r
319 minSize.width = Math.max (jssun.swing.SwingUtilities2.stringWidth (jc, fm, this.getTitle ()), minSize.width);\r
320 break;\r
321 case 3:\r
322 case 4:\r
323 case 2:\r
324 case 5:\r
325 case 0:\r
326 default:\r
327 minSize.width += jssun.swing.SwingUtilities2.stringWidth (jc, fm, this.getTitle ());\r
328 }\r
329 return minSize;\r
330 }, "java.awt.Component");\r
331 Clazz.overrideMethod (c$, "getBaseline", \r
332 function (c, width, height) {\r
333 if (c == null) {\r
334 throw  new NullPointerException ("Must supply non-null component");\r
335 }if (height < 0) {\r
336 throw  new IllegalArgumentException ("Height must be >= 0");\r
337 }var title = this.getTitle ();\r
338 if (title != null && !"".equals (title)) {\r
339 var font = this.getFont (c);\r
340 var border2 = this.getBorder ();\r
341 var borderInsets;\r
342 if (border2 != null) {\r
343 borderInsets = border2.getBorderInsets (c);\r
344 } else {\r
345 borderInsets =  new java.awt.Insets (0, 0, 0, 0);\r
346 }var fm = c.getFontMetrics (font);\r
347 var fontHeight = fm.getHeight ();\r
348 var descent = fm.getDescent ();\r
349 var ascent = fm.getAscent ();\r
350 var y = 2;\r
351 var h = height - 4;\r
352 var diff;\r
353 switch (this.getTitlePosition ()) {\r
354 case 1:\r
355 diff = ascent + descent + (Math.max (2, 4) - 2);\r
356 return y + diff - (descent + 2);\r
357 case 2:\r
358 case 0:\r
359 diff = Math.max (0, ((Clazz.doubleToInt (ascent / 2)) + 2) - 2);\r
360 return (y + diff - descent) + Clazz.doubleToInt ((borderInsets.top + ascent + descent) / 2);\r
361 case 3:\r
362 return y + borderInsets.top + ascent + 2;\r
363 case 4:\r
364 return (y + h) - (borderInsets.bottom + descent + 2);\r
365 case 5:\r
366 h -= Clazz.doubleToInt (fontHeight / 2);\r
367 return ((y + h) - descent) + Clazz.doubleToInt (((ascent + descent) - borderInsets.bottom) / 2);\r
368 case 6:\r
369 h -= fontHeight;\r
370 return y + h + ascent + 2;\r
371 }\r
372 }return -1;\r
373 }, "java.awt.Component,~N,~N");\r
374 Clazz.defineMethod (c$, "getBaselineResizeBehavior", \r
375 function (c) {\r
376 Clazz.superCall (this, javax.swing.border.TitledBorder, "getBaselineResizeBehavior", [c]);\r
377 switch (this.getTitlePosition ()) {\r
378 case 1:\r
379 case 2:\r
380 case 0:\r
381 case 3:\r
382 return java.awt.Component.BaselineResizeBehavior.CONSTANT_ASCENT;\r
383 case 4:\r
384 case 5:\r
385 case 6:\r
386 return java.awt.Component.BaselineResizeBehavior.CONSTANT_DESCENT;\r
387 }\r
388 return java.awt.Component.BaselineResizeBehavior.OTHER;\r
389 }, "java.awt.Component");\r
390 Clazz.defineMethod (c$, "getFont", \r
391 function (c) {\r
392 var font;\r
393 if ((font = this.getTitleFont ()) != null) {\r
394 return font;\r
395 } else if (c != null && (font = c.getFont ()) != null) {\r
396 return font;\r
397 }return  new java.awt.Font ("Dialog", 0, 12);\r
398 }, "java.awt.Component");\r
399 c$.computeIntersection = Clazz.defineMethod (c$, "computeIntersection", \r
400  function (dest, rx, ry, rw, rh) {\r
401 var x1 = Math.max (rx, dest.x);\r
402 var x2 = Math.min (rx + rw, dest.x + dest.width);\r
403 var y1 = Math.max (ry, dest.y);\r
404 var y2 = Math.min (ry + rh, dest.y + dest.height);\r
405 dest.x = x1;\r
406 dest.y = y1;\r
407 dest.width = x2 - x1;\r
408 dest.height = y2 - y1;\r
409 if (dest.width <= 0 || dest.height <= 0) {\r
410 return false;\r
411 }return true;\r
412 }, "java.awt.Rectangle,~N,~N,~N,~N");\r
413 Clazz.defineStatics (c$,\r
414 "DEFAULT_POSITION", 0,\r
415 "ABOVE_TOP", 1,\r
416 "TOP", 2,\r
417 "BELOW_TOP", 3,\r
418 "ABOVE_BOTTOM", 4,\r
419 "BOTTOM", 5,\r
420 "BELOW_BOTTOM", 6,\r
421 "DEFAULT_JUSTIFICATION", 0,\r
422 "LEFT", 1,\r
423 "CENTER", 2,\r
424 "RIGHT", 3,\r
425 "LEADING", 4,\r
426 "TRAILING", 5,\r
427 "EDGE_SPACING", 2,\r
428 "TEXT_SPACING", 2,\r
429 "TEXT_INSET_H", 5);\r
430 });\r