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 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. /*
  18. * @author Stanislav Lapitsky
  19. * @version 1.0
  20. */
  21. package com.dmdirc.addons.ui_swing.components.text;
  22. import javax.swing.text.BadLocationException;
  23. import javax.swing.text.Element;
  24. import javax.swing.text.LabelView;
  25. import javax.swing.text.View;
  26. /**
  27. * @author Stanislav Lapitsky
  28. * @version 1.0
  29. */
  30. public class WrapLabelView extends LabelView {
  31. /**
  32. * Creates a new wrap label view.
  33. *
  34. * @param elem Element to view
  35. */
  36. public WrapLabelView(final Element elem) {
  37. super(elem);
  38. }
  39. @Override
  40. public int getBreakWeight(final int axis, final float pos, final float len) {
  41. if (axis == View.X_AXIS) {
  42. checkPainter();
  43. final int p0 = getStartOffset();
  44. final int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos,
  45. len);
  46. if (p1 == p0) {
  47. // can't even fit a single character
  48. return View.BadBreakWeight;
  49. }
  50. try {
  51. //if the view contains line break char return forced break
  52. if (getDocument().getText(p0, p1 - p0).contains("\r")) {
  53. return View.ForcedBreakWeight;
  54. }
  55. } catch (BadLocationException ex) {
  56. //should never happen
  57. }
  58. }
  59. return super.getBreakWeight(axis, pos, len);
  60. }
  61. @Override
  62. public View breakView(final int axis, final int p0, final float pos,
  63. final float len) {
  64. if (axis == View.X_AXIS) {
  65. checkPainter();
  66. final int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos,
  67. len);
  68. try {
  69. //if the view contains line break char break the view
  70. final int index = getDocument().getText(p0, p1 - p0).indexOf('\r');
  71. if (index >= 0) {
  72. return createFragment(p0, p0 + index + 1);
  73. }
  74. } catch (BadLocationException ex) {
  75. //should never happen
  76. }
  77. }
  78. return super.breakView(axis, p0, pos, len);
  79. }
  80. }