001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027
028package org.opends.guitools.controlpanel.ui.renderer;
029
030import java.awt.Color;
031import java.awt.Component;
032
033import javax.swing.BorderFactory;
034import javax.swing.JTree;
035import javax.swing.border.Border;
036import javax.swing.tree.DefaultTreeCellRenderer;
037
038import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
039
040/**
041 * An extension of the DefaultTreeCellRenderer that uses a customized border,
042 * foreground and background.
043 *
044 */
045public class TreeCellRenderer extends DefaultTreeCellRenderer
046{
047  private static final long serialVersionUID = 4045260951231311206L;
048
049  /**
050   * Background when the cell is not selected.
051   */
052  public static final Color nonselectionBackground =
053    ColorAndFontConstants.background;
054  private static final Color nonselectionForeground =
055    ColorAndFontConstants.foreground;
056
057  /**
058   * Background when the cell is selected.
059   */
060  public static final Color selectionBackground =
061    ColorAndFontConstants.mouseOverBackground;
062
063  private static final Color selectionForeground =
064    ColorAndFontConstants.mouseOverForeground;
065
066
067  private Border rootBorder = BorderFactory.createEmptyBorder(0, 5, 0, 0);
068  private Border normalBorder = BorderFactory.createEmptyBorder(0, 0, 0, 0);
069
070  /** Constructor of the renderer. */
071  public TreeCellRenderer()
072  {
073    backgroundNonSelectionColor = nonselectionBackground;
074    backgroundSelectionColor = selectionBackground;
075    textNonSelectionColor = nonselectionForeground;
076    textSelectionColor = selectionForeground;
077    setFont(ColorAndFontConstants.treeFont);
078  }
079
080  /** {@inheritDoc} */
081  public Component getTreeCellRendererComponent(JTree tree, Object value,
082      boolean isSelected, boolean isExpanded, boolean isLeaf, int row,
083      boolean hasFocus)
084  {
085    super.getTreeCellRendererComponent(tree, value, isSelected, isExpanded,
086        isLeaf, row, hasFocus);
087    setIcon(null);
088
089    if (row == 0 && tree.isRootVisible())
090    {
091      setBorder(rootBorder);
092    }
093    else
094    {
095      setBorder(normalBorder);
096    }
097    return this;
098  }
099}