Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TopicLabel.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.addons.ui_swing.dialogs.channelsetting;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.Topic;
  25. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  26. import com.dmdirc.ui.messages.StyledDocumentMaker;
  27. import java.awt.Color;
  28. import java.util.Date;
  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. /** How many milliseconds in a second. */
  45. private static final int MILLIS_IN_SECOND = 1000;
  46. /** Topic this label represents. */
  47. private final Topic topic;
  48. /** The channel to which this label belongs. */
  49. private final Channel channel;
  50. /** Topic field. */
  51. private JEditorPane pane;
  52. /** Empty Attrib set. */
  53. private SimpleAttributeSet as;
  54. /**
  55. * Instantiates a new topic label based on the specified topic.
  56. *
  57. * @param channel The channel to which this label belongs
  58. * @param topic Specified topic
  59. *
  60. * @since 0.6.3
  61. */
  62. public TopicLabel(final Channel channel, final Topic topic) {
  63. if (topic == null) {
  64. throw new IllegalArgumentException();
  65. }
  66. this.topic = topic;
  67. this.channel = channel;
  68. super.setBackground(UIManager.getColor("Table.background"));
  69. super.setForeground(UIManager.getColor("Table.foreground"));
  70. init();
  71. }
  72. /**
  73. * Initialises the topic field.
  74. */
  75. private void initTopicField() {
  76. pane = new JEditorPane();
  77. pane.setEditorKit(new StyledEditorKit());
  78. pane.setFocusable(false);
  79. pane.setEditable(false);
  80. pane.setOpaque(false);
  81. as = new SimpleAttributeSet();
  82. StyleConstants.setFontFamily(as, pane.getFont().getFamily());
  83. StyleConstants.setFontSize(as, pane.getFont().getSize());
  84. if (getBackground() == null) {
  85. StyleConstants.setBackground(as, UIManager.getColor(
  86. "Table.background"));
  87. } else {
  88. StyleConstants.setBackground(as, getBackground());
  89. }
  90. if (getForeground() == null) {
  91. StyleConstants.setForeground(as, UIManager.getColor(
  92. "Table.foreground"));
  93. } else {
  94. StyleConstants.setForeground(as, getForeground());
  95. }
  96. StyleConstants.setUnderline(as, false);
  97. StyleConstants.setBold(as, false);
  98. StyleConstants.setItalic(as, false);
  99. }
  100. /**
  101. * Initialises all the components and layout.
  102. */
  103. private void init() {
  104. initTopicField();
  105. removeAll();
  106. setLayout(new MigLayout("fill, ins 0, debug", "[]0[]", "[]0[]"));
  107. if (!topic.getTopic().isEmpty()) {
  108. channel.getBackBuffer().getStyliser().addStyledString(
  109. new StyledDocumentMaker((StyledDocument) pane.getDocument(), as),
  110. topic.getTopic());
  111. add(pane, "wmax 450, grow, push, wrap, gapleft 5, gapleft 5");
  112. }
  113. TextLabel label;
  114. if (topic.getTopic().isEmpty()) {
  115. label = new TextLabel("Topic unset by " + topic.getClient());
  116. } else {
  117. label = new TextLabel("Topic set by " + topic.getClient());
  118. }
  119. add(label, "wmax 450, grow, push, wrap, gapleft 5, pad 0");
  120. label = new TextLabel("on " + new Date(topic.getTime() * MILLIS_IN_SECOND));
  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. if (topic != null
  137. && ((getBackground() != null && !getBackground().equals(bg))
  138. || (bg != null && !bg.equals(getBackground())))) {
  139. init();
  140. }
  141. }
  142. @Override
  143. public void setForeground(final Color fg) {
  144. super.setForeground(fg);
  145. if (topic != null
  146. && ((getForeground() != null && !getForeground().equals(fg))
  147. || (fg != null && !fg.equals(getForeground())))) {
  148. init();
  149. }
  150. }
  151. @Override
  152. public void repaint() {
  153. //Deliberate NOOP
  154. }
  155. @Override
  156. public void firePropertyChange(final String propertyName,
  157. final Object oldValue, final Object newValue) {
  158. //Deliberate NOOP
  159. }
  160. @Override
  161. public void firePropertyChange(final String propertyName,
  162. final boolean oldValue, final boolean newValue) {
  163. //Deliberate NOOP
  164. }
  165. @Override
  166. public void firePropertyChange(final String propertyName,
  167. final int oldValue, final int newValue) {
  168. //Deliberate NOOP
  169. }
  170. }