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