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