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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. *
  3. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.addons.ui_swing.dialogs.channelsetting;
  24. import com.dmdirc.Topic;
  25. import com.dmdirc.addons.ui_swing.components.text.OldTextLabel;
  26. import com.dmdirc.ui.messages.Styliser;
  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. /**
  43. * A version number for this class. It should be changed whenever the class
  44. * structure is changed (or anything else that would prevent serialized
  45. * objects being unserialized with the new class).
  46. */
  47. private static final long serialVersionUID = 1;
  48. /** Topic this label represents. */
  49. private final Topic topic;
  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 topic Specified topic
  58. */
  59. public TopicLabel(final Topic topic) {
  60. if (topic == null) {
  61. throw new IllegalArgumentException();
  62. }
  63. this.topic = topic;
  64. super.setBackground(UIManager.getColor("Table.background"));
  65. super.setForeground(UIManager.getColor("Table.foreground"));
  66. init();
  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. private void init() {
  94. initTopicField();
  95. removeAll();
  96. setLayout(new MigLayout("fill, ins 0, debug", "[]0[]", "[]0[]"));
  97. if (!topic.getTopic().isEmpty()) {
  98. Styliser.addStyledString((StyledDocument) pane.getDocument(),
  99. new String[]{topic.getTopic(),},
  100. as);
  101. add(pane, "wmax 450, grow, push, wrap, gapleft 5, gapleft 5");
  102. }
  103. OldTextLabel label;
  104. if (topic.getTopic().isEmpty()) {
  105. label = new OldTextLabel("Topic unset by " + topic.getClient());
  106. } else {
  107. label = new OldTextLabel("Topic set by " + topic.getClient());
  108. }
  109. add(label, "wmax 450, grow, push, wrap, gapleft 5, pad 0");
  110. label = new OldTextLabel("on " + new Date(topic.getTime() * 1000).
  111. toString());
  112. add(label, "wmax 450, grow, push, wrap, gapleft 5, pad 0");
  113. add(new JSeparator(), "newline, span, growx, pushx");
  114. super.validate();
  115. }
  116. /**
  117. * Returns the topic for this label.
  118. *
  119. * @return Topic
  120. */
  121. public Topic getTopic() {
  122. return topic;
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public void setBackground(final Color bg) {
  127. super.setBackground(bg);
  128. if (topic != null && !bg.equals(getBackground())) {
  129. init();
  130. }
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public void setForeground(final Color fg) {
  135. super.setForeground(fg);
  136. if (topic != null && !fg.equals(getForeground())) {
  137. init();
  138. }
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public void invalidate() {
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public void revalidate() {
  147. }
  148. /** {@inheritDoc} */
  149. @Override
  150. public void repaint() {
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. public void firePropertyChange(String propertyName, Object oldValue,
  155. Object newValue) {
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public void firePropertyChange(String propertyName, boolean oldValue,
  160. boolean newValue) {
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public void firePropertyChange(String propertyName, int oldValue,
  165. int newValue) {
  166. }
  167. }