Debian Codegraph @

src/com/jidesoft/plaf/basic/BasicStyledLabelUI.java

raw
1 /*
2 * @(#)BasicStyledLabelUI.java 6/8/2012
3 *
4 * Copyright 2002 - 2012 JIDE Software Inc. All rights reserved.
5 */
6 package com.jidesoft.plaf.basic;
7
8 import com.jidesoft.plaf.LookAndFeelFactory;
9 import com.jidesoft.plaf.UIDefaultsLookup;
10 import com.jidesoft.swing.FontUtils;
11 import com.jidesoft.swing.JideSwingUtilities;
12 import com.jidesoft.swing.StyleRange;
13 import com.jidesoft.swing.StyledLabel;
14
15 import javax.swing.*;
16 import javax.swing.plaf.ComponentUI;
17 import javax.swing.plaf.basic.BasicLabelUI;
18 import javax.swing.text.View;
19 import java.awt.*;
20 import java.beans.PropertyChangeEvent;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Comparator;
24 import java.util.List;
25
26 public class BasicStyledLabelUI extends BasicLabelUI implements SwingConstants {
27 public static Comparator<StyleRange> _comparator;
28
29 protected static BasicStyledLabelUI styledLabelUI = new BasicStyledLabelUI();
30
31 @SuppressWarnings({"UnusedDeclaration"})
32 public static ComponentUI createUI(JComponent c) {
33 return styledLabelUI;
34 }
35
36 class StyledText {
37 StyleRange styleRange;
38 String text;
39
40 public StyledText(String text) {
41 this.text = text;
42 }
43
44 public StyledText(String text, StyleRange styleRange) {
45 this.text = text;
46 this.styleRange = styleRange;
47 }
48 }
49
50 private final List<StyledText> _styledTexts = new ArrayList<StyledText>();
51 private int _preferredRowCount = 1;
52
53 @Override
54 public void propertyChange(PropertyChangeEvent e) {
55 super.propertyChange(e);
56 if (StyledLabel.PROPERTY_STYLE_RANGE.equals(e.getPropertyName())) {
57 synchronized (_styledTexts) {
58 _styledTexts.clear();
59 }
60 if (e.getSource() instanceof StyledLabel) {
61 ((StyledLabel) e.getSource()).revalidate();
62 ((StyledLabel) e.getSource()).repaint();
63 }
64 }
65 else if (StyledLabel.PROPERTY_IGNORE_COLOR_SETTINGS.equals(e.getPropertyName())) {
66 if (e.getSource() instanceof StyledLabel) {
67 ((StyledLabel) e.getSource()).repaint();
68 }
69 }
70 }
71
72 @Override
73 protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) {
74 View v = (l != null) ? (View) l.getClientProperty("html") : null;
75 if (v != null) {
76 super.paintEnabledText(l, g, s, textX, textY);
77 }
78 else {
79 paintStyledText((StyledLabel) l, g, textX, textY);
80 }
81 }
82
83 @Override
84 protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY) {
85 View v = (l != null) ? (View) l.getClientProperty("html") : null;
86 if (v != null) {
87 super.paintDisabledText(l, g, s, textX, textY);
88 }
89 else {
90 paintStyledText((StyledLabel) l, g, textX, textY);
91 }
92 }
93
94 protected void buildStyledText(StyledLabel label) {
95 synchronized (_styledTexts) {
96 _styledTexts.clear();
97 StyleRange[] styleRanges = label.getStyleRanges();
98 if (_comparator == null) {
99 _comparator = new Comparator<StyleRange>() {
100 public int compare(StyleRange r1, StyleRange r2) {
101 if (r1.getStart() < r2.getStart()) {
102 return -1;
103 }
104 else if (r1.getStart() > r2.getStart()) {
105 return 1;
106 }
107 else {
108 return 0;
109 }
110 }
111 };
112 }
113 Arrays.sort(styleRanges, _comparator);
114
115 String s = label.getText();
116 if (s != null && s.length() > 0) { // do not do anything if the text is empty
117 int index = 0;
118 for (StyleRange styleRange : styleRanges) {
119 if (index >= s.length()) {
120 break;
121 }
122 if (styleRange.getStart() > index) { // fill in the gap
123 String text = s.substring(index, Math.min(styleRange.getStart(), s.length()));
124 StyleRange newRange = new StyleRange(index, styleRange.getStart() - index, -1);
125 addStyledTexts(text, newRange, label);
126 index = styleRange.getStart();
127 }
128
129 if (styleRange.getStart() == index) { // exactly on
130 if (styleRange.getLength() == -1) {
131 String text = s.substring(index);
132 addStyledTexts(text, styleRange, label);
133 index = s.length();
134 }
135 else {
136 String text = s.substring(index, Math.min(index + styleRange.getLength(), s.length()));
137 addStyledTexts(text, styleRange, label);
138 index += styleRange.getLength();
139 }
140 }
141 else if (styleRange.getStart() < index) { // overlap
142 // ignore
143 }
144 }
145 if (index < s.length()) {
146 String text = s.substring(index, s.length());
147 StyleRange range = new StyleRange(index, s.length() - index, -1);
148 addStyledTexts(text, range, label);
149 }
150 }
151 }
152 }
153
154 private void addStyledTexts(String text, StyleRange range, StyledLabel label) {
155 range = new StyleRange(range); // keep the passed-in parameter no change
156 int index1 = text.indexOf('\r');
157 int index2 = text.indexOf('\n');
158 while (index1 >= 0 || index2 >= 0) {
159 int index = index1 >= 0 ? index1 : -1;
160 if (index2 >= 0 && (index2 < index1 || index < 0)) {
161 index = index2;
162 }
163 String subString = text.substring(0, index);
164 StyleRange newRange = new StyleRange(range);
165 newRange.setStart(range.getStart());
166 newRange.setLength(index);
167 _styledTexts.add(new StyledText(subString, newRange));
168 int length = 1;
169 if (text.charAt(index) == '\r' && index + 1 < text.length() && text.charAt(index + 1) == '\n') {
170 length++;
171 }
172 newRange = new StyleRange(range);
173 newRange.setStart(range.getStart() + index);
174 newRange.setLength(length);
175 _styledTexts.add(new StyledText(text.substring(index, index + length), newRange));
176
177 text = text.substring(index + length);
178 range.setStart(range.getStart() + index + length);
179 range.setLength(range.getLength() - index - length);
180
181 index1 = text.indexOf('\r');
182 index2 = text.indexOf('\n');
183 }
184 if (text.length() > 0) {
185 _styledTexts.add(new StyledText(text, range));
186 }
187 }
188
189 private boolean _gettingPreferredSize;
190 @Override
191 public Dimension getPreferredSize(JComponent c) {
192 _gettingPreferredSize = true;
193 Dimension preferredSize;
194 try {
195 preferredSize = super.getPreferredSize(c);
196 }
197 finally {
198 _gettingPreferredSize = false;
199 }
200 return preferredSize;
201 }
202
203 @Override
204 protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text, Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR) {
205 Dimension size = null;
206 if (label instanceof StyledLabel) {
207 int oldPreferredWidth = ((StyledLabel) label).getPreferredWidth();
208 int oldRows = ((StyledLabel) label).getRows();
209 try {
210 if (((StyledLabel) label).isLineWrap() && label.getWidth() > 0) {
211 ((StyledLabel) label).setPreferredWidth(label.getWidth());
212 }
213 size = getPreferredSize((StyledLabel) label);
214 if (oldPreferredWidth > 0 && oldPreferredWidth < label.getWidth()) {
215 ((StyledLabel) label).setPreferredWidth(oldPreferredWidth);
216 size = getPreferredSize((StyledLabel) label);
217 }
218 else if (((StyledLabel) label).isLineWrap() && ((StyledLabel) label).getMinRows() > 0) {
219 ((StyledLabel) label).setPreferredWidth(0);
220 ((StyledLabel) label).setRows(0);
221 Dimension minSize = getPreferredSize((StyledLabel) label);
222 if (minSize.height > size.height) {
223 size = minSize;
224 }
225 }
226 }
227 finally {
228 ((StyledLabel) label).setPreferredWidth(oldPreferredWidth);
229 ((StyledLabel) label).setRows(oldRows);
230 }
231 }
232 else {
233 size = label.getPreferredSize();
234 }
235 textR.width = size.width;
236 textR.height = size.height;
237 if (label.getIcon() != null) {
238 textR.width -= label.getIcon().getIconWidth() + label.getIconTextGap();
239 }
240
241 return layoutCompoundLabel(
242 label,
243 fontMetrics,
244 text,
245 icon,
246 label.getVerticalAlignment(),
247 label.getHorizontalAlignment(),
248 label.getVerticalTextPosition(),
249 label.getHorizontalTextPosition(),
250 viewR,
251 iconR,
252 textR,
253 label.getIconTextGap());
254 }
255
256 /**
257 * Gets the preferred size of the text portion of the StyledLabel including the icon.
258 *
259 * @param label the StyledLabel
260 * @return the preferred size.
261 */
262 protected Dimension getPreferredSize(StyledLabel label) {
263 buildStyledText(label);
264
265 Font font = getFont(label);
266 FontMetrics fm = label.getFontMetrics(font);
267 FontMetrics fm2;
268 int defaultFontSize = font.getSize();
269 boolean lineWrap = label.isLineWrap() || (label.getText() != null && (label.getText().contains("\r") || label.getText().contains("\n")));
270 synchronized (_styledTexts) {
271 StyledText[] texts = _styledTexts.toArray(new StyledText[_styledTexts.size()]);
272
273 // get maximum row height first by comparing all fonts of styled texts
274 int maxRowHeight = fm.getHeight();
275 for (StyledText styledText : texts) {
276 StyleRange style = styledText.styleRange;
277 int size = (style != null && (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
278
279 font = getFont(label);
280 int styleHeight = fm.getHeight();
281 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
282 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
283 fm2 = label.getFontMetrics(font);
284 styleHeight = fm2.getHeight();
285 }
286 styleHeight++;
287 /*
288 if (style != null) {
289 if (style.isWaved()) {
290 styleHeight += 4;
291 }
292 else if (style.isDotted()) {
293 styleHeight += 3;
294 }
295 else if (style.isUnderlined()) {
296 styleHeight += 2;
297 }
298 }
299 */
300 maxRowHeight = Math.max(maxRowHeight, styleHeight);
301 }
302
303 int naturalRowCount = 1;
304 int nextRowStartIndex = 0;
305 int width = 0;
306 int maxWidth = 0;
307 List<Integer> lineWidths = new ArrayList<Integer>();
308 // get one line width
309 for (StyledText styledText : _styledTexts) {
310 StyleRange style = styledText.styleRange;
311 int size = (style != null &&
312 (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
313 font = getFont(label);
314 String s = styledText.text.substring(nextRowStartIndex);
315 if (s.startsWith("\r") || s.startsWith("\n")) {
316 lineWidths.add(width);
317 maxWidth = Math.max(width, maxWidth);
318 width = 0;
319 naturalRowCount++;
320 if (label.getMaxRows() > 0 && naturalRowCount > label.getMaxRows()) {
321 break;
322 }
323 continue;
324 }
325 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
326 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
327 fm2 = label.getFontMetrics(font);
328 width += fm2.stringWidth(s);
329 }
330 else {
331 // fm2 = fm;
332 width += fm.stringWidth(s);
333 }
334 }
335 lineWidths.add(width);
336 maxWidth = Math.max(width, maxWidth);
337 int maxLineWidth = maxWidth;
338 _preferredRowCount = naturalRowCount;
339
340 // if getPreferredWidth() is not set but getRows() is set, get maximum width and row count based on the required rows.
341 if (lineWrap && label.getPreferredWidth() <= 0 && label.getRows() > 0) {
342 maxWidth = getMaximumWidth(label, maxWidth, naturalRowCount, label.getRows());
343 }
344
345 // if calculated maximum width is larger than label's maximum size, wrap again to get the updated row count and use the label's maximum width as the maximum width.
346 int preferredWidth = label.getPreferredWidth();
347 if (preferredWidth > 0 && label.getInsets() != null) {
348 preferredWidth -= label.getInsets().left + label.getInsets().right;
349 }
350 if (label.getIcon() != null && label.getHorizontalTextPosition() != SwingConstants.CENTER) {
351 preferredWidth -= label.getIcon().getIconWidth() + label.getIconTextGap();
352 }
353 if (lineWrap && preferredWidth > 0 && maxWidth > preferredWidth) {
354 maxWidth = getLayoutWidth(label, preferredWidth);
355 }
356
357 // label.getPreferredWidth() <= 0 && label.getMaxRows() > 0 && rowCount > label.getMaxRows(), recalculate the maximum width according to the maximum rows
358 if (lineWrap && label.getMaxRows() > 0 && _preferredRowCount > label.getMaxRows()) {
359 if (label.getPreferredWidth() <= 0) {
360 maxWidth = getMaximumWidth(label, maxWidth, naturalRowCount, label.getMaxRows());
361 }
362 else {
363 _preferredRowCount = label.getMaxRows();
364 }
365 }
366
367 // label.getPreferredWidth() <= 0 && label.getMinRows() > 0 && rowCount < label.getMinRows(), recalculate the maximum width according to the minimum rows
368 if (lineWrap && label.getPreferredWidth() <= 0 && label.getMinRows() > 0 && _preferredRowCount < label.getMinRows()) {
369 maxWidth = getMaximumWidth(label, maxWidth, naturalRowCount, label.getMinRows());
370 }
371 if (_gettingPreferredSize && label.getRows() > 0 && _preferredRowCount > label.getRows() && (label.getPreferredWidth() <= 0 || label.getPreferredWidth() >= maxLineWidth || naturalRowCount > label.getRows())) {
372 _preferredRowCount = label.getRows();
373 maxLineWidth = 0;
374 for (int i = 0; i < lineWidths.size() && i < _preferredRowCount; i++) {
375 maxLineWidth = Math.max(maxLineWidth, lineWidths.get(i));
376 }
377 }
378 Dimension dimension = new Dimension(Math.min(maxWidth, maxLineWidth), (maxRowHeight + Math.max(0, label.getRowGap())) * _preferredRowCount);
379 if (label.getIcon() != null) {
380 dimension = new Dimension(dimension.width + label.getIconTextGap() + label.getIcon().getIconWidth(), dimension.height);
381 }
382 return dimension;
383 }
384 }
385
386 private int getLayoutWidth(StyledLabel label, int maxWidth) {
387 int nextRowStartIndex;
388 Font font = getFont(label);
389 int defaultFontSize = font.getSize();
390 FontMetrics fm = label.getFontMetrics(font);
391 FontMetrics fm2;
392 nextRowStartIndex = 0;
393 int x = 0;
394 _preferredRowCount = 1;
395 for (int i = 0; i < _styledTexts.size(); i++) {
396 StyledText styledText = _styledTexts.get(i);
397 StyleRange style = styledText.styleRange;
398 if (styledText.text.contains("\r") || styledText.text.contains("\n")) {
399 x = 0;
400 _preferredRowCount++;
401 continue;
402 }
403
404 int size = (style != null &&
405 (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
406
407 font = getFont(label); // cannot omit this one
408 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
409 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
410 fm2 = label.getFontMetrics(font);
411 }
412 else {
413 fm2 = fm;
414 }
415
416 String s = styledText.text.substring(nextRowStartIndex);
417
418 int strWidth = fm2.stringWidth(s);
419
420 boolean wrapped = false;
421 int widthLeft = maxWidth - x;
422 if (widthLeft < strWidth) {
423 wrapped = true;
424 int availLength = s.length() * widthLeft / strWidth + 1;
425 int nextWordStartIndex;
426 int nextRowStartIndexInSubString = 0;
427 boolean needBreak = false;
428 boolean needContinue = false;
429 int loopCount = 0;
430 do {
431 String subString = s.substring(0, Math.min(availLength, s.length()));
432 int firstRowWordEndIndex = findFirstRowWordEndIndex(subString);
433 nextWordStartIndex = firstRowWordEndIndex < 0 ? 0 : findNextWordStartIndex(s, firstRowWordEndIndex);
434 if (firstRowWordEndIndex < 0) {
435 if (x != 0) {
436 x = 0;
437 i--;
438 _preferredRowCount++;
439 needContinue = true;
440 break;
441 }
442 else {
443 firstRowWordEndIndex = 0;
444 nextWordStartIndex = Math.min(s.length(), availLength);
445 }
446 }
447
448 if (label.getMaxRows() > 0 && _preferredRowCount >= label.getMaxRows()) {
449 needBreak = true;
450 }
451
452 nextRowStartIndexInSubString = firstRowWordEndIndex + 1;
453 String subStringThisRow = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
454 strWidth = fm2.stringWidth(subStringThisRow);
455 if (strWidth > widthLeft) {
456 availLength = subString.length() * widthLeft / strWidth;
457 }
458 loopCount++;
459 if (loopCount > 50) {
460 System.err.println("Painting Styled Label Error: " + styledText);
461 break;
462 }
463 } while (strWidth > widthLeft && availLength > 0);
464 if (needBreak) {
465 break;
466 }
467 if (needContinue) {
468 continue;
469 }
470 while (nextRowStartIndexInSubString < nextWordStartIndex) {
471 strWidth += fm2.charWidth(s.charAt(nextRowStartIndexInSubString));
472 if (strWidth >= widthLeft) {
473 break;
474 }
475 nextRowStartIndexInSubString++;
476 }
477 String subStringThisRow = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
478 strWidth = fm2.stringWidth(subStringThisRow);
479 while (nextRowStartIndexInSubString < nextWordStartIndex) {
480 strWidth += fm2.charWidth(s.charAt(nextRowStartIndexInSubString));
481 if (strWidth >= widthLeft) {
482 break;
483 }
484 nextRowStartIndexInSubString++;
485 }
486 s = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
487 strWidth = fm2.stringWidth(s);
488 nextRowStartIndex += nextRowStartIndexInSubString;
489 }
490 else {
491 nextRowStartIndex = 0;
492 }
493
494 if (wrapped) {
495 _preferredRowCount++;
496 x = 0;
497 i--;
498 }
499 else {
500 x += strWidth;
501 }
502 }
503 return maxWidth;
504 }
505
506 private int getMaximumWidth(StyledLabel label, int maxWidth, int naturalRowCount, int limitedRows) {
507 int textWidth = label.getWidth() - label.getInsets().left - label.getInsets().right;
508 if (label.getIcon() != null) {
509 textWidth -= label.getIcon().getIconWidth() + label.getIconTextGap();
510 }
511 if (naturalRowCount > 1) {
512 int proposedMaxWidthMin = 1;
513 int proposedMaxWidthMax = maxWidth;
514 _preferredRowCount = naturalRowCount;
515 while (proposedMaxWidthMin < proposedMaxWidthMax) {
516 int middle = (proposedMaxWidthMax + proposedMaxWidthMin) / 2;
517 maxWidth = getLayoutWidth(label, middle);
518 if (_preferredRowCount > limitedRows) {
519 proposedMaxWidthMin = middle + 1;
520 _preferredRowCount = naturalRowCount;
521 }
522 else {
523 proposedMaxWidthMax = middle - 1;
524 }
525 }
526 return maxWidth + maxWidth / 20;
527 }
528
529 int estimatedWidth = maxWidth / limitedRows + 1;
530 int x = 0;
531 int nextRowStartIndex = 0;
532 Font font = getFont(label);
533 FontMetrics fm = label.getFontMetrics(font);
534 int defaultFontSize = font.getSize();
535 FontMetrics fm2;
536 for (int i = 0; i < _styledTexts.size(); i++) {
537 StyledText styledText = _styledTexts.get(i);
538 StyleRange style = styledText.styleRange;
539 int size = (style != null && (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
540 font = getFont(label);
541 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
542 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
543 fm2 = label.getFontMetrics(font);
544 }
545 else {
546 fm2 = fm;
547 }
548
549 String s = styledText.text.substring(nextRowStartIndex);
550 int strWidth = fm2.stringWidth(s);
551 int widthLeft = estimatedWidth - x;
552 if (widthLeft < strWidth) {
553 int availLength = s.length() * widthLeft / strWidth + 1;
554 String subString = s.substring(0, Math.min(availLength, s.length()));
555 int firstRowWordEndIndex = findFirstRowWordEndIndex(subString);
556 int nextWordStartIndex = findNextWordStartIndex(s, firstRowWordEndIndex);
557 if (firstRowWordEndIndex < 0) {
558 if (nextWordStartIndex < s.length()) {
559 firstRowWordEndIndex = findFirstRowWordEndIndex(s.substring(0, nextWordStartIndex));
560 }
561 else {
562 firstRowWordEndIndex = nextWordStartIndex;
563 }
564 }
565 int nextRowStartIndexInSubString = firstRowWordEndIndex + 1;
566 String subStringThisRow = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
567 strWidth = fm2.stringWidth(subStringThisRow);
568 while (nextRowStartIndexInSubString < nextWordStartIndex) {
569 strWidth += fm2.charWidth(s.charAt(nextRowStartIndexInSubString));
570 nextRowStartIndexInSubString++;
571 if (strWidth >= widthLeft) {
572 break;
573 }
574 }
575 s = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
576 strWidth = fm2.stringWidth(s);
577 nextRowStartIndex += nextRowStartIndexInSubString;
578 if (x + strWidth >= maxWidth) {
579 x = Math.max(x, strWidth);
580 break;
581 }
582 if (x + strWidth >= estimatedWidth) {
583 x += strWidth;
584 break;
585 }
586 i--;
587 }
588 x += strWidth;
589 }
590 int paintWidth = x;
591 if (label.getInsets() != null) {
592 paintWidth += label.getInsets().left + label.getInsets().right;
593 }
594 int paintRows = internalPaintStyledText(label, null, 0, 0, paintWidth);
595 if (paintRows != limitedRows) {
596 maxWidth = Math.min(maxWidth, textWidth);
597 while (paintRows > limitedRows && paintWidth < maxWidth) {
598 paintWidth += 2;
599 paintRows = internalPaintStyledText(label, null, 0, 0, paintWidth);
600 }
601 while (paintRows < limitedRows && paintWidth > 0) {
602 paintWidth -= 2;
603 paintRows = internalPaintStyledText(label, null, 0, 0, paintWidth);
604 }
605 x = paintWidth;
606 if (label.getInsets() != null) {
607 x -= label.getInsets().left + label.getInsets().right;
608 }
609 }
610 _preferredRowCount = limitedRows;
611 return x;
612 }
613
614 /**
615 * Gets the font from the label.
616 *
617 * @param label the label.
618 * @return the font. If label's getFont is null, we will use Label.font instead.
619 */
620 protected Font getFont(StyledLabel label) {
621 Font font = label.getFont();
622 if (font == null) {
623 font = UIDefaultsLookup.getFont("Label.font");
624 }
625 return font;
626 }
627
628 protected void paintStyledText(StyledLabel label, Graphics g, int textX, int textY) {
629 label.setTruncated(false);
630 int paintWidth = label.getWidth();
631 if (label.isLineWrap()) {
632 int oldPreferredWidth = label.getPreferredWidth();
633 int oldRows = label.getRows();
634 try {
635 label.setRows(0);
636 paintWidth = getPreferredSize(label).width;
637 label.setPreferredWidth(oldPreferredWidth > 0 ? Math.min(label.getWidth(), oldPreferredWidth) : label.getWidth());
638 Dimension sizeOnWidth = getPreferredSize(label);
639 if (sizeOnWidth.width < paintWidth) {
640 paintWidth = sizeOnWidth.width;
641 }
642 }
643 finally {
644 label.setPreferredWidth(oldPreferredWidth);
645 label.setRows(oldRows);
646 }
647 }
648 Color oldColor = g.getColor();
649 int textWidth = label.getWidth();
650 if (label.getInsets() != null) {
651 textWidth -= label.getInsets().left + label.getInsets().right;
652 }
653 if (label.getIcon() != null && label.getHorizontalTextPosition() != SwingConstants.CENTER) {
654 textWidth -= label.getIcon().getIconWidth() + label.getIconTextGap();
655 }
656 paintWidth = Math.min(paintWidth, textWidth);
657 internalPaintStyledText(label, g, textX, textY, paintWidth);
658 g.setColor(oldColor);
659 }
660
661 private int internalPaintStyledText(StyledLabel label, Graphics g, int textX, int textY, int paintWidth) {
662 int labelHeight = label.getHeight();
663 if (labelHeight <= 0) {
664 labelHeight = Integer.MAX_VALUE;
665 }
666 Insets insets = label.getInsets();
667 if (insets != null) {
668 labelHeight -= insets.top + insets.bottom;
669 }
670 int leftMostX = 0;
671 if (insets != null) {
672 leftMostX += insets.left;
673 }
674 if (label.getIcon() != null) {
675 int horizontalTextPosition = label.getHorizontalTextPosition();
676 if ((horizontalTextPosition == SwingConstants.TRAILING && label.getComponentOrientation().isLeftToRight()) ||(horizontalTextPosition == SwingConstants.LEADING && !label.getComponentOrientation().isLeftToRight())) {
677 horizontalTextPosition = SwingConstants.RIGHT;
678 }
679 if (horizontalTextPosition == SwingConstants.RIGHT) {
680 leftMostX += label.getIcon().getIconWidth() + label.getIconTextGap();
681 }
682 }
683
684 int startX = textX < leftMostX ? leftMostX : textX;
685 int y;
686 int endX = paintWidth + startX;
687 int x = startX;
688 int mnemonicIndex = label.getDisplayedMnemonicIndex();
689 if (LookAndFeelFactory.isWindowsLookAndFeel(UIManager.getLookAndFeel()) &&
690 LookAndFeelFactory.isMnemonicHidden()) {
691 mnemonicIndex = -1;
692 }
693
694 int charDisplayed = 0;
695 boolean displayMnemonic;
696 int mneIndex = 0;
697 Font font = getFont(label);
698 FontMetrics fm = label.getFontMetrics(font);
699 FontMetrics fm2;
700 FontMetrics nextFm2 = null;
701 int defaultFontSize = font.getSize();
702
703 synchronized (_styledTexts) {
704 String nextS;
705 int maxRowHeight = fm.getHeight();
706 int minStartY = fm.getAscent();
707 int horizontalAlignment = label.getHorizontalAlignment();
708 switch (horizontalAlignment) {
709 case LEADING:
710 horizontalAlignment = label.getComponentOrientation().isLeftToRight() ? LEFT : RIGHT;
711 break;
712 case TRAILING:
713 horizontalAlignment = label.getComponentOrientation().isLeftToRight() ? RIGHT : LEFT;
714 break;
715 }
716 for (StyledText styledText : _styledTexts) {
717 StyleRange style = styledText.styleRange;
718 int size = (style != null && (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
719
720 font = getFont(label);
721 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
722 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
723 fm2 = label.getFontMetrics(font);
724 maxRowHeight = Math.max(maxRowHeight, fm2.getHeight());
725 minStartY = Math.max(minStartY, fm2.getAscent());
726 }
727 }
728 boolean lineWrap = label.isLineWrap();
729 if (!lineWrap) {
730 for (StyledText styledText : _styledTexts) {
731 if (styledText.text.endsWith("\n")) {
732 lineWrap = true;
733 break;
734 }
735 }
736 }
737 if (lineWrap && textY < minStartY) {
738 textY = minStartY;
739 }
740
741 int nextRowStartIndex = 0;
742 int rowCount = 0;
743 int rowStartOffset = 0;
744 for (int i = 0; i < _styledTexts.size(); i++) {
745 StyledText styledText = _styledTexts.get(i);
746 StyleRange style = styledText.styleRange;
747
748 if (mnemonicIndex >= 0 && styledText.text.length() - nextRowStartIndex > mnemonicIndex - charDisplayed) {
749 displayMnemonic = true;
750 mneIndex = mnemonicIndex - charDisplayed;
751 }
752 else {
753 displayMnemonic = false;
754 }
755 charDisplayed += styledText.text.length() - nextRowStartIndex;
756 if (styledText.text.contains("\r") || styledText.text.contains("\n")) {
757 boolean lastRow = (label.getMaxRows() > 0 && rowCount >= label.getMaxRows() - 1) || textY + maxRowHeight + Math.max(0, label.getRowGap()) > labelHeight;
758 if (horizontalAlignment != LEFT && g != null) {
759 if (lastRow && i != _styledTexts.size() - 1) {
760 x += fm.stringWidth("...");
761 }
762 paintRow(label, g, startX, x, endX, textY, rowStartOffset, style.getStart() + styledText.text.length(), lastRow);
763 }
764 rowStartOffset = style.getStart();
765 nextRowStartIndex = 0;
766 nextFm2 = null;
767 if (!lastRow) {
768 rowStartOffset += style.getLength();
769 rowCount++;
770 x = startX;
771 textY += maxRowHeight + Math.max(0, label.getRowGap());
772 continue; // continue to paint "..." if lastRow is true
773 }
774 else if (horizontalAlignment != LEFT && g != null) {
775 break;
776 }
777 }
778
779 y = textY;
780
781 if (nextFm2 == null) {
782 int size = (style != null &&
783 (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
784
785 font = getFont(label);
786 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
787 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
788 fm2 = label.getFontMetrics(font);
789 }
790 else {
791 fm2 = fm;
792 }
793 }
794 else {
795 fm2 = nextFm2;
796 }
797
798 if (g != null) {
799 g.setFont(font);
800 }
801
802 boolean stop = false;
803 String s = styledText.text.substring(Math.min(nextRowStartIndex, styledText.text.length()));
804 if (s.contains("\r") || s.contains("\n")) {
805 s = "...";
806 stop = true;
807 }
808
809 int strWidth = fm2.stringWidth(s);
810
811 boolean wrapped = false;
812 int widthLeft = endX - x;
813 if (widthLeft < strWidth && widthLeft >= 0) {
814 if (label.isLineWrap() && ((label.getMaxRows() > 0 && rowCount < label.getMaxRows() - 1) || label.getMaxRows() <= 0) && y + maxRowHeight + Math.max(0, label.getRowGap()) <= labelHeight) {
815 wrapped = true;
816 int availLength = s.length() * widthLeft / strWidth + 1;
817 int nextWordStartIndex;
818 int nextRowStartIndexInSubString = 0;
819 boolean needBreak = false;
820 boolean needContinue = false;
821 int loopCount = 0;
822 do {
823 String subString = s.substring(0, Math.max(0, Math.min(availLength, s.length())));
824 int firstRowWordEndIndex = findFirstRowWordEndIndex(subString);
825 nextWordStartIndex = firstRowWordEndIndex < 0 ? 0 : findNextWordStartIndex(s, firstRowWordEndIndex);
826 if (firstRowWordEndIndex < 0) {
827 if (x != startX) {
828 boolean lastRow = label.getMaxRows() > 0 && rowCount >= label.getMaxRows() - 1;
829 if (horizontalAlignment != LEFT && g != null) {
830 paintRow(label, g, startX, x, endX, textY, rowStartOffset, style.getStart() + Math.min(nextRowStartIndex, styledText.text.length()), lastRow);
831 }
832 textY += maxRowHeight + Math.max(0, label.getRowGap());
833 x = startX;
834 i--;
835 rowCount++;
836 rowStartOffset = style.getStart() + Math.min(nextRowStartIndex, styledText.text.length());
837 if (lastRow) {
838 needBreak = true;
839 }
840 needContinue = true;
841 break;
842 }
843 else {
844 firstRowWordEndIndex = 0;
845 nextWordStartIndex = Math.min(s.length(), availLength);
846 }
847 }
848 nextRowStartIndexInSubString = firstRowWordEndIndex + 1;
849 String subStringThisRow = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
850 strWidth = fm2.stringWidth(subStringThisRow);
851 if (strWidth > widthLeft) {
852 availLength = subString.length() * widthLeft / strWidth;
853 }
854 loopCount++;
855 if (loopCount > 15) {
856 System.err.println("Painting Styled Label Error: " + styledText);
857 break;
858 }
859 } while (strWidth > widthLeft && availLength > 0);
860 if (needBreak) {
861 break;
862 }
863 if (needContinue) {
864 continue;
865 }
866 while (nextRowStartIndexInSubString < nextWordStartIndex) {
867 strWidth += fm2.charWidth(s.charAt(nextRowStartIndexInSubString));
868 if (strWidth >= widthLeft) {
869 break;
870 }
871 nextRowStartIndexInSubString++;
872 }
873 s = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
874 strWidth = fm2.stringWidth(s);
875 charDisplayed -= styledText.text.length() - nextRowStartIndex;
876 if (displayMnemonic) {
877 if (mnemonicIndex >= 0 && s.length() > mnemonicIndex - charDisplayed) {
878 displayMnemonic = true;
879 mneIndex = mnemonicIndex - charDisplayed;
880 }
881 else {
882 displayMnemonic = false;
883 }
884 }
885 charDisplayed += s.length();
886 nextRowStartIndex += nextRowStartIndexInSubString;
887 }
888 else {
889 // use this method to clip string
890 s = SwingUtilities.layoutCompoundLabel(label, fm2, s, null, label.getVerticalAlignment(), label.getHorizontalAlignment(),
891 label.getVerticalTextPosition(), label.getHorizontalTextPosition(), new Rectangle(x, y, widthLeft, labelHeight), new Rectangle(), new Rectangle(), 0);
892 strWidth = fm2.stringWidth(s);
893 }
894 stop = !lineWrap || y + maxRowHeight + Math.max(0, label.getRowGap()) > labelHeight || (label.getMaxRows() > 0 && rowCount >= label.getMaxRows() - 1);
895 }
896 else if (lineWrap) {
897 nextRowStartIndex = 0;
898 }
899 else if (i < _styledTexts.size() - 1) {
900 StyledText nextStyledText = _styledTexts.get(i + 1);
901 String nextText = nextStyledText.text;
902 StyleRange nextStyle = nextStyledText.styleRange;
903 int size = (nextStyle != null &&
904 (nextStyle.isSuperscript() || nextStyle.isSubscript())) ? Math.round((float) defaultFontSize / nextStyle.getFontShrinkRatio()) : defaultFontSize;
905
906 font = getFont(label);
907 if (nextStyle != null && ((nextStyle.getFontStyle() != -1 && font.getStyle() != nextStyle.getFontStyle()) || font.getSize() != size)) {
908 font = FontUtils.getCachedDerivedFont(font, nextStyle.getFontStyle() == -1 ? font.getStyle() : nextStyle.getFontStyle(), size);
909 nextFm2 = label.getFontMetrics(font);
910 }
911 else {
912 nextFm2 = fm;
913 }
914 if (nextFm2.stringWidth(nextText) > widthLeft - strWidth) {
915 nextS = SwingUtilities.layoutCompoundLabel(label, nextFm2, nextText, null, label.getVerticalAlignment(), label.getHorizontalAlignment(),
916 label.getVerticalTextPosition(), label.getHorizontalTextPosition(), new Rectangle(x + strWidth, y, widthLeft - strWidth, labelHeight), new Rectangle(), new Rectangle(), 0);
917 if (nextFm2.stringWidth(nextS) > widthLeft - strWidth) {
918 s = SwingUtilities.layoutCompoundLabel(label, fm2, s, null, label.getVerticalAlignment(), label.getHorizontalAlignment(),
919 label.getVerticalTextPosition(), label.getHorizontalTextPosition(), new Rectangle(x, y, strWidth - 1, labelHeight), new Rectangle(), new Rectangle(), 0);
920 strWidth = fm2.stringWidth(s);
921 stop = true;
922 }
923 }
924 }
925
926 // start of actual painting
927 if (rowCount > 0 && x == startX && s.startsWith(" ")) {
928 s = s.substring(1);
929 strWidth = fm2.stringWidth(s);
930 }
931 if (horizontalAlignment == LEFT && g != null) {
932 if (style != null && style.isSuperscript()) {
933 y -= fm.getHeight() - fm2.getHeight();
934 }
935
936 if (style != null && style.getBackgroundColor() != null) {
937 g.setColor(style.getBackgroundColor());
938 g.fillRect(x, y - fm2.getHeight(), strWidth, fm2.getHeight() + 4);
939 }
940
941 Color textColor = (style != null && !label.isIgnoreColorSettings() && style.getFontColor() != null) ? style.getFontColor() : label.getForeground();
942 if (!label.isEnabled()) {
943 textColor = UIDefaultsLookup.getColor("Label.disabledForeground");
944 }
945 g.setColor(textColor);
946
947 if (displayMnemonic) {
948 JideSwingUtilities.drawStringUnderlineCharAt(label, g, s, mneIndex, x, y);
949 }
950 else {
951 JideSwingUtilities.drawString(label, g, s, x, y);
952 }
953
954 if (style != null) {
955 Stroke oldStroke = ((Graphics2D) g).getStroke();
956 if (style.getLineStroke() != null) {
957 ((Graphics2D) g).setStroke(style.getLineStroke());
958 }
959
960 if (!label.isIgnoreColorSettings() && style.getLineColor() != null) {
961 g.setColor(style.getLineColor());
962 }
963
964 if (style.isStrikethrough()) {
965 int lineY = y + (fm2.getDescent() - fm2.getAscent()) / 2;
966 g.drawLine(x, lineY, x + strWidth - 1, lineY);
967 }
968 if (style.isDoublestrikethrough()) {
969 int lineY = y + (fm2.getDescent() - fm2.getAscent()) / 2;
970 g.drawLine(x, lineY - 1, x + strWidth - 1, lineY - 1);
971 g.drawLine(x, lineY + 1, x + strWidth - 1, lineY + 1);
972 }
973 if (style.isUnderlined()) {
974 int lineY = y + 1;
975 g.drawLine(x, lineY, x + strWidth - 1, lineY);
976 }
977 if (style.isDotted()) {
978 int dotY = y + 1;
979 for (int dotX = x; dotX < x + strWidth; dotX += 4) {
980 g.drawRect(dotX, dotY, 1, 1);
981 }
982 }
983 if (style.isWaved()) {
984 int waveY = y + 1;
985 for (int waveX = x; waveX < x + strWidth; waveX += 4) {
986 if (waveX + 2 <= x + strWidth - 1)
987 g.drawLine(waveX, waveY + 2, waveX + 2, waveY);
988 if (waveX + 4 <= x + strWidth - 1)
989 g.drawLine(waveX + 3, waveY + 1, waveX + 4, waveY + 2);
990 }
991 }
992 if (style.getLineStroke() != null) {
993 ((Graphics2D) g).setStroke(oldStroke);
994 }
995 }
996 }
997 // end of actual painting
998
999 if (stop) {
1000 if (horizontalAlignment != LEFT && g != null) {
1001 x += strWidth;
1002 paintRow(label, g, startX, x, endX, textY, rowStartOffset, label.getText().length(), true);
1003 }
1004 label.setTruncated(true);
1005 break;
1006 }
1007
1008 if (wrapped) {
1009 boolean lastRow = (label.getMaxRows() > 0 && rowCount >= label.getMaxRows() - 1) || textY + maxRowHeight + Math.max(0, label.getRowGap()) > labelHeight;
1010 if (horizontalAlignment != LEFT && g != null) {
1011 x += strWidth;
1012 paintRow(label, g, startX, x, endX, textY, rowStartOffset, style.getStart() + Math.min(nextRowStartIndex, styledText.text.length()), lastRow);
1013 }
1014 textY += maxRowHeight + Math.max(0, label.getRowGap());
1015 x = startX;
1016 i--;
1017 rowCount++;
1018 rowStartOffset = style.getStart() + Math.min(nextRowStartIndex, styledText.text.length());
1019 if (lastRow) {
1020 break;
1021 }
1022 }
1023 else {
1024 x += strWidth;
1025 }
1026 if (i == _styledTexts.size() - 1) {
1027 if (horizontalAlignment != LEFT && g != null) {
1028 paintRow(label, g, startX, x, endX, textY, rowStartOffset, -1, true);
1029 }
1030 }
1031 }
1032 return (int) Math.ceil((double) textY / maxRowHeight);
1033 }
1034 }
1035
1036 private void paintRow(StyledLabel label, Graphics g, int leftAlignmentX, int thisLineEndX, int rightMostX, int textY, int startOffset, int endOffset, boolean lastRow) {
1037 if (g == null) {
1038 return;
1039 }
1040 int horizontalTextPosition = label.getHorizontalTextPosition();
1041 int horizontalAlignment = label.getHorizontalAlignment();
1042 if ((horizontalTextPosition == SwingConstants.TRAILING && !label.getComponentOrientation().isLeftToRight()) ||(horizontalTextPosition == SwingConstants.LEADING && label.getComponentOrientation().isLeftToRight())) {
1043 horizontalTextPosition = SwingConstants.LEFT;
1044 }
1045 if ((horizontalTextPosition == SwingConstants.LEADING && !label.getComponentOrientation().isLeftToRight()) ||(horizontalTextPosition == SwingConstants.TRAILING && label.getComponentOrientation().isLeftToRight())) {
1046 horizontalTextPosition = SwingConstants.RIGHT;
1047 }
1048 if ((horizontalAlignment == SwingConstants.TRAILING && !label.getComponentOrientation().isLeftToRight()) ||(horizontalAlignment == SwingConstants.LEADING && label.getComponentOrientation().isLeftToRight())) {
1049 horizontalAlignment = SwingConstants.LEFT;
1050 }
1051 if ((horizontalAlignment == SwingConstants.LEADING && !label.getComponentOrientation().isLeftToRight()) ||(horizontalAlignment == SwingConstants.TRAILING && label.getComponentOrientation().isLeftToRight())) {
1052 horizontalAlignment = SwingConstants.RIGHT;
1053 }
1054
1055 Insets insets = label.getInsets();
1056 int textX = leftAlignmentX;
1057 int paintWidth = thisLineEndX - leftAlignmentX;
1058 if (horizontalAlignment == RIGHT) {
1059 paintWidth = thisLineEndX - textX;
1060 textX = label.getWidth() - paintWidth;
1061 if (insets != null) {
1062 textX -= insets.right;
1063 }
1064 if (label.getIcon() != null && horizontalTextPosition == SwingConstants.LEFT) {
1065 textX -= label.getIcon().getIconWidth() + label.getIconTextGap();
1066 }
1067 }
1068 else if (horizontalAlignment == CENTER) {
1069 int leftMostX = 0;
1070 if (horizontalTextPosition == SwingConstants.RIGHT && label.getIcon() != null) {
1071 leftMostX += label.getIcon().getIconWidth() + label.getIconTextGap();
1072 }
1073 int labelWidth = label.getWidth();
1074 if (insets != null) {
1075 labelWidth -= insets.right + insets.left;
1076 leftMostX += insets.left;
1077 }
1078 if (label.getIcon() != null && horizontalTextPosition != SwingConstants.CENTER) {
1079 labelWidth -= label.getIcon().getIconWidth() + label.getIconTextGap();
1080 }
1081 textX = leftMostX + (labelWidth - paintWidth) / 2;
1082 }
1083 paintWidth = Math.min(paintWidth, rightMostX - leftAlignmentX);
1084
1085 int mnemonicIndex = label.getDisplayedMnemonicIndex();
1086 if (LookAndFeelFactory.isWindowsLookAndFeel(UIManager.getLookAndFeel()) &&
1087 LookAndFeelFactory.isMnemonicHidden()) {
1088 mnemonicIndex = -1;
1089 }
1090
1091 int charDisplayed = 0;
1092 boolean displayMnemonic;
1093 int mneIndex = 0;
1094 Font font = getFont(label);
1095 FontMetrics fm = label.getFontMetrics(font);
1096 FontMetrics fm2;
1097 FontMetrics nextFm2 = null;
1098 int defaultFontSize = font.getSize();
1099
1100 int x = textX;
1101 for (int i = 0; i < _styledTexts.size() && (endOffset < 0 || charDisplayed < endOffset); i++) {
1102 StyledText styledText = _styledTexts.get(i);
1103 StyleRange style = styledText.styleRange;
1104 int length = style.getLength();
1105 if (length < 0) {
1106 length = styledText.text.length();
1107 }
1108 if (style.getStart() + length <= startOffset) {
1109 charDisplayed += length;
1110 continue;
1111 }
1112 int nextRowStartIndex = style.getStart() >= startOffset ? 0 : startOffset - style.getStart();
1113 charDisplayed += nextRowStartIndex;
1114
1115 if (mnemonicIndex >= 0 && styledText.text.length() - nextRowStartIndex > mnemonicIndex - charDisplayed) {
1116 displayMnemonic = true;
1117 mneIndex = mnemonicIndex - charDisplayed;
1118 }
1119 else {
1120 displayMnemonic = false;
1121 }
1122 int paintLength = styledText.text.length() - nextRowStartIndex;
1123 if (endOffset >= 0 && charDisplayed + paintLength >= endOffset) {
1124 paintLength = endOffset - charDisplayed;
1125 }
1126 charDisplayed += paintLength;
1127
1128 int y = textY;
1129
1130 if (nextFm2 == null) {
1131 int size = (style != null &&
1132 (style.isSuperscript() || style.isSubscript())) ? Math.round((float) defaultFontSize / style.getFontShrinkRatio()) : defaultFontSize;
1133
1134 font = getFont(label);
1135 if (style != null && ((style.getFontStyle() != -1 && font.getStyle() != style.getFontStyle()) || font.getSize() != size)) {
1136 font = FontUtils.getCachedDerivedFont(font, style.getFontStyle() == -1 ? font.getStyle() : style.getFontStyle(), size);
1137 fm2 = label.getFontMetrics(font);
1138 }
1139 else {
1140 fm2 = fm;
1141 }
1142 }
1143 else {
1144 fm2 = nextFm2;
1145 }
1146
1147 g.setFont(font);
1148
1149 String s = styledText.text.substring(Math.min(nextRowStartIndex, styledText.text.length()));
1150 if (startOffset > 0 && x == textX && s.startsWith(" ")) {
1151 s = s.substring(1);
1152 }
1153 if (s.length() > paintLength) {
1154 s = s.substring(0, paintLength);
1155 }
1156 if (s.contains("\r") || s.contains("\n")) {
1157 if (styledText.styleRange.getStart() + styledText.styleRange.getLength() >= endOffset) {
1158 break;
1159 }
1160 s = "...";
1161 }
1162
1163 int strWidth = fm2.stringWidth(s);
1164
1165 int widthLeft = paintWidth + textX - x;
1166 if (widthLeft < strWidth) {
1167 if (strWidth <= 0) {
1168 return;
1169 }
1170 if (label.isLineWrap() && !lastRow) {
1171 int availLength = s.length() * widthLeft / strWidth + 1;
1172 int nextWordStartIndex;
1173 int nextRowStartIndexInSubString;
1174 int loopCount = 0;
1175 do {
1176 String subString = s.substring(0, Math.max(0, Math.min(availLength, s.length())));
1177 int firstRowWordEndIndex = findFirstRowWordEndIndex(subString);
1178 nextWordStartIndex = firstRowWordEndIndex < 0 ? 0 : findNextWordStartIndex(s, firstRowWordEndIndex);
1179 if (firstRowWordEndIndex < 0) {
1180 if (x == textX) {
1181 firstRowWordEndIndex = 0;
1182 nextWordStartIndex = Math.min(s.length(), availLength);
1183 }
1184 }
1185 nextRowStartIndexInSubString = firstRowWordEndIndex + 1;
1186 String subStringThisRow = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
1187 strWidth = fm2.stringWidth(subStringThisRow);
1188 if (strWidth > widthLeft) {
1189 availLength = subString.length() * widthLeft / strWidth;
1190 }
1191 loopCount++;
1192 if (loopCount > 50) {
1193 System.err.println("Painting Styled Label Error: " + styledText);
1194 break;
1195 }
1196 }
1197 while (strWidth > widthLeft && availLength > 0);
1198 while (nextRowStartIndexInSubString < nextWordStartIndex) {
1199 strWidth += fm2.charWidth(s.charAt(nextRowStartIndexInSubString));
1200 if (strWidth >= widthLeft) {
1201 break;
1202 }
1203 nextRowStartIndexInSubString++;
1204 }
1205 s = s.substring(0, Math.min(nextRowStartIndexInSubString, s.length()));
1206 strWidth = fm2.stringWidth(s);
1207 charDisplayed -= styledText.text.length() - nextRowStartIndex;
1208 if (displayMnemonic) {
1209 if (mnemonicIndex >= 0 && s.length() > mnemonicIndex - charDisplayed) {
1210 displayMnemonic = true;
1211 mneIndex = mnemonicIndex - charDisplayed;
1212 }
1213 else {
1214 displayMnemonic = false;
1215 }
1216 }
1217 charDisplayed += s.length();
1218 nextRowStartIndex += nextRowStartIndexInSubString;
1219 }
1220 else {
1221 // use this method to clip string
1222 s = SwingUtilities.layoutCompoundLabel(label, fm2, s, null, label.getVerticalAlignment(), label.getHorizontalAlignment(),
1223 label.getVerticalTextPosition(), label.getHorizontalTextPosition(), new Rectangle(x, y, widthLeft, label.getHeight()), new Rectangle(), new Rectangle(), 0);
1224 strWidth = fm2.stringWidth(s);
1225 }
1226 }
1227 else if (label.isLineWrap()) {
1228 nextRowStartIndex = 0;
1229 }
1230 else if (i < _styledTexts.size() - 1) {
1231 StyledText nextStyledText = _styledTexts.get(i + 1);
1232 String nextText = nextStyledText.text;
1233 StyleRange nextStyle = nextStyledText.styleRange;
1234 int size = (nextStyle != null &&
1235 (nextStyle.isSuperscript() || nextStyle.isSubscript())) ? Math.round((float) defaultFontSize / nextStyle.getFontShrinkRatio()) : defaultFontSize;
1236
1237 font = getFont(label);
1238 if (nextStyle != null && ((nextStyle.getFontStyle() != -1 && font.getStyle() != nextStyle.getFontStyle()) || font.getSize() != size)) {
1239 font = FontUtils.getCachedDerivedFont(font, nextStyle.getFontStyle() == -1 ? font.getStyle() : nextStyle.getFontStyle(), size);
1240 nextFm2 = label.getFontMetrics(font);
1241 }
1242 else {
1243 nextFm2 = fm;
1244 }
1245 if (nextFm2.stringWidth(nextText) > widthLeft - strWidth) {
1246 String nextS = SwingUtilities.layoutCompoundLabel(label, nextFm2, nextText, null, label.getVerticalAlignment(), label.getHorizontalAlignment(),
1247 label.getVerticalTextPosition(), label.getHorizontalTextPosition(), new Rectangle(x + strWidth, y, widthLeft - strWidth, label.getHeight()), new Rectangle(), new Rectangle(), 0);
1248 if (nextFm2.stringWidth(nextS) > widthLeft - strWidth) {
1249 s = SwingUtilities.layoutCompoundLabel(label, fm2, s, null, label.getVerticalAlignment(), label.getHorizontalAlignment(),
1250 label.getVerticalTextPosition(), label.getHorizontalTextPosition(), new Rectangle(x, y, strWidth - 1, label.getHeight()), new Rectangle(), new Rectangle(), 0);
1251 strWidth = fm2.stringWidth(s);
1252 }
1253 }
1254 }
1255
1256 // start of actual painting
1257 if (style != null && style.isSuperscript()) {
1258 y -= fm.getHeight() - fm2.getHeight();
1259 }
1260
1261 if (style != null && style.getBackgroundColor() != null) {
1262 g.setColor(style.getBackgroundColor());
1263 g.fillRect(x, y - fm2.getHeight(), strWidth, fm2.getHeight() + 4);
1264 }
1265
1266 Color textColor = (style != null && !label.isIgnoreColorSettings() && style.getFontColor() != null) ? style.getFontColor() : label.getForeground();
1267 if (!label.isEnabled()) {
1268 textColor = UIDefaultsLookup.getColor("Label.disabledForeground");
1269 }
1270 g.setColor(textColor);
1271
1272 if (displayMnemonic) {
1273 JideSwingUtilities.drawStringUnderlineCharAt(label, g, s, mneIndex, x, y);
1274 }
1275 else {
1276 JideSwingUtilities.drawString(label, g, s, x, y);
1277 }
1278
1279 if (style != null) {
1280 Stroke oldStroke = ((Graphics2D) g).getStroke();
1281 if (style.getLineStroke() != null) {
1282 ((Graphics2D) g).setStroke(style.getLineStroke());
1283 }
1284
1285 if (!label.isIgnoreColorSettings() && style.getLineColor() != null) {
1286 g.setColor(style.getLineColor());
1287 }
1288
1289 if (style.isStrikethrough()) {
1290 int lineY = y + (fm2.getDescent() - fm2.getAscent()) / 2;
1291 g.drawLine(x, lineY, x + strWidth - 1, lineY);
1292 }
1293 if (style.isDoublestrikethrough()) {
1294 int lineY = y + (fm2.getDescent() - fm2.getAscent()) / 2;
1295 g.drawLine(x, lineY - 1, x + strWidth - 1, lineY - 1);
1296 g.drawLine(x, lineY + 1, x + strWidth - 1, lineY + 1);
1297 }
1298 if (style.isUnderlined()) {
1299 int lineY = y + 1;
1300 g.drawLine(x, lineY, x + strWidth - 1, lineY);
1301 }
1302 if (style.isDotted()) {
1303 int dotY = y + 1;
1304 for (int dotX = x; dotX < x + strWidth; dotX += 4) {
1305 g.drawRect(dotX, dotY, 1, 1);
1306 }
1307 }
1308 if (style.isWaved()) {
1309 int waveY = y + 1;
1310 for (int waveX = x; waveX < x + strWidth; waveX += 4) {
1311 if (waveX + 2 <= x + strWidth - 1)
1312 g.drawLine(waveX, waveY + 2, waveX + 2, waveY);
1313 if (waveX + 4 <= x + strWidth - 1)
1314 g.drawLine(waveX + 3, waveY + 1, waveX + 4, waveY + 2);
1315 }
1316 }
1317 if (style.getLineStroke() != null) {
1318 ((Graphics2D) g).setStroke(oldStroke);
1319 }
1320 }
1321 // end of actual painting
1322 x += strWidth;
1323 }
1324 }
1325
1326 private int findNextWordStartIndex(String string, int firstRowEndIndex) {
1327 boolean skipFirstWord = firstRowEndIndex < 0;
1328 for (int i = firstRowEndIndex + 1; i < string.length(); i++) {
1329 char c = string.charAt(i);
1330 if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
1331 if (!skipFirstWord) {
1332 return i;
1333 }
1334 }
1335 else {
1336 skipFirstWord = false;
1337 }
1338 }
1339 return string.length();
1340 }
1341
1342 private int findFirstRowWordEndIndex(String string) {
1343 boolean spaceFound = false;
1344 for (int i = string.length() - 1; i >= 0; i--) {
1345 char c = string.charAt(i);
1346 if (!spaceFound) {
1347 if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
1348 spaceFound = true;
1349 }
1350 }
1351 else {
1352 if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
1353 return i;
1354 }
1355 }
1356 }
1357 return -1;
1358 }
1359
1360 /**
1361 * Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly
1362 * clipped version of the compound labels string. Locations are computed relative to the viewR rectangle. The
1363 * JComponents orientation (LEADING/TRAILING) will also be taken into account and translated into LEFT/RIGHT values
1364 * accordingly.
1365 *
1366 * @param c the component
1367 * @param fm the font metrics
1368 * @param text the text
1369 * @param icon the icon
1370 * @param verticalAlignment vertical alignment mode
1371 * @param horizontalAlignment horizontal alignment mode
1372 * @param verticalTextPosition vertical text position
1373 * @param horizontalTextPosition horizontal text position
1374 * @param viewR view rectangle
1375 * @param iconR icon rectangle
1376 * @param textR text rectangle
1377 * @param textIconGap the gap between text and icon
1378 * @return the layout string
1379 */
1380 public static String layoutCompoundLabel(JComponent c,
1381 FontMetrics fm,
1382 String text,
1383 Icon icon,
1384 int verticalAlignment,
1385 int horizontalAlignment,
1386 int verticalTextPosition,
1387 int horizontalTextPosition,
1388 Rectangle viewR,
1389 Rectangle iconR,
1390 Rectangle textR,
1391 int textIconGap) {
1392 boolean orientationIsLeftToRight = true;
1393 int hAlign = horizontalAlignment;
1394 int hTextPos = horizontalTextPosition;
1395
1396 if (c != null) {
1397 if (!(c.getComponentOrientation().isLeftToRight())) {
1398 orientationIsLeftToRight = false;
1399 }
1400 }
1401
1402 // Translate LEADING/TRAILING values in horizontalAlignment
1403 // to LEFT/RIGHT values depending on the components orientation
1404 switch (horizontalAlignment) {
1405 case LEADING:
1406 hAlign = (orientationIsLeftToRight) ? LEFT : RIGHT;
1407 break;
1408 case TRAILING:
1409 hAlign = (orientationIsLeftToRight) ? RIGHT : LEFT;
1410 break;
1411 }
1412
1413 // Translate LEADING/TRAILING values in horizontalTextPosition
1414 // to LEFT/RIGHT values depending on the components orientation
1415 switch (horizontalTextPosition) {
1416 case LEADING:
1417 hTextPos = (orientationIsLeftToRight) ? LEFT : RIGHT;
1418 break;
1419 case TRAILING:
1420 hTextPos = (orientationIsLeftToRight) ? RIGHT : LEFT;
1421 break;
1422 }
1423
1424 return layoutCompoundLabelImpl(c,
1425 fm,
1426 text,
1427 icon,
1428 verticalAlignment,
1429 hAlign,
1430 verticalTextPosition,
1431 hTextPos,
1432 viewR,
1433 iconR,
1434 textR,
1435 textIconGap);
1436 }
1437
1438 /**
1439 * Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly
1440 * clipped version of the compound labels string. Locations are computed relative to the viewR rectangle. This
1441 * layoutCompoundLabel() does not know how to handle LEADING/TRAILING values in horizontalTextPosition (they will
1442 * default to RIGHT) and in horizontalAlignment (they will default to CENTER). Use the other version of
1443 * layoutCompoundLabel() instead.
1444 *
1445 * @param fm the font metrics
1446 * @param text the text
1447 * @param icon the icon
1448 * @param verticalAlignment vertical alignment mode
1449 * @param horizontalAlignment horizontal alignment mode
1450 * @param verticalTextPosition vertical text position
1451 * @param horizontalTextPosition horizontal text position
1452 * @param viewR view rectangle
1453 * @param iconR icon rectangle
1454 * @param textR text rectangle
1455 * @param textIconGap the gap between text and icon
1456 * @return the layout string
1457 */
1458 public static String layoutCompoundLabel(
1459 FontMetrics fm,
1460 String text,
1461 Icon icon,
1462 int verticalAlignment,
1463 int horizontalAlignment,
1464 int verticalTextPosition,
1465 int horizontalTextPosition,
1466 Rectangle viewR,
1467 Rectangle iconR,
1468 Rectangle textR,
1469 int textIconGap) {
1470 return layoutCompoundLabelImpl(null, fm, text, icon,
1471 verticalAlignment,
1472 horizontalAlignment,
1473 verticalTextPosition,
1474 horizontalTextPosition,
1475 viewR, iconR, textR, textIconGap);
1476 }
1477
1478 /**
1479 * Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly
1480 * clipped version of the compound labels string. Locations are computed relative to the viewR rectangle. This
1481 * layoutCompoundLabel() does not know how to handle LEADING/TRAILING values in horizontalTextPosition (they will
1482 * default to RIGHT) and in horizontalAlignment (they will default to CENTER). Use the other version of
1483 * layoutCompoundLabel() instead.
1484 *
1485 * @param c the component
1486 * @param fm the font metrics
1487 * @param text the text
1488 * @param icon the icon
1489 * @param verticalAlignment vertical alignment mode
1490 * @param horizontalAlignment horizontal alignment mode
1491 * @param verticalTextPosition vertical text position
1492 * @param horizontalTextPosition horizontal text position
1493 * @param viewR view rectangle
1494 * @param iconR icon rectangle
1495 * @param textR text rectangle
1496 * @param textIconGap the gap between text and icon
1497 * @return the layout string
1498 */
1499 @SuppressWarnings({"UnusedDeclaration"})
1500 private static String layoutCompoundLabelImpl(
1501 JComponent c,
1502 FontMetrics fm,
1503 String text,
1504 Icon icon,
1505 int verticalAlignment,
1506 int horizontalAlignment,
1507 int verticalTextPosition,
1508 int horizontalTextPosition,
1509 Rectangle viewR,
1510 Rectangle iconR,
1511 Rectangle textR,
1512 int textIconGap) {
1513 /* Initialize the icon bounds rectangle iconR.
1514 */
1515
1516 if (icon != null) {
1517 iconR.width = icon.getIconWidth();
1518 iconR.height = icon.getIconHeight();
1519 }
1520 else {
1521 iconR.width = iconR.height = 0;
1522 }
1523
1524 /* Initialize the text bounds rectangle textR. If a null
1525 * or and empty String was specified we substitute "" here
1526 * and use 0,0,0,0 for textR.
1527 */
1528
1529 boolean textIsEmpty = (text == null) || text.equals("");
1530 int lsb = 0;
1531 /* Unless both text and icon are non-null, we effectively ignore
1532 * the value of textIconGap.
1533 */
1534 int gap;
1535
1536 View v;
1537 if (textIsEmpty) {
1538 textR.width = textR.height = 0;
1539 text = "";
1540 gap = 0;
1541 }
1542 else {
1543 int availTextWidth;
1544 gap = (icon == null) ? 0 : textIconGap;
1545
1546 if (horizontalTextPosition == CENTER) {
1547 availTextWidth = viewR.width;
1548 }
1549 else {
1550 availTextWidth = viewR.width - (iconR.width + gap);
1551 }
1552 v = (c != null) ? (View) c.getClientProperty("html") : null;
1553 if (v != null) {
1554 textR.width = Math.min(availTextWidth, (int) v.getPreferredSpan(View.X_AXIS));
1555 textR.height = (int) v.getPreferredSpan(View.Y_AXIS);
1556 }
1557 else {
1558 // this is only place that is changed for StyledLabel
1559 // textR.width = SwingUtilities2.stringWidth(c, fm, text);
1560 // lsb = SwingUtilities2.getLeftSideBearing(c, fm, text);
1561 // if (lsb < 0) {
1562 // // If lsb is negative, add it to the width and later
1563 // // adjust the x location. This gives more space than is
1564 // // actually needed.
1565 // // This is done like this for two reasons:
1566 // // 1. If we set the width to the actual bounds all
1567 // // callers would have to account for negative lsb
1568 // // (pref size calculations ONLY look at width of
1569 // // textR)
1570 // // 2. You can do a drawString at the returned location
1571 // // and the text won't be clipped.
1572 // textR.width -= lsb;
1573 // }
1574 // if (textR.width > availTextWidth) {
1575 // text = SwingUtilities2.clipString(c, fm, text,
1576 // availTextWidth);
1577 // textR.width = SwingUtilities2.stringWidth(c, fm, text);
1578 // }
1579 // textR.height = fm.getHeight();
1580 }
1581 }
1582
1583 /* Compute textR.x,y given the verticalTextPosition and
1584 * horizontalTextPosition properties
1585 */
1586
1587 if (verticalTextPosition == TOP) {
1588 if (horizontalTextPosition != CENTER) {
1589 textR.y = 0;
1590 }
1591 else {
1592 textR.y = -(textR.height + gap);
1593 }
1594 }
1595 else if (verticalTextPosition == CENTER) {
1596 textR.y = (iconR.height / 2) - (textR.height / 2);
1597 }
1598 else { // (verticalTextPosition == BOTTOM)
1599 if (horizontalTextPosition != CENTER) {
1600 textR.y = iconR.height - textR.height;
1601 }
1602 else {
1603 textR.y = (iconR.height + gap);
1604 }
1605 }
1606
1607 if (horizontalTextPosition == LEFT) {
1608 textR.x = -(textR.width + gap);
1609 }
1610 else if (horizontalTextPosition == CENTER) {
1611 textR.x = (iconR.width / 2) - (textR.width / 2);
1612 }
1613 else { // (horizontalTextPosition == RIGHT)
1614 textR.x = (iconR.width + gap);
1615 }
1616
1617 /* labelR is the rectangle that contains iconR and textR.
1618 * Move it to its proper position given the labelAlignment
1619 * properties.
1620 *
1621 * To avoid actually allocating a Rectangle, Rectangle.union
1622 * has been inlined below.
1623 */
1624 int labelR_x = Math.min(iconR.x, textR.x);
1625 int labelR_width = Math.max(iconR.x + iconR.width,
1626 textR.x + textR.width) - labelR_x;
1627 int labelR_y = Math.min(iconR.y, textR.y);
1628 int labelR_height = Math.max(iconR.y + iconR.height,
1629 textR.y + textR.height) - labelR_y;
1630
1631 int dx, dy;
1632
1633 if (verticalAlignment == TOP) {
1634 dy = viewR.y - labelR_y;
1635 }
1636 else if (verticalAlignment == CENTER) {
1637 dy = (viewR.y + (viewR.height / 2)) - (labelR_y + (labelR_height / 2));
1638 }
1639 else { // (verticalAlignment == BOTTOM)
1640 dy = (viewR.y + viewR.height) - (labelR_y + labelR_height);
1641 }
1642
1643 if (horizontalAlignment == LEFT) {
1644 dx = viewR.x - labelR_x;
1645 }
1646 else if (horizontalAlignment == RIGHT) {
1647 dx = (viewR.x + viewR.width) - (labelR_x + labelR_width);
1648 }
1649 else { // (horizontalAlignment == CENTER)
1650 dx = (viewR.x + (viewR.width / 2)) -
1651 (labelR_x + (labelR_width / 2));
1652 }
1653
1654 /* Translate textR and glypyR by dx,dy.
1655 */
1656
1657 textR.x += dx;
1658 textR.y += dy;
1659
1660 iconR.x += dx;
1661 iconR.y += dy;
1662
1663 if (lsb < 0) {
1664 // lsb is negative. Shift the x location so that the text is
1665 // visually drawn at the right location.
1666 textR.x -= lsb;
1667 }
1668 int maxIconY = viewR.height / 2;
1669 Insets insets = c.getInsets();
1670 int leftMostX = viewR.x;
1671 int rightMostX = viewR.width;
1672 rightMostX -= iconR.width;
1673 if (horizontalTextPosition == SwingConstants.CENTER) {
1674 if (viewR.width < textR.width) {
1675 iconR.x = (leftMostX + rightMostX) / 2;
1676 }
1677 else {
1678 int leftMostTextX = textR.x;
1679 int rightMostTextX = textR.x + textR.width - iconR.width;
1680 iconR.x = textR.x + (textR.width - iconR.width) / 2;
1681 }
1682 }
1683 else if (iconR.x < leftMostX) {
1684 textR.x += leftMostX - iconR.x;
1685 iconR.x = leftMostX;
1686 }
1687 else if (iconR.x > rightMostX && horizontalAlignment != LEFT) {
1688 iconR.x = rightMostX;
1689 textR.x -= iconR.x - rightMostX;
1690 }
1691 if (insets != null) {
1692 maxIconY -= (insets.bottom + insets.top) / 2;
1693 }
1694 if (icon != null) {
1695 maxIconY -= icon.getIconHeight() / 2;
1696 }
1697 if (verticalAlignment == TOP) {
1698 iconR.y = Math.min(maxIconY, iconR.y);
1699 }
1700 else if (verticalAlignment == BOTTOM) {
1701 iconR.y = Math.max(maxIconY, iconR.y);
1702 }
1703
1704 return text;
1705 }
1706 }