選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Utils.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2006-2014 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.core.util;
  23. import com.dmdirc.logger.ErrorLevel;
  24. import com.dmdirc.logger.Logger;
  25. import com.dmdirc.ui.messages.IRCTextAttribute;
  26. import com.dmdirc.ui.messages.Styliser;
  27. import java.awt.Font;
  28. import java.awt.font.TextAttribute;
  29. import java.text.AttributedString;
  30. import java.util.Enumeration;
  31. import javax.swing.text.AttributeSet;
  32. import javax.swing.text.BadLocationException;
  33. import javax.swing.text.Element;
  34. import javax.swing.text.StyleConstants.CharacterConstants;
  35. import javax.swing.text.StyleConstants.ColorConstants;
  36. import javax.swing.text.StyleConstants.FontConstants;
  37. import javax.swing.text.StyledDocument;
  38. /**
  39. * Core UI Utilities.
  40. */
  41. public final class Utils {
  42. /** Prevent instantiation. */
  43. private Utils() {
  44. }
  45. /**
  46. * Converts a StyledDocument into an AttributedString.
  47. *
  48. * @param styliser The styliser to use to style the string
  49. * @param lineParts Parts of a line comprising the whole
  50. * @param fontName Default font name to use
  51. * @param fontSize Default font size to use
  52. *
  53. * @return AttributedString representing the specified StyledDocument
  54. */
  55. public static ExtendedAttributedString getAttributedString(final Styliser styliser,
  56. final String[] lineParts, final String fontName, final int fontSize) {
  57. final StyledDocument doc = styliser.getStyledString(lineParts);
  58. AttributedString attString = null;
  59. final Element line = doc.getParagraphElement(0);
  60. try {
  61. attString = new AttributedString(line.getDocument().getText(0,
  62. line.getDocument().getLength()));
  63. } catch (BadLocationException ex) {
  64. Logger.userError(ErrorLevel.MEDIUM,
  65. "Unable to insert styled string: " + ex.getMessage());
  66. }
  67. if (attString.getIterator().getEndIndex() != 0) {
  68. final Font font = new Font(fontName, Font.PLAIN, fontSize);
  69. attString.addAttribute(TextAttribute.SIZE, font.getSize());
  70. attString.addAttribute(TextAttribute.FAMILY, font.getFamily());
  71. }
  72. for (int i = 0; i < line.getElementCount(); i++) {
  73. final Element element = line.getElement(i);
  74. final AttributeSet as = element.getAttributes();
  75. final Enumeration<?> ae = as.getAttributeNames();
  76. while (ae.hasMoreElements()) {
  77. final Object attrib = ae.nextElement();
  78. if (attrib == IRCTextAttribute.HYPERLINK) {
  79. //Hyperlink
  80. attString.addAttribute(IRCTextAttribute.HYPERLINK,
  81. as.getAttribute(attrib), element.getStartOffset(),
  82. element.getEndOffset());
  83. } else if (attrib == IRCTextAttribute.NICKNAME) {
  84. //Nicknames
  85. attString.addAttribute(IRCTextAttribute.NICKNAME,
  86. as.getAttribute(attrib), element.getStartOffset(),
  87. element.getEndOffset());
  88. } else if (attrib == IRCTextAttribute.CHANNEL) {
  89. //Channels
  90. attString.addAttribute(IRCTextAttribute.CHANNEL,
  91. as.getAttribute(attrib), element.getStartOffset(),
  92. element.getEndOffset());
  93. } else if (attrib == IRCTextAttribute.TOOLTIP) {
  94. //Tooltips
  95. attString.addAttribute(IRCTextAttribute.TOOLTIP,
  96. as.getAttribute(attrib), element.getStartOffset(),
  97. element.getEndOffset());
  98. } else if (attrib == ColorConstants.Foreground) {
  99. //Foreground
  100. attString.addAttribute(TextAttribute.FOREGROUND,
  101. as.getAttribute(attrib), element.getStartOffset(),
  102. element.getEndOffset());
  103. } else if (attrib == ColorConstants.Background) {
  104. //Background
  105. attString.addAttribute(TextAttribute.BACKGROUND,
  106. as.getAttribute(attrib), element.getStartOffset(),
  107. element.getEndOffset());
  108. } else if (attrib == FontConstants.Bold) {
  109. //Bold
  110. attString.addAttribute(TextAttribute.WEIGHT,
  111. TextAttribute.WEIGHT_BOLD, element.getStartOffset(),
  112. element.getEndOffset());
  113. } else if (attrib == FontConstants.Family) {
  114. //Family
  115. attString.addAttribute(TextAttribute.FAMILY,
  116. as.getAttribute(attrib), element.getStartOffset(),
  117. element.getEndOffset());
  118. } else if (attrib == FontConstants.Italic) {
  119. //italics
  120. attString.addAttribute(TextAttribute.POSTURE,
  121. TextAttribute.POSTURE_OBLIQUE,
  122. element.getStartOffset(),
  123. element.getEndOffset());
  124. } else if (attrib == CharacterConstants.Underline) {
  125. //Underline
  126. attString.addAttribute(TextAttribute.UNDERLINE,
  127. TextAttribute.UNDERLINE_ON, element.getStartOffset(),
  128. element.getEndOffset());
  129. } /* else if (attrib == IRCTextAttribute.SMILEY) { Lets avoid showing broken smileys
  130. * shall we! final Image image = IconManager.getIconManager().getImage((String)
  131. * as.getAttribute(attrib)). getScaledInstance(14, 14, Image.SCALE_DEFAULT);
  132. * ImageGraphicAttribute iga = new ImageGraphicAttribute(image, (int)
  133. * BOTTOM_ALIGNMENT, 5, 5); attString.addAttribute(TextAttribute.CHAR_REPLACEMENT,
  134. * iga, element.getStartOffset(), element.getEndOffset());
  135. *
  136. * } */
  137. }
  138. }
  139. if (attString.getIterator().getEndIndex() == 0) {
  140. return new ExtendedAttributedString(new AttributedString("\n"), fontSize);
  141. }
  142. return new ExtendedAttributedString(attString, fontSize);
  143. }
  144. }