Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TopicLabel.java 6.5KB

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