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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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("Table.background"));
  79. } else {
  80. StyleConstants.setBackground(as, getBackground());
  81. }
  82. if (getForeground() == null) {
  83. StyleConstants.setForeground(as, UIManager.getColor("Table.foreground"));
  84. } else {
  85. StyleConstants.setForeground(as, getForeground());
  86. }
  87. StyleConstants.setUnderline(as, false);
  88. StyleConstants.setBold(as, false);
  89. StyleConstants.setItalic(as, false);
  90. }
  91. private void init() {
  92. initTopicField();
  93. removeAll();
  94. setLayout(new MigLayout("fillx, ins 0, debug", "[]0[]", "[]0[]"));
  95. if (!topic.getTopic().isEmpty()) {
  96. Styliser.addStyledString((StyledDocument) pane.getDocument(),
  97. new String[]{topic.getTopic(),},
  98. as);
  99. add(pane, "wmax 450, grow, push, wrap, gapleft 5, gapleft 5");
  100. }
  101. OldTextLabel label;
  102. if (topic.getTopic().isEmpty()) {
  103. label = new OldTextLabel("Topic unset by " + topic.getClient());
  104. } else {
  105. label = new OldTextLabel("Topic set by " + topic.getClient());
  106. }
  107. add(label, "wmax 450, growy, pushy, wrap, gapleft 5, pad 0");
  108. label = new OldTextLabel("on " + new Date(topic.getTime() * 1000).
  109. toString());
  110. add(label, "wmax 450, growy, pushy, wrap, gapleft 5, pad 0");
  111. add(new JSeparator(), "newline, span, growx, pushx");
  112. }
  113. /**
  114. * Returns the topic for this label.
  115. *
  116. * @return Topic
  117. */
  118. public Topic getTopic() {
  119. return topic;
  120. }
  121. /** {@inheritDoc} */
  122. @Override
  123. public void setBackground(final Color bg) {
  124. super.setBackground(bg);
  125. if (topic != null) {
  126. init();
  127. }
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public void setForeground(final Color fg) {
  132. super.setForeground(fg);
  133. if (topic != null) {
  134. init();
  135. }
  136. }
  137. }