Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.interfaces.ConfigChangeListener;
  25. import com.dmdirc.ui.core.util.ExtendedAttributedString;
  26. import com.dmdirc.ui.core.util.Utils;
  27. import com.dmdirc.ui.messages.Styliser;
  28. import java.awt.Font;
  29. import java.text.AttributedString;
  30. import java.util.Arrays;
  31. import javax.swing.UIManager;
  32. /**
  33. * Represents a line of text in IRC.
  34. */
  35. class Line implements ConfigChangeListener {
  36. private final String[] lineParts;
  37. private final ConfigManager config;
  38. private int lineHeight;
  39. private String fontName;
  40. /**
  41. * Creates a new line.
  42. *
  43. * @param lineParts Parts of the line
  44. * @param config Configuration manager for this line
  45. */
  46. public Line(final String[] lineParts, final ConfigManager config) {
  47. this.lineParts = lineParts;
  48. this.config = config;
  49. setCachedSettings();
  50. config.addChangeListener("ui", "textPaneFontSize", this);
  51. config.addChangeListener("ui", "textPaneFontName", this);
  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. setCachedSettings();
  65. config.addChangeListener("ui", "textPaneFontSize", this);
  66. config.addChangeListener("ui", "textPaneFontName", this);
  67. }
  68. /**
  69. * Returns the line parts of this line.
  70. *
  71. * @return Lines parts
  72. */
  73. public String[] getLineParts() {
  74. return lineParts;
  75. }
  76. /**
  77. * Returns the length of the specified line
  78. *
  79. * @return Length of the line
  80. */
  81. public int getLength() {
  82. int length = 0;
  83. for (String linePart : lineParts) {
  84. length += linePart.length();
  85. }
  86. return length;
  87. }
  88. /**
  89. * Returns the height of the specified line.
  90. *
  91. * @return Line height
  92. */
  93. public int getHeight() {
  94. return lineHeight;
  95. }
  96. /**
  97. * Returns the Line text at the specified number.
  98. *
  99. * @return Line at the specified number or null
  100. */
  101. public String getText() {
  102. StringBuilder lineText = new StringBuilder();
  103. for (String linePart : lineParts) {
  104. lineText.append(linePart);
  105. }
  106. return Styliser.stipControlCodes(lineText.toString());
  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. StringBuilder lineText = new StringBuilder();
  117. for (String linePart : lineParts) {
  118. lineText.append(linePart);
  119. }
  120. return lineText.toString();
  121. }
  122. /**
  123. * Converts a StyledDocument into an AttributedString.
  124. *
  125. * @return AttributedString representing the specified StyledDocument
  126. */
  127. public AttributedString getStyled() {
  128. final ExtendedAttributedString string = Utils.getAttributedString(lineParts,
  129. fontName, lineHeight);
  130. lineHeight = string.getMaxLineHeight();
  131. return string.getAttributedString();
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public boolean equals(Object obj) {
  136. if (obj instanceof Line) {
  137. return Arrays.equals(((Line) obj).getLineParts(), getLineParts());
  138. }
  139. return false;
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public int hashCode() {
  144. return getLineParts().hashCode();
  145. }
  146. /** {@inheritDoc} */
  147. @Override
  148. public void configChanged(final String domain, final String key) {
  149. setCachedSettings();
  150. }
  151. private void setCachedSettings() {
  152. final Font defaultFont = UIManager.getFont("TextPane.font");
  153. if (config.hasOptionString("ui", "textPaneFontName")) {
  154. fontName = config.getOption("ui", "textPaneFontName");
  155. } else {
  156. fontName = defaultFont.getName();
  157. }
  158. if (config.hasOptionString("ui", "textPaneFontSize")) {
  159. lineHeight = config.getOptionInt("ui", "textPaneFontSize");
  160. } else {
  161. lineHeight = defaultFont.getSize();
  162. }
  163. }
  164. }