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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 IRCLine implements 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 IRCLine(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. private 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. @Override
  69. public int getLength() {
  70. return timestamp.length() + text.length();
  71. }
  72. @Override
  73. public int getFontSize() {
  74. return fontSize;
  75. }
  76. @Override
  77. public void setFontSize(final int fontSize) {
  78. this.fontSize = fontSize;
  79. }
  80. @Override
  81. public void setFontName(final String fontName) {
  82. this.fontName = fontName;
  83. }
  84. @Override
  85. public String getText() {
  86. return Styliser.stipControlCodes(timestamp + text);
  87. }
  88. @Override
  89. public String getStyledText() {
  90. return timestamp + text;
  91. }
  92. @Override
  93. public <T> T getStyled(final StyledMessageMaker<T> maker) {
  94. maker.setDefaultFont(fontName, fontSize);
  95. styliser.addStyledString(maker, getLineParts());;
  96. final T styledString = maker.getStyledMessage();
  97. fontSize = maker.getMaximumFontSize();
  98. maker.clear();
  99. return styledString;
  100. }
  101. @Override
  102. public boolean equals(final Object obj) {
  103. return obj instanceof Line && Arrays.equals(((IRCLine) obj).getLineParts(), getLineParts());
  104. }
  105. @Override
  106. public int hashCode() {
  107. return Arrays.hashCode(getLineParts());
  108. }
  109. @Override
  110. public <T> Optional<T> getDisplayableProperty(final DisplayProperty<T> property) {
  111. return displayProperties.get(property);
  112. }
  113. }