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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.ui.messages;
  23. import com.dmdirc.events.DisplayProperty;
  24. import com.dmdirc.events.DisplayPropertyMap;
  25. import java.util.Arrays;
  26. import java.util.Optional;
  27. /**
  28. * Represents a line of text in IRC.
  29. */
  30. public class Line {
  31. private final String timestamp;
  32. private final String text;
  33. private final Styliser styliser;
  34. private final DisplayPropertyMap displayProperties;
  35. private int fontSize;
  36. private String fontName;
  37. /**
  38. * Creates a new line with a specified height.
  39. *
  40. * @param styliser The styliser to use to style this line
  41. * @param timestamp The textual timestamp to use for the line
  42. * @param text The textual content of the line
  43. * @param displayProperties The properties to use when displaying the line.
  44. * @param fontSize The height for this line
  45. * @param fontName The name of the font to use for this line
  46. */
  47. public Line(final Styliser styliser, final String timestamp, final String text,
  48. final DisplayPropertyMap displayProperties, final int fontSize, final String fontName) {
  49. this.styliser = styliser;
  50. this.timestamp = timestamp; // TODO: Make this a long and convert further down the line
  51. this.text = text;
  52. this.displayProperties = displayProperties;
  53. this.fontName = fontName;
  54. this.fontSize = fontSize;
  55. }
  56. /**
  57. * Returns the line parts of this line.
  58. *
  59. * @return Lines parts
  60. */
  61. public String[] getLineParts() {
  62. if (displayProperties.get(DisplayProperty.NO_TIMESTAMPS).orElse(false)) {
  63. return new String[] { text };
  64. } else {
  65. return new String[] { timestamp, text };
  66. }
  67. }
  68. /**
  69. * Returns the length of the specified line.
  70. *
  71. * @return Length of the line
  72. */
  73. public int getLength() {
  74. return timestamp.length() + text.length();
  75. }
  76. /**
  77. * Returns the height of the specified line.
  78. *
  79. * @return Line height
  80. */
  81. public int getFontSize() {
  82. return fontSize;
  83. }
  84. /**
  85. * Sets the default font size for this line.
  86. *
  87. * @param fontSize New default font size
  88. */
  89. public void setFontSize(final int fontSize) {
  90. this.fontSize = fontSize;
  91. }
  92. /**
  93. * Sets the default font name for this line.
  94. *
  95. * @param fontName New default font name
  96. */
  97. public void setFontName(final String fontName) {
  98. this.fontName = fontName;
  99. }
  100. /**
  101. * Returns the Line text at the specified number.
  102. *
  103. * @return Line at the specified number or null
  104. */
  105. public String getText() {
  106. return Styliser.stipControlCodes(timestamp + text);
  107. }
  108. /**
  109. * Returns the Line text at the specified number.
  110. *
  111. * @return Line at the specified number or null
  112. *
  113. * @since 0.6.3m1
  114. */
  115. public String getStyledText() {
  116. return timestamp + text;
  117. }
  118. /**
  119. * Converts a StyledDocument into an AttributedString.
  120. *
  121. * @return AttributedString representing the specified StyledDocument
  122. */
  123. public <T> T getStyled(final StyledMessageMaker<T> maker) {
  124. maker.setDefaultFont(fontName, fontSize);
  125. final T styledString = styliser.getStyledString(getLineParts(), maker);
  126. fontSize = maker.getMaximumFontSize();
  127. maker.clear();
  128. return styledString;
  129. }
  130. @Override
  131. public boolean equals(final Object obj) {
  132. return obj instanceof Line && Arrays.equals(((Line) obj).getLineParts(), getLineParts());
  133. }
  134. @Override
  135. public int hashCode() {
  136. return Arrays.hashCode(getLineParts());
  137. }
  138. public <T> Optional<T> getDisplayableProperty(final DisplayProperty<T> property) {
  139. return displayProperties.get(property);
  140. }
  141. }