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.Component;
021import java.awt.Font;
022
023import javax.swing.JTree;
024
025import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
026import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
027
028/** The renderer used to render the nodes in the LDAP entry browser. */
029public class BrowserCellRenderer extends TreeCellRenderer {
030
031  private static final long serialVersionUID = 6756291700611741513L;
032  private final Font defaultFont = ColorAndFontConstants.treeFont;
033  private final Font italicFont = defaultFont.deriveFont(Font.ITALIC);
034  private final Font boldFont = defaultFont.deriveFont(Font.BOLD);
035  private final Font italicBoldFont = defaultFont.deriveFont(Font.ITALIC | Font.BOLD);
036  private BasicNode inspectedNode;
037
038  /**
039   * Sets which is the inspected node.  This method simply marks the selected
040   * node in the tree so that it can have a different rendering.  This is
041   * useful for instance when the right panel has a list of entries to which
042   * the menu action apply, to make a difference between the selected node in
043   * the tree (to which the action in the main menu will not apply) and the
044   * selected nodes in the right pane.
045   * @param node the selected node.
046   */
047  public void setInspectedNode(BasicNode node) {
048    inspectedNode = node;
049  }
050
051  @Override
052  public Component getTreeCellRendererComponent(
053      JTree tree,
054      Object value,
055      boolean isSelected,
056    boolean isExpanded,
057    boolean isLeaf,
058    int row,
059      boolean cellHasFocus)
060  {
061    BasicNode node = (BasicNode)value;
062    super.getTreeCellRendererComponent(tree, node, isSelected,
063        isExpanded, isLeaf,
064        row, cellHasFocus);
065
066    setIcon(node.getIcon());
067    setText(node.getDisplayName());
068
069    Font newFont = defaultFont;
070    int style = node.getFontStyle();
071    if (node == inspectedNode) {
072      style |= Font.BOLD;
073    }
074    if ((style & Font.ITALIC & Font.BOLD) != 0) {
075      newFont = italicBoldFont;
076    }
077    else if ((style & Font.ITALIC) != 0) {
078      newFont = italicFont;
079    }
080    else if ((style & Font.BOLD) != 0) {
081      newFont = boldFont;
082    }
083    else {
084      newFont = defaultFont;
085    }
086    setFont(newFont);
087    return this;
088  }
089}