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.

NodeLabel.java 7.0KB

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