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.

NodeLabel.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.framemanager.tree;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.IconManager;
  25. import com.dmdirc.addons.ui_swing.components.ImageButton;
  26. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  27. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  28. import com.dmdirc.addons.ui_swing.textpane.StyledDocumentMaker;
  29. import com.dmdirc.events.FrameIconChangedEvent;
  30. import com.dmdirc.events.FrameNameChangedEvent;
  31. import com.dmdirc.events.UnreadStatusChangedEvent;
  32. import com.dmdirc.ui.messages.Styliser;
  33. import java.awt.Color;
  34. import java.awt.Font;
  35. import javax.swing.JPanel;
  36. import javax.swing.JTextPane;
  37. import javax.swing.UIManager;
  38. import javax.swing.text.DefaultStyledDocument;
  39. import javax.swing.text.StyledDocument;
  40. import net.engio.mbassy.listener.Handler;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * Node label.
  44. */
  45. public class NodeLabel extends JPanel {
  46. /** A version number for this class. */
  47. private static final long serialVersionUID = 1;
  48. /** The window this node represents in the tree. */
  49. private final TextFrame window;
  50. /** Colour used to show if the mouse is over this node. */
  51. private boolean rollover;
  52. /** Colour used to show if this node has an active notification. */
  53. private Color notificationColour;
  54. /** Are we the selected window? */
  55. private boolean selected;
  56. /** Node icon. */
  57. private final ImageButton<?> icon = new ImageButton<>("", null);
  58. /** Text label. */
  59. private final JTextPane text = new JTextPane(new DefaultStyledDocument());
  60. /** Icon manager. */
  61. private final IconManager iconManager;
  62. /** Default foreground colour when there are no notifications. */
  63. private Color defaultForegroundColour;
  64. /** Current styled text. */
  65. private String currentText = "";
  66. /**
  67. * Instantiates a new node label.
  68. *
  69. * @param window Window for this node
  70. */
  71. public NodeLabel(final TextFrame window, final IconManager iconManager, final Color defaultForegroundColour) {
  72. this.window = window;
  73. this.iconManager = iconManager;
  74. this.defaultForegroundColour = defaultForegroundColour;
  75. init();
  76. }
  77. /**
  78. * Initialises the label.
  79. */
  80. private void init() {
  81. if (window == null) {
  82. return;
  83. }
  84. icon.setIcon(iconManager.getIcon(window.getContainer().getIcon()));
  85. text.setText(window.getContainer().getName());
  86. text.setBorder(null);
  87. setLayout(new MigLayout("ins 0"));
  88. add(icon, "left");
  89. add(text, "left, grow, pushx");
  90. icon.setToolTipText(null);
  91. text.setToolTipText(null);
  92. notificationColour = null;
  93. selected = false;
  94. }
  95. @Handler
  96. public void selectionChanged(final SwingWindowSelectedEvent event) {
  97. selected = event.getWindow().isPresent()
  98. && window.equals(event.getWindow().get());
  99. }
  100. public void unreadStatusChanged(final UnreadStatusChangedEvent event) {
  101. notificationColour = event.getNotificationColour().map(UIUtilities::convertColour)
  102. .orElse(defaultForegroundColour);
  103. }
  104. @Handler
  105. public void iconChanged(final FrameIconChangedEvent event) {
  106. if (window.getContainer().equals(event.getContainer())) {
  107. icon.setIcon(iconManager.getIcon(event.getIcon()));
  108. }
  109. }
  110. @Handler
  111. public void nameChanged(final FrameNameChangedEvent event) {
  112. if (window.getContainer().equals(event.getContainer())) {
  113. text.setText(event.getName());
  114. }
  115. }
  116. /**
  117. * Sets the rollover state for the node.
  118. *
  119. * @param rollover rollover state
  120. */
  121. public void setRollover(final boolean rollover) {
  122. this.rollover = rollover;
  123. }
  124. /**
  125. * Is this node a rollover node?
  126. *
  127. * @return true iff this node is a rollover node
  128. */
  129. public boolean isRollover() {
  130. return rollover;
  131. }
  132. /**
  133. * Is this node a selected node?
  134. *
  135. * @return true iff this node is a selected node
  136. */
  137. public boolean isSelected() {
  138. return selected;
  139. }
  140. /**
  141. * Returns the notification colour for this node.
  142. *
  143. * @return notification colour or null if non set
  144. */
  145. public Color getNotificationColour() {
  146. return notificationColour;
  147. }
  148. /**
  149. * Sets the default foreground colour to use when no notification is present.
  150. *
  151. * @param defaultForegroundColour The default colour to use.
  152. */
  153. public void setDefaultForegroundColour(Color defaultForegroundColour) {
  154. this.defaultForegroundColour = defaultForegroundColour;
  155. }
  156. @Override
  157. public Font getFont() {
  158. return UIManager.getFont("TextPane.font");
  159. }
  160. @Override
  161. public void setForeground(final Color colour) {
  162. if (text != null) {
  163. text.setForeground(colour);
  164. }
  165. }
  166. @Override
  167. public void setBackground(final Color colour) {
  168. if (text != null) {
  169. text.setBackground(colour);
  170. }
  171. }
  172. @Override
  173. public void setFont(final Font font) {
  174. if (text != null) {
  175. text.setFont(font);
  176. }
  177. }
  178. @Override
  179. public void setOpaque(final boolean opacity) {
  180. if (text != null) {
  181. text.setOpaque(opacity);
  182. }
  183. }
  184. /**
  185. * Sets the styles for the text in this label.
  186. *
  187. * @param styliser Styliser to use
  188. * @param newText Style to set
  189. */
  190. public void setTextStyle(final Styliser styliser, final String newText) {
  191. if (currentText.equals(newText + window.getContainer().getName())) {
  192. return;
  193. }
  194. text.setText("");
  195. currentText = newText + window.getContainer().getName();
  196. styliser.addStyledString(
  197. new StyledDocumentMaker((StyledDocument) text.getDocument()), newText,
  198. window.getContainer().getName());
  199. }
  200. }