You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WrapLabelView.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * @author Stanislav Lapitsky
  3. * @version 1.0
  4. */
  5. package com.dmdirc.addons.ui_swing.components.text;
  6. import javax.swing.text.BadLocationException;
  7. import javax.swing.text.Element;
  8. import javax.swing.text.LabelView;
  9. import javax.swing.text.View;
  10. /**
  11. * @author Stanislav Lapitsky
  12. * @version 1.0
  13. */
  14. public class WrapLabelView extends LabelView {
  15. /**
  16. * Creates a new wrap label view.
  17. *
  18. * @param elem Element to view
  19. */
  20. public WrapLabelView(final Element elem) {
  21. super(elem);
  22. }
  23. @Override
  24. public int getBreakWeight(final int axis, final float pos, final float len) {
  25. if (axis == View.X_AXIS) {
  26. checkPainter();
  27. final int p0 = getStartOffset();
  28. final int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos,
  29. len);
  30. if (p1 == p0) {
  31. // can't even fit a single character
  32. return View.BadBreakWeight;
  33. }
  34. try {
  35. //if the view contains line break char return forced break
  36. if (getDocument().getText(p0, p1 - p0).contains("\r")) {
  37. return View.ForcedBreakWeight;
  38. }
  39. } catch (BadLocationException ex) {
  40. //should never happen
  41. }
  42. }
  43. return super.getBreakWeight(axis, pos, len);
  44. }
  45. @Override
  46. public View breakView(final int axis, final int p0, final float pos,
  47. final float len) {
  48. if (axis == View.X_AXIS) {
  49. checkPainter();
  50. final int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos,
  51. len);
  52. try {
  53. //if the view contains line break char break the view
  54. final int index = getDocument().getText(p0, p1 - p0)
  55. .indexOf("\r");
  56. if (index >= 0) {
  57. return createFragment(p0, p0 + index + 1);
  58. }
  59. } catch (BadLocationException ex) {
  60. //should never happen
  61. }
  62. }
  63. return super.breakView(axis, p0, pos, len);
  64. }
  65. }