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.

TreeViewTreeCellRenderer.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.config.provider.AggregateConfigProvider;
  20. import com.dmdirc.config.provider.ConfigChangeListener;
  21. import com.dmdirc.ui.messages.ColourManager;
  22. import com.dmdirc.ui.messages.IRCControlCodes;
  23. import com.dmdirc.ui.messages.Styliser;
  24. import java.awt.Color;
  25. import java.awt.Component;
  26. import java.awt.Dimension;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.JLabel;
  29. import javax.swing.JTree;
  30. import javax.swing.UIManager;
  31. import javax.swing.tree.TreeCellRenderer;
  32. /**
  33. * Displays a node in a tree according to its type.
  34. */
  35. public class TreeViewTreeCellRenderer implements TreeCellRenderer,
  36. ConfigChangeListener {
  37. /** Parent frame manager. */
  38. private final TreeFrameManager manager;
  39. /** Config manager. */
  40. private final AggregateConfigProvider config;
  41. /** The colour manager to use to resolve colours. */
  42. private final ColourManager colourManager;
  43. /** Styliser to use. */
  44. private final Styliser styliser;
  45. /** Rollover colours. */
  46. private Color rolloverColour;
  47. /** Active bold. */
  48. private boolean activeBold;
  49. /** Active background. */
  50. private Color activeBackground;
  51. /** Active foreground. */
  52. private Color activeForeground;
  53. /**
  54. * Creates a new instance of TreeViewTreeCellRenderer.
  55. *
  56. * @param config Config manager to retrieve settings from
  57. * @param colourManager The colour manager to use to resolve colours.
  58. * @param manager Parent TreeFrameManager
  59. */
  60. public TreeViewTreeCellRenderer(final AggregateConfigProvider config,
  61. final ColourManager colourManager, final TreeFrameManager manager) {
  62. this.manager = manager;
  63. this.config = config;
  64. this.colourManager = colourManager;
  65. styliser = new Styliser(null, config, colourManager);
  66. setColours();
  67. config.addChangeListener("ui", this);
  68. config.addChangeListener("treeview", this);
  69. }
  70. /**
  71. * Configures the renderer based on the passed parameters.
  72. *
  73. * @param tree JTree for this renderer.
  74. * @param value node to be rendered.
  75. * @param sel whether the node is selected.
  76. * @param expanded whether the node is expanded.
  77. * @param leaf whether the node is a leaf.
  78. * @param row the node's row.
  79. * @param hasFocus whether the node has focus.
  80. *
  81. * @return RendererComponent for this node.
  82. */
  83. @Override
  84. public Component getTreeCellRendererComponent(final JTree tree,
  85. final Object value, final boolean sel, final boolean expanded,
  86. final boolean leaf, final int row, final boolean hasFocus) {
  87. if (value == null) {
  88. return new JLabel("Node == null");
  89. }
  90. final NodeLabel label = ((TreeViewNode) value).getLabel();
  91. if (label == null) {
  92. return new JLabel("Label == null");
  93. }
  94. Color background = tree.getBackground();
  95. Color foreground = tree.getForeground();
  96. if (label.isRollover()) {
  97. background = rolloverColour;
  98. }
  99. final Color colour = label.getNotificationColour();
  100. if (colour != null) {
  101. foreground = colour;
  102. }
  103. final boolean bold;
  104. if (label.isSelected()) {
  105. bold = activeBold;
  106. if (!tree.getBackground().equals(activeBackground)) {
  107. background = activeBackground;
  108. }
  109. foreground = activeForeground;
  110. } else {
  111. bold = false;
  112. }
  113. final StringBuilder sb = new StringBuilder();
  114. if (bold) {
  115. sb.append(IRCControlCodes.BOLD);
  116. }
  117. sb.append(IRCControlCodes.COLOUR_HEX);
  118. sb.append(UIUtilities.getHex(foreground));
  119. sb.append(',');
  120. sb.append(UIUtilities.getHex(background));
  121. label.setBackground(background);
  122. label.setOpaque(true);
  123. label.setTextStyle(styliser, sb.toString());
  124. label.setBorder(BorderFactory.createEmptyBorder(1, 0, 2, 0));
  125. label.setPreferredSize(new Dimension(100000, tree.getFontMetrics(
  126. UIManager.getFont("Tree.font")).getHeight() + 2));
  127. return label;
  128. }
  129. /** Sets the colours for the renderer. */
  130. private void setColours() {
  131. rolloverColour = UIUtilities.convertColour(
  132. colourManager.getColourFromString(
  133. config.getOptionString(
  134. "ui", "treeviewRolloverColour",
  135. "treeview", "backgroundcolour",
  136. "ui", "backgroundcolour"), null));
  137. activeBackground = UIUtilities.convertColour(
  138. colourManager.getColourFromString(
  139. config.getOptionString(
  140. "ui", "treeviewActiveBackground",
  141. "treeview", "backgroundcolour",
  142. "ui", "backgroundcolour"), null));
  143. activeForeground = UIUtilities.convertColour(
  144. colourManager.getColourFromString(
  145. config.getOptionString(
  146. "ui", "treeviewActiveForeground",
  147. "treeview", "foregroundcolour",
  148. "ui", "foregroundcolour"), null));
  149. activeBold = config.getOptionBool("ui", "treeviewActiveBold");
  150. manager.getTree().repaint();
  151. }
  152. @Override
  153. public void configChanged(final String domain, final String key) {
  154. if (("ui".equals(domain) || "treeview".equals(domain))
  155. && ("treeviewRolloverColour".equals(key)
  156. || "treeviewActiveBackground".equals(key)
  157. || "treeviewActiveForeground".equals(key)
  158. || "treeviewActiveBold".equals(key)
  159. || "backgroundcolour".equals(key)
  160. || "foregroundcolour".equals(key))) {
  161. setColours();
  162. }
  163. }
  164. }