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.

Line.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.textpane;
  23. import com.dmdirc.config.ConfigManager;
  24. import com.dmdirc.ui.core.util.ExtendedAttributedString;
  25. import com.dmdirc.ui.core.util.Utils;
  26. import com.dmdirc.ui.messages.Styliser;
  27. import java.text.AttributedString;
  28. import java.util.Arrays;
  29. import javax.swing.UIManager;
  30. /**
  31. * Represents a line of text in IRC.
  32. */
  33. class Line {
  34. private final String[] lineParts;
  35. private final ConfigManager config;
  36. private int lineHeight;
  37. /**
  38. * Creates a new line.
  39. *
  40. * @param lineParts Parts of the line
  41. * @param config Configuration manager for this line
  42. */
  43. public Line(final String[] lineParts, final ConfigManager config) {
  44. this.lineParts = lineParts;
  45. this.config = config;
  46. this.lineHeight = -1;
  47. if (config.hasOptionString("ui", "textPaneFontSize")) {
  48. this.lineHeight = config.getOptionInt("ui", "textPaneFontSize");
  49. } else {
  50. this.lineHeight = UIManager.getFont("TextPane.font").getSize();
  51. }
  52. }
  53. /**
  54. * Creates a new line with a specified height.
  55. *
  56. * @param lineParts Parts of the line
  57. * @param config Configuration manager for this line
  58. * @param lineHeight The height for this line
  59. */
  60. public Line(final String[] lineParts, final ConfigManager config,
  61. final int lineHeight) {
  62. this.lineParts = lineParts;
  63. this.config = config;
  64. this.lineHeight = lineHeight;
  65. }
  66. /**
  67. * Returns the line parts of this line.
  68. *
  69. * @return Lines parts
  70. */
  71. public String[] getLineParts() {
  72. return lineParts;
  73. }
  74. /**
  75. * Returns the length of the specified line
  76. *
  77. * @return Length of the line
  78. */
  79. public int getLength() {
  80. int length = 0;
  81. for (String linePart : lineParts) {
  82. length += linePart.length();
  83. }
  84. return length;
  85. }
  86. /**
  87. * Returns the height of the specified line.
  88. *
  89. * @return Line height
  90. */
  91. public int getHeight() {
  92. return lineHeight;
  93. }
  94. /**
  95. * Returns the Line text at the specified number.
  96. *
  97. * @return Line at the specified number or null
  98. */
  99. public String getText() {
  100. StringBuilder lineText = new StringBuilder();
  101. for (String linePart : lineParts) {
  102. lineText.append(linePart);
  103. }
  104. return Styliser.stipControlCodes(lineText.toString());
  105. }
  106. /**
  107. * Returns the Line text at the specified number.
  108. *
  109. * @return Line at the specified number or null
  110. *
  111. * @since 0.6.3m1
  112. */
  113. public String getStyledText() {
  114. StringBuilder lineText = new StringBuilder();
  115. for (String linePart : lineParts) {
  116. lineText.append(linePart);
  117. }
  118. return lineText.toString();
  119. }
  120. /**
  121. * Converts a StyledDocument into an AttributedString.
  122. *
  123. * @return AttributedString representing the specified StyledDocument
  124. */
  125. public AttributedString getStyled() {
  126. final ExtendedAttributedString string = Utils.getAttributedString(lineParts,
  127. config);
  128. lineHeight = string.getMaxLineHeight();
  129. return string.getAttributedString();
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. public boolean equals(Object obj) {
  134. if (obj instanceof Line) {
  135. return Arrays.equals(((Line) obj).getLineParts(), getLineParts());
  136. }
  137. return false;
  138. }
  139. /** {@inheritDoc} */
  140. @Override
  141. public int hashCode() {
  142. return getLineParts().hashCode();
  143. }
  144. }