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.

TopicLabel.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.addons.ui_swing.dialogs.channelsetting;
  23. import com.dmdirc.Topic;
  24. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  25. import com.dmdirc.addons.ui_swing.textpane.StyledDocumentMaker;
  26. import com.dmdirc.interfaces.GroupChat;
  27. import com.dmdirc.interfaces.GroupChatUser;
  28. import java.awt.Color;
  29. import javax.swing.JEditorPane;
  30. import javax.swing.JPanel;
  31. import javax.swing.JSeparator;
  32. import javax.swing.UIManager;
  33. import javax.swing.text.SimpleAttributeSet;
  34. import javax.swing.text.StyleConstants;
  35. import javax.swing.text.StyledDocument;
  36. import javax.swing.text.StyledEditorKit;
  37. import net.miginfocom.swing.MigLayout;
  38. /**
  39. * Topic Label for use in the topic history panel.
  40. */
  41. public class TopicLabel extends JPanel {
  42. /** A version number for this class. */
  43. private static final long serialVersionUID = 1;
  44. /** Topic this label represents. */
  45. private final Topic topic;
  46. /** The group chat to which this label belongs. */
  47. private final GroupChat groupChat;
  48. /** Topic field. */
  49. private JEditorPane pane;
  50. /** Empty Attrib set. */
  51. private SimpleAttributeSet as;
  52. /**
  53. * Instantiates a new topic label based on the specified topic.
  54. *
  55. * @param groupChat The group chat to which this label belongs
  56. * @param topic Specified topic
  57. *
  58. * @since 0.6.3
  59. */
  60. public TopicLabel(final GroupChat groupChat, final Topic topic) {
  61. if (topic == null) {
  62. throw new IllegalArgumentException();
  63. }
  64. this.topic = topic;
  65. this.groupChat = groupChat;
  66. super.setBackground(UIManager.getColor("Table.background"));
  67. super.setForeground(UIManager.getColor("Table.foreground"));
  68. init();
  69. }
  70. /**
  71. * Initialises the topic field.
  72. */
  73. private void initTopicField() {
  74. pane = new JEditorPane();
  75. pane.setEditorKit(new StyledEditorKit());
  76. pane.setFocusable(false);
  77. pane.setEditable(false);
  78. pane.setOpaque(false);
  79. as = new SimpleAttributeSet();
  80. StyleConstants.setFontFamily(as, pane.getFont().getFamily());
  81. StyleConstants.setFontSize(as, pane.getFont().getSize());
  82. if (getBackground() == null) {
  83. StyleConstants.setBackground(as, UIManager.getColor(
  84. "Table.background"));
  85. } else {
  86. StyleConstants.setBackground(as, getBackground());
  87. }
  88. if (getForeground() == null) {
  89. StyleConstants.setForeground(as, UIManager.getColor(
  90. "Table.foreground"));
  91. } else {
  92. StyleConstants.setForeground(as, getForeground());
  93. }
  94. StyleConstants.setUnderline(as, false);
  95. StyleConstants.setBold(as, false);
  96. StyleConstants.setItalic(as, false);
  97. }
  98. /**
  99. * Initialises all the components and layout.
  100. */
  101. private void init() {
  102. initTopicField();
  103. removeAll();
  104. setLayout(new MigLayout("fill, ins 0, debug", "[]0[]", "[]0[]"));
  105. if (!topic.getTopic().isEmpty()) {
  106. groupChat.getWindowModel().getBackBuffer().getStyliser().addStyledString(
  107. new StyledDocumentMaker((StyledDocument) pane.getDocument(), as),
  108. topic.getTopic());
  109. add(pane, "wmax 450, grow, push, wrap, gapleft 5, gapleft 5");
  110. }
  111. TextLabel label;
  112. if (topic.getTopic().isEmpty()) {
  113. label = new TextLabel("Topic unset by " + topic.getClient()
  114. .map(GroupChatUser::getNickname).orElse("Unknown"));
  115. } else {
  116. label = new TextLabel("Topic set by " + topic.getClient()
  117. .map(GroupChatUser::getNickname).orElse("Unknown"));
  118. }
  119. add(label, "wmax 450, grow, push, wrap, gapleft 5, pad 0");
  120. label = new TextLabel("on " + topic.getDate());
  121. add(label, "wmax 450, grow, push, wrap, gapleft 5, pad 0");
  122. add(new JSeparator(), "newline, span, growx, pushx");
  123. validate();
  124. }
  125. /**
  126. * Returns the topic for this label.
  127. *
  128. * @return Topic
  129. */
  130. public Topic getTopic() {
  131. return topic;
  132. }
  133. @Override
  134. public void setBackground(final Color bg) {
  135. super.setBackground(bg);
  136. // This method can be called from the super class constructor, so topic may not have been
  137. // instansiated.
  138. if (topic != null
  139. && ((getBackground() != null && !getBackground().equals(bg))
  140. || (bg != null && !bg.equals(getBackground())))) {
  141. init();
  142. }
  143. }
  144. @Override
  145. public void setForeground(final Color fg) {
  146. super.setForeground(fg);
  147. // This method can be called from the super class constructor, so topic may not have been
  148. // instansiated.
  149. if (topic != null
  150. && ((getForeground() != null && !getForeground().equals(fg))
  151. || (fg != null && !fg.equals(getForeground())))) {
  152. init();
  153. }
  154. }
  155. @Override
  156. public void repaint() {
  157. //Deliberate NOOP
  158. }
  159. @Override
  160. public void firePropertyChange(final String propertyName,
  161. final Object oldValue, final Object newValue) {
  162. //Deliberate NOOP
  163. }
  164. @Override
  165. public void firePropertyChange(final String propertyName,
  166. final boolean oldValue, final boolean newValue) {
  167. //Deliberate NOOP
  168. }
  169. @Override
  170. public void firePropertyChange(final String propertyName,
  171. final int oldValue, final int newValue) {
  172. //Deliberate NOOP
  173. }
  174. }