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ů.

TextLabel.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.components.text;
  18. import com.dmdirc.util.URLBuilder;
  19. import java.awt.Color;
  20. import java.awt.Font;
  21. import java.awt.Insets;
  22. import javax.annotation.Nullable;
  23. import javax.swing.JTextPane;
  24. import javax.swing.UIManager;
  25. import javax.swing.plaf.basic.BasicTextPaneUI;
  26. import javax.swing.text.DefaultStyledDocument;
  27. import javax.swing.text.SimpleAttributeSet;
  28. import javax.swing.text.StyleConstants;
  29. import javax.swing.text.StyledDocument;
  30. import javax.swing.text.html.HTMLDocument;
  31. import javax.swing.text.html.StyleSheet;
  32. /**
  33. * Dynamic text label.
  34. */
  35. public class TextLabel extends JTextPane {
  36. /** A version number for this class. */
  37. private static final long serialVersionUID = 1;
  38. /** Simple attribute set. */
  39. private final SimpleAttributeSet sas = new SimpleAttributeSet();
  40. /**
  41. * Creates a new instance of TextLabel.
  42. *
  43. * <p>
  44. * Labels constructed without a {@link URLBuilder} will not be able to resolve DMDirc-specific
  45. * URLs in image tags.
  46. */
  47. public TextLabel() {
  48. this(null, true);
  49. }
  50. /**
  51. * Creates a new instance of TextLabel.
  52. *
  53. * <p>
  54. * Labels constructed without a {@link URLBuilder} will not be able to resolve DMDirc-specific
  55. * URLs in image tags.
  56. *
  57. * @param justified Justify the text?
  58. */
  59. public TextLabel(final boolean justified) {
  60. this(null, justified);
  61. }
  62. /**
  63. * Creates a new instance of TextLabel.
  64. *
  65. * <p>
  66. * Labels constructed without a {@link URLBuilder} will not be able to resolve DMDirc-specific
  67. * URLs in image tags.
  68. *
  69. * @param text Text to display
  70. */
  71. public TextLabel(final String text) {
  72. this(text, true);
  73. }
  74. /**
  75. * Creates a new instance of TextLabel.
  76. *
  77. * <p>
  78. * Labels constructed without a {@link URLBuilder} will not be able to resolve DMDirc-specific
  79. * URLs in image tags.
  80. *
  81. * @param text Text to display
  82. * @param justified Justify the text?
  83. */
  84. public TextLabel(final String text, final boolean justified) {
  85. this(null, text, justified);
  86. }
  87. /**
  88. * Creates a new instance of TextLabel.
  89. *
  90. * @param urlBuilder The URL builder to use for embedded image URLs. If {@code null}, then only
  91. * standard URLs will be handled in image tags (not DMDirc-specific ones).
  92. * @param text Text to display
  93. * @param justified Justify the text?
  94. */
  95. public TextLabel(
  96. @Nullable final URLBuilder urlBuilder,
  97. final String text,
  98. final boolean justified) {
  99. super(new DefaultStyledDocument());
  100. setEditorKit(new DMDircHTMLEditorKit(urlBuilder));
  101. setUI(new BasicTextPaneUI());
  102. final StyleSheet styleSheet = ((HTMLDocument) getDocument()).
  103. getStyleSheet();
  104. final Font font = UIManager.getFont("Label.font");
  105. final Color colour = UIManager.getColor("Label.foreground");
  106. styleSheet.addRule("body "
  107. + "{ font-family: " + font.getFamily() + "; "
  108. + "font-size: " + font.getSize() + "pt; "
  109. + "color: rgb(" + colour.getRed() + ", " + colour.getGreen()
  110. + ", " + colour.getBlue() + "); }");
  111. setOpaque(false);
  112. setEditable(false);
  113. setHighlighter(null);
  114. setMargin(new Insets(0, 0, 0, 0));
  115. if (justified) {
  116. StyleConstants.setAlignment(sas, StyleConstants.ALIGN_JUSTIFIED);
  117. }
  118. setText(text);
  119. }
  120. @Override
  121. public final StyledDocument getDocument() {
  122. return (StyledDocument) super.getDocument();
  123. }
  124. @Override
  125. public final void setText(final String t) {
  126. super.setText(t);
  127. if (t != null && !t.isEmpty()) {
  128. getDocument().setParagraphAttributes(0, t.length(), sas, false);
  129. }
  130. }
  131. @Override
  132. public void setForeground(final Color colour) {
  133. if (sas == null) {
  134. return;
  135. }
  136. if (colour != null) {
  137. StyleConstants.setForeground(sas, colour);
  138. getDocument().setParagraphAttributes(0, getDocument().getLength(),
  139. sas, false);
  140. }
  141. }
  142. @Override
  143. public void setFont(final Font font) {
  144. super.setFont(font);
  145. if (sas == null) {
  146. return;
  147. }
  148. if (font != null) {
  149. StyleConstants.setFontFamily(sas, font.getFamily());
  150. StyleConstants.setFontSize(sas, font.getSize());
  151. StyleConstants.setBold(sas, font.isBold());
  152. StyleConstants.setItalic(sas, font.isItalic());
  153. getDocument().setParagraphAttributes(0, getDocument().getLength(),
  154. sas, false);
  155. }
  156. }
  157. /**
  158. * Sets the alignment of the text in this label.
  159. *
  160. * @param alignment One of the following values
  161. * <ul>
  162. * <li>StyleConstants.ALIGN_CENTER
  163. * <li>StyleConstants.ALIGN_JUSTIFIED
  164. * <li>StyleConstants.ALIGN_LEFT
  165. * <li>StyleConstants.ALIGN_RIGHT
  166. * </ul>
  167. */
  168. public void setAlignment(final int alignment) {
  169. StyleConstants.setAlignment(sas, alignment);
  170. getDocument().setParagraphAttributes(0, getDocument().getLength(),
  171. sas, false);
  172. }
  173. }