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.

AttributedStringMessageMaker.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.addons.ui_swing.textpane;
  18. import com.dmdirc.ui.core.util.ExtendedAttributedString;
  19. import com.dmdirc.ui.messages.DelegatingStyledMessageMaker;
  20. import com.dmdirc.ui.messages.IRCTextAttribute;
  21. import java.awt.Font;
  22. import java.awt.font.TextAttribute;
  23. import java.text.AttributedString;
  24. import java.util.Enumeration;
  25. import javax.swing.text.AttributeSet;
  26. import javax.swing.text.BadLocationException;
  27. import javax.swing.text.Element;
  28. import javax.swing.text.StyleConstants;
  29. import javax.swing.text.StyledDocument;
  30. /**
  31. * Creates an attributed string from a styled document.
  32. */
  33. public class AttributedStringMessageMaker
  34. extends DelegatingStyledMessageMaker<StyledDocument, AttributedString> {
  35. private String fontName;
  36. private int fontSize;
  37. public AttributedStringMessageMaker() {
  38. super(new StyledDocumentMaker());
  39. }
  40. @Override
  41. public int getMaximumFontSize() {
  42. return fontSize;
  43. }
  44. @Override
  45. public void setDefaultFont(final String fontName, final int fontSize) {
  46. this.fontName = fontName;
  47. this.fontSize = fontSize;
  48. }
  49. @Override
  50. protected AttributedString convert(final StyledDocument styledMessage) {
  51. final Element line = styledMessage.getParagraphElement(0);
  52. final AttributedString attString;
  53. try {
  54. attString = new AttributedString(
  55. line.getDocument().getText(0,
  56. line.getDocument().getLength()));
  57. } catch (BadLocationException ex) {
  58. // Shouldn't happen
  59. return null;
  60. }
  61. if (attString.getIterator().getEndIndex() != 0) {
  62. final Font font = new Font(fontName, Font.PLAIN, fontSize);
  63. attString.addAttribute(TextAttribute.SIZE, font.getSize());
  64. attString.addAttribute(TextAttribute.FAMILY, font.getFamily());
  65. }
  66. for (int i = 0; i < line.getElementCount(); i++) {
  67. final Element element = line.getElement(i);
  68. final AttributeSet as = element.getAttributes();
  69. final Enumeration<?> ae = as.getAttributeNames();
  70. while (ae.hasMoreElements()) {
  71. final Object attrib = ae.nextElement();
  72. if (attrib == IRCTextAttribute.HYPERLINK) {
  73. //Hyperlink
  74. attString.addAttribute(IRCTextAttribute.HYPERLINK,
  75. as.getAttribute(attrib), element.getStartOffset(),
  76. element.getEndOffset());
  77. } else if (attrib == IRCTextAttribute.NICKNAME) {
  78. //Nicknames
  79. attString.addAttribute(IRCTextAttribute.NICKNAME,
  80. as.getAttribute(attrib), element.getStartOffset(),
  81. element.getEndOffset());
  82. } else if (attrib == IRCTextAttribute.CHANNEL) {
  83. //Channels
  84. attString.addAttribute(IRCTextAttribute.CHANNEL,
  85. as.getAttribute(attrib), element.getStartOffset(),
  86. element.getEndOffset());
  87. } else if (attrib == IRCTextAttribute.TOOLTIP) {
  88. //Tooltips
  89. attString.addAttribute(IRCTextAttribute.TOOLTIP,
  90. as.getAttribute(attrib), element.getStartOffset(),
  91. element.getEndOffset());
  92. } else if (attrib == StyleConstants.Foreground) {
  93. //Foreground
  94. attString.addAttribute(TextAttribute.FOREGROUND,
  95. as.getAttribute(attrib), element.getStartOffset(),
  96. element.getEndOffset());
  97. } else if (attrib == StyleConstants.Background) {
  98. //Background
  99. attString.addAttribute(TextAttribute.BACKGROUND,
  100. as.getAttribute(attrib), element.getStartOffset(),
  101. element.getEndOffset());
  102. } else if (attrib == StyleConstants.Bold) {
  103. //Bold
  104. attString.addAttribute(TextAttribute.WEIGHT,
  105. TextAttribute.WEIGHT_BOLD, element.getStartOffset(),
  106. element.getEndOffset());
  107. } else if (attrib == StyleConstants.Family) {
  108. //Family
  109. attString.addAttribute(TextAttribute.FAMILY,
  110. as.getAttribute(attrib), element.getStartOffset(),
  111. element.getEndOffset());
  112. } else if (attrib == StyleConstants.Italic) {
  113. //italics
  114. attString.addAttribute(TextAttribute.POSTURE,
  115. TextAttribute.POSTURE_OBLIQUE,
  116. element.getStartOffset(),
  117. element.getEndOffset());
  118. } else if (attrib == StyleConstants.Underline) {
  119. //Underline
  120. attString.addAttribute(TextAttribute.UNDERLINE,
  121. TextAttribute.UNDERLINE_ON, element.getStartOffset(),
  122. element.getEndOffset());
  123. }
  124. }
  125. }
  126. final ExtendedAttributedString attributedString =
  127. attString.getIterator().getEndIndex() == 0
  128. ? new ExtendedAttributedString(new AttributedString("\n"), fontSize)
  129. : new ExtendedAttributedString(attString, fontSize);
  130. fontSize = attributedString.getMaxLineHeight();
  131. return attributedString.getAttributedString();
  132. }
  133. }