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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.ui.framemanager.tree;
  23. import java.awt.Color;
  24. import java.awt.Component;
  25. import java.awt.Dimension;
  26. import java.awt.Font;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JTree;
  30. import javax.swing.tree.DefaultMutableTreeNode;
  31. import javax.swing.tree.DefaultTreeCellRenderer;
  32. import com.dmdirc.Config;
  33. import com.dmdirc.FrameContainer;
  34. import com.dmdirc.ui.MainFrame;
  35. import com.dmdirc.ui.messages.ColourManager;
  36. import static com.dmdirc.ui.UIUtilities.SMALL_BORDER;
  37. /**
  38. * Displays a node in a tree according to its type.
  39. */
  40. public class TreeViewTreeCellRenderer extends DefaultTreeCellRenderer {
  41. /**
  42. * A version number for this class. It should be changed whenever the class
  43. * structure is changed (or anything else that would prevent serialized
  44. * objects being unserialized with the new class).
  45. */
  46. private static final long serialVersionUID = 1;
  47. /**
  48. * The preferred width of each cell.
  49. */
  50. private static final int WIDTH = 110;
  51. /**
  52. * The default icon to use for unknown frames.
  53. */
  54. private final ImageIcon defaultIcon;
  55. /**
  56. * Creates a new instance of TreeViewTreeCellRenderer.
  57. */
  58. public TreeViewTreeCellRenderer() {
  59. super();
  60. defaultIcon = new ImageIcon(this.getClass().getClassLoader()
  61. .getResource("uk/org/ownage/dmdirc/res/icon.png"));
  62. }
  63. /**
  64. * Configures the renderer based on the passed parameters.
  65. * @param tree JTree for this renderer.
  66. * @param value node to be renderered.
  67. * @param sel whether the node is selected.
  68. * @param expanded whether the node is expanded.
  69. * @param leaf whether the node is a leaf.
  70. * @param row the node's row.
  71. * @param hasFocus whether the node has focus.
  72. * @return RendererComponent for this node.
  73. */
  74. public final Component getTreeCellRendererComponent(final JTree tree,
  75. final Object value, final boolean sel, final boolean expanded,
  76. final boolean leaf, final int row, final boolean hasFocus) {
  77. super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
  78. final DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
  79. TreeFrameManager manager = null;
  80. setBackground(tree.getBackground());
  81. setOpaque(true);
  82. setToolTipText(null);
  83. setBorder(BorderFactory.createEmptyBorder(1, 0, 2, 0));
  84. setForeground(tree.getForeground());
  85. setPreferredSize(new Dimension(WIDTH, getFont().getSize()
  86. + SMALL_BORDER));
  87. if (MainFrame.hasMainFrame()) {
  88. manager = (TreeFrameManager) MainFrame.getMainFrame().getFrameManager();
  89. }
  90. if (manager != null) {
  91. if (manager.getRollover() == value) {
  92. final Color fallback = ColourManager.getColour("b8d6e6");
  93. setBackground(Config.getOptionColor("ui", "treeviewRolloverColour", fallback));
  94. }
  95. final Object nodeObject = node.getUserObject();
  96. if (nodeObject.equals(manager.getSelected())) {
  97. setFont(getFont().deriveFont(Font.BOLD));
  98. } else {
  99. setFont(getFont().deriveFont(Font.PLAIN));
  100. }
  101. if (nodeObject instanceof FrameContainer) {
  102. final Color colour =
  103. manager.getNodeColour((FrameContainer) nodeObject);
  104. if (colour != null) {
  105. setForeground(colour);
  106. }
  107. setIcon(((FrameContainer) nodeObject).getIcon());
  108. } else {
  109. setIcon(defaultIcon);
  110. }
  111. }
  112. return this;
  113. }
  114. }