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.

IRCLine.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. package com.dmdirc.ui.messages;
  18. import com.dmdirc.events.DisplayProperty;
  19. import com.dmdirc.events.DisplayPropertyMap;
  20. import java.util.Arrays;
  21. import java.util.Optional;
  22. /**
  23. * Represents a line of text in IRC.
  24. */
  25. public class IRCLine implements Line {
  26. private final String timestamp;
  27. private final String text;
  28. private final Styliser styliser;
  29. private final StyledMessageUtils styleUtils = new StyledMessageUtils(); // TODO: Inject
  30. private final DisplayPropertyMap displayProperties;
  31. private int fontSize;
  32. private String fontName;
  33. /**
  34. * Creates a new line with a specified height.
  35. *
  36. * @param styliser The styliser to use to style this line
  37. * @param timestamp The textual timestamp to use for the line
  38. * @param text The textual content of the line
  39. * @param displayProperties The properties to use when displaying the line.
  40. * @param fontSize The height for this line
  41. * @param fontName The name of the font to use for this line
  42. */
  43. public IRCLine(final Styliser styliser, final String timestamp, final String text,
  44. final DisplayPropertyMap displayProperties, final int fontSize, final String fontName) {
  45. this.styliser = styliser;
  46. this.timestamp = timestamp; // TODO: Make this a long and convert further down the line
  47. this.text = text;
  48. this.displayProperties = displayProperties;
  49. this.fontName = fontName;
  50. this.fontSize = fontSize;
  51. }
  52. /**
  53. * Returns the line parts of this line.
  54. *
  55. * @return Lines parts
  56. */
  57. private String[] getLineParts() {
  58. if (displayProperties.get(DisplayProperty.NO_TIMESTAMPS).orElse(false)) {
  59. return new String[] { text };
  60. } else {
  61. return new String[] { timestamp, text };
  62. }
  63. }
  64. @Override
  65. public int getLength() {
  66. return timestamp.length() + text.length();
  67. }
  68. @Override
  69. public int getFontSize() {
  70. return fontSize;
  71. }
  72. @Override
  73. public void setFontSize(final int fontSize) {
  74. this.fontSize = fontSize;
  75. }
  76. @Override
  77. public void setFontName(final String fontName) {
  78. this.fontName = fontName;
  79. }
  80. @Override
  81. public String getText() {
  82. return styleUtils.stripControlCodes(timestamp + text);
  83. }
  84. @Override
  85. public String getStyledText() {
  86. return timestamp + text;
  87. }
  88. @Override
  89. public <T> T getStyled(final StyledMessageMaker<T> maker) {
  90. maker.setDefaultFont(fontName, fontSize);
  91. styliser.addStyledString(maker, getLineParts());
  92. final T styledString = maker.getStyledMessage();
  93. fontSize = maker.getMaximumFontSize();
  94. maker.clear();
  95. return styledString;
  96. }
  97. @Override
  98. public boolean equals(final Object obj) {
  99. return obj instanceof Line && Arrays.equals(((IRCLine) obj).getLineParts(), getLineParts());
  100. }
  101. @Override
  102. public int hashCode() {
  103. return Arrays.hashCode(getLineParts());
  104. }
  105. @Override
  106. public <T> Optional<T> getDisplayableProperty(final DisplayProperty<T> property) {
  107. return displayProperties.get(property);
  108. }
  109. }