Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCLine.java 4.2KB

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